add helm chart

This commit is contained in:
Jonas Leder 2026-10-11 11:37:50 +02:00
commit 7335be6cac
10 changed files with 347 additions and 0 deletions

View file

@ -1,3 +1,32 @@
include:
- project: 'infra/ci'
file: '/pipelines/docker.yml'
Helm:
stage: bundle
image: alpine:latest
dependencies:
- Docker
before_script:
- apk add curl bash openssl git
- curl -L https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-4 | bash
script:
- helm dependency update charts/shelly-prometheus-exporter
- "export NEXT_PATCH_VERSION=\"$(cat charts/shelly-prometheus-exporter/Chart.yaml | grep ^version: | cut -d ' ' -f 2 | awk -F. '{print $1 \".\" $2 \".\"}')\""
- "sed -i \"s/^ tag: .*/ tag: $CI_COMMIT_SHA/\" charts/shelly-prometheus-exporter/values.yaml"
- helm lint charts/shelly-prometheus-exporter
- helm package charts/shelly-prometheus-exporter --version "${NEXT_PATCH_VERSION}${CI_PIPELINE_IID}"
artifacts:
paths:
- shelly-prometheus-exporter-*.tgz
Deploy Helm:
image: curlimages/curl:latest
stage: deploy
script:
- HELM_TGZ=$(ls shelly-prometheus-exporter-*.tgz)
- CLEANED_BRANCH=$(echo "${CI_COMMIT_BRANCH}" | sed 's/[^a-zA-Z0-9]/-/g')
- 'curl --request POST --user gitlab-ci-token:$CI_JOB_TOKEN --form "chart=@${HELM_TGZ}" "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/helm/api/${CLEANED_BRANCH}/charts"'
dependencies:
- Helm

View file

@ -44,3 +44,29 @@ To export more values, add an entry to the `mappings` table in [metrics.go](metr
```sh
MQTT_BROKER=tcp://192.168.1.10:1883 MQTT_WILDCARD_TOPIC=+ go run .
```
## Helm
A chart is located in [charts/shelly-prometheus-exporter](charts/shelly-prometheus-exporter). It deploys the exporter,
a Service and a `ServiceMonitor` (requires the Prometheus Operator CRDs, disable with `serviceMonitor.enabled=false`).
Inline connection settings:
```sh
helm install shelly-exporter charts/shelly-prometheus-exporter \
--set mqtt.broker=tcp://192.168.1.10:1883 \
--set mqtt.username=exporter --set mqtt.password=secret \
--set mqtt.wildcardTopic=+
```
Existing secret (keys `MQTT_BROKER`, optional `MQTT_USERNAME` / `MQTT_PASSWORD`, configurable via `mqtt.existingSecretKeys`):
```sh
kubectl create secret generic shelly-mqtt \
--from-literal=MQTT_BROKER=tcp://192.168.1.10:1883 \
--from-literal=MQTT_USERNAME=exporter --from-literal=MQTT_PASSWORD=secret
helm install shelly-exporter charts/shelly-prometheus-exporter \
--set mqtt.existingSecret=shelly-mqtt \
--set 'mqtt.topics={shellyplussmoke-halle-2,shellyplussmoke-funkzentrale}'
```

View file

@ -0,0 +1,5 @@
.DS_Store
.git/
*.swp
*.bak
*.tmp

View file

@ -0,0 +1,6 @@
apiVersion: v2
name: shelly-prometheus-exporter
description: Prometheus exporter for Shelly devices via MQTT
type: application
version: 0.1.0
appVersion: "latest"

View file

@ -0,0 +1,32 @@
{{- define "shelly-exporter.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- define "shelly-exporter.fullname" -}}
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- $name := default .Chart.Name .Values.nameOverride }}
{{- if contains $name .Release.Name }}
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- end }}
{{- end }}
{{- define "shelly-exporter.labels" -}}
helm.sh/chart: {{ printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{ include "shelly-exporter.selectorLabels" . }}
app.kubernetes.io/version: {{ (default .Chart.AppVersion .Values.image.tag) | quote }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}
{{- define "shelly-exporter.selectorLabels" -}}
app.kubernetes.io/name: {{ include "shelly-exporter.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}
{{- define "shelly-exporter.secretName" -}}
{{- default (include "shelly-exporter.fullname" .) .Values.mqtt.existingSecret }}
{{- end }}

View file

@ -0,0 +1,110 @@
{{- if and (not .Values.mqtt.topics) (not .Values.mqtt.wildcardTopic) }}
{{- fail "at least one of mqtt.topics or mqtt.wildcardTopic is required" }}
{{- end }}
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "shelly-exporter.fullname" . }}
labels:
{{- include "shelly-exporter.labels" . | nindent 4 }}
spec:
# A single replica only: multiple instances would fight over the MQTT client ID
replicas: 1
strategy:
type: Recreate
selector:
matchLabels:
{{- include "shelly-exporter.selectorLabels" . | nindent 6 }}
template:
metadata:
annotations:
{{- if not .Values.mqtt.existingSecret }}
checksum/secret: {{ include (print $.Template.BasePath "/secret.yaml") . | sha256sum }}
{{- end }}
{{- with .Values.podAnnotations }}
{{- toYaml . | nindent 8 }}
{{- end }}
labels:
{{- include "shelly-exporter.selectorLabels" . | nindent 8 }}
{{- with .Values.podLabels }}
{{- toYaml . | nindent 8 }}
{{- end }}
spec:
{{- with .Values.imagePullSecrets }}
imagePullSecrets:
{{- toYaml . | nindent 8 }}
{{- end }}
automountServiceAccountToken: false
securityContext:
{{- toYaml .Values.podSecurityContext | nindent 8 }}
containers:
- name: exporter
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
{{- with .Values.image.pullPolicy }}
imagePullPolicy: {{ . }}
{{- end }}
securityContext:
{{- toYaml .Values.securityContext | nindent 12 }}
env:
- name: LISTEN_ADDRESS
value: ":{{ .Values.service.port }}"
- name: LOG_LEVEL
value: {{ .Values.logLevel | quote }}
- name: MQTT_BROKER
valueFrom:
secretKeyRef:
name: {{ include "shelly-exporter.secretName" . }}
key: {{ .Values.mqtt.existingSecretKeys.broker }}
- name: MQTT_USERNAME
valueFrom:
secretKeyRef:
name: {{ include "shelly-exporter.secretName" . }}
key: {{ .Values.mqtt.existingSecretKeys.username }}
optional: true
- name: MQTT_PASSWORD
valueFrom:
secretKeyRef:
name: {{ include "shelly-exporter.secretName" . }}
key: {{ .Values.mqtt.existingSecretKeys.password }}
optional: true
- name: MQTT_CLIENT_ID
value: {{ default (include "shelly-exporter.fullname" .) .Values.mqtt.clientId | quote }}
- name: MQTT_QOS
value: {{ .Values.mqtt.qos | quote }}
{{- with .Values.mqtt.topics }}
- name: MQTT_TOPICS
value: {{ join "," . | quote }}
{{- end }}
{{- with .Values.mqtt.wildcardTopic }}
- name: MQTT_WILDCARD_TOPIC
value: {{ . | quote }}
{{- end }}
{{- with .Values.extraEnv }}
{{- toYaml . | nindent 12 }}
{{- end }}
ports:
- name: metrics
containerPort: {{ .Values.service.port }}
protocol: TCP
livenessProbe:
httpGet:
path: /metrics
port: metrics
readinessProbe:
httpGet:
path: /metrics
port: metrics
resources:
{{- toYaml .Values.resources | nindent 12 }}
{{- with .Values.nodeSelector }}
nodeSelector:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.affinity }}
affinity:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.tolerations }}
tolerations:
{{- toYaml . | nindent 8 }}
{{- end }}

View file

@ -0,0 +1,17 @@
{{- if not .Values.mqtt.existingSecret }}
apiVersion: v1
kind: Secret
metadata:
name: {{ include "shelly-exporter.fullname" . }}
labels:
{{- include "shelly-exporter.labels" . | nindent 4 }}
type: Opaque
stringData:
{{ .Values.mqtt.existingSecretKeys.broker }}: {{ required "mqtt.broker or mqtt.existingSecret is required" .Values.mqtt.broker | quote }}
{{- with .Values.mqtt.username }}
{{ $.Values.mqtt.existingSecretKeys.username }}: {{ . | quote }}
{{- end }}
{{- with .Values.mqtt.password }}
{{ $.Values.mqtt.existingSecretKeys.password }}: {{ . | quote }}
{{- end }}
{{- end }}

View file

@ -0,0 +1,15 @@
apiVersion: v1
kind: Service
metadata:
name: {{ include "shelly-exporter.fullname" . }}
labels:
{{- include "shelly-exporter.labels" . | nindent 4 }}
spec:
type: {{ .Values.service.type }}
ports:
- name: metrics
port: {{ .Values.service.port }}
targetPort: metrics
protocol: TCP
selector:
{{- include "shelly-exporter.selectorLabels" . | nindent 4 }}

View file

@ -0,0 +1,28 @@
{{- if .Values.serviceMonitor.enabled }}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: {{ include "shelly-exporter.fullname" . }}
labels:
{{- include "shelly-exporter.labels" . | nindent 4 }}
{{- with .Values.serviceMonitor.labels }}
{{- toYaml . | nindent 4 }}
{{- end }}
spec:
selector:
matchLabels:
{{- include "shelly-exporter.selectorLabels" . | nindent 6 }}
endpoints:
- port: metrics
path: /metrics
interval: {{ .Values.serviceMonitor.interval }}
scrapeTimeout: {{ .Values.serviceMonitor.scrapeTimeout }}
{{- with .Values.serviceMonitor.relabelings }}
relabelings:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.serviceMonitor.metricRelabelings }}
metricRelabelings:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- end }}

View file

@ -0,0 +1,79 @@
image:
repository: jonasled.dev/jonasled/shelly-prometheus-exporter
# Defaults to the chart appVersion
tag: ""
# Empty uses the Kubernetes default (Always for "latest", IfNotPresent otherwise)
pullPolicy: ""
imagePullSecrets: []
nameOverride: ""
fullnameOverride: ""
mqtt:
# Inline connection settings. Ignored when existingSecret is set.
# Broker URL, e.g. tcp://192.168.1.10:1883 or ssl://broker:8883
broker: ""
# Leave username/password empty for brokers without authentication
username: ""
password: ""
# Use an existing secret instead of the inline values above.
# The username and password keys are optional in the secret.
existingSecret: ""
existingSecretKeys:
broker: MQTT_BROKER
username: MQTT_USERNAME
password: MQTT_PASSWORD
# Defaults to the release fullname. Must be unique per broker.
clientId: ""
qos: 0
# Explicit device topic prefixes, e.g. [shellyplussmoke-halle-2]
topics: []
# Topic prefix with MQTT wildcard, e.g. "+" or "shellies/+"
wildcardTopic: ""
# ERROR, WARN, INFO or DEBUG
logLevel: INFO
# Additional environment variables for the container
extraEnv: []
service:
type: ClusterIP
port: 9784
serviceMonitor:
enabled: true
interval: 30s
scrapeTimeout: 10s
# Additional labels, e.g. to match the serviceMonitorSelector of your Prometheus
labels: {}
relabelings: []
metricRelabelings: []
podAnnotations: {}
podLabels: {}
podSecurityContext:
runAsNonRoot: true
seccompProfile:
type: RuntimeDefault
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop:
- ALL
resources:
requests:
cpu: 10m
memory: 16Mi
limits:
memory: 64Mi
nodeSelector: {}
tolerations: []
affinity: {}