Skip to content

Commit

Permalink
Add pod monitor for Prometheus
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrunner committed May 24, 2023
1 parent b3fbc37 commit a27a2bd
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ jobs:
- name: Setup k3s/k3d
run: c2cciutils-k8s-install

- name: Install prometheus CRD
run: |
curl https://raw.githubusercontent.com/prometheus-operator/prometheus-operator/main/jsonnet/prometheus-operator/podmonitors-crd.json --output /tmp/podmonitors-crd.json
kubectl apply -f /tmp/podmonitors-crd.json
- name: Apply
run: kubectl apply -f tests/expected.yaml

Expand Down
24 changes: 24 additions & 0 deletions templates/podmonitor.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{{- range $serviceName, $serviceDefinition := .Values.services }}
{{- if hasKey $serviceDefinition "podMonitor" }}
{{- if eq $serviceDefinition.podMonitor.enabled true }}
---
apiVersion: monitoring.coreos.com/v1
kind: PodMonitor
metadata:
{{- if hasKey $serviceDefinition.service "name" }}
name: {{ $serviceDefinition.service.name }}
{{- else }}
name: {{ include "common.fullname" ( dict "root" $ "service" $serviceDefinition "serviceName" $serviceName ) }}
{{- end }}
{{- $svcDict := deepCopy $serviceDefinition }}
{{- $svcDict := merge $svcDict (dict "annotations" $serviceDefinition.service.annotations "labels" $serviceDefinition.service.labels) }}
{{- include "common.metadata" ( dict "root" $ "service" $svcDict "serviceName" $serviceName ) | nindent 2 }}
spec:
selector: {{- include "common.selectorLabels" ( dict "root" $ "service" $serviceDefinition "serviceName" $serviceName ) | nindent 4 }}
{{- with $serviceDefinition.podMonitor.endpoint }}
podMetricsEndpoints:
- {{ . | toYaml | nindent 6 }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}
27 changes: 27 additions & 0 deletions tests/expected.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -619,3 +619,30 @@ spec:
name: hardcoded-service-name
port:
number: 8080
---
# Source: custom-pod/templates/podmonitor.yaml
apiVersion: monitoring.coreos.com/v1
kind: PodMonitor
metadata:
name: custom-custom-pod-with-service-name
labels:
helm.sh/chart: custom-pod
app.kubernetes.io/version: "1.0"
app.kubernetes.io/managed-by: Helm
app.kubernetes.io/name: custom-pod
app.kubernetes.io/instance: custom
app.kubernetes.io/component: with-service-name
one-custom-label: hello-world
annotations:
one-custom-annotation: abc
two-custom-annotations: xyz
spec:
selector:
app.kubernetes.io/name: custom-pod
app.kubernetes.io/instance: custom
app.kubernetes.io/component: with-service-name
podMetricsEndpoints:
-
honorLabels: true
interval: 10s
port: prometheus
8 changes: 7 additions & 1 deletion tests/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,16 @@ ingress:

services:
example1:
enabled: true
ingress:
enabled: true
path: /one
enabled: true
podMonitor:
enabled: true
endpoint:
port: prometheus
interval: 10s
honorLabels: true
replicaCount: 1
nodeSelector: {}
affinity: {}
Expand Down

0 comments on commit a27a2bd

Please sign in to comment.