From 075caf5e0999d4ef92027656f3d26ab8bdbfcdef Mon Sep 17 00:00:00 2001 From: Aris Boutselis Date: Wed, 31 May 2023 17:48:57 +0100 Subject: [PATCH] feat: add namespace selector for ServiceMonitor (#135) Signed-off-by: Aris Boutselis Co-authored-by: Aris Boutselis --- chart/operator/templates/_helpers.tpl | 7 +++++++ .../templates/controller-manager-metrics-monitor.yaml | 8 +++++++- chart/operator/values.yaml | 2 ++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/chart/operator/templates/_helpers.tpl b/chart/operator/templates/_helpers.tpl index 7ba5edc2..70adb03a 100644 --- a/chart/operator/templates/_helpers.tpl +++ b/chart/operator/templates/_helpers.tpl @@ -60,3 +60,10 @@ Create the name of the service account to use {{- default "default" .Values.serviceAccount.name }} {{- end }} {{- end }} + +{{/* +Create the name of the service account to use. +*/}} +{{- define "k8sgpt-operator.namespace" -}} +{{- default .Release.Namespace }} +{{- end }} diff --git a/chart/operator/templates/controller-manager-metrics-monitor.yaml b/chart/operator/templates/controller-manager-metrics-monitor.yaml index c9a93bb1..b3c48348 100644 --- a/chart/operator/templates/controller-manager-metrics-monitor.yaml +++ b/chart/operator/templates/controller-manager-metrics-monitor.yaml @@ -3,6 +3,7 @@ apiVersion: monitoring.coreos.com/v1 kind: ServiceMonitor metadata: name: {{ include "chart.fullname" . }}-controller-manager-metrics-monitor + namespace: {{ .Values.serviceMonitor.namespace | default (include "k8sgpt-operator.namespace" . ) }} labels: app.kubernetes.io/component: metrics app.kubernetes.io/created-by: k8sgpt-operator @@ -13,6 +14,11 @@ metadata: {{- toYaml .Values.serviceMonitor.additionalLabels | nindent 4 }} {{- end }} spec: + {{- if .Values.serviceMonitor.namespace }} + namespaceSelector: + matchNames: + - {{ include "k8sgpt-operator.namespace" . }} + {{- end }} endpoints: - bearerTokenFile: /var/run/secrets/kubernetes.io/serviceaccount/token path: /metrics @@ -23,4 +29,4 @@ spec: selector: matchLabels: control-plane: controller-manager -{{ end -}} \ No newline at end of file +{{ end -}} diff --git a/chart/operator/values.yaml b/chart/operator/values.yaml index 6a95f7d1..cc983bbe 100644 --- a/chart/operator/values.yaml +++ b/chart/operator/values.yaml @@ -1,6 +1,8 @@ serviceMonitor: enabled: false additionalLabels: {} + # The namespace where Prometheus expects to find the serviceMonitor + # namespace: "" grafanaDashboard: enabled: false folder: