From 3cddf31eb6d2656178908f67f431429c035fd8d3 Mon Sep 17 00:00:00 2001 From: Aleix Murtra Date: Thu, 19 Oct 2023 16:03:14 +0200 Subject: [PATCH] Helm Chart: Support StatefulSet PVC retention Add new variable on the Helm chart to define the PVC retention policy for the StatefulSet --- .changelog/3180.txt | 3 + .../consul/templates/server-statefulset.yaml | 3 + .../consul/test/unit/server-statefulset.bats | 67 +++++++++++++++++++ charts/consul/values.yaml | 15 +++++ 4 files changed, 88 insertions(+) create mode 100644 .changelog/3180.txt diff --git a/.changelog/3180.txt b/.changelog/3180.txt new file mode 100644 index 0000000000..aee3bfb8aa --- /dev/null +++ b/.changelog/3180.txt @@ -0,0 +1,3 @@ +```release-note:feature +helm: add persistentVolumeClaimRetentionPolicy variable for managing Statefulsets PVC retain policy when deleting or downsizing the statefulset. +``` diff --git a/charts/consul/templates/server-statefulset.yaml b/charts/consul/templates/server-statefulset.yaml index d2785369c7..c5935f1499 100644 --- a/charts/consul/templates/server-statefulset.yaml +++ b/charts/consul/templates/server-statefulset.yaml @@ -44,6 +44,9 @@ spec: rollingUpdate: partition: {{ .Values.server.updatePartition }} {{- end }} + {{- if and (semverCompare ">= 1.23-0" .Capabilities.KubeVersion.Version) (.Values.server.persistentVolumeClaimRetentionPolicy) }} + persistentVolumeClaimRetentionPolicy: {{ toYaml .Values.server.persistentVolumeClaimRetentionPolicy | nindent 4 }} + {{- end }} selector: matchLabels: app: {{ template "consul.name" . }} diff --git a/charts/consul/test/unit/server-statefulset.bats b/charts/consul/test/unit/server-statefulset.bats index f4ab239aa8..895bdeb4e8 100755 --- a/charts/consul/test/unit/server-statefulset.bats +++ b/charts/consul/test/unit/server-statefulset.bats @@ -196,6 +196,73 @@ load _helpers [ "${actual}" = "foo" ] } +#-------------------------------------------------------------------- +# persistentVolumeClaimRetentionPolicy + +@test "server/StatefulSet: persistentVolumeClaimRetentionPolicy not set by default when kubernetes < 1.23" { + cd `chart_dir` + local actual=$(helm template \ + -s templates/server-statefulset.yaml \ + --kube-version "1.22" \ + . | tee /dev/stderr | + yq -r '.spec.persistentVolumeClaimRetentionPolicy' | tee /dev/stderr) + [ "${actual}" = "null" ] +} + +@test "server/StatefulSet: unset persistentVolumeClaimRetentionPolicy.whenDeleted when kubernetes < 1.23" { + cd `chart_dir` + local actual=$(helm template \ + -s templates/server-statefulset.yaml \ + --kube-version "1.22" \ + --set 'server.persistentVolumeClaimRetentionPolicy.whenDeleted=Delete' \ + . | tee /dev/stderr | + yq -r '.spec.persistentVolumeClaimRetentionPolicy.whenDeleted' | tee /dev/stderr) + [ "${actual}" = "null" ] +} + +@test "server/StatefulSet: unset persistentVolumeClaimRetentionPolicy.whenScaled when kubernetes < 1.23" { + cd `chart_dir` + local actual=$(helm template \ + -s templates/server-statefulset.yaml \ + --kube-version "1.22" \ + --set 'server.persistentVolumeClaimRetentionPolicy.whenScaled=Delete' \ + . | tee /dev/stderr | + yq -r '.spec.persistentVolumeClaimRetentionPolicy.whenScaled' | tee /dev/stderr) + [ "${actual}" = "null" ] +} + +@test "server/StatefulSet: persistentVolumeClaimRetentionPolicy not set by default when kubernetes >= 1.23" { + cd `chart_dir` + local actual=$(helm template \ + -s templates/server-statefulset.yaml \ + --kube-version "1.23" \ + . | tee /dev/stderr | + yq -r '.spec.persistentVolumeClaimRetentionPolicy' | tee /dev/stderr) + [ "${actual}" = "null" ] +} + +@test "server/StatefulSet: can set persistentVolumeClaimRetentionPolicy.whenDeleted when kubernetes >= 1.23" { + cd `chart_dir` + local actual=$(helm template \ + -s templates/server-statefulset.yaml \ + --kube-version "1.23" \ + --set 'server.persistentVolumeClaimRetentionPolicy.whenDeleted=Delete' \ + . | tee /dev/stderr | + yq -r '.spec.persistentVolumeClaimRetentionPolicy.whenDeleted' | tee /dev/stderr) + [ "${actual}" = "Delete" ] +} + +@test "server/StatefulSet: can set persistentVolumeClaimRetentionPolicy.whenScaled when kubernetes >= 1.23" { + cd `chart_dir` + local actual=$(helm template \ + -s templates/server-statefulset.yaml \ + --kube-version "1.23" \ + --set 'server.persistentVolumeClaimRetentionPolicy.whenScaled=Delete' \ + . | tee /dev/stderr | + yq -r '.spec.persistentVolumeClaimRetentionPolicy.whenScaled' | tee /dev/stderr) + [ "${actual}" = "Delete" ] +} + #-------------------------------------------------------------------- # serverCert diff --git a/charts/consul/values.yaml b/charts/consul/values.yaml index c9abd21713..577d1af369 100644 --- a/charts/consul/values.yaml +++ b/charts/consul/values.yaml @@ -882,6 +882,21 @@ server: # @type: string storageClass: null + # The [Persistent Volume Claim (PVC) retention policy](https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/#persistentvolumeclaim-retention) + # controls if and how PVCs are deleted during the lifecycle of a StatefulSet. + # WhenDeleted specifies what happens to PVCs created from StatefulSet VolumeClaimTemplates when the StatefulSet is deleted, + # and WhenScaled specifies what happens to PVCs created from StatefulSet VolumeClaimTemplates when the StatefulSet is scaled down. + # + # Example: + # + # ```yaml + # persistentVolumeClaimRetentionPolicy: + # whenDeleted: Retain + # whenScaled: Retain + # ``` + # @type: map + persistentVolumeClaimRetentionPolicy: null + # This will enable/disable [service mesh](https://developer.hashicorp.com/consul/docs/connect). Setting this to true # _will not_ automatically secure pod communication, this # setting will only enable usage of the feature. Consul will automatically initialize