Skip to content

Commit e3d4fb6

Browse files
jeremie-seguinJérémie Seguin
authored andcommitted
Add Support for Custom TLS Certificates in Connection Pooler (zalando#2146)
* add volume with custom TLS config to pooler deployment * bump pg bouncer image tag which support new feature Co-authored-by: Jérémie Seguin <[email protected]>
1 parent 0745ce9 commit e3d4fb6

File tree

7 files changed

+54
-6
lines changed

7 files changed

+54
-6
lines changed

charts/postgres-operator/crds/operatorconfigurations.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ spec:
631631
default: "pooler"
632632
connection_pooler_image:
633633
type: string
634-
default: "registry.opensource.zalan.do/acid/pgbouncer:master-24"
634+
default: "registry.opensource.zalan.do/acid/pgbouncer:master-26"
635635
connection_pooler_max_db_connections:
636636
type: integer
637637
default: 60

charts/postgres-operator/values.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ configConnectionPooler:
412412
# db user for pooler to use
413413
connection_pooler_user: "pooler"
414414
# docker image
415-
connection_pooler_image: "registry.opensource.zalan.do/acid/pgbouncer:master-24"
415+
connection_pooler_image: "registry.opensource.zalan.do/acid/pgbouncer:master-26"
416416
# max db connections the pooler should hold
417417
connection_pooler_max_db_connections: 60
418418
# default pooling mode

manifests/configmap.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ data:
1717
# connection_pooler_default_cpu_request: "500m"
1818
# connection_pooler_default_memory_limit: 100Mi
1919
# connection_pooler_default_memory_request: 100Mi
20-
connection_pooler_image: "registry.opensource.zalan.do/acid/pgbouncer:master-24"
20+
connection_pooler_image: "registry.opensource.zalan.do/acid/pgbouncer:master-26"
2121
# connection_pooler_max_db_connections: 60
2222
# connection_pooler_mode: "transaction"
2323
# connection_pooler_number_of_instances: 2

manifests/minimal-fake-pooler-deployment.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ spec:
2323
serviceAccountName: postgres-operator
2424
containers:
2525
- name: postgres-operator
26-
image: registry.opensource.zalan.do/acid/pgbouncer:master-24
26+
image: registry.opensource.zalan.do/acid/pgbouncer:master-26
2727
imagePullPolicy: IfNotPresent
2828
resources:
2929
requests:

manifests/operatorconfiguration.crd.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ spec:
629629
default: "pooler"
630630
connection_pooler_image:
631631
type: string
632-
default: "registry.opensource.zalan.do/acid/pgbouncer:master-24"
632+
default: "registry.opensource.zalan.do/acid/pgbouncer:master-26"
633633
connection_pooler_max_db_connections:
634634
type: integer
635635
default: 60

manifests/postgresql-operator-default-configuration.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ configuration:
201201
connection_pooler_default_cpu_request: "500m"
202202
connection_pooler_default_memory_limit: 100Mi
203203
connection_pooler_default_memory_request: 100Mi
204-
connection_pooler_image: "registry.opensource.zalan.do/acid/pgbouncer:master-24"
204+
connection_pooler_image: "registry.opensource.zalan.do/acid/pgbouncer:master-26"
205205
# connection_pooler_max_db_connections: 60
206206
connection_pooler_mode: "transaction"
207207
connection_pooler_number_of_instances: 2

pkg/cluster/connection_pooler.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cluster
33
import (
44
"context"
55
"fmt"
6+
"path/filepath"
67
"strings"
78
"time"
89

@@ -336,6 +337,52 @@ func (c *Cluster) generateConnectionPoolerPodTemplate(role PostgresRole) (
336337
},
337338
}
338339

340+
// If the cluster has custom TLS certificates configured, we do the following:
341+
// 1. Add environment variables to tell pgBouncer where to find the TLS certificates
342+
// 2. Reference the secret in a volume
343+
// 3. Mount the volume to the container at /tls
344+
poolerVolumes := []v1.Volume{}
345+
if spec.TLS != nil && spec.TLS.SecretName != "" {
346+
// Env vars
347+
crtFile := spec.TLS.CertificateFile
348+
keyFile := spec.TLS.PrivateKeyFile
349+
if crtFile == "" {
350+
crtFile = "tls.crt"
351+
}
352+
if keyFile == "" {
353+
crtFile = "tls.key"
354+
}
355+
356+
envVars = append(
357+
envVars,
358+
v1.EnvVar{
359+
Name: "CONNECTION_POOLER_CLIENT_TLS_CRT", Value: filepath.Join("/tls", crtFile),
360+
},
361+
v1.EnvVar{
362+
Name: "CONNECTION_POOLER_CLIENT_TLS_KEY", Value: filepath.Join("/tls", keyFile),
363+
},
364+
)
365+
366+
// Volume
367+
mode := int32(0640)
368+
volume := v1.Volume{
369+
Name: "tls",
370+
VolumeSource: v1.VolumeSource{
371+
Secret: &v1.SecretVolumeSource{
372+
SecretName: spec.TLS.SecretName,
373+
DefaultMode: &mode,
374+
},
375+
},
376+
}
377+
poolerVolumes = append(poolerVolumes, volume)
378+
379+
// Mount
380+
poolerContainer.VolumeMounts = []v1.VolumeMount{{
381+
Name: "tls",
382+
MountPath: "/tls",
383+
}}
384+
}
385+
339386
tolerationsSpec := tolerations(&spec.Tolerations, c.OpConfig.PodToleration)
340387

341388
podTemplate := &v1.PodTemplateSpec{
@@ -348,6 +395,7 @@ func (c *Cluster) generateConnectionPoolerPodTemplate(role PostgresRole) (
348395
TerminationGracePeriodSeconds: &gracePeriod,
349396
Containers: []v1.Container{poolerContainer},
350397
Tolerations: tolerationsSpec,
398+
Volumes: poolerVolumes,
351399
},
352400
}
353401

0 commit comments

Comments
 (0)