diff --git a/library/common-test/tests/container/env_test.yaml b/library/common-test/tests/container/env_test.yaml index 0681a72c..a29235d6 100644 --- a/library/common-test/tests/container/env_test.yaml +++ b/library/common-test/tests/container/env_test.yaml @@ -64,6 +64,7 @@ tests: VAR7: fieldRef: fieldPath: metadata.name + VAR8: "" asserts: - documentIndex: &deploymentDoc 2 isKind: @@ -127,6 +128,12 @@ tests: valueFrom: fieldRef: fieldPath: metadata.name + - documentIndex: *deploymentDoc + contains: + path: spec.template.spec.containers[0].env + content: + name: VAR8 + value: "" # Failures - it: it should fail invalid ref diff --git a/library/common-test/tests/container/volumeMounts_test.yaml b/library/common-test/tests/container/volumeMounts_test.yaml index 4922865a..fab4812b 100644 --- a/library/common-test/tests/container/volumeMounts_test.yaml +++ b/library/common-test/tests/container/volumeMounts_test.yaml @@ -663,6 +663,149 @@ tests: isAPIVersion: of: apps/v1 + - it: should pass with volume from persistenceList with selected pod and containers and specific values from tpl + set: + some_path: /some/other/path + some_propagation: None + some_sub_path: /some/sub/path + image: *image + workload: + workload-name: + 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 + persistenceList: + - enabled: true + type: emptyDir + mountPath: /some/path + readOnly: true + targetSelector: + workload-name: + container-name1: + mountPath: "{{ .Values.some_path }}" + readOnly: false + mountPropagation: "{{ .Values.some_propagation }}" + subPath: "{{ .Values.some_sub_path }}" + container-name2: {} + asserts: + - documentIndex: &deploymentDoc 0 + isKind: + of: Deployment + - documentIndex: *deploymentDoc + isAPIVersion: + of: apps/v1 + - documentIndex: *deploymentDoc + contains: + path: spec.template.spec.containers[0].volumeMounts + content: + name: persist-list-0 + mountPath: /some/other/path + readOnly: false + mountPropagation: None + subPath: /some/sub/path + - documentIndex: *deploymentDoc + contains: + path: spec.template.spec.containers[1].volumeMounts + content: + name: persist-list-0 + mountPath: /some/path + readOnly: true + + - it: should pass with volume from persistenceList with name + set: + image: *image + workload: + workload-name: + enabled: true + primary: true + type: Deployment + podSpec: + containers: + container-name1: + enabled: true + primary: true + imageSelector: image + probes: *probes + persistenceList: + - name: some-vol + enabled: true + type: emptyDir + mountPath: /some/path + readOnly: true + asserts: + - documentIndex: &deploymentDoc 0 + isKind: + of: Deployment + - documentIndex: *deploymentDoc + isAPIVersion: + of: apps/v1 + - documentIndex: *deploymentDoc + contains: + path: spec.template.spec.containers[0].volumeMounts + content: + name: some-vol + mountPath: /some/path + readOnly: true + + - it: should pass with volume from persistenceList and from persistence + set: + image: *image + workload: + workload-name: + enabled: true + primary: true + type: Deployment + podSpec: + containers: + container-name1: + enabled: true + primary: true + imageSelector: image + probes: *probes + persistence: + some-vol: + enabled: true + type: emptyDir + mountPath: /some/path + persistenceList: + - name: some-other-vol + enabled: true + type: emptyDir + mountPath: /some/other/path + asserts: + - documentIndex: &deploymentDoc 0 + isKind: + of: Deployment + - documentIndex: *deploymentDoc + isAPIVersion: + of: apps/v1 + - documentIndex: *deploymentDoc + contains: + path: spec.template.spec.containers[0].volumeMounts + content: + name: some-vol + mountPath: /some/path + readOnly: false + - documentIndex: *deploymentDoc + contains: + path: spec.template.spec.containers[0].volumeMounts + content: + name: some-other-vol + mountPath: /some/other/path + readOnly: false + # Failures - it: should fail with invalid mountPropagation set: diff --git a/library/common-test/tests/pod/volume_emptyDIr_test.yaml b/library/common-test/tests/pod/volume_emptyDIr_test.yaml index b851576d..550bb474 100644 --- a/library/common-test/tests/pod/volume_emptyDIr_test.yaml +++ b/library/common-test/tests/pod/volume_emptyDIr_test.yaml @@ -121,3 +121,20 @@ tests: asserts: - failedTemplate: errorMessage: Persistence - Expected [medium] to be one of ["", Memory], but got [not-a-valid-medium] on type + + - it: should fail with invalid size format + set: + workload: + some-workload: + enabled: true + primary: true + type: Deployment + podSpec: {} + persistence: + volume1: + enabled: true + type: emptyDir + size: not-a-valid-size + asserts: + - failedTemplate: + errorMessage: Persistence Expected to have one of the following formats [(Suffixed with E/P/T/G/M/K - eg. 1G), (Suffixed with Ei/Pi/Ti/Gi/Mi/Ki - eg. 1Gi), (Plain Integer in bytes - eg. 1024), (Exponent - eg. 134e6)], but got [not-a-valid-size] diff --git a/library/common/Chart.yaml b/library/common/Chart.yaml index a4c20687..55461194 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.1 +version: 12.4.2 diff --git a/library/common/templates/lib/container/_env.tpl b/library/common/templates/lib/container/_env.tpl index e2408845..80bcdddc 100644 --- a/library/common/templates/lib/container/_env.tpl +++ b/library/common/templates/lib/container/_env.tpl @@ -11,8 +11,12 @@ objectData: The object data to be used to render the container. {{- range $k, $v := $objectData.env -}} {{- include "tc.v1.common.helper.container.envDupeCheck" (dict "rootCtx" $rootCtx "objectData" $objectData "source" "env" "key" $k) }} - name: {{ $k | quote }} - {{- if not (kindIs "map" $v) }} - value: {{ tpl (toString $v) $rootCtx | quote }} + {{- if not (kindIs "map" $v) -}} + {{- $value := "" -}} + {{- if $v -}} {{/* Only tpl non-empty values */}} + {{- $value = tpl (toString $v) $rootCtx -}} + {{- end }} + value: {{ $value | quote }} {{- else if kindIs "map" $v }} valueFrom: {{- $refs := (list "configMapKeyRef" "secretKeyRef" "fieldRef") -}} diff --git a/library/common/templates/lib/pod/volumes/_emptyDir.tpl b/library/common/templates/lib/pod/volumes/_emptyDir.tpl index 1ca7195b..30d84313 100644 --- a/library/common/templates/lib/pod/volumes/_emptyDir.tpl +++ b/library/common/templates/lib/pod/volumes/_emptyDir.tpl @@ -17,6 +17,14 @@ objectData: The object data to be used to render the volume. {{- $size = tpl . $rootCtx -}} {{- end -}} + {{- if $size -}} + {{/* Size: https://regex101.com/r/NNPV2D/1 */}} + {{- if not (mustRegexMatch "^[1-9][0-9]*([EPTGMK]i?|e[0-9]+)?$" (toString $size)) -}} + {{- $formats := "(Suffixed with E/P/T/G/M/K - eg. 1G), (Suffixed with Ei/Pi/Ti/Gi/Mi/Ki - eg. 1Gi), (Plain Integer in bytes - eg. 1024), (Exponent - eg. 134e6)" -}} + {{- fail (printf "Persistence Expected to have one of the following formats [%s], but got [%s]" $formats $size) -}} + {{- end -}} + {{- end -}} + {{- if and $medium (ne $medium "Memory") -}} {{- fail (printf "Persistence - Expected [medium] to be one of [\"\", Memory], but got [%s] on type" $medium) -}} {{- end }} diff --git a/library/common/templates/loader/_init.tpl b/library/common/templates/loader/_init.tpl index f49e4a35..546c2ff5 100644 --- a/library/common/templates/loader/_init.tpl +++ b/library/common/templates/loader/_init.tpl @@ -4,6 +4,9 @@ {{/* Merge chart values and the common chart defaults */}} {{- include "tc.v1.common.values.init" . -}} + {{/* Parse lists and append to values */}} + {{- include "tc.v1.common.loader.lists" . -}} + {{/* Ensure TrueCharts chart context information is available */}} {{- include "tc.v1.common.lib.util.chartcontext" . -}} diff --git a/library/common/templates/loader/_lists.tpl b/library/common/templates/loader/_lists.tpl new file mode 100644 index 00000000..5bf08fd8 --- /dev/null +++ b/library/common/templates/loader/_lists.tpl @@ -0,0 +1,5 @@ +{{- define "tc.v1.common.loader.lists" -}} + + {{- include "tc.v1.common.values.persistenceList" . -}} + +{{- end -}} diff --git a/library/common/templates/values/lists/_persistenceList.tpl b/library/common/templates/values/lists/_persistenceList.tpl new file mode 100644 index 00000000..17e2eeb5 --- /dev/null +++ b/library/common/templates/values/lists/_persistenceList.tpl @@ -0,0 +1,18 @@ +{{- define "tc.v1.common.values.persistenceList" -}} + {{- $rootCtx := . -}} + + {{- range $idx, $persistenceValues := $rootCtx.Values.persistenceList -}} + {{- $name := (printf "persist-list-%s" (toString $idx)) -}} + + {{- with $persistenceValues.name -}} + {{- $name = . -}} + {{- end -}} + + {{- if not (hasKey $rootCtx.Values "persistence") -}} + {{- $_ := set $rootCtx.Values "persistence" dict -}} + {{- end -}} + + {{- $_ := set $rootCtx.Values.persistence $name $persistenceValues -}} + + {{- end -}} +{{- end -}} diff --git a/library/common/values.yaml b/library/common/values.yaml index a22f476c..35d408e8 100644 --- a/library/common/values.yaml +++ b/library/common/values.yaml @@ -207,7 +207,7 @@ persistence: mountPath: /dev/shm targetSelectAll: true - +persistenceList: [] # -- Injected from SCALE middleware # Only for reference here