diff --git a/traefik/templates/prometheusrules.yaml b/traefik/templates/prometheusrules.yaml index fa786051b..4534964d1 100644 --- a/traefik/templates/prometheusrules.yaml +++ b/traefik/templates/prometheusrules.yaml @@ -9,9 +9,7 @@ apiVersion: monitoring.coreos.com/v1 kind: PrometheusRule metadata: name: {{ template "traefik.fullname" . }} - {{- if .Values.metrics.prometheus.prometheusRule.namespace }} - namespace: {{ .Values.metrics.prometheus.prometheusRule.namespace }} - {{- end }} + namespace: {{ .Values.metrics.prometheus.prometheusRule.namespace | default (include "traefik.namespace" .) }} labels: {{- include "traefik.labels" . | nindent 4 }} {{- with .Values.metrics.prometheus.prometheusRule.additionalLabels }} diff --git a/traefik/templates/servicemonitor.yaml b/traefik/templates/servicemonitor.yaml index f3e128405..a4a8eda92 100644 --- a/traefik/templates/servicemonitor.yaml +++ b/traefik/templates/servicemonitor.yaml @@ -9,9 +9,7 @@ apiVersion: monitoring.coreos.com/v1 kind: ServiceMonitor metadata: name: {{ template "traefik.fullname" . }} - {{- with .Values.metrics.prometheus.serviceMonitor.namespace }} - namespace: {{ . }} - {{- end }} + namespace: {{ .Values.metrics.prometheus.serviceMonitor.namespace | default (include "traefik.namespace" .) }} labels: {{- if (.Values.metrics.prometheus.service).enabled }} {{- include "traefik.metricsservicelabels" . | nindent 4 }} diff --git a/traefik/tests/prometheusrules-config_test.yaml b/traefik/tests/prometheusrules-config_test.yaml index 50399cc46..09d3dc4a0 100644 --- a/traefik/tests/prometheusrules-config_test.yaml +++ b/traefik/tests/prometheusrules-config_test.yaml @@ -51,3 +51,36 @@ tests: labels: context: traefik severity: warning + - it: should use the release namespace by default if metrics.prometheus.prometheusRule.namespace is not set + capabilities: + apiVersions: + - monitoring.coreos.com/v1 + values: + - ./values/prometheusrules.yaml + release: + namespace: release-namespace + set: + metrics: + prometheus: + prometheusRule: + namespace: "" + asserts: + - hasDocuments: + count: 1 + - equal: + path: metadata.namespace + value: release-namespace + - it: should use the release namespace if specified + capabilities: + apiVersions: + - monitoring.coreos.com/v1 + values: + - ./values/prometheusrules.yaml + release: + namespace: release-namespace + asserts: + - hasDocuments: + count: 1 + - equal: + path: metadata.namespace + value: another-namespace diff --git a/traefik/tests/servicemonitor-config_test.yaml b/traefik/tests/servicemonitor-config_test.yaml index 20f640ecc..210fc3776 100644 --- a/traefik/tests/servicemonitor-config_test.yaml +++ b/traefik/tests/servicemonitor-config_test.yaml @@ -83,3 +83,36 @@ tests: - equal: path: spec.selector.matchLabels["app.kubernetes.io/component"] value: metrics + - it: should use the release namespace by default if metrics.prometheus.serviceMonitor.namespace is not set + capabilities: + apiVersions: + - monitoring.coreos.com/v1 + values: + - ./values/servicemonitor.yaml + release: + namespace: release-namespace + set: + metrics: + prometheus: + serviceMonitor: + namespace: "" + asserts: + - hasDocuments: + count: 1 + - equal: + path: metadata.namespace + value: release-namespace + - it: should use the release namespace if specified + capabilities: + apiVersions: + - monitoring.coreos.com/v1 + values: + - ./values/servicemonitor.yaml + release: + namespace: release-namespace + asserts: + - hasDocuments: + count: 1 + - equal: + path: metadata.namespace + value: another-namespace