Skip to content

Commit 08089ed

Browse files
FxKudmotte
andauthored
add option to prevent PVC removal on cluster deletion (#2579)
* add option to prevent PVC removal on cluster deletion * Update docs/reference/operator_parameters.md Co-authored-by: Motte <37443982+dmotte@users.noreply.github.com>
1 parent 773c039 commit 08089ed

13 files changed

+50
-3
lines changed

charts/postgres-operator/crds/operatorconfigurations.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,9 @@ spec:
211211
enable_init_containers:
212212
type: boolean
213213
default: true
214+
enable_persistent_volume_claim_deletion:
215+
type: boolean
216+
default: true
214217
enable_pod_antiaffinity:
215218
type: boolean
216219
default: false

charts/postgres-operator/values.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ configKubernetes:
129129
enable_finalizers: false
130130
# enables initContainers to run actions before Spilo is started
131131
enable_init_containers: true
132+
# toggles if operator should delete PVCs on cluster deletion
133+
enable_persistent_volume_claim_deletion: true
132134
# toggles pod anti affinity on the Postgres pods
133135
enable_pod_antiaffinity: false
134136
# toggles PDB to set to MinAvailabe 0 or 1

docs/reference/operator_parameters.md

+20
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,26 @@ configuration they are grouped under the `kubernetes` key.
346346
gone at this point.
347347
The default is `false`.
348348

349+
* **persistent_volume_claim_retention_policy**
350+
The operator tries to protect volumes as much as possible. If somebody
351+
accidentally deletes the statefulset or scales in the `numberOfInstances` the
352+
Persistent Volume Claims and thus Persistent Volumes will be retained.
353+
However, this can have some consequences when you scale out again at a much
354+
later point, for example after the cluster's Postgres major version has been
355+
upgraded, because the old volume runs the old Postgres version with stale data.
356+
Even if the version has not changed the replication lag could be massive. In
357+
this case a reinitialization of the re-added member would make sense. You can
358+
also modify the [retention policy of PVCs](https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/#persistentvolumeclaim-retention) in the operator configuration.
359+
The behavior can be changed for two scenarios: `when_deleted` - default is
360+
`"retain"` - or `when_scaled` - default is also `"retain"`. The other possible
361+
option is `delete`.
362+
363+
* **enable_persistent_volume_claim_deletion**
364+
By default, the operator deletes PersistentVolumeClaims when removing the
365+
Postgres cluster manifest, no matter if `persistent_volume_claim_retention_policy`
366+
on the statefulset is set to `retain`. To keep PVCs set this option to `false`.
367+
The default is `true`.
368+
349369
* **enable_pod_disruption_budget**
350370
PDB is enabled by default to protect the cluster from voluntarily disruptions
351371
and hence unwanted DB downtime. However, on some cloud providers it could be

e2e/tests/test_e2e.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -2048,7 +2048,8 @@ def test_zz_cluster_deletion(self):
20482048
patch_delete_annotations = {
20492049
"data": {
20502050
"delete_annotation_date_key": "delete-date",
2051-
"delete_annotation_name_key": "delete-clustername"
2051+
"delete_annotation_name_key": "delete-clustername",
2052+
"enable_persistent_volume_claim_deletion": "false"
20522053
}
20532054
}
20542055
k8s.update_config(patch_delete_annotations)
@@ -2109,6 +2110,7 @@ def test_zz_cluster_deletion(self):
21092110
self.eventuallyEqual(lambda: k8s.count_deployments_with_label(cluster_label), 0, "Deployments not deleted")
21102111
self.eventuallyEqual(lambda: k8s.count_pdbs_with_label(cluster_label), 0, "Pod disruption budget not deleted")
21112112
self.eventuallyEqual(lambda: k8s.count_secrets_with_label(cluster_label), 0, "Secrets not deleted")
2113+
self.eventuallyEqual(lambda: k8s.count_pvcs_with_label(cluster_label), 3, "PVCs were deleted although disabled in config")
21122114

21132115
except timeout_decorator.TimeoutError:
21142116
print('Operator log: {}'.format(k8s.get_operator_log()))

manifests/configmap.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ data:
4949
enable_master_pooler_load_balancer: "false"
5050
enable_password_rotation: "false"
5151
enable_patroni_failsafe_mode: "false"
52+
enable_persistent_volume_claim_deletion: "true"
5253
enable_pgversion_env_var: "true"
5354
# enable_pod_antiaffinity: "false"
5455
# enable_pod_disruption_budget: "true"

manifests/operatorconfiguration.crd.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,9 @@ spec:
209209
enable_init_containers:
210210
type: boolean
211211
default: true
212+
enable_persistent_volume_claim_deletion:
213+
type: boolean
214+
default: true
212215
enable_pod_antiaffinity:
213216
type: boolean
214217
default: false

manifests/postgresql-operator-default-configuration.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ configuration:
5959
# enable_cross_namespace_secret: "false"
6060
enable_finalizers: false
6161
enable_init_containers: true
62+
enable_persistent_volume_claim_deletion: true
6263
enable_pod_antiaffinity: false
6364
enable_pod_disruption_budget: true
6465
enable_readiness_probe: false

pkg/apis/acid.zalan.do/v1/crds.go

+3
Original file line numberDiff line numberDiff line change
@@ -1320,6 +1320,9 @@ var OperatorConfigCRDResourceValidation = apiextv1.CustomResourceValidation{
13201320
"enable_init_containers": {
13211321
Type: "boolean",
13221322
},
1323+
"enable_persistent_volume_claim_deletion": {
1324+
Type: "boolean",
1325+
},
13231326
"enable_pod_antiaffinity": {
13241327
Type: "boolean",
13251328
},

pkg/apis/acid.zalan.do/v1/operator_configuration_type.go

+1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ type KubernetesMetaConfiguration struct {
102102
PodAntiAffinityTopologyKey string `json:"pod_antiaffinity_topology_key,omitempty"`
103103
PodManagementPolicy string `json:"pod_management_policy,omitempty"`
104104
PersistentVolumeClaimRetentionPolicy map[string]string `json:"persistent_volume_claim_retention_policy,omitempty"`
105+
EnablePersistentVolumeClaimDeletion *bool `json:"enable_persistent_volume_claim_deletion,omitempty"`
105106
EnableReadinessProbe bool `json:"enable_readiness_probe,omitempty"`
106107
EnableCrossNamespaceSecret bool `json:"enable_cross_namespace_secret,omitempty"`
107108
EnableFinalizers *bool `json:"enable_finalizers,omitempty"`

pkg/apis/acid.zalan.do/v1/zz_generated.deepcopy.go

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/cluster/resources.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,12 @@ func (c *Cluster) deleteStatefulSet() error {
261261
return fmt.Errorf("could not delete pods: %v", err)
262262
}
263263

264-
if err := c.deletePersistentVolumeClaims(); err != nil {
265-
return fmt.Errorf("could not delete PersistentVolumeClaims: %v", err)
264+
if c.OpConfig.EnablePersistentVolumeClaimDeletion != nil && *c.OpConfig.EnablePersistentVolumeClaimDeletion {
265+
if err := c.deletePersistentVolumeClaims(); err != nil {
266+
return fmt.Errorf("could not delete PersistentVolumeClaims: %v", err)
267+
}
268+
} else {
269+
c.logger.Info("not deleting PersistentVolumeClaims because disabled in configuration")
266270
}
267271

268272
return nil

pkg/controller/operator_config.go

+1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ func (c *Controller) importConfigurationFromCRD(fromCRD *acidv1.OperatorConfigur
122122
result.PodPriorityClassName = fromCRD.Kubernetes.PodPriorityClassName
123123
result.PodManagementPolicy = util.Coalesce(fromCRD.Kubernetes.PodManagementPolicy, "ordered_ready")
124124
result.PersistentVolumeClaimRetentionPolicy = fromCRD.Kubernetes.PersistentVolumeClaimRetentionPolicy
125+
result.EnablePersistentVolumeClaimDeletion = util.CoalesceBool(fromCRD.Kubernetes.EnablePersistentVolumeClaimDeletion, util.True())
125126
result.EnableReadinessProbe = fromCRD.Kubernetes.EnableReadinessProbe
126127
result.MasterPodMoveTimeout = util.CoalesceDuration(time.Duration(fromCRD.Kubernetes.MasterPodMoveTimeout), "10m")
127128
result.EnablePodAntiAffinity = fromCRD.Kubernetes.EnablePodAntiAffinity

pkg/util/config/config.go

+1
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ type Config struct {
249249
PatroniAPICheckInterval time.Duration `name:"patroni_api_check_interval" default:"1s"`
250250
PatroniAPICheckTimeout time.Duration `name:"patroni_api_check_timeout" default:"5s"`
251251
EnablePatroniFailsafeMode *bool `name:"enable_patroni_failsafe_mode" default:"false"`
252+
EnablePersistentVolumeClaimDeletion *bool `name:"enable_persistent_volume_claim_deletion" default:"true"`
252253
PersistentVolumeClaimRetentionPolicy map[string]string `name:"persistent_volume_claim_retention_policy" default:"when_deleted:retain,when_scaled:retain"`
253254
}
254255

0 commit comments

Comments
 (0)