Skip to content

Commit

Permalink
feat: Use podman-compose for dev environment
Browse files Browse the repository at this point in the history
  • Loading branch information
ShimShtein committed Oct 24, 2023
1 parent de088bd commit 167c5b0
Show file tree
Hide file tree
Showing 15 changed files with 357 additions and 61 deletions.
12 changes: 1 addition & 11 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1 @@
FROM registry.access.redhat.com/ubi7

RUN yum install go-toolset-1.19 git make curl gzip tar --enablerepo=rhel-7-server-devtools-rpms -y

COPY ./entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh

COPY ./enable_go_scl.sh /etc/profile.d/
RUN chmod +x /etc/profile.d/enable_go_scl.sh

ENTRYPOINT ["entrypoint.sh"]
FROM registry.access.redhat.com/ubi7/go-toolset:1.19
19 changes: 19 additions & 0 deletions .devcontainer/commands/prepare_containers.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

pushd $(dirname "$0")/..
DEVCONTAINER_PATH=$(pwd)
popd

LOCAL_COMPOSE_FILE=$DEVCONTAINER_PATH/docker-compose.local.yml

if [ ! -f "$LOCAL_COMPOSE_FILE" ]; then
cat >"$LOCAL_COMPOSE_FILE" <<EOF
version: '3'
services:
host-metering:
build:
context: ${DEVCONTAINER_PATH}
command: /bin/sh -c "while sleep 1000; do :; done"
EOF
fi
23 changes: 16 additions & 7 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@
"name": "Go",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
// "image": "mcr.microsoft.com/devcontainers/go:1.19",
"build": {
"dockerfile": "Dockerfile"
},

"workspaceFolder": "/workspace/host-metering",

// to set the local folder prepare_containers script should run first.
// "initializeCommand": "${localWorkspaceFolder}/.devcontainer/commands/prepare_containers.sh",

"dockerComposeFile" : ["./docker-compose.yml", "./docker-compose.local.yml"],
"service" : "host-metering",
"shutdownAction" : "stopCompose",

// Features to add to the dev container. More info: https://containers.dev/features.
"features": {
Expand Down Expand Up @@ -34,8 +40,11 @@
"HOME": "/home",
"WORKDIR": "/workspaces/${localWorkspaceFolderBasename}"
},
"workspaceMount": "",
"runArgs": [
"--volume=${localWorkspaceFolder}:/workspaces/${localWorkspaceFolderBasename}:Z"
]
"customizations": {
"vscode": {
"extensions": [
"golang.go"
]
}
}
}
22 changes: 22 additions & 0 deletions .devcontainer/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
version: '3'

services:
host-metering:
build:
context: ./
dockerfile: ./Dockerfile
volumes:
- ../:/workspace/host-metering:Z

prometheus:
image: prometheus/prometheus
ports:
- 9090:9090
volumes:
- ./local_prometheus/prometheus.yml:/etc/prometheus/prometheus.yml:Z
command:
- '--config.file=/etc/prometheus/prometheus.yml'
- '--storage.tsdb.path=/prometheus'
- '--web.console.libraries=/usr/share/prometheus/console_libraries'
- '--web.console.templates=/usr/share/prometheus/consoles'
- '--web.enable-remote-write-receiver'
17 changes: 17 additions & 0 deletions .devcontainer/host-metering.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Test host-metering configuration file which writes to a local prometheus
# and uses a test certificate and key.

# E.g. could be used as:
# ./host-metering -config hack/host-metering.conf daemon

[host-metering]
write_url=http://prometheus:9090/api/v1/write
write_interval_sec=5
host_cert_path=./mocks/consumer/cert.pem
host_cert_key_path=./mocks/consumer/key.pem
collect_interval_sec=0
label_refresh_interval_sec=10
write_retry_attempts=4
write_retry_interval_sec=2
metrics_wal_path=./mocks/cpumetrics
log_level=DEBUG
1 change: 1 addition & 0 deletions .devcontainer/local_prometheus/prometheus.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
---
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ node_modules
#dev
mocks/subscription-manager-*
mocks/consumer/
.devcontainer/docker-compose.local.yml
mocks/cpumetrics/
mocks/cpumetrics/
.devcontainer/docker-compose.local.yml
6 changes: 2 additions & 4 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/main.go",
"args": ["once"],
"args": ["--config", "${workspaceFolder}/.devcontainer/host-metering.conf", "once"],
"env": {
"HOST_METERING_HOST_CERT_PATH": "${workspaceFolder}/mocks/consumer/cert.pem",
"HOST_METERING_HOST_CERT_KEY_PATH": "${workspaceFolder}/mocks/consumer/key.pem",
"PATH": "${workspaceFolder}/mocks:${env:PATH}"
"PATH": "${workspaceFolder}/mocks:${env:PATH}",
}
}
]
Expand Down
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"go.alternateTools": {
"go": "/opt/rh/go-toolset-1.19/root/usr/bin/go"
}
}
66 changes: 32 additions & 34 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@ RPMTOPDIR := $(DISTDIR)/rpmbuild
GO := go
TESTDIR := $(CURDIR)/test

CONTAINER_POD := host-metering-pod
DASHBOARD_URL = http://localhost:9090/graph?g0.expr=system_cpu_logical_count&g0.tab=0&g0.range_input=1m

MOCKS_DIR = $(TESTDIR)/../mocks

# Test
.PHONY: test
test:
@echo "Running the unit tests..."

PATH=$(TESTDIR)/bin:$(PATH) \
PATH=$(MOCKS_DIR):$(PATH) \
$(GO) test -v \
-coverprofile=coverage.out \
-covermode=atomic \
Expand Down Expand Up @@ -53,51 +54,48 @@ build-selinux:
test-daemon: cert build prometheus
@echo "Running the $(PROJECT) in deamon mode..."

PATH=$(TESTDIR)/bin:$(PATH) \
$(DISTDIR)/$(PROJECT) --config hack/host-metering.conf daemon
PATH=$(MOCKS_DIR):$(PATH) \
$(DISTDIR)/$(PROJECT) --config .devcontainer/host-metering.conf daemon

cert: hack/test-cert.crt hack/test-cert.key
cert: mocks/consumer/cert.pem mocks/consumer/key.pem

hack/test-cert.crt hack/test-cert.key:
mocks/consumer/cert.pem mocks/consumer/key.pem:
@echo "Generating test certificates..."
cd hack && ./create-cert.sh
cd mocks && ./create-cert.sh

# Containers

.PHONY: container-pod
container-pod:
if podman pod exists $(CONTAINER_POD); then \
echo "Pod $(CONTAINER_POD) exists."; \
exit 0; \
else \
echo "Creating the $(CONTAINER_POD)..."; \
podman pod create --replace -p 9090:9090 ${CONTAINER_POD}; \
fi
.PHONY: podman-compose
podman-compose:
ifeq (,$(shell which podman-compose))
$(error "No podman-compose in $(PATH), please install podman-compose")
endif

.PHONY: podman-containers
podman-containers: podman-compose
podman-compose -f .devcontainer/docker-compose.yml build

.PHONY: prometheus
prometheus: container-pod
prometheus: podman-containers
@echo "See the dashboard at: ${DASHBOARD_URL}"

if podman ps --filter "name=hm-prometheus" --format '{{.Names}}' | grep -q "hm-prometheus"; then \
echo "Prometheus is already running."; \
exit 0; \
else \
echo "Starting Prometheus..."; \
podman run --pod ${CONTAINER_POD} \
--name hm-prometheus \
-d \
-v ./hack/prometheus.yml:/etc/prometheus/prometheus.yml:Z \
prometheus/prometheus \
--config.file=/etc/prometheus/prometheus.yml \
--storage.tsdb.path=/prometheus \
--web.console.libraries=/usr/share/prometheus/console_libraries \
--web.console.templates=/usr/share/prometheus/consoles \
--web.enable-remote-write-receiver; \
fi
podman-compose -f .devcontainer/docker-compose.yml up -d prometheus

.PHONY: prometheus-stop
prometheus-stop: podman-compose
podman-compose -f .devcontainer/docker-compose.yml stop prometheus

.PHONY: podman-mocked-daemon
podman-mocked-daemon: cert prometheus
podman-compose -f .devcontainer/docker-compose.yml run -u root host-metering bash -c "cd /workspace/host-metering && go run main.go --config .devcontainer/host-metering.conf daemon"

.PHONY: podman-%
podman-%:
podman-compose -f .devcontainer/docker-compose.yml run -u root host-metering bash -c "cd /workspace/host-metering && make $(subst podman-,,$@)"

.PHONY: clean-pod
clean-pod:
podman pod rm -f ${CONTAINER_POD}
podman-compose -f .devcontainer/docker-compose.yml down

# Release
.PHONY: version
Expand Down
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,21 @@ $ make clean-pod # destroy podman pod

## Running in a container
This project has configuration for running inside VSCode container.
### Prerequisites:
1. It requires podman and podman-compose to be installed on the host machine.
To install podman-compose, please run
```
sudo dnf install podman-compose
```
2. Make sure to add the following settings to user's settings.json (`ctrl+shift+P` -> `Preferences: Open user settings (JSON)`). This will set up the dev containers plugin to work with `podman` and `podman-compose`
```
"dev.containers.dockerComposePath": "podman-compose",
"dev.containers.dockerPath": "podman"
```
3. Execute `.devcontainer/commands/prepare_containers.sh`. It will create `docker-compose.local.yml` file that will be used to run the container properly.

You have to have a system with `subscription-manager` installed and registered correctly to use the UBI image (which is the base for this Dockerfile) and to have access to the relevan Red Hat repositories.
### Running make commands in a container
There is an option to run make commands inside the `docker-compose` generated environment. Just prefix a make command you would like to run with `podman-`. e.g. to run `make test` in a container, use `make podman-test`.

## Mocking subscription-manager commands
`mocked_run.sh` is a wrapper of `go run main.go` to execute the command in mocked context.
Expand Down
20 changes: 20 additions & 0 deletions mocks/create-cert.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

# Execute this script in the directory with test-cert.cnf
# Purpose: Create a self-signed certificate for testing

if [ -d consumer ]; then
rm -rf consumer
fi

mkdir consumer

openssl genrsa -out consumer/key.pem

# To create a CSR for submitting to CA
# openssl req -new -key test-cert.key -out test-cert.csr -config test-cert.cnf

# Create a self-signed certificate
openssl req -x509 -new -key consumer/key.pem -out consumer/cert.pem -config mock-cert.cnf

# Show the certificate
openssl x509 -in consumer/cert.pem -text -noout
16 changes: 16 additions & 0 deletions mocks/mock-cert.cnf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[req]
prompt = no
req_extensions = req_ext
distinguished_name = dn

[ dn ]
C=US
ST=North Carolina
L=Raleigh
O=Milton
OU=Information Technology
emailAddress[email protected]
CN = test-host.host-metering.test

[ req_ext ]
subjectAltName = DNS:test-host.host-metering.test
File renamed without changes.
Loading

0 comments on commit 167c5b0

Please sign in to comment.