From 64a46e9e14f783d4d7899bf7e1581cb5c9730516 Mon Sep 17 00:00:00 2001 From: Stavros kois Date: Wed, 1 Feb 2023 16:35:40 +0200 Subject: [PATCH] tolerations --- .../tests/pod/tolerations_test.yaml | 201 ++++++++++++++++++ library/common/1.0.0/docs/controllers.md | 10 + .../1.0.0/templates/lib/controller/_pod.tpl | 8 +- .../1.0.0/templates/lib/pod/_tolerations.tpl | 65 ++++++ 4 files changed, 282 insertions(+), 2 deletions(-) create mode 100644 library/common-test/tests/pod/tolerations_test.yaml create mode 100644 library/common/1.0.0/templates/lib/pod/_tolerations.tpl diff --git a/library/common-test/tests/pod/tolerations_test.yaml b/library/common-test/tests/pod/tolerations_test.yaml new file mode 100644 index 0000000000..a2f7c61370 --- /dev/null +++ b/library/common-test/tests/pod/tolerations_test.yaml @@ -0,0 +1,201 @@ +suite: pod tolerations test +templates: + - common.yaml +tests: + - it: should pass with empty tolerations + set: + podOptions: + tolerations: [] + controllers: + controller-name1: + enabled: true + primary: true + type: Deployment + podSpec: {} + asserts: + - documentIndex: &deploymentDoc 0 + isKind: + of: Deployment + - documentIndex: *deploymentDoc + isNull: + path: spec.template.spec.tolerations + + - it: should pass with tolerations from "global" + set: + podOptions: + tolerations: + - operator: Exists + effect: NoExecute + tolerationSeconds: 3600 + controllers: + controller-name1: + enabled: true + primary: true + type: Deployment + podSpec: {} + asserts: + - documentIndex: *deploymentDoc + equal: + path: spec.template.spec.tolerations + value: + - operator: Exists + effect: NoExecute + tolerationSeconds: 3600 + + - it: should pass with tolerations from "pod" + set: + podOptions: + tolerations: + - operator: Exists + effect: NoExecute + tolerationSeconds: 3600 + controllers: + controller-name1: + enabled: true + primary: true + type: Deployment + podSpec: + tolerations: + - operator: Exists + effect: NoSchedule + tolerationSeconds: 1800 + asserts: + - documentIndex: *deploymentDoc + equal: + path: spec.template.spec.tolerations + value: + - operator: Exists + effect: NoSchedule + tolerationSeconds: 1800 + + - it: should pass with tolerations from "pod" with tpl + set: + op: Exists + effect: NoSchedule + op1: Equal + effect1: PreferNoSchedule + key: key + value: value + podOptions: + tolerations: + - operator: Exists + effect: NoExecute + tolerationSeconds: 3600 + controllers: + controller-name1: + enabled: true + primary: true + type: Deployment + podSpec: + tolerations: + - operator: "{{ .Values.op }}" + effect: "{{ .Values.effect }}" + tolerationSeconds: 1800 + - operator: "{{ .Values.op1 }}" + effect: "{{ .Values.effect1 }}" + tolerationSeconds: 1800 + key: "{{ .Values.key }}" + value: "{{ .Values.value }}" + asserts: + - documentIndex: *deploymentDoc + equal: + path: spec.template.spec.tolerations + value: + - operator: Exists + effect: NoSchedule + tolerationSeconds: 1800 + - operator: Equal + effect: PreferNoSchedule + tolerationSeconds: 1800 + key: key + value: value + + # Failures + - it: should fail with invalid operator + set: + controllers: + controller-name1: + enabled: true + primary: true + type: Deployment + podSpec: + tolerations: + - operator: Invalid + asserts: + - failedTemplate: + errorMessage: Expected to be one of [Exists, Equal] but got [Invalid] + + - it: should fail with empty key and operator set to Equal + set: + controllers: + controller-name1: + enabled: true + primary: true + type: Deployment + podSpec: + tolerations: + - operator: Equal + value: value + asserts: + - failedTemplate: + errorMessage: Expected non-empty and with set to [Equal] + + - it: should fail with empty value and operator set to Equal + set: + controllers: + controller-name1: + enabled: true + primary: true + type: Deployment + podSpec: + tolerations: + - operator: Equal + key: key + asserts: + - failedTemplate: + errorMessage: Expected non-empty and with set to [Equal] + + - it: should fail with value set and operator set to Exists + set: + controllers: + controller-name1: + enabled: true + primary: true + type: Deployment + podSpec: + tolerations: + - operator: Exists + value: value + asserts: + - failedTemplate: + errorMessage: Expected empty with set to [Exists], but got [value] + + - it: should fail with effect set to Invalid + set: + controllers: + controller-name1: + enabled: true + primary: true + type: Deployment + podSpec: + tolerations: + - operator: Exists + effect: Invalid + asserts: + - failedTemplate: + errorMessage: Expected to be one of [NoExecute, NoSchedule, PreferNoSchedule], but got [Invalid] + + - it: should fail with tolerationSeconds not a number + set: + controllers: + controller-name1: + enabled: true + primary: true + type: Deployment + podSpec: + tolerations: + - operator: Exists + tolerationSeconds: not-a-number + asserts: + - failedTemplate: + errorMessage: Expected to be a number, but got [not-a-number] diff --git a/library/common/1.0.0/docs/controllers.md b/library/common/1.0.0/docs/controllers.md index f21de7557e..ee95117b2c 100644 --- a/library/common/1.0.0/docs/controllers.md +++ b/library/common/1.0.0/docs/controllers.md @@ -35,6 +35,12 @@ | controllers.[controller-name].podSpec.hostAliases.hostnames | `list` | ❌ | ❌ | | Hostnames for the `ip` in hosts aliases | | controllers.[controller-name].podSpec.hostAliases.hostnames.[host-name] | `string` | ❌ | ✅ | | [Value] for `hostnames` for the `ip` in hosts aliases | | controllers.[controller-name].podSpec.dnsPolicy | `string` | ❌ | ✅ | `{{ .Values.podOptions.dnsPolicy }}` (ClusterFirst) | Pod's DNS Policy (ClusterFirst, ClusterFirstWithHostNet, Default, None). | +| controllers.[controller-name].podSpec.tolerations | `list` | ❌ | ❌ | `{{ .Values.podOptions.tolerations }}` ([]) | Pod's Tolerations | +| controllers.[controller-name].podSpec.tolerations.operator | `string` | ✅ | ✅ | | Toleration's `operator` (Equal, Exists) | +| controllers.[controller-name].podSpec.tolerations.key | `string` | ❌/✅ | ✅ | | Toleration's `key`. Required only when `operator` = `Equal` | +| controllers.[controller-name].podSpec.tolerations.value | `string` | ❌/✅ | ✅ | | Toleration's `value`. Required only when `operator` = `Equal` | +| controllers.[controller-name].podSpec.tolerations.effect | `string` | ❌ | ✅ | | Toleration's `effect`.(NoExecute, NoSchedule, PreferNoSchedule) | +| controllers.[controller-name].podSpec.tolerations.tolerationSeconds | `int` | ❌ | ❌ | | Toleration's `tolerationSeconds`. | --- @@ -113,4 +119,8 @@ controllers: - name: ndots value: "2" - name: edns0 + tolerations: + - operator: Exists + effect: NoExecute + tolerationSeconds: 3600 ``` diff --git a/library/common/1.0.0/templates/lib/controller/_pod.tpl b/library/common/1.0.0/templates/lib/controller/_pod.tpl index b407a756d8..7f1545cffb 100644 --- a/library/common/1.0.0/templates/lib/controller/_pod.tpl +++ b/library/common/1.0.0/templates/lib/controller/_pod.tpl @@ -20,7 +20,7 @@ schedulerName: {{ . }} {{- end -}} {{- with (include "ix.v1.common.lib.pod.priorityClassName" (dict "rootCtx" $rootCtx "objectData" $objectData)) }} priorityClassName: {{ . }} - {{- end }} + {{- end -}} {{- with (include "ix.v1.common.lib.pod.nodeSelector" (dict "rootCtx" $rootCtx "objectData" $objectData) | trim) }} nodeSelector: {{- . | nindent 2 }} @@ -35,5 +35,9 @@ hostname: {{ . }} {{- include "ix.v1.common.lib.pod.dns" (dict "rootCtx" $rootCtx "objectData" $objectData) -}} {{- with (include "ix.v1.common.lib.pod.terminationGracePeriodSeconds" (dict "rootCtx" $rootCtx "objectData" $objectData)) }} terminationGracePeriodSeconds: {{ . }} - {{- end }} + {{- end -}} + {{- with (include "ix.v1.common.lib.pod.tolerations" (dict "rootCtx" $rootCtx "objectData" $objectData) | trim) }} +tolerations: + {{- . | nindent 2 }} + {{- end -}} {{- end -}} diff --git a/library/common/1.0.0/templates/lib/pod/_tolerations.tpl b/library/common/1.0.0/templates/lib/pod/_tolerations.tpl new file mode 100644 index 0000000000..cf4668394b --- /dev/null +++ b/library/common/1.0.0/templates/lib/pod/_tolerations.tpl @@ -0,0 +1,65 @@ +{{/* Returns Tolerations */}} +{{/* Call this template: +{{ include "ix.v1.common.lib.pod.tolerations" (dict "rootCtx" $ "objectData" $objectData) }} +rootCtx: The root context of the template. It is used to access the global context. +objectData: The object data to be used to render the Pod. +*/}} +{{- define "ix.v1.common.lib.pod.tolerations" -}} + {{- $rootCtx := .rootCtx -}} + {{- $objectData := .objectData -}} + + {{- $tolerations := list -}} + + {{/* Initialize from the "global" option */}} + {{- with $rootCtx.Values.podOptions.tolerations -}} + {{- $tolerations = . -}} + {{- end -}} + + {{/* Override from the "pod" option */}} + {{- with $objectData.podSpec.tolerations -}} + {{- $tolerations = . -}} + {{- end -}} + + {{- range $tolerations -}} + {{/* Expand values */}} + {{- $operator := (tpl (.operator | default "") $rootCtx) -}} + {{- $key := (tpl (.key | default "") $rootCtx) -}} + {{- $value := (tpl (.value | default "") $rootCtx) -}} + {{- $effect := (tpl (.effect | default "") $rootCtx) -}} + {{- $tolSeconds := .tolerationSeconds -}} + + {{- if not (mustHas $operator (list "Exists" "Equal")) -}} + {{- fail (printf "Expected to be one of [Exists, Equal] but got [%s]" $operator) -}} + {{- end -}} + + {{- if and (eq $operator "Equal") (or (not $key) (not $value)) -}} + {{- fail "Expected non-empty and with set to [Equal]" -}} + {{- end -}} + + {{- if and (eq $operator "Exists") $value -}} + {{- fail (printf "Expected empty with set to [Exists], but got [%s]" $value) -}} + {{- end -}} + + {{- if and $effect (not (mustHas $effect (list "NoExecute" "NoSchedule" "PreferNoSchedule"))) -}} + {{- fail (printf "Expected to be one of [NoExecute, NoSchedule, PreferNoSchedule], but got [%s]" $effect) -}} + {{- end -}} + + {{- if and (not (kindIs "invalid" $tolSeconds)) (not (mustHas (kindOf $tolSeconds) (list "int" "float64"))) -}} + {{- fail (printf "Expected to be a number, but got [%s]" $tolSeconds) -}} + {{- end }} +- operator: {{ $operator }} + {{- with $key }} + key: {{ $key }} + {{- end -}} + {{- with $effect }} + effect: {{ $effect }} + {{- end -}} + {{- with $value }} + value: {{ . }} + {{- end -}} + {{- if (mustHas (kindOf $tolSeconds) (list "int" "float64")) }} + tolerationSeconds: {{ $tolSeconds }} + {{- end -}} + + {{- end -}} +{{- end -}}