diff --git a/charts/cluster/README.md b/charts/cluster/README.md index d042268365..bf853c9a30 100644 --- a/charts/cluster/README.md +++ b/charts/cluster/README.md @@ -169,6 +169,7 @@ refer to the [CloudNativePG Documentation](https://cloudnative-pg.io/documentat | cluster.monitoring.disableDefaultQueries | bool | `false` | Whether the default queries should be injected. Set it to true if you don't want to inject default queries into the cluster. | | cluster.monitoring.enabled | bool | `false` | Whether to enable monitoring | | cluster.monitoring.podMonitor.enabled | bool | `true` | Whether to enable the PodMonitor | +| cluster.monitoring.podMonitor.labels | object | `{}` | Additional labels to set on the generated PodMonitor resource. Add labels your monitoring stack requires (for example `team-name`). | | cluster.monitoring.podMonitor.metricRelabelings | list | `[]` | The list of metric relabelings for the PodMonitor. Applied to samples before ingestion. | | cluster.monitoring.podMonitor.relabelings | list | `[]` | The list of relabelings for the PodMonitor. Applied to samples before scraping. | | cluster.monitoring.prometheusRule.enabled | bool | `true` | Whether to enable the PrometheusRule automated alerts | diff --git a/charts/cluster/examples/pgbouncer.yaml b/charts/cluster/examples/pgbouncer.yaml index 94d4987e68..75755e2a28 100644 --- a/charts/cluster/examples/pgbouncer.yaml +++ b/charts/cluster/examples/pgbouncer.yaml @@ -7,6 +7,8 @@ cluster: enabled: true podMonitor: enabled: true + # labels: + # team-name: my-team backups: enabled: false diff --git a/charts/cluster/templates/cluster.yaml b/charts/cluster/templates/cluster.yaml index bb9ea770cc..26bb43ade1 100644 --- a/charts/cluster/templates/cluster.yaml +++ b/charts/cluster/templates/cluster.yaml @@ -116,7 +116,7 @@ spec: {{- end }} monitoring: - enablePodMonitor: {{ and .Values.cluster.monitoring.enabled .Values.cluster.monitoring.podMonitor.enabled }} + enablePodMonitor: false disableDefaultQueries: {{ .Values.cluster.monitoring.disableDefaultQueries }} {{- if not (empty .Values.cluster.monitoring.customQueries) }} customQueriesConfigMap: @@ -129,17 +129,5 @@ spec: {{- toYaml . | nindent 6 }} {{ end }} {{- end }} - {{- if not (empty .Values.cluster.monitoring.podMonitor.relabelings) }} - {{- with .Values.cluster.monitoring.podMonitor.relabelings }} - podMonitorRelabelings: - {{- toYaml . | nindent 6 }} - {{ end }} - {{- end }} - {{- if not (empty .Values.cluster.monitoring.podMonitor.metricRelabelings) }} - {{- with .Values.cluster.monitoring.podMonitor.metricRelabelings }} - podMonitorMetricRelabelings: - {{- toYaml . | nindent 6 }} - {{ end }} - {{- end }} {{ include "cluster.bootstrap" . | nindent 2 }} {{ include "cluster.backup" . | nindent 2 }} diff --git a/charts/cluster/templates/podmonitor.yaml b/charts/cluster/templates/podmonitor.yaml new file mode 100644 index 0000000000..10cd04b29f --- /dev/null +++ b/charts/cluster/templates/podmonitor.yaml @@ -0,0 +1,48 @@ +# +# Copyright © contributors to CloudNativePG, established as +# CloudNativePG a Series of LF Projects, LLC. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# +{{- if and .Values.cluster.monitoring.enabled .Values.cluster.monitoring.podMonitor.enabled }} +apiVersion: monitoring.coreos.com/v1 +kind: PodMonitor +metadata: + name: {{ include "cluster.fullname" . }} + namespace: {{ include "cluster.namespace" . }} + labels: + {{- include "cluster.labels" . | nindent 4 }} + {{- with .Values.cluster.monitoring.podMonitor.labels }} + {{- toYaml . | nindent 4 }} + {{- end }} + {{- with .Values.cluster.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +spec: + selector: + matchLabels: + cnpg.io/cluster: {{ include "cluster.fullname" . }} + podMetricsEndpoints: + - port: metrics + {{- with .Values.cluster.monitoring.podMonitor.relabelings }} + relabelings: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.cluster.monitoring.podMonitor.metricRelabelings }} + metricRelabelings: + {{- toYaml . | nindent 8 }} + {{- end }} +{{- end }} diff --git a/charts/cluster/test/monitoring/01-monitoring_cluster-assert.yaml b/charts/cluster/test/monitoring/01-monitoring_cluster-assert.yaml index 92ac447b5e..9086c7b1f4 100644 --- a/charts/cluster/test/monitoring/01-monitoring_cluster-assert.yaml +++ b/charts/cluster/test/monitoring/01-monitoring_cluster-assert.yaml @@ -36,6 +36,9 @@ apiVersion: monitoring.coreos.com/v1 kind: PodMonitor metadata: name: monitoring-cluster + labels: + team-name: test-team + environment: test spec: selector: matchLabels: diff --git a/charts/cluster/test/monitoring/01-monitoring_cluster.yaml b/charts/cluster/test/monitoring/01-monitoring_cluster.yaml index 314fccfb22..9e07374dc9 100644 --- a/charts/cluster/test/monitoring/01-monitoring_cluster.yaml +++ b/charts/cluster/test/monitoring/01-monitoring_cluster.yaml @@ -21,6 +21,9 @@ cluster: usage: GAUGE description: "Cache hit ratio" podMonitor: + labels: + team-name: test-team + environment: test relabelings: - targetLabel: environment replacement: test diff --git a/charts/cluster/values.schema.json b/charts/cluster/values.schema.json index 8890226868..d49975eca5 100644 --- a/charts/cluster/values.schema.json +++ b/charts/cluster/values.schema.json @@ -244,6 +244,9 @@ "metricRelabelings": { "type": "array" }, + "labels": { + "type": "object" + }, "relabelings": { "type": "array" } diff --git a/charts/cluster/values.yaml b/charts/cluster/values.yaml index 3d5f11dc1a..5a1f36c2e9 100644 --- a/charts/cluster/values.yaml +++ b/charts/cluster/values.yaml @@ -309,6 +309,9 @@ cluster: podMonitor: # -- Whether to enable the PodMonitor enabled: true + # -- Additional labels to set on the generated PodMonitor resource. + # Add labels your monitoring stack requires (for example `team-name`). + labels: {} # --The list of relabelings for the PodMonitor. # Applied to samples before scraping. relabelings: []