From 30bbceed0185a30dfe53758476bcd74afab81c5b Mon Sep 17 00:00:00 2001 From: Stavros Kois <47820033+stavros-k@users.noreply.github.com> Date: Sat, 15 Apr 2023 13:06:56 +0300 Subject: [PATCH] fix(resources): correctly handle "0" gpu (#389) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit **Description** ⚒️ Fixes https://github.com/truecharts/charts/issues/8004 ⚒️ Fixes https://github.com/truecharts/charts/issues/8013 (OVPN needs MKNOD) **⚙️ Type of change** - [ ] ⚙️ Feature/App addition - [ ] 🪛 Bugfix - [ ] ⚠️ Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] 🔃 Refactor of current code **🧪 How Has This Been Tested?** **📃 Notes:** **✔️ Checklist:** - [ ] ⚖️ My code follows the style guidelines of this project - [ ] 👀 I have performed a self-review of my own code - [ ] #️⃣ I have commented my code, particularly in hard-to-understand areas - [ ] 📄 I have made corresponding changes to the documentation - [ ] ⚠️ My changes generate no new warnings - [ ] 🧪 I have added tests to this description that prove my fix is effective or that my feature works - [ ] ⬆️ I increased versions for any altered app according to semantic versioning **➕ App addition** If this PR is an app addition please make sure you have done the following. - [ ] 🪞 I have opened a PR on [truecharts/containers](https://github.com/truecharts/containers) adding the container to TrueCharts mirror repo. - [ ] 🖼️ I have added an icon in the Chart's root directory called `icon.png` --- _Please don't blindly check all the boxes. Read them and only check those that apply. Those checkboxes are there for the reviewer to see what is this all about and the status of this PR with a quick glance._ --- .../common-test/tests/addons/vpn_test.yaml | 17 ++ .../tests/container/resources_test.yaml | 166 ++++++++++++++++++ library/common/Chart.yaml | 2 +- .../addons/code-server/_container.tpl | 3 + .../addons/vpn/_gluetunContainer.tpl | 2 +- .../addons/vpn/_openvpnContainer.tpl | 3 +- .../addons/vpn/_wireguardContainer.tpl | 4 +- .../templates/lib/container/_resources.tpl | 6 +- 8 files changed, 197 insertions(+), 6 deletions(-) diff --git a/library/common-test/tests/addons/vpn_test.yaml b/library/common-test/tests/addons/vpn_test.yaml index aea8daaa..244efd9e 100644 --- a/library/common-test/tests/addons/vpn_test.yaml +++ b/library/common-test/tests/addons/vpn_test.yaml @@ -13,6 +13,11 @@ tests: env: key: value key1: value1 + envList: + - name: key2 + value: value2 + - name: key3 + value: value3 workload: &workload main: enabled: true @@ -102,6 +107,18 @@ tests: content: name: key1 value: value1 + - documentIndex: *DeploymentDocument + contains: + path: spec.template.spec.containers[1].env + content: + name: key2 + value: value2 + - documentIndex: *DeploymentDocument + contains: + path: spec.template.spec.containers[1].env + content: + name: key3 + value: value3 - documentIndex: *DeploymentDocument contains: path: spec.template.spec.containers[1].volumeMounts diff --git a/library/common-test/tests/container/resources_test.yaml b/library/common-test/tests/container/resources_test.yaml index 6dbb6349..a2924eae 100644 --- a/library/common-test/tests/container/resources_test.yaml +++ b/library/common-test/tests/container/resources_test.yaml @@ -628,6 +628,147 @@ tests: cpu: 10m memory: 50Mi + - it: should assign GPU on the selected pod/container with multiple GPUs + set: + image: *image + global: + ixChartContext: + addNvidiaRuntimeClass: true + nvidiaRuntimeClassName: nvidia + scaleGPU: + - gpu: + nvidia.com/gpu: 1 + amd.com/gpu: 0 + targetSelector: + workload-name1: + - container-name1 + - container-name2 + workload: + workload-name1: + enabled: true + primary: true + type: Deployment + podSpec: + containers: + container-name1: + enabled: true + primary: true + imageSelector: image + probes: *probes + container-name2: + enabled: true + primary: false + imageSelector: image + probes: *probes + asserts: + - documentIndex: &deploymentDoc 0 + isKind: + of: Deployment + - documentIndex: *deploymentDoc + isAPIVersion: + of: apps/v1 + - documentIndex: *deploymentDoc + equal: + path: spec.template.spec.runtimeClassName + value: nvidia + - documentIndex: *deploymentDoc + isSubset: + path: spec.template.spec.containers[0] + content: + resources: + limits: + cpu: 4000m + memory: 8Gi + nvidia.com/gpu: "1" + requests: + cpu: 10m + memory: 50Mi + - documentIndex: *deploymentDoc + isSubset: + path: spec.template.spec.containers[1] + content: + resources: + limits: + cpu: 4000m + memory: 8Gi + nvidia.com/gpu: "1" + requests: + cpu: 10m + memory: 50Mi + + - it: should assign multiple GPU on the selected pod/container with multiple selected GPUs + set: + image: *image + global: + ixChartContext: + addNvidiaRuntimeClass: true + nvidiaRuntimeClassName: nvidia + scaleGPU: + - gpu: + nvidia.com/gpu: 1 + amd.com/gpu: 0 + targetSelector: + workload-name1: + - container-name1 + - gpu: + nvidia.com/gpu: 0 + amd.com/gpu: 1 + targetSelector: + workload-name1: + - container-name2 + workload: + workload-name1: + enabled: true + primary: true + type: Deployment + podSpec: + containers: + container-name1: + enabled: true + primary: true + imageSelector: image + probes: *probes + container-name2: + enabled: true + primary: false + imageSelector: image + probes: *probes + asserts: + - documentIndex: &deploymentDoc 0 + isKind: + of: Deployment + - documentIndex: *deploymentDoc + isAPIVersion: + of: apps/v1 + - documentIndex: *deploymentDoc + equal: + path: spec.template.spec.runtimeClassName + value: nvidia + - documentIndex: *deploymentDoc + isSubset: + path: spec.template.spec.containers[0] + content: + resources: + limits: + cpu: 4000m + memory: 8Gi + nvidia.com/gpu: "1" + requests: + cpu: 10m + memory: 50Mi + - documentIndex: *deploymentDoc + isSubset: + path: spec.template.spec.containers[1] + content: + resources: + limits: + cpu: 4000m + memory: 8Gi + amd.com/gpu: "1" + requests: + cpu: 10m + memory: 50Mi + # Failures - it: should fail with empty requests set: @@ -868,3 +1009,28 @@ tests: asserts: - failedTemplate: errorMessage: Container - Expected non-empty + + - it: should fail with no value in gpu + set: + image: *image + scaleGPU: + - gpu: + key: + targetSelector: + workload-name1: + - container-name1 + workload: + workload-name1: + enabled: true + primary: true + type: Deployment + podSpec: + containers: + container-name1: + enabled: true + primary: true + imageSelector: image + probes: *probes + asserts: + - failedTemplate: + errorMessage: Container - Expected non-empty diff --git a/library/common/Chart.yaml b/library/common/Chart.yaml index 6d2b30fe..e8cf746c 100644 --- a/library/common/Chart.yaml +++ b/library/common/Chart.yaml @@ -15,4 +15,4 @@ maintainers: name: common sources: null type: library -version: 12.4.11 +version: 12.4.12 diff --git a/library/common/templates/addons/code-server/_container.tpl b/library/common/templates/addons/code-server/_container.tpl index 7344f880..a66572d2 100644 --- a/library/common/templates/addons/code-server/_container.tpl +++ b/library/common/templates/addons/code-server/_container.tpl @@ -7,12 +7,15 @@ probes: liveness: enabled: true port: {{ .Values.addons.codeserver.service.ports.codeserver.port }} + path: "/" readiness: enabled: true port: {{ .Values.addons.codeserver.service.ports.codeserver.port }} + path: "/" startup: enabled: true port: {{ .Values.addons.codeserver.service.ports.codeserver.port }} + path: "/" imageSelector: "codeserverImage" imagePullPolicy: {{ .Values.codeserverImage.pullPolicy }} securityContext: diff --git a/library/common/templates/addons/vpn/_gluetunContainer.tpl b/library/common/templates/addons/vpn/_gluetunContainer.tpl index 38cfbd03..8958e036 100644 --- a/library/common/templates/addons/vpn/_gluetunContainer.tpl +++ b/library/common/templates/addons/vpn/_gluetunContainer.tpl @@ -27,8 +27,8 @@ securityContext: - NET_RAW - SYS_MODULE -{{- with $.Values.addons.vpn.env }} env: +{{- with $.Values.addons.vpn.env }} {{- . | toYaml | nindent 2 }} {{- end -}} diff --git a/library/common/templates/addons/vpn/_openvpnContainer.tpl b/library/common/templates/addons/vpn/_openvpnContainer.tpl index 8ca3476a..0d976501 100644 --- a/library/common/templates/addons/vpn/_openvpnContainer.tpl +++ b/library/common/templates/addons/vpn/_openvpnContainer.tpl @@ -23,10 +23,11 @@ securityContext: add: - NET_ADMIN - NET_RAW + - MKNOD - SYS_MODULE -{{- with $.Values.addons.vpn.env }} env: +{{- with $.Values.addons.vpn.env }} {{- . | toYaml | nindent 2 }} {{- end }} {{- if and .Values.addons.vpn.openvpn.username .Values.addons.vpn.openvpn.password }} diff --git a/library/common/templates/addons/vpn/_wireguardContainer.tpl b/library/common/templates/addons/vpn/_wireguardContainer.tpl index 7822175f..c30016c9 100644 --- a/library/common/templates/addons/vpn/_wireguardContainer.tpl +++ b/library/common/templates/addons/vpn/_wireguardContainer.tpl @@ -23,10 +23,12 @@ securityContext: add: - NET_ADMIN - NET_RAW + - SETUID + - SETGID - SYS_MODULE -{{- with $.Values.addons.vpn.env }} env: +{{- with $.Values.addons.vpn.env }} {{- . | toYaml | nindent 2 }} {{- end }} SEPARATOR: ";" diff --git a/library/common/templates/lib/container/_resources.tpl b/library/common/templates/lib/container/_resources.tpl index aa0fdc1d..b97de7eb 100644 --- a/library/common/templates/lib/container/_resources.tpl +++ b/library/common/templates/lib/container/_resources.tpl @@ -75,10 +75,12 @@ objectData: The object data to be used to render the container. {{- if not $returnBool -}} {{- range $gpu := $gpuResource -}} {{- range $k, $v := $gpu -}} - {{- if not $v -}} + {{- if or (kindIs "invalid" $v) (eq (toString $v) "") -}} {{- fail "Container - Expected non-empty " -}} - {{- end }} + {{- end -}} {{/* Don't try to schedule 0 GPUs */}} + {{- if gt (int $v) 0 }} {{ $k }}: {{ $v | quote }} + {{- end -}} {{- end -}} {{- end -}} {{- else -}}