From c5b724d69f0bb83f208344b7123ed7ad09830e41 Mon Sep 17 00:00:00 2001 From: Aspen Date: Mon, 24 Feb 2025 07:09:20 -0500 Subject: [PATCH] feat(common): Allow configuring default topologyKey (#32461) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit **Description** This PR adds a global option to control the default topologyKey used for topologySpreadConstraints ⚒️ Fixes #32457 **⚙️ Type of change** - [x] ⚙️ Feature/App addition - [ ] 🪛 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?** Ran the common library unit tests, added one to verify the usage of the field **📃 Notes:** **✔️ Checklist:** - [x] ⚖️ My code follows the style guidelines of this project - [x] 👀 I have performed a self-review of my own code - [x] #️⃣ I have commented my code, particularly in hard-to-understand areas - [x] 📄 I have made changes to the documentation - [x] 🧪 I have added tests to this description that prove my fix is effective or that my feature works - [x] ⬆️ I increased versions for any altered app according to semantic versioning - [x] 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._ --- charts/library/common-test/Chart.yaml | 2 +- .../tests/pod/topologySpreadConstraints.yaml | 34 +++++++++++++++++++ charts/library/common/Chart.yaml | 2 +- .../lib/pod/_topologySpreadConstraints .tpl | 2 +- charts/library/common/values.yaml | 3 ++ .../content/docs/common/fallbackDefaults.md | 18 ++++++++++ 6 files changed, 58 insertions(+), 3 deletions(-) diff --git a/charts/library/common-test/Chart.yaml b/charts/library/common-test/Chart.yaml index 181e6bb405f..a01d9f9715f 100644 --- a/charts/library/common-test/Chart.yaml +++ b/charts/library/common-test/Chart.yaml @@ -7,7 +7,7 @@ apiVersion: v2 appVersion: unknown dependencies: - name: common - version: ~26.3.0 + version: ~26.4.0 repository: file://../common/ condition: "" alias: "" diff --git a/charts/library/common-test/tests/pod/topologySpreadConstraints.yaml b/charts/library/common-test/tests/pod/topologySpreadConstraints.yaml index ecee7a5815c..3b5f930fe72 100644 --- a/charts/library/common-test/tests/pod/topologySpreadConstraints.yaml +++ b/charts/library/common-test/tests/pod/topologySpreadConstraints.yaml @@ -120,6 +120,40 @@ tests: topologyKey: truecharts.org/test whenUnsatisfiable: ScheduleAnyway + - it: should pass with topologyKey from "global" + set: + global: + fallbackDefaults: + topologyKey: "truecharts.org/test" + workload: + workload-name1: + enabled: true + primary: true + type: Deployment + podSpec: {} + asserts: + - documentIndex: *deploymentDoc + equal: + path: spec.selector.matchLabels + value: + app.kubernetes.io/instance: test-release-name + app.kubernetes.io/name: common-test + pod.name: workload-name1 + - documentIndex: *deploymentDoc + equal: + path: spec.template.spec.topologySpreadConstraints + value: + - labelSelector: + matchLabels: + app.kubernetes.io/instance: test-release-name + app.kubernetes.io/name: common-test + pod.name: workload-name1 + maxSkew: 1 + nodeAffinityPolicy: Honor + nodeTaintsPolicy: Honor + topologyKey: truecharts.org/test + whenUnsatisfiable: ScheduleAnyway + - it: should pass with multiple topologySpreadConstraints from "pod" set: podOptions: diff --git a/charts/library/common/Chart.yaml b/charts/library/common/Chart.yaml index 3e0457d2f52..e00cfe954f7 100644 --- a/charts/library/common/Chart.yaml +++ b/charts/library/common/Chart.yaml @@ -48,4 +48,4 @@ sources: - https://hub.docker.com/_/ - https://hub.docker.com/r/mikefarah/yq type: library -version: 26.3.0 +version: 26.4.0 diff --git a/charts/library/common/templates/lib/pod/_topologySpreadConstraints .tpl b/charts/library/common/templates/lib/pod/_topologySpreadConstraints .tpl index 59ddf988420..66c018fa12e 100644 --- a/charts/library/common/templates/lib/pod/_topologySpreadConstraints .tpl +++ b/charts/library/common/templates/lib/pod/_topologySpreadConstraints .tpl @@ -24,7 +24,7 @@ objectData: The object data to be used to render the Pod. {{- if and (mustHas $objectData.type $validTypes) $rootCtx.Values.podOptions.defaultSpread }} - maxSkew: 1 whenUnsatisfiable: ScheduleAnyway - topologyKey: "kubernetes.io/hostname" + topologyKey: {{ default "kubernetes.io/hostname" $rootCtx.Values.global.fallbackDefaults.topologyKey }} labelSelector: matchLabels: {{- include "tc.v1.common.lib.metadata.selectorLabels" (dict "rootCtx" $rootCtx "objectType" "pod" "objectName" $objectData.shortName) | indent 6 }} diff --git a/charts/library/common/values.yaml b/charts/library/common/values.yaml index a2b3af98b93..4630135b524 100644 --- a/charts/library/common/values.yaml +++ b/charts/library/common/values.yaml @@ -57,6 +57,9 @@ global: # will be used for all CNPG objects # Can be overruled per CNPG objects pgVersion: 16 + # -- Define a topologyKey for default topologySpreadConstraints + # Will be used when defaultSpread: true + topologyKey: kubernetes.io/hostname metallb: # -- Adds metalLB annotations to services addServiceAnnotations: true diff --git a/website/src/content/docs/common/fallbackDefaults.md b/website/src/content/docs/common/fallbackDefaults.md index 236a89815ca..923d52e50f5 100644 --- a/website/src/content/docs/common/fallbackDefaults.md +++ b/website/src/content/docs/common/fallbackDefaults.md @@ -48,6 +48,7 @@ fallbackDefaults: timeoutSeconds: 2 failureThreshold: 60 successThreshold: 1 + topologyKey: kubernetes.io/hostname ``` ## `probeType` @@ -779,6 +780,22 @@ fallbackDefaults: successThreshold: 1 ``` +--- + +## `topologyKey` + +Define default topologyKey for topologySpreadConstraints in podOptions + + +| | | +| ---------- | ------------------------------ | +| Key | `fallbackDefaults.topologyKey` | +| Type | `string` | +| Required | ❌ | +| Helm `tpl` | ❌ | +| Default | `kubernetes.io/hostname` | + + --- ## Full Examples @@ -808,4 +825,5 @@ fallbackDefaults: timeoutSeconds: 2 failureThreshold: 60 successThreshold: 1 + topologyKey: truecharts.org/example ```