Skip to content

Commit 5ce7275

Browse files
authored
feat: Containerization & Helm Charts (#4)
* Empty commit * Add containerization (AI-driven) * Add GH CI smoke tests * Empty commit to ping CI * Try fixing CI * Fix version smoke test
1 parent 1828316 commit 5ce7275

File tree

20 files changed

+522
-476
lines changed

20 files changed

+522
-476
lines changed

.dockerignore

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
.git
2+
.github
3+
.cursor
4+
.vscode
5+
bin
6+
dist
7+
demo
8+
testdata
9+
*.md
10+
*.log
11+
*.tmp
12+
*.out
13+
coverage.out
14+
deploy/nginx.conf

.github/workflows/ci.yml

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,126 @@ jobs:
6060
- name: Validate rules
6161
run: ./bin/metrics-analyzer validate ./automated-rules
6262

63+
container-smoke:
64+
runs-on: ubuntu-latest
65+
needs: build
66+
steps:
67+
- uses: actions/checkout@v4
68+
69+
- name: Read version
70+
id: version
71+
run: echo "version=$(cat VERSION)" >> "$GITHUB_OUTPUT"
72+
73+
- name: Set build time
74+
id: build_time
75+
run: echo "build_time=$(date -u +%Y-%m-%dT%H:%M:%SZ)" >> "$GITHUB_OUTPUT"
76+
77+
- name: Build image
78+
run: |
79+
docker build \
80+
--build-arg VERSION="${{ steps.version.outputs.version }}" \
81+
--build-arg BUILD_TIME="${{ steps.build_time.outputs.build_time }}" \
82+
-t sma:ci .
83+
84+
- name: Start container
85+
run: |
86+
docker run -d --rm --name sma-ci -p 8080:8080 sma:ci
87+
88+
- name: Wait for readiness
89+
run: |
90+
for i in {1..10}; do
91+
if curl -fsS http://localhost:8080/health >/dev/null; then
92+
exit 0
93+
fi
94+
sleep 1
95+
done
96+
docker logs sma-ci
97+
exit 1
98+
99+
- name: Check version endpoint
100+
run: |
101+
for i in {1..5}; do
102+
if curl -fsS http://localhost:8080/version -o /tmp/version.json; then
103+
if [ -s /tmp/version.json ]; then
104+
break
105+
fi
106+
fi
107+
sleep 1
108+
done
109+
if [ ! -s /tmp/version.json ]; then
110+
echo "version response was empty"
111+
cat /tmp/version.json || true
112+
docker logs sma-ci || true
113+
exit 1
114+
fi
115+
python3 - <<'PY'
116+
import json
117+
with open("/tmp/version.json") as f:
118+
data = json.load(f)
119+
assert "version" in data
120+
assert "lastUpdate" in data
121+
print("version ok")
122+
PY
123+
124+
- name: Analyze sample metrics
125+
run: |
126+
curl -fsS -o /tmp/analysis.json -X POST \
127+
-F "file=@testdata/fixtures/sample_metrics.txt" \
128+
http://localhost:8080/api/analyze/both
129+
python3 - <<'PY'
130+
import json
131+
with open("/tmp/analysis.json") as f:
132+
data = json.load(f)
133+
assert "console" in data
134+
assert "markdown" in data
135+
print("analysis ok")
136+
PY
137+
138+
- name: Show container logs on failure
139+
if: failure()
140+
run: docker logs sma-ci
141+
142+
container-image:
143+
runs-on: ubuntu-latest
144+
needs: build
145+
if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master')
146+
env:
147+
QUAY_USERNAME: ${{ secrets.QUAY_USERNAME }}
148+
QUAY_PASSWORD: ${{ secrets.QUAY_PASSWORD }}
149+
steps:
150+
- uses: actions/checkout@v4
151+
152+
- name: Read version
153+
id: version
154+
run: echo "version=$(cat VERSION)" >> "$GITHUB_OUTPUT"
155+
156+
- name: Set build time
157+
id: build_time
158+
run: echo "build_time=$(date -u +%Y-%m-%dT%H:%M:%SZ)" >> "$GITHUB_OUTPUT"
159+
160+
- name: Set up Docker Buildx
161+
uses: docker/setup-buildx-action@v3
162+
163+
- name: Log in to Quay
164+
id: login
165+
if: ${{ env.QUAY_USERNAME != '' && env.QUAY_PASSWORD != '' }}
166+
uses: docker/login-action@v3
167+
with:
168+
registry: quay.io
169+
username: ${{ env.QUAY_USERNAME }}
170+
password: ${{ env.QUAY_PASSWORD }}
171+
172+
- name: Build and push image
173+
uses: docker/build-push-action@v5
174+
with:
175+
context: .
176+
file: ./Dockerfile
177+
push: ${{ steps.login.conclusion == 'success' }}
178+
tags: |
179+
quay.io/prygiels/sma:${{ steps.version.outputs.version }}
180+
quay.io/prygiels/sma:${{ github.sha }}
181+
quay.io/prygiels/sma:latest
182+
build-args: |
183+
VERSION=${{ steps.version.outputs.version }}
184+
BUILD_TIME=${{ steps.build_time.outputs.build_time }}
185+

Dockerfile

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
FROM golang:1.25-alpine AS builder
2+
3+
WORKDIR /src
4+
5+
RUN apk add --no-cache git
6+
7+
COPY go.mod go.sum ./
8+
RUN go mod download
9+
10+
COPY . .
11+
12+
ARG VERSION=dev
13+
ARG BUILD_TIME=""
14+
15+
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
16+
go build -ldflags "-X main.buildVersion=${VERSION} -X main.buildTime=${BUILD_TIME}" \
17+
-o /out/web-server ./web/server
18+
19+
FROM alpine:3.19
20+
21+
WORKDIR /app
22+
23+
RUN apk add --no-cache ca-certificates nginx
24+
RUN adduser -D -H -u 10001 appuser
25+
26+
COPY --from=builder /out/web-server /app/web-server
27+
COPY web/static /app/web/static
28+
COPY automated-rules /app/automated-rules
29+
COPY templates/markdown.tmpl /app/templates/markdown.tmpl
30+
COPY deploy/nginx.container.conf /etc/nginx/nginx.conf
31+
COPY deploy/container-entrypoint.sh /app/entrypoint.sh
32+
33+
RUN chmod +x /app/entrypoint.sh \
34+
&& chown -R 10001:10001 /app /etc/nginx
35+
36+
EXPOSE 8080
37+
38+
USER 10001
39+
40+
ENTRYPOINT ["/app/entrypoint.sh"]

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.0.3
1+
0.0.4
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
apiVersion: v2
2+
name: sma
3+
description: Sensor Metrics Analyzer web server
4+
type: application
5+
version: 0.1.0
6+
appVersion: "0.0.3"
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{{- define "sensor-metrics-analyzer.name" -}}
2+
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}}
3+
{{- end -}}
4+
5+
{{- define "sensor-metrics-analyzer.fullname" -}}
6+
{{- if .Values.fullnameOverride -}}
7+
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" -}}
8+
{{- else -}}
9+
{{- printf "%s" (include "sensor-metrics-analyzer.name" .) | trunc 63 | trimSuffix "-" -}}
10+
{{- end -}}
11+
{{- end -}}
12+
13+
{{- define "sensor-metrics-analyzer.labels" -}}
14+
app.kubernetes.io/name: {{ include "sensor-metrics-analyzer.name" . }}
15+
app.kubernetes.io/instance: {{ .Release.Name }}
16+
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
17+
app.kubernetes.io/managed-by: {{ .Release.Service }}
18+
helm.sh/chart: {{ printf "%s-%s" .Chart.Name .Chart.Version | quote }}
19+
{{- end -}}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: {{ include "sensor-metrics-analyzer.fullname" . }}
5+
labels:
6+
{{- include "sensor-metrics-analyzer.labels" . | nindent 4 }}
7+
spec:
8+
replicas: {{ .Values.replicaCount }}
9+
selector:
10+
matchLabels:
11+
app.kubernetes.io/name: {{ include "sensor-metrics-analyzer.name" . }}
12+
app.kubernetes.io/instance: {{ .Release.Name }}
13+
template:
14+
metadata:
15+
labels:
16+
app.kubernetes.io/name: {{ include "sensor-metrics-analyzer.name" . }}
17+
app.kubernetes.io/instance: {{ .Release.Name }}
18+
spec:
19+
containers:
20+
- name: app
21+
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
22+
imagePullPolicy: {{ .Values.image.pullPolicy }}
23+
env:
24+
- name: LISTEN_ADDR
25+
value: ":8081"
26+
- name: RULES_DIR
27+
value: {{ .Values.config.rulesDir | quote }}
28+
- name: LOAD_LEVEL_DIR
29+
value: {{ .Values.config.loadLevelDir | quote }}
30+
- name: TEMPLATE_PATH
31+
value: {{ .Values.config.templatePath | quote }}
32+
- name: MAX_FILE_SIZE
33+
value: {{ .Values.config.maxFileSize | quote }}
34+
- name: REQUEST_TIMEOUT
35+
value: {{ .Values.config.requestTimeout | quote }}
36+
ports:
37+
- name: http
38+
containerPort: 8080
39+
livenessProbe:
40+
httpGet:
41+
path: /health
42+
port: 8080
43+
initialDelaySeconds: 10
44+
periodSeconds: 10
45+
readinessProbe:
46+
httpGet:
47+
path: /health
48+
port: 8080
49+
initialDelaySeconds: 5
50+
periodSeconds: 10
51+
resources:
52+
{{- toYaml .Values.resources | nindent 12 }}
53+
securityContext:
54+
runAsNonRoot: true
55+
runAsUser: 10001
56+
runAsGroup: 10001
57+
allowPrivilegeEscalation: false
58+
readOnlyRootFilesystem: true
59+
seccompProfile:
60+
type: RuntimeDefault
61+
capabilities:
62+
drop:
63+
- ALL
64+
volumeMounts:
65+
- name: tmp
66+
mountPath: /tmp
67+
volumes:
68+
- name: tmp
69+
emptyDir: {}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{{- if .Values.ingress.enabled -}}
2+
apiVersion: networking.k8s.io/v1
3+
kind: Ingress
4+
metadata:
5+
name: {{ include "sensor-metrics-analyzer.fullname" . }}
6+
labels:
7+
{{- include "sensor-metrics-analyzer.labels" . | nindent 4 }}
8+
{{- with .Values.ingress.annotations }}
9+
annotations:
10+
{{- toYaml . | nindent 4 }}
11+
{{- end }}
12+
spec:
13+
ingressClassName: {{ .Values.ingress.className }}
14+
rules:
15+
{{- range .Values.ingress.hosts }}
16+
- host: {{ .host }}
17+
http:
18+
paths:
19+
{{- range .paths }}
20+
- path: {{ .path }}
21+
pathType: {{ .pathType }}
22+
backend:
23+
service:
24+
name: {{ include "sensor-metrics-analyzer.fullname" $ }}
25+
port:
26+
number: {{ $.Values.service.port }}
27+
{{- end }}
28+
{{- end }}
29+
{{- with .Values.ingress.tls }}
30+
tls:
31+
{{- toYaml . | nindent 4 }}
32+
{{- end }}
33+
{{- end }}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
apiVersion: v1
2+
kind: Service
3+
metadata:
4+
name: {{ include "sensor-metrics-analyzer.fullname" . }}
5+
labels:
6+
{{- include "sensor-metrics-analyzer.labels" . | nindent 4 }}
7+
spec:
8+
type: {{ .Values.service.type }}
9+
selector:
10+
app.kubernetes.io/name: {{ include "sensor-metrics-analyzer.name" . }}
11+
app.kubernetes.io/instance: {{ .Release.Name }}
12+
ports:
13+
- name: http
14+
port: {{ .Values.service.port }}
15+
targetPort: 8080
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
replicaCount: 2
2+
3+
image:
4+
repository: quay.io/prygiels/sma
5+
tag: "0.0.3"
6+
pullPolicy: IfNotPresent
7+
8+
service:
9+
type: ClusterIP
10+
port: 80
11+
12+
ingress:
13+
enabled: true
14+
className: nginx
15+
annotations: {}
16+
hosts:
17+
- host: sensor-metrics.rhacs.io
18+
paths:
19+
- path: /
20+
pathType: Prefix
21+
tls: []
22+
23+
config:
24+
rulesDir: /app/automated-rules
25+
loadLevelDir: /app/automated-rules/load-level
26+
templatePath: /app/templates/markdown.tmpl
27+
maxFileSize: "52428800"
28+
requestTimeout: "60s"
29+
30+
resources:
31+
limits:
32+
cpu: 500m
33+
memory: 512Mi
34+
requests:
35+
cpu: 100m
36+
memory: 256Mi

0 commit comments

Comments
 (0)