From 119258623a0a275a6e26e475e1c6dc813a115de5 Mon Sep 17 00:00:00 2001 From: Paramoshka Date: Wed, 17 Dec 2025 19:50:55 +0300 Subject: [PATCH 1/2] refactored affinity Signed-off-by: Paramoshka --- charts/helm_lib/Chart.yaml | 2 +- charts/helm_lib/README.md | 14 ++-- charts/helm_lib/templates/_node_affinity.tpl | 4 +- .../templates/_spec_for_high_availability.tpl | 27 ++++--- .../helm_lib_affinity_arch_require.yaml | 4 - .../helm_lib_affinity_arch_required.yaml | 4 + ...elm_lib_affinity_ha_with_arch_require.yaml | 4 - tests/templates/helm_lib_pod_affinity.yaml | 4 + ...helm_lib_affinity_arch_required_test.yaml} | 4 +- ...ib_affinity_ha_with_arch_require_test.yaml | 64 ---------------- tests/tests/helm_lib_pod_affinity_test.yaml | 74 +++++++++++++++++++ 11 files changed, 112 insertions(+), 93 deletions(-) delete mode 100644 tests/templates/helm_lib_affinity_arch_require.yaml create mode 100644 tests/templates/helm_lib_affinity_arch_required.yaml delete mode 100644 tests/templates/helm_lib_affinity_ha_with_arch_require.yaml create mode 100644 tests/templates/helm_lib_pod_affinity.yaml rename tests/tests/{helm_lib_affinity_arch_require_test.yaml => helm_lib_affinity_arch_required_test.yaml} (93%) delete mode 100644 tests/tests/helm_lib_affinity_ha_with_arch_require_test.yaml create mode 100644 tests/tests/helm_lib_pod_affinity_test.yaml diff --git a/charts/helm_lib/Chart.yaml b/charts/helm_lib/Chart.yaml index f105506..84b95d9 100644 --- a/charts/helm_lib/Chart.yaml +++ b/charts/helm_lib/Chart.yaml @@ -1,5 +1,5 @@ apiVersion: v2 type: library name: deckhouse_lib_helm -version: 1.68.0 +version: 1.68.1 description: "Helm utils template definitions for Deckhouse modules." diff --git a/charts/helm_lib/README.md b/charts/helm_lib/README.md index 42914a9..9a3cef2 100644 --- a/charts/helm_lib/README.md +++ b/charts/helm_lib/README.md @@ -125,7 +125,7 @@ | [_helm_lib_additional_tolerations_storage_problems](#_helm_lib_additional_tolerations_storage_problems) | | [_helm_lib_additional_tolerations_no_csi](#_helm_lib_additional_tolerations_no_csi) | | [_helm_lib_additional_tolerations_cloud_provider_uninitialized](#_helm_lib_additional_tolerations_cloud_provider_uninitialized) | -| [helm_lib_affinity_arch_require](#helm_lib_affinity_arch_require) | +| [helm_lib_affinity_arch_required](#helm_lib_affinity_arch_required) | | **Pod Disruption Budget** | | [helm_lib_pdb_daemonset](#helm_lib_pdb_daemonset) | | **Priority Class** | @@ -140,7 +140,7 @@ | [helm_lib_container_kube_rbac_proxy_resources](#helm_lib_container_kube_rbac_proxy_resources) | | **Spec For High Availability** | | [helm_lib_pod_anti_affinity_for_ha](#helm_lib_pod_anti_affinity_for_ha) | -| [helm_lib_affinity_ha_with_arch_require](#helm_lib_affinity_ha_with_arch_require) | +| [helm_lib_pod_affinity](#helm_lib_pod_affinity) | | [helm_lib_deployment_on_master_strategy_and_replicas_for_ha](#helm_lib_deployment_on_master_strategy_and_replicas_for_ha) | | [helm_lib_deployment_on_master_custom_strategy_and_replicas_for_ha](#helm_lib_deployment_on_master_custom_strategy_and_replicas_for_ha) | | [helm_lib_deployment_strategy_and_replicas_for_ha](#helm_lib_deployment_strategy_and_replicas_for_ha) | @@ -1428,13 +1428,13 @@ list: -### helm_lib_affinity_arch_require +### helm_lib_affinity_arch_required Returns nodeAffinity that schedules pods only on specified architectures. #### Usage -`{{- include "helm_lib_affinity_arch_require" (list . (list "amd64" "arm64")) ` +`{{- include "helm_lib_affinity_arch_required" (list . (list "amd64" "arm64")) ` ## Pod Disruption Budget @@ -1573,13 +1573,13 @@ list: - Match labels for podAntiAffinity label selector -### helm_lib_affinity_ha_with_arch_require +### helm_lib_pod_affinity - Returns affinity spec for HA components that combines: podAntiAffinity by provided labels (same as helm_lib_pod_anti_affinity_for_ha) and nodeAffinity that schedules pods only on specified architectures. If the list of architectures is not provided, defaults to ["amd64"]. + Returns affinity spec that combines: podAntiAffinity by provided labels when HA is enabled and optional nodeAffinity that schedules pods only on specified architectures. If the list of architectures is not provided or empty, node affinity is not rendered. #### Usage -`{{- include "helm_lib_affinity_ha_with_arch_require" (list . (dict "app" "test") (list "amd64")) }} ` +`{{- include "helm_lib_pod_affinity" (list . (dict "app" "test") (list "amd64")) }} ` #### Arguments diff --git a/charts/helm_lib/templates/_node_affinity.tpl b/charts/helm_lib/templates/_node_affinity.tpl index 45c2436..f3d623c 100644 --- a/charts/helm_lib/templates/_node_affinity.tpl +++ b/charts/helm_lib/templates/_node_affinity.tpl @@ -262,8 +262,8 @@ tolerations: {{- end }} {{- /* Returns nodeAffinity that schedules pods only on specified architectures.*/ -}} -{{- /* Usage: {{- include "helm_lib_affinity_arch_require" (list . (list "amd64" "arm64")) */ -}} -{{- define "helm_lib_affinity_arch_require" -}} +{{- /* Usage: {{- include "helm_lib_affinity_arch_required" (list . (list "amd64" "arm64")) */ -}} +{{- define "helm_lib_affinity_arch_required" -}} {{- $context := index . 0 -}} {{- $allowedArchs := index . 1 -}} affinity: diff --git a/charts/helm_lib/templates/_spec_for_high_availability.tpl b/charts/helm_lib/templates/_spec_for_high_availability.tpl index f01b411..2cd393a 100644 --- a/charts/helm_lib/templates/_spec_for_high_availability.tpl +++ b/charts/helm_lib/templates/_spec_for_high_availability.tpl @@ -16,17 +16,23 @@ affinity: {{- end }} {{- end }} -{{- /* Usage: {{- include "helm_lib_affinity_ha_with_arch_require" (list . (dict "app" "test") (list "amd64")) }} */}} -{{- /* Returns affinity spec for HA components that combines: podAntiAffinity by provided labels (same as helm_lib_pod_anti_affinity_for_ha) and nodeAffinity that schedules pods only on specified architectures. If the list of architectures is not provided, defaults to ["amd64"]. */ -}} -{{- define "helm_lib_affinity_ha_with_arch_require" }} +{{- /* Usage: {{- include "helm_lib_pod_affinity" (list . (dict "app" "test") (list "amd64")) }} */}} +{{- /* Returns affinity spec that combines: podAntiAffinity by provided labels when HA is enabled and optional nodeAffinity that schedules pods only on specified architectures. If the list of architectures is not provided or empty, node affinity is not rendered. */ -}} +{{- define "helm_lib_pod_affinity" }} {{- $context := index . 0 -}} {{- /* Template context with .Values, .Chart, etc */ -}} -{{- $labels := index . 1 }} {{- /* Match labels for podAntiAffinity label selector */ -}} -{{- $allowedArchs := list "amd64" -}} +{{- $labels := dict -}} {{- /* Match labels for podAntiAffinity label selector */ -}} +{{- if ge (len .) 2 }} + {{- $labels = index . 1 }} +{{- end }} +{{- $allowedArchs := list -}} {{- /* List of supported architectures */ -}} {{- if ge (len .) 3 }} {{- $allowedArchs = index . 2 }} {{- end }} - {{- if (include "helm_lib_ha_enabled" $context) }} +{{- $haEnabled := (include "helm_lib_ha_enabled" $context) -}} +{{- $hasArch := gt (len $allowedArchs) 0 -}} +{{- if or $haEnabled $hasArch }} affinity: + {{- if $haEnabled }} podAntiAffinity: requiredDuringSchedulingIgnoredDuringExecution: - labelSelector: @@ -35,18 +41,21 @@ affinity: {{ $key }}: {{ $value | quote }} {{- end }} topologyKey: kubernetes.io/hostname + {{- end }} + {{- if $hasArch }} nodeAffinity: requiredDuringSchedulingIgnoredDuringExecution: nodeSelectorTerms: - matchExpressions: - - key: kubernetes.io/arch - operator: In - values: + - key: kubernetes.io/arch + operator: In + values: {{- range $allowedArchs }} - {{ . | quote }} {{- end }} {{- end }} {{- end }} +{{- end }} {{- /* Usage: {{ include "helm_lib_deployment_on_master_strategy_and_replicas_for_ha" }} */ -}} {{- /* returns deployment strategy and replicas for ha components running on master nodes */ -}} diff --git a/tests/templates/helm_lib_affinity_arch_require.yaml b/tests/templates/helm_lib_affinity_arch_require.yaml deleted file mode 100644 index 6fdd329..0000000 --- a/tests/templates/helm_lib_affinity_arch_require.yaml +++ /dev/null @@ -1,4 +0,0 @@ -{{- $default := include "helm_lib_affinity_arch_require" (list . (list "arm" "arm64")) }} -resultDefault: {{ $default | toString | quote }} -{{- $custom := include "helm_lib_affinity_arch_require" (list . (list "amd64" "ppc64le")) }} -resultCustom: {{ $custom | toString | quote }} diff --git a/tests/templates/helm_lib_affinity_arch_required.yaml b/tests/templates/helm_lib_affinity_arch_required.yaml new file mode 100644 index 0000000..2c6f59e --- /dev/null +++ b/tests/templates/helm_lib_affinity_arch_required.yaml @@ -0,0 +1,4 @@ +{{- $default := include "helm_lib_affinity_arch_required" (list . (list "arm" "arm64")) }} +resultDefault: {{ $default | toString | quote }} +{{- $custom := include "helm_lib_affinity_arch_required" (list . (list "amd64" "ppc64le")) }} +resultCustom: {{ $custom | toString | quote }} diff --git a/tests/templates/helm_lib_affinity_ha_with_arch_require.yaml b/tests/templates/helm_lib_affinity_ha_with_arch_require.yaml deleted file mode 100644 index d90a7d6..0000000 --- a/tests/templates/helm_lib_affinity_ha_with_arch_require.yaml +++ /dev/null @@ -1,4 +0,0 @@ -{{- $default := include "helm_lib_affinity_ha_with_arch_require" (list . (dict "app" "test")) }} -resultDefault: {{ $default | toString | quote }} -{{- $custom := include "helm_lib_affinity_ha_with_arch_require" (list . (dict "app" "test") (list "amd64" "ppc64le")) }} -resultCustom: {{ $custom | toString | quote }} diff --git a/tests/templates/helm_lib_pod_affinity.yaml b/tests/templates/helm_lib_pod_affinity.yaml new file mode 100644 index 0000000..4bc056a --- /dev/null +++ b/tests/templates/helm_lib_pod_affinity.yaml @@ -0,0 +1,4 @@ +{{- $noArch := include "helm_lib_pod_affinity" (list . (dict "app" "test")) }} +resultNoArch: {{ $noArch | toString | quote }} +{{- $withArch := include "helm_lib_pod_affinity" (list . (dict "app" "test") (list "amd64" "ppc64le")) }} +resultWithArch: {{ $withArch | toString | quote }} diff --git a/tests/tests/helm_lib_affinity_arch_require_test.yaml b/tests/tests/helm_lib_affinity_arch_required_test.yaml similarity index 93% rename from tests/tests/helm_lib_affinity_arch_require_test.yaml rename to tests/tests/helm_lib_affinity_arch_required_test.yaml index 602422b..d1cf680 100644 --- a/tests/tests/helm_lib_affinity_arch_require_test.yaml +++ b/tests/tests/helm_lib_affinity_arch_required_test.yaml @@ -1,6 +1,6 @@ -suite: helm_lib_affinity_arch_require definition +suite: helm_lib_affinity_arch_required definition templates: - - helm_lib_affinity_arch_require.yaml + - helm_lib_affinity_arch_required.yaml tests: - it: should render node affinity for allowed architectures set: diff --git a/tests/tests/helm_lib_affinity_ha_with_arch_require_test.yaml b/tests/tests/helm_lib_affinity_ha_with_arch_require_test.yaml deleted file mode 100644 index e190a1a..0000000 --- a/tests/tests/helm_lib_affinity_ha_with_arch_require_test.yaml +++ /dev/null @@ -1,64 +0,0 @@ -suite: helm_lib_affinity_ha_with_arch_require definition -templates: - - helm_lib_affinity_ha_with_arch_require.yaml -tests: - - it: should render pod anti-affinity and default allowed architectures when HA is enabled - set: - Chart: - Name: "testModule" - global: - highAvailability: true - asserts: - - notEqual: - path: resultDefault - value: "" - - matchRegex: - path: resultDefault - pattern: "podAntiAffinity" - - matchRegex: - path: resultDefault - pattern: "kubernetes.io/arch" - - matchRegex: - path: resultDefault - pattern: "operator:\\s*In" - - notMatchRegex: - path: resultDefault - pattern: "operator:\\s*NotIn" - - matchRegex: - path: resultDefault - pattern: "values:\\s*- \\\"amd64\\\"" - - it: should use provided allowed architectures list when passed explicitly - set: - Chart: - Name: "testModule" - global: - highAvailability: true - asserts: - - matchRegex: - path: resultCustom - pattern: "operator:\\s*In" - - notMatchRegex: - path: resultCustom - pattern: "operator:\\s*NotIn" - - matchRegex: - path: resultCustom - pattern: "- \\\"amd64\\\"" - - matchRegex: - path: resultCustom - pattern: "- \\\"ppc64le\\\"" - - notMatchRegex: - path: resultCustom - pattern: "\\\"arm\\\"" - - it: should render empty string when HA is disabled - set: - Chart: - Name: "testModule" - global: - highAvailability: false - asserts: - - equal: - path: resultDefault - value: "" - - equal: - path: resultCustom - value: "" diff --git a/tests/tests/helm_lib_pod_affinity_test.yaml b/tests/tests/helm_lib_pod_affinity_test.yaml new file mode 100644 index 0000000..c50e951 --- /dev/null +++ b/tests/tests/helm_lib_pod_affinity_test.yaml @@ -0,0 +1,74 @@ +suite: helm_lib_pod_affinity definition +templates: + - helm_lib_pod_affinity.yaml +tests: + - it: renders only pod anti-affinity when HA is enabled and architectures are not provided + set: + Chart: + Name: "testModule" + global: + highAvailability: true + asserts: + - matchRegex: + path: resultNoArch + pattern: "podAntiAffinity" + - matchRegex: + path: resultNoArch + pattern: "kubernetes.io/hostname" + - notMatchRegex: + path: resultNoArch + pattern: "nodeAffinity" + - it: renders pod anti-affinity and node affinity when HA is enabled and architectures are provided + set: + Chart: + Name: "testModule" + global: + highAvailability: true + asserts: + - matchRegex: + path: resultWithArch + pattern: "podAntiAffinity" + - matchRegex: + path: resultWithArch + pattern: "nodeAffinity" + - matchRegex: + path: resultWithArch + pattern: "operator:\\s*In" + - notMatchRegex: + path: resultWithArch + pattern: "operator:\\s*NotIn" + - matchRegex: + path: resultWithArch + pattern: "-\\s*\"amd64\"" + - matchRegex: + path: resultWithArch + pattern: "-\\s*\"ppc64le\"" + - it: renders only node affinity when HA is disabled but architectures are provided + set: + Chart: + Name: "testModule" + global: + highAvailability: false + asserts: + - matchRegex: + path: resultWithArch + pattern: "nodeAffinity" + - notMatchRegex: + path: resultWithArch + pattern: "podAntiAffinity" + - matchRegex: + path: resultWithArch + pattern: "-\\s*\"amd64\"" + - matchRegex: + path: resultWithArch + pattern: "-\\s*\"ppc64le\"" + - it: renders empty string when HA is disabled and architectures are not provided + set: + Chart: + Name: "testModule" + global: + highAvailability: false + asserts: + - equal: + path: resultNoArch + value: "" From 87b54487b9e72c61336aeacdd98cf00d65069e3e Mon Sep 17 00:00:00 2001 From: Paramoshka Date: Thu, 18 Dec 2025 11:34:18 +0300 Subject: [PATCH 2/2] removed redundant function Signed-off-by: Paramoshka --- charts/helm_lib/README.md | 11 ---- charts/helm_lib/templates/_node_affinity.tpl | 18 ------- .../helm_lib_affinity_arch_required.yaml | 4 -- .../helm_lib_affinity_arch_required_test.yaml | 50 ------------------- 4 files changed, 83 deletions(-) delete mode 100644 tests/templates/helm_lib_affinity_arch_required.yaml delete mode 100644 tests/tests/helm_lib_affinity_arch_required_test.yaml diff --git a/charts/helm_lib/README.md b/charts/helm_lib/README.md index 9a3cef2..8ab2707 100644 --- a/charts/helm_lib/README.md +++ b/charts/helm_lib/README.md @@ -125,7 +125,6 @@ | [_helm_lib_additional_tolerations_storage_problems](#_helm_lib_additional_tolerations_storage_problems) | | [_helm_lib_additional_tolerations_no_csi](#_helm_lib_additional_tolerations_no_csi) | | [_helm_lib_additional_tolerations_cloud_provider_uninitialized](#_helm_lib_additional_tolerations_cloud_provider_uninitialized) | -| [helm_lib_affinity_arch_required](#helm_lib_affinity_arch_required) | | **Pod Disruption Budget** | | [helm_lib_pdb_daemonset](#helm_lib_pdb_daemonset) | | **Priority Class** | @@ -1427,16 +1426,6 @@ list: `{{ include "helm_lib_tolerations" (tuple . "any-node" "with-cloud-provider-uninitialized") }} ` - -### helm_lib_affinity_arch_required - - Returns nodeAffinity that schedules pods only on specified architectures. - -#### Usage - -`{{- include "helm_lib_affinity_arch_required" (list . (list "amd64" "arm64")) ` - - ## Pod Disruption Budget ### helm_lib_pdb_daemonset diff --git a/charts/helm_lib/templates/_node_affinity.tpl b/charts/helm_lib/templates/_node_affinity.tpl index f3d623c..ab06d95 100644 --- a/charts/helm_lib/templates/_node_affinity.tpl +++ b/charts/helm_lib/templates/_node_affinity.tpl @@ -260,21 +260,3 @@ tolerations: operator: Exists {{- end }} {{- end }} - -{{- /* Returns nodeAffinity that schedules pods only on specified architectures.*/ -}} -{{- /* Usage: {{- include "helm_lib_affinity_arch_required" (list . (list "amd64" "arm64")) */ -}} -{{- define "helm_lib_affinity_arch_required" -}} - {{- $context := index . 0 -}} - {{- $allowedArchs := index . 1 -}} -affinity: - nodeAffinity: - requiredDuringSchedulingIgnoredDuringExecution: - nodeSelectorTerms: - - matchExpressions: - - key: kubernetes.io/arch - operator: In - values: - {{- range $allowedArchs }} - - {{ . | quote }} - {{- end }} -{{- end -}} diff --git a/tests/templates/helm_lib_affinity_arch_required.yaml b/tests/templates/helm_lib_affinity_arch_required.yaml deleted file mode 100644 index 2c6f59e..0000000 --- a/tests/templates/helm_lib_affinity_arch_required.yaml +++ /dev/null @@ -1,4 +0,0 @@ -{{- $default := include "helm_lib_affinity_arch_required" (list . (list "arm" "arm64")) }} -resultDefault: {{ $default | toString | quote }} -{{- $custom := include "helm_lib_affinity_arch_required" (list . (list "amd64" "ppc64le")) }} -resultCustom: {{ $custom | toString | quote }} diff --git a/tests/tests/helm_lib_affinity_arch_required_test.yaml b/tests/tests/helm_lib_affinity_arch_required_test.yaml deleted file mode 100644 index d1cf680..0000000 --- a/tests/tests/helm_lib_affinity_arch_required_test.yaml +++ /dev/null @@ -1,50 +0,0 @@ -suite: helm_lib_affinity_arch_required definition -templates: - - helm_lib_affinity_arch_required.yaml -tests: - - it: should render node affinity for allowed architectures - set: - Chart: - Name: "testModule" - asserts: - - notEqual: - path: resultDefault - value: "" - - matchRegex: - path: resultDefault - pattern: "nodeAffinity" - - matchRegex: - path: resultDefault - pattern: "kubernetes.io/arch" - - matchRegex: - path: resultDefault - pattern: "operator:\\s*In" - - notMatchRegex: - path: resultDefault - pattern: "operator:\\s*NotIn" - - matchRegex: - path: resultDefault - pattern: "- \\\"arm\\\"" - - matchRegex: - path: resultDefault - pattern: "- \\\"arm64\\\"" - - it: should allow overriding architectures list - set: - Chart: - Name: "testModule" - asserts: - - matchRegex: - path: resultCustom - pattern: "operator:\\s*In" - - notMatchRegex: - path: resultCustom - pattern: "operator:\\s*NotIn" - - matchRegex: - path: resultCustom - pattern: "- \\\"amd64\\\"" - - matchRegex: - path: resultCustom - pattern: "- \\\"ppc64le\\\"" - - notMatchRegex: - path: resultCustom - pattern: "\\\"arm\\\""