Skip to content

Commit 8fedcc6

Browse files
authored
Merge pull request #167 from deckhouse/refactor-affinty
refactored affinity
2 parents 5d0e62c + 87b5448 commit 8fedcc6

10 files changed

+101
-165
lines changed

charts/helm_lib/Chart.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
apiVersion: v2
22
type: library
33
name: deckhouse_lib_helm
4-
version: 1.68.0
4+
version: 1.68.1
55
description: "Helm utils template definitions for Deckhouse modules."

charts/helm_lib/README.md

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@
125125
| [_helm_lib_additional_tolerations_storage_problems](#_helm_lib_additional_tolerations_storage_problems) |
126126
| [_helm_lib_additional_tolerations_no_csi](#_helm_lib_additional_tolerations_no_csi) |
127127
| [_helm_lib_additional_tolerations_cloud_provider_uninitialized](#_helm_lib_additional_tolerations_cloud_provider_uninitialized) |
128-
| [helm_lib_affinity_arch_require](#helm_lib_affinity_arch_require) |
129128
| **Pod Disruption Budget** |
130129
| [helm_lib_pdb_daemonset](#helm_lib_pdb_daemonset) |
131130
| **Priority Class** |
@@ -140,7 +139,7 @@
140139
| [helm_lib_container_kube_rbac_proxy_resources](#helm_lib_container_kube_rbac_proxy_resources) |
141140
| **Spec For High Availability** |
142141
| [helm_lib_pod_anti_affinity_for_ha](#helm_lib_pod_anti_affinity_for_ha) |
143-
| [helm_lib_affinity_ha_with_arch_require](#helm_lib_affinity_ha_with_arch_require) |
142+
| [helm_lib_pod_affinity](#helm_lib_pod_affinity) |
144143
| [helm_lib_deployment_on_master_strategy_and_replicas_for_ha](#helm_lib_deployment_on_master_strategy_and_replicas_for_ha) |
145144
| [helm_lib_deployment_on_master_custom_strategy_and_replicas_for_ha](#helm_lib_deployment_on_master_custom_strategy_and_replicas_for_ha) |
146145
| [helm_lib_deployment_strategy_and_replicas_for_ha](#helm_lib_deployment_strategy_and_replicas_for_ha) |
@@ -1427,16 +1426,6 @@ list:
14271426
`{{ include "helm_lib_tolerations" (tuple . "any-node" "with-cloud-provider-uninitialized") }} `
14281427

14291428

1430-
1431-
### helm_lib_affinity_arch_require
1432-
1433-
Returns nodeAffinity that schedules pods only on specified architectures.
1434-
1435-
#### Usage
1436-
1437-
`{{- include "helm_lib_affinity_arch_require" (list . (list "amd64" "arm64")) `
1438-
1439-
14401429
## Pod Disruption Budget
14411430

14421431
### helm_lib_pdb_daemonset
@@ -1573,13 +1562,13 @@ list:
15731562
- Match labels for podAntiAffinity label selector
15741563

15751564

1576-
### helm_lib_affinity_ha_with_arch_require
1565+
### helm_lib_pod_affinity
15771566

1578-
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"].
1567+
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.
15791568

15801569
#### Usage
15811570

1582-
`{{- include "helm_lib_affinity_ha_with_arch_require" (list . (dict "app" "test") (list "amd64")) }} `
1571+
`{{- include "helm_lib_pod_affinity" (list . (dict "app" "test") (list "amd64")) }} `
15831572

15841573
#### Arguments
15851574

charts/helm_lib/templates/_node_affinity.tpl

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -260,21 +260,3 @@ tolerations:
260260
operator: Exists
261261
{{- end }}
262262
{{- end }}
263-
264-
{{- /* Returns nodeAffinity that schedules pods only on specified architectures.*/ -}}
265-
{{- /* Usage: {{- include "helm_lib_affinity_arch_require" (list . (list "amd64" "arm64")) */ -}}
266-
{{- define "helm_lib_affinity_arch_require" -}}
267-
{{- $context := index . 0 -}}
268-
{{- $allowedArchs := index . 1 -}}
269-
affinity:
270-
nodeAffinity:
271-
requiredDuringSchedulingIgnoredDuringExecution:
272-
nodeSelectorTerms:
273-
- matchExpressions:
274-
- key: kubernetes.io/arch
275-
operator: In
276-
values:
277-
{{- range $allowedArchs }}
278-
- {{ . | quote }}
279-
{{- end }}
280-
{{- end -}}

charts/helm_lib/templates/_spec_for_high_availability.tpl

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,23 @@ affinity:
1616
{{- end }}
1717
{{- end }}
1818

19-
{{- /* Usage: {{- include "helm_lib_affinity_ha_with_arch_require" (list . (dict "app" "test") (list "amd64")) }} */}}
20-
{{- /* 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"]. */ -}}
21-
{{- define "helm_lib_affinity_ha_with_arch_require" }}
19+
{{- /* Usage: {{- include "helm_lib_pod_affinity" (list . (dict "app" "test") (list "amd64")) }} */}}
20+
{{- /* 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. */ -}}
21+
{{- define "helm_lib_pod_affinity" }}
2222
{{- $context := index . 0 -}} {{- /* Template context with .Values, .Chart, etc */ -}}
23-
{{- $labels := index . 1 }} {{- /* Match labels for podAntiAffinity label selector */ -}}
24-
{{- $allowedArchs := list "amd64" -}}
23+
{{- $labels := dict -}} {{- /* Match labels for podAntiAffinity label selector */ -}}
24+
{{- if ge (len .) 2 }}
25+
{{- $labels = index . 1 }}
26+
{{- end }}
27+
{{- $allowedArchs := list -}} {{- /* List of supported architectures */ -}}
2528
{{- if ge (len .) 3 }}
2629
{{- $allowedArchs = index . 2 }}
2730
{{- end }}
28-
{{- if (include "helm_lib_ha_enabled" $context) }}
31+
{{- $haEnabled := (include "helm_lib_ha_enabled" $context) -}}
32+
{{- $hasArch := gt (len $allowedArchs) 0 -}}
33+
{{- if or $haEnabled $hasArch }}
2934
affinity:
35+
{{- if $haEnabled }}
3036
podAntiAffinity:
3137
requiredDuringSchedulingIgnoredDuringExecution:
3238
- labelSelector:
@@ -35,18 +41,21 @@ affinity:
3541
{{ $key }}: {{ $value | quote }}
3642
{{- end }}
3743
topologyKey: kubernetes.io/hostname
44+
{{- end }}
45+
{{- if $hasArch }}
3846
nodeAffinity:
3947
requiredDuringSchedulingIgnoredDuringExecution:
4048
nodeSelectorTerms:
4149
- matchExpressions:
42-
- key: kubernetes.io/arch
43-
operator: In
44-
values:
50+
- key: kubernetes.io/arch
51+
operator: In
52+
values:
4553
{{- range $allowedArchs }}
4654
- {{ . | quote }}
4755
{{- end }}
4856
{{- end }}
4957
{{- end }}
58+
{{- end }}
5059

5160
{{- /* Usage: {{ include "helm_lib_deployment_on_master_strategy_and_replicas_for_ha" }} */ -}}
5261
{{- /* returns deployment strategy and replicas for ha components running on master nodes */ -}}

tests/templates/helm_lib_affinity_arch_require.yaml

Lines changed: 0 additions & 4 deletions
This file was deleted.

tests/templates/helm_lib_affinity_ha_with_arch_require.yaml

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{{- $noArch := include "helm_lib_pod_affinity" (list . (dict "app" "test")) }}
2+
resultNoArch: {{ $noArch | toString | quote }}
3+
{{- $withArch := include "helm_lib_pod_affinity" (list . (dict "app" "test") (list "amd64" "ppc64le")) }}
4+
resultWithArch: {{ $withArch | toString | quote }}

tests/tests/helm_lib_affinity_arch_require_test.yaml

Lines changed: 0 additions & 50 deletions
This file was deleted.

tests/tests/helm_lib_affinity_ha_with_arch_require_test.yaml

Lines changed: 0 additions & 64 deletions
This file was deleted.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
suite: helm_lib_pod_affinity definition
2+
templates:
3+
- helm_lib_pod_affinity.yaml
4+
tests:
5+
- it: renders only pod anti-affinity when HA is enabled and architectures are not provided
6+
set:
7+
Chart:
8+
Name: "testModule"
9+
global:
10+
highAvailability: true
11+
asserts:
12+
- matchRegex:
13+
path: resultNoArch
14+
pattern: "podAntiAffinity"
15+
- matchRegex:
16+
path: resultNoArch
17+
pattern: "kubernetes.io/hostname"
18+
- notMatchRegex:
19+
path: resultNoArch
20+
pattern: "nodeAffinity"
21+
- it: renders pod anti-affinity and node affinity when HA is enabled and architectures are provided
22+
set:
23+
Chart:
24+
Name: "testModule"
25+
global:
26+
highAvailability: true
27+
asserts:
28+
- matchRegex:
29+
path: resultWithArch
30+
pattern: "podAntiAffinity"
31+
- matchRegex:
32+
path: resultWithArch
33+
pattern: "nodeAffinity"
34+
- matchRegex:
35+
path: resultWithArch
36+
pattern: "operator:\\s*In"
37+
- notMatchRegex:
38+
path: resultWithArch
39+
pattern: "operator:\\s*NotIn"
40+
- matchRegex:
41+
path: resultWithArch
42+
pattern: "-\\s*\"amd64\""
43+
- matchRegex:
44+
path: resultWithArch
45+
pattern: "-\\s*\"ppc64le\""
46+
- it: renders only node affinity when HA is disabled but architectures are provided
47+
set:
48+
Chart:
49+
Name: "testModule"
50+
global:
51+
highAvailability: false
52+
asserts:
53+
- matchRegex:
54+
path: resultWithArch
55+
pattern: "nodeAffinity"
56+
- notMatchRegex:
57+
path: resultWithArch
58+
pattern: "podAntiAffinity"
59+
- matchRegex:
60+
path: resultWithArch
61+
pattern: "-\\s*\"amd64\""
62+
- matchRegex:
63+
path: resultWithArch
64+
pattern: "-\\s*\"ppc64le\""
65+
- it: renders empty string when HA is disabled and architectures are not provided
66+
set:
67+
Chart:
68+
Name: "testModule"
69+
global:
70+
highAvailability: false
71+
asserts:
72+
- equal:
73+
path: resultNoArch
74+
value: ""

0 commit comments

Comments
 (0)