Skip to content

Commit 34678e9

Browse files
committed
Attach additional volume for pgbouncer
* Add additional volume for pgbouncer pods Change the API to allow users to specify preexisting PVCs to attach to specified containers in the pgbouncer instance pods. The spec allows users to specify whether to add the volume to - all containers (by omitting the containers list) - no containers (by specifying an empty containers list) - a list of named containers If any of the named containers isn't present, we continue to reconcile, but issue a warning event with the names of the missing containers. Issues: [PGO-2557]
1 parent 6b01868 commit 34678e9

File tree

6 files changed

+177
-1
lines changed

6 files changed

+177
-1
lines changed

config/crd/bases/postgres-operator.crunchydata.com_postgresclusters.yaml

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16097,6 +16097,55 @@ spec:
1609716097
- whenUnsatisfiable
1609816098
type: object
1609916099
type: array
16100+
volumes:
16101+
description: PGBouncerVolumesSpec defines the configuration
16102+
for pgBouncer additional volumes
16103+
properties:
16104+
additional:
16105+
description: Additional pre-existing volumes to add to
16106+
the pod.
16107+
items:
16108+
properties:
16109+
claimName:
16110+
description: A reference to a preexisting PVC.
16111+
maxLength: 253
16112+
minLength: 1
16113+
pattern: ^[a-z0-9]([-a-z0-9]*[a-z0-9])?([.][a-z0-9]([-a-z0-9]*[a-z0-9])?)*$
16114+
type: string
16115+
containers:
16116+
description: |-
16117+
The containers to attach this volume to.
16118+
An omitted `Containers` field matches all containers.
16119+
An empty `Containers` field matches no containers.
16120+
items:
16121+
type: string
16122+
maxItems: 10
16123+
type: array
16124+
x-kubernetes-list-type: atomic
16125+
name:
16126+
allOf:
16127+
- maxLength: 63
16128+
- maxLength: 55
16129+
description: |-
16130+
The name of the volume used for mounting path.
16131+
Volumes are mounted in the pods at `volumes/<NAME>`
16132+
Must be unique.
16133+
minLength: 1
16134+
pattern: ^[a-z0-9]([-a-z0-9]*[a-z0-9])?$
16135+
type: string
16136+
readOnly:
16137+
description: Sets the write/read mode of the volume
16138+
type: boolean
16139+
required:
16140+
- claimName
16141+
- name
16142+
type: object
16143+
maxItems: 10
16144+
type: array
16145+
x-kubernetes-list-map-keys:
16146+
- name
16147+
x-kubernetes-list-type: map
16148+
type: object
1610016149
type: object
1610116150
required:
1610216151
- pgBouncer
@@ -34715,6 +34764,55 @@ spec:
3471534764
- whenUnsatisfiable
3471634765
type: object
3471734766
type: array
34767+
volumes:
34768+
description: PGBouncerVolumesSpec defines the configuration
34769+
for pgBouncer additional volumes
34770+
properties:
34771+
additional:
34772+
description: Additional pre-existing volumes to add to
34773+
the pod.
34774+
items:
34775+
properties:
34776+
claimName:
34777+
description: A reference to a preexisting PVC.
34778+
maxLength: 253
34779+
minLength: 1
34780+
pattern: ^[a-z0-9]([-a-z0-9]*[a-z0-9])?([.][a-z0-9]([-a-z0-9]*[a-z0-9])?)*$
34781+
type: string
34782+
containers:
34783+
description: |-
34784+
The containers to attach this volume to.
34785+
An omitted `Containers` field matches all containers.
34786+
An empty `Containers` field matches no containers.
34787+
items:
34788+
type: string
34789+
maxItems: 10
34790+
type: array
34791+
x-kubernetes-list-type: atomic
34792+
name:
34793+
allOf:
34794+
- maxLength: 63
34795+
- maxLength: 55
34796+
description: |-
34797+
The name of the volume used for mounting path.
34798+
Volumes are mounted in the pods at `volumes/<NAME>`
34799+
Must be unique.
34800+
minLength: 1
34801+
pattern: ^[a-z0-9]([-a-z0-9]*[a-z0-9])?$
34802+
type: string
34803+
readOnly:
34804+
description: Sets the write/read mode of the volume
34805+
type: boolean
34806+
required:
34807+
- claimName
34808+
- name
34809+
type: object
34810+
maxItems: 10
34811+
type: array
34812+
x-kubernetes-list-map-keys:
34813+
- name
34814+
x-kubernetes-list-type: map
34815+
type: object
3471834816
type: object
3471934817
required:
3472034818
- pgBouncer

internal/controller/postgrescluster/instance.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1259,7 +1259,7 @@ func (r *Reconciler) reconcileInstance(
12591259

12601260
if len(missingContainers) > 0 {
12611261
r.Recorder.Eventf(cluster, corev1.EventTypeWarning, "SpecifiedContainerNotFound",
1262-
"The following containers were specified for additional volumes but cannot be found: %s.", missingContainers)
1262+
"The following Postgres pod containers were specified for additional volumes but cannot be found: %s.", missingContainers)
12631263
}
12641264
}
12651265

internal/controller/postgrescluster/pgbouncer.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,16 @@ func (r *Reconciler) generatePGBouncerDeployment(
475475
// Add tmp directory and volume for log files
476476
AddTMPEmptyDir(&deploy.Spec.Template)
477477

478+
// mount additional volumes to the pgbouncer containers
479+
if err == nil && cluster.Spec.Proxy.PGBouncer.Volumes != nil && len(cluster.Spec.Proxy.PGBouncer.Volumes.Additional) > 0 {
480+
missingContainers := addAdditionalVolumesToSpecifiedContainers(&deploy.Spec.Template, cluster.Spec.Proxy.PGBouncer.Volumes.Additional)
481+
482+
if len(missingContainers) > 0 {
483+
r.Recorder.Eventf(cluster, corev1.EventTypeWarning, "SpecifiedContainerNotFound",
484+
"The following pgBouncer pod containers were specified for additional volumes but cannot be found: %s.", missingContainers)
485+
}
486+
}
487+
478488
return deploy, true, err
479489
}
480490

internal/controller/postgrescluster/pgbouncer_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,34 @@ topologySpreadConstraints:
509509
assert.Assert(t, deploy.Spec.Template.Spec.TopologySpreadConstraints == nil)
510510
})
511511
})
512+
513+
t.Run("PodSpecWithAdditionalVolumes", func(t *testing.T) {
514+
cluster := cluster.DeepCopy()
515+
cluster.Spec.Proxy.PGBouncer.Volumes = &v1beta1.PGBouncerVolumesSpec{
516+
Additional: []v1beta1.AdditionalVolume{{
517+
ClaimName: "required",
518+
Name: "required",
519+
}},
520+
}
521+
522+
deploy, specified, err := reconciler.generatePGBouncerDeployment(
523+
ctx, cluster, primary, configmap, secret)
524+
525+
assert.NilError(t, err)
526+
assert.Assert(t, specified)
527+
528+
for _, container := range deploy.Spec.Template.Spec.Containers {
529+
assert.Assert(t, cmp.MarshalContains(container.VolumeMounts,
530+
`- mountPath: /volumes/required
531+
name: volumes-required`))
532+
}
533+
534+
assert.Assert(t, cmp.MarshalContains(
535+
deploy.Spec.Template.Spec.Volumes,
536+
`- name: volumes-required
537+
persistentVolumeClaim:
538+
claimName: required`))
539+
})
512540
}
513541

514542
func TestReconcilePGBouncerDisruptionBudget(t *testing.T) {

pkg/apis/postgres-operator.crunchydata.com/v1beta1/pgbouncer_types.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,19 @@ type PGBouncerPodSpec struct {
131131
// More info: https://kubernetes.io/docs/concepts/workloads/pods/pod-topology-spread-constraints/
132132
// +optional
133133
TopologySpreadConstraints []corev1.TopologySpreadConstraint `json:"topologySpreadConstraints,omitempty"`
134+
135+
Volumes *PGBouncerVolumesSpec `json:"volumes,omitempty"`
136+
}
137+
138+
// PGBouncerVolumesSpec defines the configuration for pgBouncer additional volumes
139+
type PGBouncerVolumesSpec struct {
140+
// Additional pre-existing volumes to add to the pod.
141+
// ---
142+
// +optional
143+
// +listType=map
144+
// +listMapKey=name
145+
// +kubebuilder:validation:MaxItems=10
146+
Additional []AdditionalVolume `json:"additional,omitempty"`
134147
}
135148

136149
// PGBouncerSidecars defines the configuration for pgBouncer sidecar containers

pkg/apis/postgres-operator.crunchydata.com/v1beta1/zz_generated.deepcopy.go

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)