Skip to content

Commit

Permalink
More controls for pgbouncer secrets configuration (apache#45248)
Browse files Browse the repository at this point in the history
closes: apache#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]>
  • Loading branch information
andrii-korotkov-verkada authored Jan 12, 2025
1 parent 282063b commit a90ec20
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 13 deletions.
18 changes: 15 additions & 3 deletions chart/templates/pgbouncer/pgbouncer-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ spec:
tier: airflow
component: pgbouncer
release: {{ .Release.Name }}
{{- with .Values.labels }}
{{- toYaml . | nindent 8 }}
{{- if or (.Values.labels) (.Values.pgbouncer.labels) }}
{{- mustMerge .Values.pgbouncer.labels .Values.labels | toYaml | nindent 8 }}
{{- end }}
annotations:
checksum/pgbouncer-config-secret: {{ include (print $.Template.BasePath "/secrets/pgbouncer-config-secret.yaml") . | sha256sum }}
Expand Down Expand Up @@ -118,7 +118,9 @@ spec:
readinessProbe:
tcpSocket:
port: {{ .Values.ports.pgbouncer }}
{{- if or .Values.pgbouncer.mountConfigSecret .Values.pgbouncer.ssl.ca .Values.pgbouncer.ssl.cert .Values.pgbouncer.ssl.key .Values.volumeMounts .Values.pgbouncer.extraVolumeMounts }}
volumeMounts:
{{- if .Values.pgbouncer.mountConfigSecret }}
- name: pgbouncer-config
subPath: pgbouncer.ini
mountPath: /etc/pgbouncer/pgbouncer.ini
Expand All @@ -127,6 +129,7 @@ spec:
subPath: users.txt
mountPath: /etc/pgbouncer/users.txt
readOnly: true
{{- end}}
{{- if .Values.pgbouncer.ssl.ca }}
- name: pgbouncer-certificates
subPath: root.crt
Expand All @@ -151,6 +154,7 @@ spec:
{{- if .Values.pgbouncer.extraVolumeMounts }}
{{- tpl (toYaml .Values.pgbouncer.extraVolumeMounts) . | nindent 12 }}
{{- end }}
{{- end}}
{{- if $containerLifecycleHooks }}
lifecycle: {{- tpl (toYaml $containerLifecycleHooks) . | nindent 12 }}
{{- end }}
Expand Down Expand Up @@ -191,14 +195,21 @@ spec:
{{- if $containerLifecycleHooksMetricsExporter }}
lifecycle: {{- tpl (toYaml $containerLifecycleHooksMetricsExporter) . | nindent 12 }}
{{- end }}
{{- if .Values.pgbouncer.metricsExporterSidecar.extraVolumeMounts }}
volumeMounts:
{{- tpl (toYaml .Values.pgbouncer.metricsExporterSidecar.extraVolumeMounts) . | nindent 12 }}
{{- end}}
{{- if .Values.pgbouncer.extraContainers }}
{{- tpl (toYaml .Values.pgbouncer.extraContainers) . | nindent 8 }}
{{- end }}
{{- if or .Values.pgbouncer.mountConfigSecret .Values.pgbouncer.ssl.ca .Values.pgbouncer.ssl.cert .Values.pgbouncer.ssl.key .Values.volumes .Values.pgbouncer.extraVolumes }}
volumes:
{{- if .Values.pgbouncer.mountConfigSecret }}
- name: pgbouncer-config
secret:
secretName: {{ template "pgbouncer_config_secret" . }}
{{- if or .Values.pgbouncer.ssl.ca .Values.pgbouncer.ssl.cert .Values.pgbouncer.ssl.key }}
{{- end}}
{{- if or .Values.pgbouncer.ssl.ca .Values.pgbouncer.ssl.cert .Values.pgbouncer.ssl.key }}
- name: pgbouncer-certificates
secret:
secretName: {{ template "pgbouncer_certificates_secret" . }}
Expand All @@ -209,4 +220,5 @@ spec:
{{- if .Values.pgbouncer.extraVolumes }}
{{- tpl (toYaml .Values.pgbouncer.extraVolumes) . | nindent 8 }}
{{- end }}
{{- end }}
{{- end }}
4 changes: 2 additions & 2 deletions chart/templates/pgbouncer/pgbouncer-ingress.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ metadata:
release: {{ .Release.Name }}
chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
heritage: {{ .Release.Service }}
{{- with .Values.labels }}
{{- toYaml . | nindent 4 }}
{{- if or (.Values.labels) (.Values.pgbouncer.labels) }}
{{- mustMerge .Values.pgbouncer.labels .Values.labels | toYaml | nindent 4 }}
{{- end }}
{{- with .Values.ingress.pgbouncer.annotations }}
annotations: {{- toYaml . | nindent 4 }}
Expand Down
4 changes: 2 additions & 2 deletions chart/templates/pgbouncer/pgbouncer-networkpolicy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ metadata:
release: {{ .Release.Name }}
chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
heritage: {{ .Release.Service }}
{{- with .Values.labels }}
{{- toYaml . | nindent 4 }}
{{- if or (.Values.labels) (.Values.pgbouncer.labels) }}
{{- mustMerge .Values.pgbouncer.labels .Values.labels | toYaml | nindent 4 }}
{{- end }}
spec:
podSelector:
Expand Down
4 changes: 2 additions & 2 deletions chart/templates/pgbouncer/pgbouncer-poddisruptionbudget.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ metadata:
release: {{ .Release.Name }}
chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
heritage: {{ .Release.Service }}
{{- with .Values.labels }}
{{- toYaml . | nindent 4 }}
{{- if or (.Values.labels) (.Values.pgbouncer.labels) }}
{{- mustMerge .Values.pgbouncer.labels .Values.labels | toYaml | nindent 4 }}
{{- end }}
spec:
selector:
Expand Down
4 changes: 2 additions & 2 deletions chart/templates/pgbouncer/pgbouncer-service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ metadata:
release: {{ .Release.Name }}
chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
heritage: {{ .Release.Service }}
{{- with .Values.labels }}
{{- toYaml . | nindent 4 }}
{{- if or (.Values.labels) (.Values.pgbouncer.labels) }}
{{- mustMerge .Values.pgbouncer.labels .Values.labels | toYaml | nindent 4 }}
{{- end }}
annotations:
prometheus.io/scrape: "true"
Expand Down
4 changes: 2 additions & 2 deletions chart/templates/pgbouncer/pgbouncer-serviceaccount.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ metadata:
release: {{ .Release.Name }}
chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
heritage: {{ .Release.Service }}
{{- with .Values.labels }}
{{- toYaml . | nindent 4 }}
{{- if or (.Values.labels) (.Values.pgbouncer.labels) }}
{{- mustMerge .Values.pgbouncer.labels .Values.labels | toYaml | nindent 4 }}
{{- end }}
{{- with .Values.pgbouncer.serviceAccount.annotations }}
annotations: {{- toYaml . | nindent 4 }}
Expand Down
22 changes: 22 additions & 0 deletions chart/values.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -7026,6 +7026,14 @@
"additionalProperties": false
}
},
"labels": {
"description": "Labels to add to the PgBouncer objects and pods.",
"type": "object",
"default": {},
"additionalProperties": {
"type": "string"
}
},
"enabled": {
"description": "Enable PgBouncer.",
"type": "boolean",
Expand Down Expand Up @@ -7081,6 +7089,12 @@
},
"default": null
},
"mountConfigSecret": {
"description": "Whether to mount the config secret files under `/etc/pgbouncer/` by default.",
"type": "boolean",
"x-docsSection": "Common",
"default": true
},
"extraNetworkPolicies": {
"description": "Additional NetworkPolicies as needed.",
"type": "array",
Expand Down Expand Up @@ -7619,6 +7633,14 @@
"default": 1
}
}
},
"extraVolumeMounts": {
"description": "Mount additional volumes into PgBouncer Metrics Exporter.",
"type": "array",
"default": [],
"items": {
"$ref": "#/definitions/io.k8s.api.core.v1.VolumeMount"
}
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions chart/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2173,6 +2173,10 @@ pgbouncer:
auth_type: scram-sha-256
auth_file: /etc/pgbouncer/users.txt

# Whether to mount the config secret files at a default location (/etc/pgbouncer/*).
# Can be skipped to allow for other means to get the values, e.g. secrets provider class.
mountConfigSecret: true

# annotations to be added to the PgBouncer deployment
annotations: {}

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

Expand Down Expand Up @@ -2352,6 +2358,15 @@ pgbouncer:
periodSeconds: 10
timeoutSeconds: 1

# Mount additional volumes into the metrics exporter. It can be templated like in the following example:
# extraVolumeMounts:
# - name: my-templated-extra-volume
# mountPath: "{{ .Values.my_custom_path }}"
# readOnly: true
extraVolumeMounts: []

# Labels specific to pgbouncer objects and pods
labels: {}
# Environment variables to add to pgbouncer container
env: []

Expand Down
54 changes: 54 additions & 0 deletions helm_tests/other/test_pgbouncer.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,19 @@ def test_should_add_component_specific_annotations(self):
assert "annotations" in jmespath.search("metadata", docs[0])
assert jmespath.search("metadata.annotations", docs[0])["test_annotation"] == "test_annotation_value"

def test_should_add_component_specific_labels(self):
docs = render_chart(
values={
"pgbouncer": {
"enabled": True,
"labels": {"test_label": "test_label_value"},
},
},
show_only=["templates/pgbouncer/pgbouncer-deployment.yaml"],
)
assert "labels" in jmespath.search("spec.template.metadata", docs[0])
assert jmespath.search("spec.template.metadata.labels", docs[0])["test_label"] == "test_label_value"


class TestPgbouncerConfig:
"""Tests PgBouncer config."""
Expand Down Expand Up @@ -595,6 +608,21 @@ def test_should_add_extra_containers(self):
"image": "test-registry/test-repo:test-tag",
}

def test_no_config_secret_mount(self):
docs = render_chart(
values={
"pgbouncer": {
"enabled": True,
"mountConfigSecret": False,
},
},
show_only=["templates/pgbouncer/pgbouncer-deployment.yaml"],
)

spec = jmespath.search("spec.template.spec", docs[0])
assert spec is not None
assert "volumes" not in spec


class TestPgbouncerExporter:
"""Tests PgBouncer exporter."""
Expand Down Expand Up @@ -710,6 +738,32 @@ def test_unused_secret_key(self):
"key": "connection",
}

def test_extra_volume_mounts(self):
extra_volume_mounts = [
{
"name": "test-volume",
"mountPath": "/mnt/test_volume",
}
]

docs = render_chart(
"test-pgbouncer-stats",
values={
"pgbouncer": {
"enabled": True,
"metricsExporterSidecar": {
"statsSecretKey": "unused",
"extraVolumeMounts": extra_volume_mounts,
},
},
},
show_only=["templates/pgbouncer/pgbouncer-deployment.yaml"],
)

assert (
jmespath.search("spec.template.spec.containers[1].volumeMounts", docs[0]) == extra_volume_mounts
)


class TestPgBouncerServiceAccount:
"""Tests PgBouncer Service Account."""
Expand Down

0 comments on commit a90ec20

Please sign in to comment.