Skip to content

Commit 6dd3aab

Browse files
authored
Pgbouncer chart: refactoring and unit tests (#2)
* tests * refactoring * push 0.1.0 version
1 parent 564dbfd commit 6dd3aab

15 files changed

Lines changed: 293 additions & 153 deletions

charts/pgbouncer/Chart.lock

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
dependencies:
2+
- name: common
3+
repository: https://code-tool.github.io/helm-charts
4+
version: 0.0.1
5+
digest: sha256:c71c9fe0d98a042fd55080c614c10679cb533c30ed4bd8a943a67c40a456f9a6
6+
generated: "2025-12-03T17:29:06.521605867+02:00"

charts/pgbouncer/Chart.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: pgbouncer
2-
version: 0.0.1
2+
version: 0.1.0
33
appVersion: 1.24.1
44
apiVersion: v2
55
description: PgBouncer is lightweight connection pooler for PostgreSQL
Lines changed: 1 addition & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,4 @@
11
{{/* vim: set filetype=mustache: */}}
2-
{{/*
3-
Expand the name of the chart.
4-
*/}}
5-
{{- define "pgbouncer.name" -}}
6-
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}}
7-
{{- end -}}
8-
9-
{{/*
10-
Create a default fully qualified app name.
11-
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
12-
If release name contains chart name it will be used as a full name.
13-
*/}}
14-
{{- define "pgbouncer.fullname" -}}
15-
{{- if .Values.fullnameOverride -}}
16-
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" -}}
17-
{{- else -}}
18-
{{- $name := default .Chart.Name .Values.nameOverride -}}
19-
{{- if contains $name .Release.Name -}}
20-
{{- .Release.Name | trunc 63 | trimSuffix "-" -}}
21-
{{- else -}}
22-
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
23-
{{- end -}}
24-
{{- end -}}
25-
{{- end -}}
26-
27-
{{/*
28-
Create chart name and version as used by the chart label.
29-
*/}}
30-
{{- define "pgbouncer.chart" -}}
31-
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}}
32-
{{- end -}}
332

343
{{/*
354
Create content for userlist.txt secret
@@ -51,7 +20,7 @@ Create the name of the service account to use
5120
*/}}
5221
{{- define "pgbouncer.serviceAccountName" -}}
5322
{{- if not .Values.serviceAccount.name -}}
54-
{{ template "pgbouncer.fullname" . }}
23+
{{ template "common.names.fullname" . }}
5524
{{- else -}}
5625
{{- .Values.serviceAccount.name | trunc 63 | trimSuffix "-" -}}
5726
{{- end -}}
@@ -79,42 +48,3 @@ Contruct and return the exporter image to use
7948
{{- end -}}
8049
{{- end -}}
8150

82-
{{/*
83-
Common labels
84-
*/}}
85-
{{- define "pgbouncer.labels" -}}
86-
helm.sh/chart: {{ include "pgbouncer.chart" . }}
87-
{{- if .Chart.AppVersion }}
88-
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
89-
{{- end }}
90-
app.kubernetes.io/name: {{ include "pgbouncer.name" . }}
91-
app.kubernetes.io/instance: {{ .Release.Name }}
92-
app.kubernetes.io/managed-by: {{ .Release.Service }}
93-
{{- end -}}
94-
95-
{{/*
96-
Selector labels
97-
*/}}
98-
{{- define "pgbouncer.selectorLabels" -}}
99-
app.kubernetes.io/name: {{ include "pgbouncer.name" . }}
100-
app.kubernetes.io/instance: {{ .Release.Name }}
101-
{{- end -}}
102-
103-
{{/*
104-
Use stable API versions (Kubernetes >=1.22)
105-
*/}}
106-
{{- define "deployment.apiVersion" -}}
107-
apps/v1
108-
{{- end -}}
109-
110-
{{- define "podDisruptionBudget.apiVersion" -}}
111-
policy/v1
112-
{{- end -}}
113-
114-
{{- define "role.apiVersion" -}}
115-
rbac.authorization.k8s.io/v1
116-
{{- end -}}
117-
118-
{{- define "roleBinding.apiVersion" -}}
119-
rbac.authorization.k8s.io/v1
120-
{{- end -}}

charts/pgbouncer/templates/configmap.yaml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
apiVersion: v1
22
kind: ConfigMap
33
metadata:
4-
name: {{ include "pgbouncer.fullname" . }}-configmap
5-
labels:
6-
{{- include "pgbouncer.labels" . | nindent 4 }}
4+
name: {{ include "common.names.fullname" . }}-configmap
5+
labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
6+
{{- if .Values.commonAnnotations }}
7+
annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
8+
{{- end }}
79
data:
810
pgbouncer.ini: |
911
[databases]

charts/pgbouncer/templates/daemonset.yaml

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,23 @@
11
{{- if eq .Values.kind "DaemonSet" }}
2-
apiVersion: {{ template "deployment.apiVersion" . }}
2+
apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
33
kind: DaemonSet
44
metadata:
5-
name: {{ include "pgbouncer.fullname" . }}
6-
labels:
7-
{{- include "pgbouncer.labels" . | nindent 4 }}
5+
name: {{ include "common.names.fullname" . }}
6+
labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
7+
{{- if .Values.commonAnnotations }}
8+
annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
9+
{{- end }}
810
spec:
911
revisionHistoryLimit: {{ .Values.revisionHistoryLimit }}
1012
{{ if .Values.minReadySeconds -}}
1113
minReadySeconds: {{ .Values.minReadySeconds }}
1214
{{ end -}}
15+
{{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.podLabels .Values.commonLabels ) "context" . ) }}
1316
selector:
14-
matchLabels:
15-
{{- include "pgbouncer.selectorLabels" . | nindent 6 }}
17+
matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
1618
template:
1719
metadata:
18-
labels:
19-
{{- include "pgbouncer.selectorLabels" . | nindent 8 }}
20-
{{- if .Values.podLabels }}
21-
{{- range $key, $value := .Values.podLabels }}
22-
{{ $key }}: {{ $value | quote }}
23-
{{- end -}}
24-
{{- end }}
20+
labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
2521
annotations:
2622
checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
2723
checksum/userlist: {{ include (print $.Template.BasePath "/userlist-secret.yaml") . | sha256sum }}
@@ -31,8 +27,8 @@ spec:
3127
prometheus.io/port: "{{ .Values.pgbouncerExporter.servicePort }}"
3228
prometheus.io/path: "/metrics"
3329
{{- end }}
34-
{{- range $key, $value := .Values.podAnnotations }}
35-
{{ $key }}: {{ $value | quote }}
30+
{{- if .Values.podAnnotations }}
31+
{{- include "common.tplvalues.render" (dict "value" .Values.podAnnotations "context" $) | nindent 8 }}
3632
{{- end }}
3733
spec:
3834
serviceAccountName: {{ template "pgbouncer.serviceAccountName" . }}
@@ -143,22 +139,22 @@ spec:
143139
- name: PGBOUNCER_USER
144140
valueFrom:
145141
secretKeyRef:
146-
name: {{ .Values.config.existingAdminSecret | default (printf "%s-secret" (include "pgbouncer.fullname" .)) }}
142+
name: {{ .Values.config.existingAdminSecret | default (printf "%s-secret" (include "common.names.fullname" .)) }}
147143
key: {{ .Values.config.adminUserKey | default "adminUser" }}
148144
- name: PGBOUNCER_PASS
149145
valueFrom:
150146
secretKeyRef:
151-
name: {{ .Values.config.existingAdminSecret | default (printf "%s-secret" (include "pgbouncer.fullname" .)) }}
147+
name: {{ .Values.config.existingAdminSecret | default (printf "%s-secret" (include "common.names.fullname" .)) }}
152148
key: {{ .Values.config.adminPasswordKey | default "adminPassword" }}
153149
- name: PGUSER
154150
valueFrom:
155151
secretKeyRef:
156-
name: {{ .Values.config.existingAdminSecret | default (printf "%s-secret" (include "pgbouncer.fullname" .)) }}
152+
name: {{ .Values.config.existingAdminSecret | default (printf "%s-secret" (include "common.names.fullname" .)) }}
157153
key: {{ .Values.config.adminUserKey | default "adminUser" }}
158154
- name: PGPASSWORD
159155
valueFrom:
160156
secretKeyRef:
161-
name: {{ .Values.config.existingAdminSecret | default (printf "%s-secret" (include "pgbouncer.fullname" .)) }}
157+
name: {{ .Values.config.existingAdminSecret | default (printf "%s-secret" (include "common.names.fullname" .)) }}
162158
key: {{ .Values.config.adminPasswordKey | default "adminPassword" }}
163159
{{- if .Values.pgbouncerExporter.resources }}
164160
resources: {{ toYaml .Values.pgbouncerExporter.resources | trimSuffix "\n" | nindent 10 }}
@@ -179,10 +175,10 @@ spec:
179175
emptyDir: {}
180176
- name: config
181177
configMap:
182-
name: {{ template "pgbouncer.fullname" . }}-configmap
178+
name: {{ template "common.names.fullname" . }}-configmap
183179
- name: userlist
184180
secret:
185-
secretName: {{ .Values.config.existingUserlistSecret | default (printf "%s-userlist-secret" (include "pgbouncer.fullname" .)) }}
181+
secretName: {{ .Values.config.existingUserlistSecret | default (printf "%s-userlist-secret" (include "common.names.fullname" .)) }}
186182
{{- if .Values.extraVolumes -}}
187183
{{ toYaml .Values.extraVolumes | trimSuffix "\n" | nindent 6 }}
188184
{{ end -}}

charts/pgbouncer/templates/deployment.yaml

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
{{- if eq .Values.kind "Deployment" }}
2-
apiVersion: {{ template "deployment.apiVersion" . }}
2+
apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
33
kind: Deployment
44
metadata:
5-
name: {{ include "pgbouncer.fullname" . }}
6-
labels:
7-
{{- include "pgbouncer.labels" . | nindent 4 }}
5+
name: {{ include "common.names.fullname" . }}
6+
labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
7+
{{- if .Values.commonAnnotations }}
8+
annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
9+
{{- end }}
810
spec:
911
replicas: {{ .Values.replicaCount }}
1012
revisionHistoryLimit: {{ .Values.revisionHistoryLimit }}
@@ -14,18 +16,12 @@ spec:
1416
{{ if .Values.minReadySeconds -}}
1517
minReadySeconds: {{ .Values.minReadySeconds }}
1618
{{ end -}}
19+
{{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.podLabels .Values.commonLabels ) "context" . ) }}
1720
selector:
18-
matchLabels:
19-
{{- include "pgbouncer.selectorLabels" . | nindent 6 }}
21+
matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
2022
template:
2123
metadata:
22-
labels:
23-
{{- include "pgbouncer.selectorLabels" . | nindent 8 }}
24-
{{- if .Values.podLabels }}
25-
{{- range $key, $value := .Values.podLabels }}
26-
{{ $key }}: {{ $value | quote }}
27-
{{- end -}}
28-
{{- end }}
24+
labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
2925
annotations:
3026
checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
3127
checksum/userlist: {{ include (print $.Template.BasePath "/userlist-secret.yaml") . | sha256sum }}
@@ -35,8 +31,8 @@ spec:
3531
prometheus.io/port: "{{ .Values.pgbouncerExporter.servicePort }}"
3632
prometheus.io/path: "/metrics"
3733
{{- end }}
38-
{{- range $key, $value := .Values.podAnnotations }}
39-
{{ $key }}: {{ $value | quote }}
34+
{{- if .Values.podAnnotations }}
35+
{{- include "common.tplvalues.render" (dict "value" .Values.podAnnotations "context" $) | nindent 8 }}
4036
{{- end }}
4137
spec:
4238
serviceAccountName: {{ template "pgbouncer.serviceAccountName" . }}
@@ -150,22 +146,22 @@ spec:
150146
- name: PGBOUNCER_USER
151147
valueFrom:
152148
secretKeyRef:
153-
name: {{ .Values.config.existingAdminSecret | default (printf "%s-secret" (include "pgbouncer.fullname" .)) }}
149+
name: {{ .Values.config.existingAdminSecret | default (printf "%s-secret" (include "common.names.fullname" .)) }}
154150
key: {{ .Values.config.adminUserKey | default "adminUser" }}
155151
- name: PGBOUNCER_PASS
156152
valueFrom:
157153
secretKeyRef:
158-
name: {{ .Values.config.existingAdminSecret | default (printf "%s-secret" (include "pgbouncer.fullname" .)) }}
154+
name: {{ .Values.config.existingAdminSecret | default (printf "%s-secret" (include "common.names.fullname" .)) }}
159155
key: {{ .Values.config.adminPasswordKey | default "adminPassword" }}
160156
- name: PGUSER
161157
valueFrom:
162158
secretKeyRef:
163-
name: {{ .Values.config.existingAdminSecret | default (printf "%s-secret" (include "pgbouncer.fullname" .)) }}
159+
name: {{ .Values.config.existingAdminSecret | default (printf "%s-secret" (include "common.names.fullname" .)) }}
164160
key: {{ .Values.config.adminUserKey | default "adminUser" }}
165161
- name: PGPASSWORD
166162
valueFrom:
167163
secretKeyRef:
168-
name: {{ .Values.config.existingAdminSecret | default (printf "%s-secret" (include "pgbouncer.fullname" .)) }}
164+
name: {{ .Values.config.existingAdminSecret | default (printf "%s-secret" (include "common.names.fullname" .)) }}
169165
key: {{ .Values.config.adminPasswordKey | default "adminPassword" }}
170166
{{- if .Values.pgbouncerExporter.resources }}
171167
resources: {{ toYaml .Values.pgbouncerExporter.resources | trimSuffix "\n" | nindent 10 }}
@@ -186,10 +182,10 @@ spec:
186182
emptyDir: {}
187183
- name: config
188184
configMap:
189-
name: {{ template "pgbouncer.fullname" . }}-configmap
185+
name: {{ template "common.names.fullname" . }}-configmap
190186
- name: userlist
191187
secret:
192-
secretName: {{ .Values.config.existingUserlistSecret | default (printf "%s-userlist-secret" (include "pgbouncer.fullname" .)) }}
188+
secretName: {{ .Values.config.existingUserlistSecret | default (printf "%s-userlist-secret" (include "common.names.fullname" .)) }}
193189
{{- if .Values.extraVolumes -}}
194190
{{ toYaml .Values.extraVolumes | trimSuffix "\n" | nindent 6 }}
195191
{{ end -}}
Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
{{- if .Values.podDisruptionBudget.enabled }}
2-
apiVersion: {{ template "podDisruptionBudget.apiVersion" . }}
1+
{{- if and .Values.pdb.create }}
2+
apiVersion: {{ include "common.capabilities.policy.apiVersion" . }}
33
kind: PodDisruptionBudget
44
metadata:
5-
name: {{ template "pgbouncer.fullname" . }}
6-
labels:
7-
{{- include "pgbouncer.labels" . | nindent 4 }}
5+
name: {{ template "common.names.fullname" . }}
6+
labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
7+
{{- if .Values.commonAnnotations }}
8+
annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
9+
{{- end }}
810
spec:
9-
{{- if .Values.podDisruptionBudget.maxUnavailable }}
10-
maxUnavailable: {{ .Values.podDisruptionBudget.maxUnavailable }}
11+
{{- if .Values.pdb.minAvailable }}
12+
minAvailable: {{ .Values.pdb.minAvailable }}
1113
{{- end }}
12-
{{- if .Values.podDisruptionBudget.minAvailable }}
13-
minAvailable: {{ .Values.podDisruptionBudget.minAvailable }}
14+
{{- if or .Values.pdb.maxUnavailable (not .Values.pdb.minAvailable) }}
15+
maxUnavailable: {{ .Values.pdb.maxUnavailable | default 1 }}
1416
{{- end }}
17+
{{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.podLabels .Values.commonLabels ) "context" . ) }}
1518
selector:
16-
matchLabels:
17-
{{- include "pgbouncer.selectorLabels" . | nindent 6 }}
18-
{{- end }}
19+
matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
20+
{{- end -}}

charts/pgbouncer/templates/podmonitor.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
apiVersion: monitoring.coreos.com/v1
33
kind: PodMonitor
44
metadata:
5-
name: {{ include "pgbouncer.fullname" . }}
6-
labels:
7-
{{- include "pgbouncer.selectorLabels" . | nindent 8 }}
5+
name: {{ include "common.names.fullname" . }}
6+
labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
87
spec:
8+
spec:
9+
{{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.podLabels .Values.commonLabels ) "context" . ) }}
910
selector:
10-
matchLabels:
11-
{{- include "pgbouncer.selectorLabels" . | nindent 8 }}
11+
matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 4 }}
1212
podMetricsEndpoints:
1313
- port: exporter
1414
{{- end }}

charts/pgbouncer/templates/secret.yaml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
apiVersion: v1
33
kind: Secret
44
metadata:
5-
name: {{ template "pgbouncer.fullname" . }}-secret
6-
labels:
7-
{{- include "pgbouncer.labels" . | nindent 4 }}
5+
name: {{ template "common.names.fullname" . }}-secret
6+
labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
7+
{{- if or .Values.secretAnnotations .Values.commonAnnotations }}
8+
{{- $annotations := include "common.tplvalues.merge" ( dict "values" ( list .Values.secretAnnotations .Values.commonAnnotations ) "context" . ) }}
9+
annotations: {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $) | nindent 4 }}
10+
{{- end }}
811
type: Opaque
912
data:
1013
adminUser: {{ required "A valid .Values.config.adminUser entry required!" .Values.config.adminUser | b64enc | quote }}
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
apiVersion: v1
22
kind: Service
33
metadata:
4-
name: {{ template "pgbouncer.fullname" . }}
5-
labels:
6-
{{- include "pgbouncer.labels" . | nindent 4 }}
7-
{{- with .Values.service.annotations }}
8-
annotations:
9-
{{- toYaml . | nindent 4 }}
4+
name: {{ template "common.names.fullname" . }}
5+
labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
6+
{{- if or .Values.secretAnnotations .Values.commonAnnotations }}
7+
{{- $annotations := include "common.tplvalues.merge" ( dict "values" ( list .Values.secretAnnotations .Values.commonAnnotations ) "context" . ) }}
8+
annotations: {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $) | nindent 4 }}
109
{{- end }}
1110
spec:
1211
type: {{ .Values.service.type }}
@@ -19,5 +18,5 @@ spec:
1918
targetPort: psql
2019
protocol: TCP
2120
name: psql
22-
selector:
23-
{{- include "pgbouncer.selectorLabels" . | nindent 4 }}
21+
{{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.podLabels .Values.commonLabels ) "context" . ) }}
22+
selector: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 4 }}

0 commit comments

Comments
 (0)