Skip to content

Commit f2711f3

Browse files
Dashing-Nelsonjessebotitay-grudev
authored
feat(monitoring): add support for custom PodMonitor (#724)
Signed-off-by: Nelson Alfonso <45660392+Dashing-Nelson@users.noreply.github.com> Signed-off-by: Itay Grudev <itay@verito.digital> Co-authored-by: Jesse Hitch <jessebot@linux.com> Co-authored-by: Itay Grudev <itay@verito.digital>
1 parent b1d64c2 commit f2711f3

10 files changed

Lines changed: 109 additions & 47 deletions

File tree

charts/cluster/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ refer to the [CloudNativePG Documentation](https://cloudnative-pg.io/documentati
171171
| cluster.monitoring.enabled | bool | `false` | Whether to enable monitoring |
172172
| cluster.monitoring.instrumentation.logicalReplication | bool | `true` | Enable logical replication metrics |
173173
| cluster.monitoring.podMonitor.enabled | bool | `true` | Whether to enable the PodMonitor |
174+
| cluster.monitoring.podMonitor.labels | object | `{}` | Additional labels to set on the generated PodMonitor resource. Add labels your monitoring stack requires (for example `team-name`). |
174175
| cluster.monitoring.podMonitor.metricRelabelings | list | `[]` | The list of metric relabelings for the PodMonitor. Applied to samples before ingestion. |
175176
| cluster.monitoring.podMonitor.relabelings | list | `[]` | The list of relabelings for the PodMonitor. Applied to samples before scraping. |
176177
| cluster.monitoring.prometheusRule.enabled | bool | `true` | Whether to enable the PrometheusRule automated alerts |

charts/cluster/examples/pgbouncer.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ cluster:
77
enabled: true
88
podMonitor:
99
enabled: true
10+
labels: {}
11+
# team-name: my-team
1012

1113
backups:
1214
enabled: false

charts/cluster/templates/cluster.yaml

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ spec:
126126
{{ end }}
127127

128128
monitoring:
129-
enablePodMonitor: {{ and .Values.cluster.monitoring.enabled .Values.cluster.monitoring.podMonitor.enabled }}
130129
disableDefaultQueries: {{ .Values.cluster.monitoring.disableDefaultQueries }}
131130
{{- if not (empty .Values.cluster.monitoring.customQueries) }}
132131
customQueriesConfigMap:
@@ -139,18 +138,6 @@ spec:
139138
{{- toYaml . | nindent 6 }}
140139
{{ end }}
141140
{{- end }}
142-
{{- if not (empty .Values.cluster.monitoring.podMonitor.relabelings) }}
143-
{{- with .Values.cluster.monitoring.podMonitor.relabelings }}
144-
podMonitorRelabelings:
145-
{{- toYaml . | nindent 6 }}
146-
{{ end }}
147-
{{- end }}
148-
{{- if not (empty .Values.cluster.monitoring.podMonitor.metricRelabelings) }}
149-
{{- with .Values.cluster.monitoring.podMonitor.metricRelabelings }}
150-
podMonitorMetricRelabelings:
151-
{{- toYaml . | nindent 6 }}
152-
{{ end }}
153-
{{- end }}
154141
{{ include "cluster.bootstrap" . | nindent 2 }}
155142
{{ include "cluster.externalClusters" . | nindent 2 }}
156143
{{ include "cluster.backup" . | nindent 2 }}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{{- if and .Values.cluster.monitoring.enabled .Values.cluster.monitoring.podMonitor.enabled }}
2+
apiVersion: monitoring.coreos.com/v1
3+
kind: PodMonitor
4+
metadata:
5+
name: {{ include "cluster.fullname" . }}-cluster-podmonitor
6+
namespace: {{ include "cluster.namespace" . }}
7+
labels:
8+
cnpg.io/cluster: {{ include "cluster.fullname" . }}
9+
{{- include "cluster.labels" . | nindent 4 }}
10+
{{- with .Values.cluster.monitoring.podMonitor.labels }}
11+
{{- toYaml . | nindent 4 }}
12+
{{- end }}
13+
{{- with .Values.cluster.annotations }}
14+
annotations:
15+
{{- toYaml . | nindent 4 }}
16+
{{- end }}
17+
spec:
18+
selector:
19+
matchLabels:
20+
cnpg.io/cluster: {{ include "cluster.fullname" . }}
21+
cnpg.io/podRole: instance
22+
podMetricsEndpoints:
23+
- port: metrics
24+
{{- with .Values.cluster.monitoring.podMonitor.relabelings }}
25+
relabelings:
26+
{{- toYaml . | nindent 8 }}
27+
{{- end }}
28+
{{- with .Values.cluster.monitoring.podMonitor.metricRelabelings }}
29+
metricRelabelings:
30+
{{- toYaml . | nindent 8 }}
31+
{{- end }}
32+
{{- end }}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{{- range .Values.poolers }}
2+
---
3+
{{- if and .monitoring .monitoring.enabled .monitoring.podMonitor.enabled }}
4+
apiVersion: monitoring.coreos.com/v1
5+
kind: PodMonitor
6+
metadata:
7+
name: {{ include "cluster.fullname" $ }}-pooler-{{ .name }}-podmonitor
8+
namespace: {{ include "cluster.namespace" $ }}
9+
labels:
10+
cnpg.io/cluster: {{ include "cluster.fullname" $ }}
11+
cnpg.io/poolerName: {{ include "cluster.fullname" $ }}-pooler-{{ .name }}
12+
{{- include "cluster.labels" $ | nindent 4 }}
13+
{{- with $.Values.cluster.monitoring.podMonitor.labels }}
14+
{{- toYaml . | nindent 4 }}
15+
{{- end }}
16+
{{- with $.Values.cluster.annotations }}
17+
annotations:
18+
{{- toYaml . | nindent 4 }}
19+
{{- end }}
20+
spec:
21+
selector:
22+
matchLabels:
23+
cnpg.io/cluster: {{ include "cluster.fullname" $ }}
24+
cnpg.io/podRole: pooler
25+
cnpg.io/poolerName: {{ include "cluster.fullname" $ }}-pooler-{{ .name }}
26+
podMetricsEndpoints:
27+
- port: metrics
28+
{{- with .monitoring.podMonitor.relabelings }}
29+
relabelings:
30+
{{- toYaml . | nindent 8 }}
31+
{{- end }}
32+
{{- with .monitoring.podMonitor.metricRelabelings }}
33+
metricRelabelings:
34+
{{- toYaml . | nindent 8 }}
35+
{{- end }}
36+
{{- end }}
37+
{{- end }}

charts/cluster/templates/pooler.yaml

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,6 @@ spec:
2828
pg_hba:
2929
{{- toYaml . | nindent 6 }}
3030
{{- end }}
31-
{{ with .monitoring }}
32-
monitoring:
33-
{{- if not (empty .podMonitor) }}
34-
enablePodMonitor: {{ and .enabled .podMonitor.enabled }}
35-
{{- with .podMonitor.relabelings }}
36-
podMonitorRelabelings:
37-
{{- toYaml . | nindent 6 }}
38-
{{ end }}
39-
{{- with .podMonitor.metricRelabelings }}
40-
podMonitorMetricRelabelings:
41-
{{- toYaml . | nindent 6 }}
42-
{{ end }}
43-
{{- end }}
44-
{{- end }}
4531
{{- with .template }}
4632
template:
4733
{{- . | toYaml | nindent 4 }}

charts/cluster/test/monitoring/01-monitoring_cluster-assert.yaml

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,20 @@ spec:
1616
customQueriesConfigMap:
1717
- name: monitoring-cluster-monitoring
1818
key: custom-queries
19-
enablePodMonitor: true
20-
podMonitorRelabelings:
21-
- action: replace
22-
replacement: test
23-
targetLabel: environment
24-
- action: replace
25-
replacement: alpha
26-
targetLabel: team
27-
podMonitorMetricRelabelings:
28-
- action: replace
29-
sourceLabels:
30-
- cluster
31-
targetLabel: cnpg_cluster
32-
- action: labeldrop
33-
regex: cluster
19+
enablePodMonitor: false
3420
---
3521
apiVersion: monitoring.coreos.com/v1
3622
kind: PodMonitor
3723
metadata:
38-
name: monitoring-cluster
24+
labels:
25+
cnpg.io/cluster: monitoring-cluster
26+
team-name: test-team
27+
environment: test
3928
spec:
4029
selector:
4130
matchLabels:
4231
cnpg.io/cluster: monitoring-cluster
32+
cnpg.io/podRole: instance
4333
podMetricsEndpoints:
4434
- relabelings:
4535
- targetLabel: environment
@@ -56,12 +46,12 @@ spec:
5646
---
5747
apiVersion: monitoring.coreos.com/v1
5848
kind: PodMonitor
59-
metadata:
60-
name: monitoring-cluster-pooler-rw
6149
spec:
6250
selector:
6351
matchLabels:
52+
cnpg.io/cluster: monitoring-cluster
6453
cnpg.io/poolerName: monitoring-cluster-pooler-rw
54+
cnpg.io/podRole: pooler
6555
podMetricsEndpoints:
6656
- relabelings:
6757
- targetLabel: type
@@ -80,12 +70,12 @@ spec:
8070
---
8171
apiVersion: monitoring.coreos.com/v1
8272
kind: PodMonitor
83-
metadata:
84-
name: monitoring-cluster-pooler-ro
8573
spec:
8674
selector:
8775
matchLabels:
76+
cnpg.io/cluster: monitoring-cluster
8877
cnpg.io/poolerName: monitoring-cluster-pooler-ro
78+
cnpg.io/podRole: pooler
8979
podMetricsEndpoints:
9080
- relabelings:
9181
- targetLabel: type

charts/cluster/test/monitoring/01-monitoring_cluster.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ cluster:
2121
usage: GAUGE
2222
description: "Cache hit ratio"
2323
podMonitor:
24+
labels:
25+
team-name: test-team
26+
environment: test
2427
relabelings:
2528
- targetLabel: environment
2629
replacement: test

charts/cluster/values.schema.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,9 @@
257257
"enabled": {
258258
"type": "boolean"
259259
},
260+
"labels": {
261+
"type": "object"
262+
},
260263
"metricRelabelings": {
261264
"type": "array"
262265
},

charts/cluster/values.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,9 @@ cluster:
309309
podMonitor:
310310
# -- Whether to enable the PodMonitor
311311
enabled: true
312+
# -- Additional labels to set on the generated PodMonitor resource.
313+
# Add labels your monitoring stack requires (for example `team-name`).
314+
labels: {}
312315
# --The list of relabelings for the PodMonitor.
313316
# Applied to samples before scraping.
314317
relabelings: []
@@ -629,6 +632,15 @@ poolers: []
629632
# podMonitor:
630633
# # -- Whether to enable the PodMonitor
631634
# enabled: true
635+
# # -- Additional labels to set on the generated PodMonitor resource.
636+
# # Add labels your monitoring stack requires (for example `team-name`).
637+
# labels: {}
638+
# # --The list of relabelings for the PodMonitor.
639+
# # Applied to samples before scraping.
640+
# relabelings: []
641+
# # -- The list of metric relabelings for the PodMonitor.
642+
# # Applied to samples before ingestion.
643+
# metricRelabelings: []
632644
# # -- Custom PgBouncer deployment template.
633645
# # Use to override image, specify resources, etc.
634646
# template: {}
@@ -651,6 +663,15 @@ poolers: []
651663
# podMonitor:
652664
# # -- Whether to enable the PodMonitor
653665
# enabled: true
666+
# # -- Additional labels to set on the generated PodMonitor resource.
667+
# # Add labels your monitoring stack requires (for example `team-name`).
668+
# labels: {}
669+
# # --The list of relabelings for the PodMonitor.
670+
# # Applied to samples before scraping.
671+
# relabelings: []
672+
# # -- The list of metric relabelings for the PodMonitor.
673+
# # Applied to samples before ingestion.
674+
# metricRelabelings: []
654675
# # -- Custom PgBouncer deployment template.
655676
# # Use to override image, specify resources, etc.
656677
# template: {}

0 commit comments

Comments
 (0)