From f0d8df5e89fddc5709fc269c26745fd8c8fcb258 Mon Sep 17 00:00:00 2001 From: Stavros kois Date: Wed, 1 Feb 2023 19:37:19 +0200 Subject: [PATCH] add runtimeclassname --- .../tests/pod/runtime_class_name_test.yaml | 191 ++++++++++++++++++ library/common/1.0.0/docs/controllers.md | 5 + .../templates/lib/pod/_runtimeClassName.tpl | 48 +++++ 3 files changed, 244 insertions(+) create mode 100644 library/common-test/tests/pod/runtime_class_name_test.yaml create mode 100644 library/common/1.0.0/templates/lib/pod/_runtimeClassName.tpl diff --git a/library/common-test/tests/pod/runtime_class_name_test.yaml b/library/common-test/tests/pod/runtime_class_name_test.yaml new file mode 100644 index 0000000000..fa8624a72d --- /dev/null +++ b/library/common-test/tests/pod/runtime_class_name_test.yaml @@ -0,0 +1,191 @@ +suite: pod runtime class name test +templates: + - common.yaml +tests: + - it: should pass with empty runtimeClassName + set: + podOptions: + runtimeClassName: "" + controllers: + controller-name1: + enabled: true + primary: true + type: Deployment + podSpec: {} + asserts: + - documentIndex: &deploymentDoc 0 + isKind: + of: Deployment + - documentIndex: *deploymentDoc + isNull: + path: spec.template.spec.runtimeClassName + + - it: should pass with runtimeClassName from "global" + set: + podOptions: + runtimeClassName: some-runtime-class + controllers: + controller-name1: + enabled: true + primary: true + type: Deployment + podSpec: {} + asserts: + - documentIndex: *deploymentDoc + equal: + path: spec.template.spec.runtimeClassName + value: some-runtime-class + + - it: should pass with runtimeClassName from "pod" + set: + podOptions: + runtimeClassName: some-runtime-class + controllers: + controller-name1: + enabled: true + primary: true + type: Deployment + podSpec: + runtimeClassName: some-other-runtime-class + asserts: + - documentIndex: *deploymentDoc + equal: + path: spec.template.spec.runtimeClassName + value: some-other-runtime-class + + - it: should pass with runtimeClassName from "pod" with tpl + set: + runtimeClass: some-other-runtime-class + podOptions: + runtimeClassName: some-runtime-class + controllers: + controller-name1: + enabled: true + primary: true + type: Deployment + podSpec: + runtimeClassName: "{{ .Values.runtimeClass }}" + asserts: + - documentIndex: *deploymentDoc + equal: + path: spec.template.spec.runtimeClassName + value: some-other-runtime-class + + - it: should pass with runtimeClassName from ixChartContext with targetSelector + set: + scaleGPU: + - gpu: + key: value + targetSelector: + controller-name1: + - container-name1 + controller-name3: + - container-name1 + global: + ixChartContext: + addNvidiaRuntimeClass: true + nvidiaRuntimeClassName: ix-runtime + podOptions: + runtimeClassName: some-class + controllers: + controller-name1: + enabled: true + primary: true + type: Deployment + podSpec: + runtimeClassName: some-other-class + controller-name2: + enabled: true + primary: false + type: DaemonSet + podSpec: + runtimeClassName: some-class + controller-name3: + enabled: true + primary: false + type: StatefulSet + podSpec: + runtimeClassName: some-class + asserts: + - documentIndex: *deploymentDoc + equal: + path: spec.template.spec.runtimeClassName + value: ix-runtime + - documentIndex: &daemonSetDoc 1 + isKind: + of: DaemonSet + - documentIndex: *daemonSetDoc + equal: + path: spec.template.spec.runtimeClassName + value: some-class + - documentIndex: &statefulSetDoc 2 + isKind: + of: StatefulSet + - documentIndex: *statefulSetDoc + equal: + path: spec.template.spec.runtimeClassName + value: ix-runtime + + - it: should pass with runtimeClassName from ixChartContext without targetSelector (on primary controller) + set: + scaleGPU: + - gpu: + key: value + global: + ixChartContext: + addNvidiaRuntimeClass: true + nvidiaRuntimeClassName: ix-runtime + controllers: + controller-name1: + enabled: true + primary: true + type: Job + podSpec: + runtimeClassName: some-other-class + controller-name2: + enabled: true + primary: false + type: CronJob + schedule: "* * * * *" + podSpec: {} + asserts: + - documentIndex: &jobDoc 0 + isKind: + of: Job + - documentIndex: *jobDoc + equal: + path: spec.template.spec.runtimeClassName + value: ix-runtime + - documentIndex: &cronJobDoc 1 + isKind: + of: CronJob + - documentIndex: *cronJobDoc + isNull: + path: spec.jobTemplate.spec.template.spec.runtimeClassName + + - it: should pass with runtimeClassName not set from ixChartContext without gpu value + set: + scaleGPU: + - gpu: {} + targetSelector: + controller-name1: + - container-name1 + global: + ixChartContext: + addNvidiaRuntimeClass: true + nvidiaRuntimeClassName: ix-runtime + controllers: + controller-name1: + enabled: true + primary: true + type: Job + podSpec: + runtimeClassName: some-other-class + asserts: + - documentIndex: *jobDoc + isKind: + of: Job + - documentIndex: *jobDoc + equal: + path: spec.template.spec.runtimeClassName + value: some-other-class diff --git a/library/common/1.0.0/docs/controllers.md b/library/common/1.0.0/docs/controllers.md index d9128e3dd7..598bb3f32e 100644 --- a/library/common/1.0.0/docs/controllers.md +++ b/library/common/1.0.0/docs/controllers.md @@ -42,12 +42,16 @@ | 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`. | +| controllers.[controller-name].podSpec.runtimeClassName | `string` | ✅ | ❌ | `{{ .Values.podOptions.runtimeClassName }}` ("") | Pod's runtimeClassName | --- Notes > `dnsPolicy` is set automatically to `ClusterFirstWithHostNet` when `hostNetwork` is `true` +> `runtimeClassName` will ignore any value set and use the `.Values.global.ixChartContext.nvidiaRuntimeClassName`, +> if a GPU is assigned to a container and Scale Middleware sets `.Values.global.ixChartContext.addNvidiaRuntimeClass` to `true`. +> Note that it will only set the `runtimeClassName` on the pod that this container belongs to. --- @@ -125,4 +129,5 @@ controllers: - operator: Exists effect: NoExecute tolerationSeconds: 3600 + runtimeClassName: some-runtime-class ``` diff --git a/library/common/1.0.0/templates/lib/pod/_runtimeClassName.tpl b/library/common/1.0.0/templates/lib/pod/_runtimeClassName.tpl new file mode 100644 index 0000000000..731b985b5f --- /dev/null +++ b/library/common/1.0.0/templates/lib/pod/_runtimeClassName.tpl @@ -0,0 +1,48 @@ +{{/* Returns Runtime Class Name */}} +{{/* Call this template: +{{ include "ix.v1.common.lib.pod.runtimeClassName" (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.runtimeClassName" -}} + {{- $rootCtx := .rootCtx -}} + {{- $objectData := .objectData -}} + + {{- $runtime := "" -}} + + {{/* Initialize from the "defaults" */}} + {{- with $rootCtx.Values.podOptions.runtimeClassName -}} + {{- $runtime = tpl . $rootCtx -}} + {{- end -}} + + {{/* Override from the pod values, if defined */}} + {{- with $objectData.podSpec.runtimeClassName -}} + {{- $runtime = tpl . $rootCtx -}} + {{- end -}} + + {{- if hasKey $rootCtx.Values.global "ixChartContext" -}} + {{- if $rootCtx.Values.global.ixChartContext.addNvidiaRuntimeClass -}} + + {{- range $rootCtx.Values.scaleGPU -}} + {{- if .gpu -}} {{/* Make sure it has a value... */}} + + {{- if (kindIs "map" .targetSelector) -}} + {{- range $podName, $containers := .targetSelector -}} + {{- if eq $objectData.shortName $podName -}} {{/* If the pod is selected */}} + {{- $runtime = $rootCtx.Values.global.ixChartContext.nvidiaRuntimeClassName -}} + {{- end -}} + {{- end -}} + + {{- else if $objectData.primary -}} + + {{/* If the pod is primary and no targetSelector is given, assign to primary */}} + {{- $runtime = $rootCtx.Values.global.ixChartContext.nvidiaRuntimeClassName -}} + + {{- end -}} + {{- end -}} + {{- end -}} + {{- end -}} + {{- end -}} + + {{- $runtime -}} +{{- end -}}