Skip to content

Commit 2dae26c

Browse files
More controls for pgbouncer secrets configuration
closes: #45171 Allow to disable adding default secret mounts for pgbouncer configs as well as metrics exported database url env variable. This can be useful for cases, where the value is retrieved other way, e.g. secrets provider class. Signed-off-by: Andrii Korotkov <[email protected]>
1 parent 282063b commit 2dae26c

9 files changed

+116
-13
lines changed

chart/templates/pgbouncer/pgbouncer-deployment.yaml

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ spec:
6767
tier: airflow
6868
component: pgbouncer
6969
release: {{ .Release.Name }}
70-
{{- with .Values.labels }}
71-
{{- toYaml . | nindent 8 }}
70+
{{- if or (.Values.labels) (.Values.pgbouncer.labels) }}
71+
{{- mustMerge .Values.pgbouncer.labels .Values.labels | toYaml | nindent 8 }}
7272
{{- end }}
7373
annotations:
7474
checksum/pgbouncer-config-secret: {{ include (print $.Template.BasePath "/secrets/pgbouncer-config-secret.yaml") . | sha256sum }}
@@ -118,7 +118,9 @@ spec:
118118
readinessProbe:
119119
tcpSocket:
120120
port: {{ .Values.ports.pgbouncer }}
121+
{{- if or .Values.pgbouncer.mountConfigSecret .Values.pgbouncer.ssl.ca .Values.pgbouncer.ssl.cert .Values.pgbouncer.ssl.key .Values.volumeMounts .Values.pgbouncer.extraVolumeMounts }}
121122
volumeMounts:
123+
{{- if .Values.pgbouncer.mountConfigSecret }}
122124
- name: pgbouncer-config
123125
subPath: pgbouncer.ini
124126
mountPath: /etc/pgbouncer/pgbouncer.ini
@@ -127,6 +129,7 @@ spec:
127129
subPath: users.txt
128130
mountPath: /etc/pgbouncer/users.txt
129131
readOnly: true
132+
{{- end}}
130133
{{- if .Values.pgbouncer.ssl.ca }}
131134
- name: pgbouncer-certificates
132135
subPath: root.crt
@@ -151,6 +154,7 @@ spec:
151154
{{- if .Values.pgbouncer.extraVolumeMounts }}
152155
{{- tpl (toYaml .Values.pgbouncer.extraVolumeMounts) . | nindent 12 }}
153156
{{- end }}
157+
{{- end}}
154158
{{- if $containerLifecycleHooks }}
155159
lifecycle: {{- tpl (toYaml $containerLifecycleHooks) . | nindent 12 }}
156160
{{- end }}
@@ -191,14 +195,21 @@ spec:
191195
{{- if $containerLifecycleHooksMetricsExporter }}
192196
lifecycle: {{- tpl (toYaml $containerLifecycleHooksMetricsExporter) . | nindent 12 }}
193197
{{- end }}
198+
{{- if .Values.pgbouncer.metricsExporterSidecar.extraVolumeMounts }}
199+
volumeMounts:
200+
{{- tpl (toYaml .Values.pgbouncer.metricsExporterSidecar.extraVolumeMounts) . | nindent 12 }}
201+
{{- end}}
194202
{{- if .Values.pgbouncer.extraContainers }}
195203
{{- tpl (toYaml .Values.pgbouncer.extraContainers) . | nindent 8 }}
196204
{{- end }}
205+
{{- if or .Values.pgbouncer.mountConfigSecret .Values.pgbouncer.ssl.ca .Values.pgbouncer.ssl.cert .Values.pgbouncer.ssl.key .Values.volumes .Values.pgbouncer.extraVolumes }}
197206
volumes:
207+
{{- if .Values.pgbouncer.mountConfigSecret }}
198208
- name: pgbouncer-config
199209
secret:
200210
secretName: {{ template "pgbouncer_config_secret" . }}
201-
{{- if or .Values.pgbouncer.ssl.ca .Values.pgbouncer.ssl.cert .Values.pgbouncer.ssl.key }}
211+
{{- end}}
212+
{{- if or .Values.pgbouncer.ssl.ca .Values.pgbouncer.ssl.cert .Values.pgbouncer.ssl.key }}
202213
- name: pgbouncer-certificates
203214
secret:
204215
secretName: {{ template "pgbouncer_certificates_secret" . }}
@@ -209,4 +220,5 @@ spec:
209220
{{- if .Values.pgbouncer.extraVolumes }}
210221
{{- tpl (toYaml .Values.pgbouncer.extraVolumes) . | nindent 8 }}
211222
{{- end }}
223+
{{- end }}
212224
{{- end }}

chart/templates/pgbouncer/pgbouncer-ingress.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ metadata:
3131
release: {{ .Release.Name }}
3232
chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
3333
heritage: {{ .Release.Service }}
34-
{{- with .Values.labels }}
35-
{{- toYaml . | nindent 4 }}
34+
{{- if or (.Values.labels) (.Values.pgbouncer.labels) }}
35+
{{- mustMerge .Values.pgbouncer.labels .Values.labels | toYaml | nindent 4 }}
3636
{{- end }}
3737
{{- with .Values.ingress.pgbouncer.annotations }}
3838
annotations: {{- toYaml . | nindent 4 }}

chart/templates/pgbouncer/pgbouncer-networkpolicy.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ metadata:
3434
release: {{ .Release.Name }}
3535
chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
3636
heritage: {{ .Release.Service }}
37-
{{- with .Values.labels }}
38-
{{- toYaml . | nindent 4 }}
37+
{{- if or (.Values.labels) (.Values.pgbouncer.labels) }}
38+
{{- mustMerge .Values.pgbouncer.labels .Values.labels | toYaml | nindent 4 }}
3939
{{- end }}
4040
spec:
4141
podSelector:

chart/templates/pgbouncer/pgbouncer-poddisruptionbudget.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ metadata:
3131
release: {{ .Release.Name }}
3232
chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
3333
heritage: {{ .Release.Service }}
34-
{{- with .Values.labels }}
35-
{{- toYaml . | nindent 4 }}
34+
{{- if or (.Values.labels) (.Values.pgbouncer.labels) }}
35+
{{- mustMerge .Values.pgbouncer.labels .Values.labels | toYaml | nindent 4 }}
3636
{{- end }}
3737
spec:
3838
selector:

chart/templates/pgbouncer/pgbouncer-service.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ metadata:
3131
release: {{ .Release.Name }}
3232
chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
3333
heritage: {{ .Release.Service }}
34-
{{- with .Values.labels }}
35-
{{- toYaml . | nindent 4 }}
34+
{{- if or (.Values.labels) (.Values.pgbouncer.labels) }}
35+
{{- mustMerge .Values.pgbouncer.labels .Values.labels | toYaml | nindent 4 }}
3636
{{- end }}
3737
annotations:
3838
prometheus.io/scrape: "true"

chart/templates/pgbouncer/pgbouncer-serviceaccount.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ metadata:
3232
release: {{ .Release.Name }}
3333
chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
3434
heritage: {{ .Release.Service }}
35-
{{- with .Values.labels }}
36-
{{- toYaml . | nindent 4 }}
35+
{{- if or (.Values.labels) (.Values.pgbouncer.labels) }}
36+
{{- mustMerge .Values.pgbouncer.labels .Values.labels | toYaml | nindent 4 }}
3737
{{- end }}
3838
{{- with .Values.pgbouncer.serviceAccount.annotations }}
3939
annotations: {{- toYaml . | nindent 4 }}

chart/values.schema.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7026,6 +7026,14 @@
70267026
"additionalProperties": false
70277027
}
70287028
},
7029+
"labels": {
7030+
"description": "Labels to add to the PgBouncer objects and pods.",
7031+
"type": "object",
7032+
"default": {},
7033+
"additionalProperties": {
7034+
"type": "string"
7035+
}
7036+
},
70297037
"enabled": {
70307038
"description": "Enable PgBouncer.",
70317039
"type": "boolean",
@@ -7081,6 +7089,12 @@
70817089
},
70827090
"default": null
70837091
},
7092+
"mountConfigSecret": {
7093+
"description": "Whether to mount the config secret files under `/etc/pgbouncer/` by default.",
7094+
"type": "boolean",
7095+
"x-docsSection": "Common",
7096+
"default": true
7097+
},
70847098
"extraNetworkPolicies": {
70857099
"description": "Additional NetworkPolicies as needed.",
70867100
"type": "array",
@@ -7619,6 +7633,14 @@
76197633
"default": 1
76207634
}
76217635
}
7636+
},
7637+
"extraVolumeMounts": {
7638+
"description": "Mount additional volumes into PgBouncer Metrics Exporter.",
7639+
"type": "array",
7640+
"default": [],
7641+
"items": {
7642+
"$ref": "#/definitions/io.k8s.api.core.v1.VolumeMount"
7643+
}
76227644
}
76237645
}
76247646
}

chart/values.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2173,6 +2173,10 @@ pgbouncer:
21732173
auth_type: scram-sha-256
21742174
auth_file: /etc/pgbouncer/users.txt
21752175

2176+
# Whether to mount the config secret files at a default location (/etc/pgbouncer/*).
2177+
# Can be skipped to allow for other means to get the values, e.g. secrets provider class.
2178+
mountConfigSecret: true
2179+
21762180
# annotations to be added to the PgBouncer deployment
21772181
annotations: {}
21782182

@@ -2279,6 +2283,8 @@ pgbouncer:
22792283
# - name: my-templated-extra-volume
22802284
# mountPath: "{{ .Values.my_custom_path }}"
22812285
# readOnly: true
2286+
# Volumes apply to all pgbouncer containers, while volume mounts apply to the pgbouncer
2287+
# container itself. Metrics exporter container has its own mounts.
22822288
extraVolumes: []
22832289
extraVolumeMounts: []
22842290

@@ -2352,6 +2358,15 @@ pgbouncer:
23522358
periodSeconds: 10
23532359
timeoutSeconds: 1
23542360

2361+
# Mount additional volumes into the metrics exporter. It can be templated like in the following example:
2362+
# extraVolumeMounts:
2363+
# - name: my-templated-extra-volume
2364+
# mountPath: "{{ .Values.my_custom_path }}"
2365+
# readOnly: true
2366+
extraVolumeMounts: []
2367+
2368+
# Labels specific to pgbouncer objects and pods
2369+
labels: {}
23552370
# Environment variables to add to pgbouncer container
23562371
env: []
23572372

helm_tests/other/test_pgbouncer.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,19 @@ def test_should_add_component_specific_annotations(self):
400400
assert "annotations" in jmespath.search("metadata", docs[0])
401401
assert jmespath.search("metadata.annotations", docs[0])["test_annotation"] == "test_annotation_value"
402402

403+
def test_should_add_component_specific_labels(self):
404+
docs = render_chart(
405+
values={
406+
"pgbouncer": {
407+
"enabled": True,
408+
"labels": {"test_label": "test_label_value"},
409+
},
410+
},
411+
show_only=["templates/pgbouncer/pgbouncer-deployment.yaml"],
412+
)
413+
assert "labels" in jmespath.search("spec.template.metadata", docs[0])
414+
assert jmespath.search("spec.template.metadata.labels", docs[0])["test_label"] == "test_label_value"
415+
403416

404417
class TestPgbouncerConfig:
405418
"""Tests PgBouncer config."""
@@ -595,6 +608,21 @@ def test_should_add_extra_containers(self):
595608
"image": "test-registry/test-repo:test-tag",
596609
}
597610

611+
def test_no_config_secret_mount(self):
612+
docs = render_chart(
613+
values={
614+
"pgbouncer": {
615+
"enabled": True,
616+
"mountConfigSecret": False,
617+
},
618+
},
619+
show_only=["templates/pgbouncer/pgbouncer-deployment.yaml"],
620+
)
621+
622+
spec = jmespath.search("spec.template.spec", docs[0])
623+
assert spec is not None
624+
assert "volumes" not in spec
625+
598626

599627
class TestPgbouncerExporter:
600628
"""Tests PgBouncer exporter."""
@@ -710,6 +738,32 @@ def test_unused_secret_key(self):
710738
"key": "connection",
711739
}
712740

741+
def test_extra_volume_mounts(self):
742+
extra_volume_mounts = [
743+
{
744+
"name": "test-volume",
745+
"mountPath": "/mnt/test_volume",
746+
}
747+
]
748+
749+
docs = render_chart(
750+
"test-pgbouncer-stats",
751+
values={
752+
"pgbouncer": {
753+
"enabled": True,
754+
"metricsExporterSidecar": {
755+
"statsSecretKey": "unused",
756+
"extraVolumeMounts": extra_volume_mounts,
757+
},
758+
},
759+
},
760+
show_only=["templates/pgbouncer/pgbouncer-deployment.yaml"],
761+
)
762+
763+
assert (
764+
jmespath.search("spec.template.spec.containers[1].volumeMounts", docs[0]) == extra_volume_mounts
765+
)
766+
713767

714768
class TestPgBouncerServiceAccount:
715769
"""Tests PgBouncer Service Account."""

0 commit comments

Comments
 (0)