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..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_require](#helm_lib_affinity_arch_require) | | **Pod Disruption Budget** | | [helm_lib_pdb_daemonset](#helm_lib_pdb_daemonset) | | **Priority Class** | @@ -140,7 +139,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) | @@ -1427,16 +1426,6 @@ list: `{{ include "helm_lib_tolerations" (tuple . "any-node" "with-cloud-provider-uninitialized") }} ` - -### helm_lib_affinity_arch_require - - Returns nodeAffinity that schedules pods only on specified architectures. - -#### Usage - -`{{- include "helm_lib_affinity_arch_require" (list . (list "amd64" "arm64")) ` - - ## Pod Disruption Budget ### helm_lib_pdb_daemonset @@ -1573,13 +1562,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..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_require" (list . (list "amd64" "arm64")) */ -}} -{{- define "helm_lib_affinity_arch_require" -}} - {{- $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/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_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_require_test.yaml deleted file mode 100644 index 602422b..0000000 --- a/tests/tests/helm_lib_affinity_arch_require_test.yaml +++ /dev/null @@ -1,50 +0,0 @@ -suite: helm_lib_affinity_arch_require definition -templates: - - helm_lib_affinity_arch_require.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\\\"" 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: ""