From f2c88150c68411c92fc438dadef2fb140cc9f3e0 Mon Sep 17 00:00:00 2001 From: Stavros Kois <47820033+stavros-k@users.noreply.github.com> Date: Wed, 8 Nov 2023 23:57:01 +0200 Subject: [PATCH] chore(TSC): add couple extra tests on topologySpreadConstraints (#552) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit **Description** ⚒️ Fixes # **⚙️ Type of change** - [ ] ⚙️ Feature/App addition - [ ] 🪛 Bugfix - [ ] ⚠️ Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] 🔃 Refactor of current code **🧪 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 corresponding changes to the documentation - [ ] ⚠️ My changes generate no new warnings - [ ] 🧪 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 **➕ App addition** If this PR is an app addition please make sure you have done the following. - [ ] 🪞 I have opened a PR on [truecharts/containers](https://github.com/truecharts/containers) adding the container to TrueCharts mirror repo. - [ ] 🖼️ 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._ --- .../tests/pod/topologySpreadConstraints.yaml | 41 +++++++++++++++---- library/common/templates/class/_service.tpl | 4 +- .../lib/pod/_topologySpreadConstraints .tpl | 9 ++-- 3 files changed, 41 insertions(+), 13 deletions(-) diff --git a/library/common-test/tests/pod/topologySpreadConstraints.yaml b/library/common-test/tests/pod/topologySpreadConstraints.yaml index 297a97c2..60e16b39 100644 --- a/library/common-test/tests/pod/topologySpreadConstraints.yaml +++ b/library/common-test/tests/pod/topologySpreadConstraints.yaml @@ -5,10 +5,8 @@ release: name: test-release-name namespace: test-release-namespace tests: - - it: should pass with empty topologySpreadConstraints + - it: should pass with missing topologySpreadConstraints on DaemonSet set: - podOptions: - topologySpreadConstraints: [] workload: workload-name1: enabled: true @@ -23,10 +21,26 @@ tests: isNull: path: spec.template.spec.topologySpreadConstraints - - it: should pass defaults with empty topologySpreadConstraints on Deployment + - it: should pass with defaultSpread disabled set: podOptions: - topologySpreadConstraints: [] + defaultSpread: false + workload: + workload-name1: + enabled: true + primary: true + type: Deployment + podSpec: {} + asserts: + - documentIndex: &deploymentSetDoc 0 + isKind: + of: Deployment + - documentIndex: *deploymentSetDoc + isNull: + path: spec.template.spec.topologySpreadConstraints + + - it: should pass defaults with missing topologySpreadConstraints on Deployment + set: workload: workload-name1: enabled: true @@ -112,8 +126,7 @@ tests: topologyKey: truecharts.org/test whenUnsatisfiable: ScheduleAnyway - - - it: should pass with topologySpreadConstraints from "pod" + - it: should pass with multiple topologySpreadConstraints from "pod" set: podOptions: topologySpreadConstraints: @@ -138,6 +151,13 @@ tests: matchLabels: {} nodeAffinityPolicy: Honor nodeTaintsPolicy: Honor + - maxSkew: 1 + whenUnsatisfiable: ScheduleAnyway + topologyKey: "truecharts.org/test3" + labelSelector: + matchLabels: {} + nodeAffinityPolicy: Honor + nodeTaintsPolicy: Honor asserts: - documentIndex: *deploymentDoc equal: @@ -170,3 +190,10 @@ tests: nodeTaintsPolicy: Honor topologyKey: truecharts.org/test2 whenUnsatisfiable: ScheduleAnyway + - labelSelector: + matchLabels: {} + maxSkew: 1 + nodeAffinityPolicy: Honor + nodeTaintsPolicy: Honor + topologyKey: truecharts.org/test3 + whenUnsatisfiable: ScheduleAnyway diff --git a/library/common/templates/class/_service.tpl b/library/common/templates/class/_service.tpl index e1fd3d4b..bd03ec37 100644 --- a/library/common/templates/class/_service.tpl +++ b/library/common/templates/class/_service.tpl @@ -95,9 +95,9 @@ spec: {{- if not (mustHas $objectData.type $specialTypes) }} selector: {{- if $objectData.selectorLabels }} - {{- tpl ( toYaml $objectData.selectorLabels) $rootCtx | nindent 4 }} + {{- tpl (toYaml $objectData.selectorLabels) $rootCtx | nindent 4 }} {{- else }} - {{- include "tc.v1.common.lib.metadata.selectorLabels" (dict "rootCtx" $rootCtx "objectType" "pod" "objectName" $podValues.shortName) | trim | nindent 4 -}} + {{- include "tc.v1.common.lib.metadata.selectorLabels" (dict "rootCtx" $rootCtx "objectType" "pod" "objectName" $podValues.shortName) | trim | nindent 4 -}} {{- end }} {{- end -}} {{- if eq $objectData.type "ExternalIP" -}} diff --git a/library/common/templates/lib/pod/_topologySpreadConstraints .tpl b/library/common/templates/lib/pod/_topologySpreadConstraints .tpl index 5f24d708..49c2f202 100644 --- a/library/common/templates/lib/pod/_topologySpreadConstraints .tpl +++ b/library/common/templates/lib/pod/_topologySpreadConstraints .tpl @@ -20,7 +20,8 @@ objectData: The object data to be used to render the Pod. {{- $constraints = . -}} {{- end -}} - {{- if and ( or ( eq $objectData.type "Deployment" ) ( eq $objectData.type "StatefulSet" )) $rootCtx.Values.podOptions.defaultSpread -}} + {{- $validTypes := (list "Deployment" "StatefulSet") -}} + {{- if and (mustHas $objectData.type $validTypes) $rootCtx.Values.podOptions.defaultSpread }} - maxSkew: 1 whenUnsatisfiable: ScheduleAnyway topologyKey: "truecharts.org/rack" @@ -38,7 +39,7 @@ objectData: The object data to be used to render the Pod. nodeAffinityPolicy: Honor nodeTaintsPolicy: Honor {{- end -}} - {{ with $constraints }} -{{ . | toYaml | indent 0 }} - {{ end }} + {{- with $constraints -}} {{/* TODO: Template this, so we can add some validation around easy to make mistakes. Low Prio */}} + {{- . | toYaml | nindent 0 }} + {{- end -}} {{- end -}}