From 3cd69ef570dfbe297291dd59bf67cc3cf8c82f0d Mon Sep 17 00:00:00 2001 From: Kjeld Schouten Date: Mon, 21 Apr 2025 17:48:32 +0200 Subject: [PATCH] feat(common): force recreate on single-replica with RWO storage (#33990) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit **Description** This should ensure users cannot set "rolling update" when using RWO storage and a single-replica, as this would inherently brick pod updates, as storage would, inherently, stay locked with the previous pod instance ⚒️ Fixes #33097 **⚙️ Type of change** - [ ] ⚙️ Feature/App addition - [x] 🪛 Bugfix - [ ] ⚠️ Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] 🔃 Refactor of current code - [ ] 📜 Documentation Changes **🧪 How Has This Been Tested?** **📃 Notes:** **✔️ Checklist:** - [ ] ⚖️ My code follows the style guidelines of this project - [ ] 👀 I have performed a self-review of my own code - [ ] #️⃣ I have commented my code, particularly in hard-to-understand areas - [ ] 📄 I have made changes to the documentation - [ ] 🧪 I have added tests to this description that prove my fix is effective or that my feature works - [ ] ⬆️ I increased versions for any altered app according to semantic versioning - [ ] I made sure the title starts with `feat(chart-name):`, `fix(chart-name):`, `chore(chart-name):`, `docs(chart-name):` or `fix(docs):` **➕ App addition** If this PR is an app addition please make sure you have done the following. - [ ] 🖼️ I have added an icon in the Chart's root directory called `icon.png` --- _Please don't blindly check all the boxes. Read them and only check those that apply. Those checkboxes are there for the reviewer to see what is this all about and the status of this PR with a quick glance._ --------- Signed-off-by: Alfred Göppel <43101280+alfi0812@users.noreply.github.com> Co-authored-by: Stavros Kois Co-authored-by: Alfred Göppel <43101280+alfi0812@users.noreply.github.com> --- charts/library/common-test/Chart.yaml | 1 - .../persistence/pvc_stategy_type_test.yaml | 153 ++++++++++++++++++ .../common/templates/lib/pod/_volumes.tpl | 22 ++- .../templates/lib/workload/_daemonsetSpec.tpl | 7 +- .../lib/workload/_deploymentSpec.tpl | 7 +- .../lib/workload/_statefulsetSpec.tpl | 7 +- .../lib/workload/components/_strategyType.tpl | 71 ++++++++ 7 files changed, 253 insertions(+), 15 deletions(-) create mode 100644 charts/library/common-test/tests/persistence/pvc_stategy_type_test.yaml create mode 100644 charts/library/common/templates/lib/workload/components/_strategyType.tpl diff --git a/charts/library/common-test/Chart.yaml b/charts/library/common-test/Chart.yaml index dc852063acd..0c72d74cb6a 100644 --- a/charts/library/common-test/Chart.yaml +++ b/charts/library/common-test/Chart.yaml @@ -35,4 +35,3 @@ sources: - https://github.com/truecharts/charts/tree/master/charts/library/common-test type: application version: 1.0.0 - diff --git a/charts/library/common-test/tests/persistence/pvc_stategy_type_test.yaml b/charts/library/common-test/tests/persistence/pvc_stategy_type_test.yaml new file mode 100644 index 00000000000..c53fd1f645f --- /dev/null +++ b/charts/library/common-test/tests/persistence/pvc_stategy_type_test.yaml @@ -0,0 +1,153 @@ +suite: pvc strategy type test +templates: + - common.yaml +release: + name: test-release-name + namespace: test-release-namespace +tests: + - it: should leave strategy as defined with non RWO accessModes and single replica + set: + workload: + some-workload: + enabled: true + primary: true + type: Deployment + strategy: RollingUpdate + podSpec: {} + persistence: + my-volume1: + enabled: true + type: pvc + accessModes: + - ReadWriteMany + asserts: + - documentIndex: &deploymentDoc 0 + isKind: + of: Deployment + - documentIndex: *deploymentDoc + equal: + path: spec.strategy.type + value: RollingUpdate + - documentIndex: &pvcDoc 1 + isKind: + of: PersistentVolumeClaim + - documentIndex: *pvcDoc + equal: + path: metadata.name + value: test-release-name-common-test-my-volume1 + - documentIndex: *pvcDoc + equal: + path: spec.accessModes + value: + - ReadWriteMany + - it: should set strategy to Recreate with RWO accessModes and single replica + set: + workload: + some-workload: + enabled: true + primary: true + type: Deployment + strategy: RollingUpdate + podSpec: {} + persistence: + my-volume1: + enabled: true + type: pvc + accessModes: + - ReadWriteMany + - ReadWriteOnce + asserts: + - documentIndex: &deploymentDoc 0 + isKind: + of: Deployment + - documentIndex: *deploymentDoc + equal: + path: spec.strategy.type + value: Recreate + - documentIndex: &pvcDoc 1 + isKind: + of: PersistentVolumeClaim + - documentIndex: *pvcDoc + equal: + path: metadata.name + value: test-release-name-common-test-my-volume1 + - documentIndex: *pvcDoc + equal: + path: spec.accessModes + value: + - ReadWriteMany + - ReadWriteOnce + - it: should leave strategy as defined with non RWO accessModes and multiple replica + set: + workload: + some-workload: + enabled: true + primary: true + type: Deployment + replicas: 2 + strategy: RollingUpdate + podSpec: {} + persistence: + my-volume1: + enabled: true + type: pvc + accessModes: + - ReadWriteMany + asserts: + - documentIndex: &deploymentDoc 0 + isKind: + of: Deployment + - documentIndex: *deploymentDoc + equal: + path: spec.strategy.type + value: RollingUpdate + - documentIndex: &pvcDoc 1 + isKind: + of: PersistentVolumeClaim + - documentIndex: *pvcDoc + equal: + path: metadata.name + value: test-release-name-common-test-my-volume1 + - documentIndex: *pvcDoc + equal: + path: spec.accessModes + value: + - ReadWriteMany + - it: should leave strategy as defined with RWO accessModes and multiple replica + set: + workload: + some-workload: + enabled: true + primary: true + type: Deployment + replicas: 2 + strategy: RollingUpdate + podSpec: {} + persistence: + my-volume1: + enabled: true + type: pvc + accessModes: + - ReadWriteMany + - ReadWriteOnce + asserts: + - documentIndex: &deploymentDoc 0 + isKind: + of: Deployment + - documentIndex: *deploymentDoc + equal: + path: spec.strategy.type + value: RollingUpdate + - documentIndex: &pvcDoc 1 + isKind: + of: PersistentVolumeClaim + - documentIndex: *pvcDoc + equal: + path: metadata.name + value: test-release-name-common-test-my-volume1 + - documentIndex: *pvcDoc + equal: + path: spec.accessModes + value: + - ReadWriteMany + - ReadWriteOnce diff --git a/charts/library/common/templates/lib/pod/_volumes.tpl b/charts/library/common/templates/lib/pod/_volumes.tpl index db9b40e700d..12d872c8a6e 100644 --- a/charts/library/common/templates/lib/pod/_volumes.tpl +++ b/charts/library/common/templates/lib/pod/_volumes.tpl @@ -96,15 +96,9 @@ objectData: The object data to be used to render the Pod. "objectData" $persistence "caller" "Volumes") | fromYamlArray -}} - {{- $hasRWO := false -}} - {{- range $m := $modes -}} - {{- if eq $m "ReadWriteOnce" -}} - {{- $hasRWO = true -}} - {{- break -}} - {{- end -}} - {{- end -}} + {{- $hasRWO := include "tc.v1.common.lib.pod.volumes.hasRWO" (dict "modes" $modes) -}} - {{- if $hasRWO -}} + {{- if eq $hasRWO "true" -}} {{- if eq $objectData.type "DaemonSet" -}} {{- fail "Expected [accessMode] to not be [ReadWriteOnce] when used on a [DaemonSet]" -}} @@ -117,3 +111,15 @@ objectData: The object data to be used to render the Pod. {{- end -}} {{- end -}} + +{{- define "tc.v1.common.lib.pod.volumes.hasRWO" -}} + {{- $modes := .modes -}} + {{- $hasRWO := false -}} + {{- range $m := $modes -}} + {{- if eq $m "ReadWriteOnce" -}} + {{- $hasRWO = true -}} + {{- break -}} + {{- end -}} + {{- end -}} + {{- $hasRWO -}} +{{- end -}} diff --git a/charts/library/common/templates/lib/workload/_daemonsetSpec.tpl b/charts/library/common/templates/lib/workload/_daemonsetSpec.tpl index 82e5abae92a..c17771994d4 100644 --- a/charts/library/common/templates/lib/workload/_daemonsetSpec.tpl +++ b/charts/library/common/templates/lib/workload/_daemonsetSpec.tpl @@ -9,10 +9,13 @@ objectData: {{- define "tc.v1.common.lib.workload.daemonsetSpec" -}} {{- $objectData := .objectData -}} {{- $rootCtx := .rootCtx -}} - {{- $strategy := $objectData.strategy | default "RollingUpdate" }} + {{- include "tc.v1.common.lib.workload.components.strategyType" (dict + "rootCtx" $rootCtx "objectData" $objectData + "defaultStrategy" "RollingUpdate" "resource" "DaemonSet" + ) }} revisionHistoryLimit: {{ $objectData.revisionHistoryLimit | default 3 }} updateStrategy: - type: {{ $strategy }} + type: {{ $objectData.strategy }} {{- if and (eq $objectData.strategy "RollingUpdate") $objectData.rollingUpdate -}} {{ if (or (hasKey $objectData.rollingUpdate "maxUnavailable") (hasKey $objectData.rollingUpdate "maxSurge")) }} rollingUpdate: diff --git a/charts/library/common/templates/lib/workload/_deploymentSpec.tpl b/charts/library/common/templates/lib/workload/_deploymentSpec.tpl index bae179099b1..067238198fd 100644 --- a/charts/library/common/templates/lib/workload/_deploymentSpec.tpl +++ b/charts/library/common/templates/lib/workload/_deploymentSpec.tpl @@ -10,7 +10,10 @@ objectData: {{- define "tc.v1.common.lib.workload.deploymentSpec" -}} {{- $objectData := .objectData -}} {{- $rootCtx := .rootCtx -}} - {{- $strategy := $objectData.strategy | default "Recreate" -}} + {{- include "tc.v1.common.lib.workload.components.strategyType" (dict + "rootCtx" $rootCtx "objectData" $objectData + "defaultStrategy" "Recreate" "resource" "Deployment" + ) -}} {{- $replicas := 1 -}} {{- if hasKey $objectData "replicas" -}} {{- $replicas = $objectData.replicas -}} @@ -21,7 +24,7 @@ objectData: replicas: {{ $replicas }} revisionHistoryLimit: {{ $objectData.revisionHistoryLimit | default 3 }} strategy: - type: {{ $strategy }} + type: {{ $objectData.strategy }} {{- if and (eq $objectData.strategy "RollingUpdate") $objectData.rollingUpdate -}} {{ if (or (hasKey $objectData.rollingUpdate "maxUnavailable") (hasKey $objectData.rollingUpdate "maxSurge")) }} rollingUpdate: diff --git a/charts/library/common/templates/lib/workload/_statefulsetSpec.tpl b/charts/library/common/templates/lib/workload/_statefulsetSpec.tpl index 364376d038e..bd26f339294 100644 --- a/charts/library/common/templates/lib/workload/_statefulsetSpec.tpl +++ b/charts/library/common/templates/lib/workload/_statefulsetSpec.tpl @@ -10,7 +10,10 @@ objectData: {{- define "tc.v1.common.lib.workload.statefulsetSpec" -}} {{- $objectData := .objectData -}} {{- $rootCtx := .rootCtx -}} - {{- $strategy := $objectData.strategy | default "RollingUpdate" -}} + {{- include "tc.v1.common.lib.workload.components.strategyType" (dict + "rootCtx" $rootCtx "objectData" $objectData + "defaultStrategy" "RollingUpdate" "resource" "StatefulSet" + ) -}} {{- $replicas := 1 -}} {{- if hasKey $objectData "replicas" -}} {{- $replicas = $objectData.replicas -}} @@ -22,7 +25,7 @@ replicas: {{ $replicas }} revisionHistoryLimit: {{ $objectData.revisionHistoryLimit | default 3 }} serviceName: {{ $objectData.name }} updateStrategy: - type: {{ $strategy }} + type: {{ $objectData.strategy }} {{- if and (eq $objectData.strategy "RollingUpdate") $objectData.rollingUpdate -}} {{- if (or (hasKey $objectData.rollingUpdate "maxUnavailable") (hasKey $objectData.rollingUpdate "partition")) }} rollingUpdate: diff --git a/charts/library/common/templates/lib/workload/components/_strategyType.tpl b/charts/library/common/templates/lib/workload/components/_strategyType.tpl new file mode 100644 index 00000000000..99fdceaa96b --- /dev/null +++ b/charts/library/common/templates/lib/workload/components/_strategyType.tpl @@ -0,0 +1,71 @@ +{{/* Call this template: +{{ include "tc.v1.common.lib.workload.components.strategyType" (dict "rootCtx" $rootCtx "objectData" $objectData) -}} +rootCtx: The root context of the chart. +objectData: + replicas: The number of replicas. + strategy: The deployment strategy to use to replace existing pods with new ones. +*/}} +{{- define "tc.v1.common.lib.workload.components.strategyType" -}} + {{- $objectData := .objectData -}} + {{- $rootCtx := .rootCtx -}} + {{- $defaultStrategy := .defaultStrategy -}} + {{- $resource := .resource -}} + {{- $strategy := $objectData.strategy | default $defaultStrategy -}} + + {{- $replicas := 1 -}} + {{- if hasKey $objectData "replicas" -}} + {{- $replicas = $objectData.replicas -}} + {{- end -}} + {{- $replicas = $replicas | int -}} + + {{- $volsRWO := list -}} + {{- range $name, $persistence := $rootCtx.Values.persistence }} + {{- $enabled := (include "tc.v1.common.lib.util.enabled" (dict + "rootCtx" $rootCtx "objectData" $persistence + "name" $name "caller" "Volumes" + "key" "persistence")) -}} + + {{- if (ne $enabled "true") -}}{{- continue -}}{{- end -}} + + {{- $type := ($persistence.type | default $rootCtx.Values.global.fallbackDefaults.persistenceType) -}} + {{- $typesWithAccessMode := (list "pvc") -}} + + {{- if (mustHas $type $typesWithAccessMode) -}} + {{- $modes := include "tc.v1.common.lib.pvc.accessModes" (dict "rootCtx" $rootCtx + "objectData" $persistence "caller" "Volumes") | fromYamlArray + -}} + + {{- $hasRWO := include "tc.v1.common.lib.pod.volumes.hasRWO" (dict "modes" $modes) -}} + {{- if ne $hasRWO "true" -}}{{- continue -}}{{- end -}} + {{- $volsRWO = mustAppend $volsRWO $name -}} + {{- end -}} + {{- end -}} + + {{/* If there are any RWO vols, do some checks and add warnings */}} + {{- if gt (len $volsRWO) 0 -}} + {{/* RWO + replicas > 1 is a no-no */}} + {{- if gt $replicas 1 -}} + {{- include "add.warning" (dict "rootCtx" $rootCtx "warn" (printf + "WARNING: The [accessModes] on volume(s) [%s] is set to [ReadWriteOnce] with a more than 1 replica. This is not stables" (join "," $volsRWO) + )) -}} + {{- else -}} + {{/* DaemonSets and StatefulSets can have RWO with 1 replica under their supported strategies (OnDelete, RollingUpdate) */}} + + {{- if eq $resource "Deployment" -}} + + {{/* On Deployments with single replicas, warn if strategy is not recreate */}} + {{- if eq $strategy "Recreate" -}} + {{- include "add.warning" (dict "rootCtx" $rootCtx "warn" (printf + "WARNING: The [accessModes] on volume(s) [%s] is set to [ReadWriteOnce] with a single replica and an strategy of [%s]. %s" + (join "," $volsRWO) $strategy "This is not stable, defaulting to [Recreate] strategy" + )) -}} + {{- end -}} + {{- $strategy = "Recreate" -}} + + {{- end -}} + {{- end -}} + {{- end -}} + + {{/* Update strategy */}} + {{- $_ := set $objectData "strategy" $strategy -}} +{{- end -}}