From 542152aaaa54674fa61ddd58f06c033dbfe05ee8 Mon Sep 17 00:00:00 2001 From: Stavros kois Date: Thu, 22 Dec 2022 18:44:05 +0200 Subject: [PATCH] make it more readable abit --- .../1.0.0/templates/lib/container/_env_from.tpl | 6 +++--- .../1.0.0/templates/lib/container/_env_vars.tpl | 8 ++++---- .../lib/container/env/_checkDuplicates.tpl | 15 +++++++++------ .../1.0.0/templates/lib/container/env/_env.tpl | 4 ++-- .../templates/lib/container/env/_envList.tpl | 4 ++-- .../templates/lib/container/env/_fixedEnvs.tpl | 4 ++-- .../common/1.0.0/templates/lib/pod/_container.tpl | 4 ++-- .../1.0.0/templates/lib/pod/_initContainers.tpl | 4 ++-- 8 files changed, 26 insertions(+), 23 deletions(-) diff --git a/library/common/1.0.0/templates/lib/container/_env_from.tpl b/library/common/1.0.0/templates/lib/container/_env_from.tpl index 6160f7aefd..5559db7516 100644 --- a/library/common/1.0.0/templates/lib/container/_env_from.tpl +++ b/library/common/1.0.0/templates/lib/container/_env_from.tpl @@ -9,7 +9,7 @@ That's why the custom dict is expected. {{/* Environment Variables From included by the container */}} {{- define "ix.v1.common.container.envFrom" -}} {{- $envFrom := .envFrom -}} - {{- $container := .container -}} + {{- $containerName := .containerName -}} {{- $root := .root -}} {{- range $envFrom -}} @@ -20,12 +20,12 @@ That's why the custom dict is expected. {{- $secretName := (tpl (required "Name is required for secretRef in envFrom." .secretRef.name) $root) }} - secretRef: name: {{ $secretName | quote }} - {{- include "ix.v1.common.util.storeEnvFromVarsForCheck" (dict "root" $root "container" $container "name" $secretName "type" "secret") -}} + {{- include "ix.v1.common.util.storeEnvFromVarsForCheck" (dict "root" $root "containers" (list $containerName) "name" $secretName "type" "secret") -}} {{- else if .configMapRef }} {{- $configName := (tpl (required "Name is required for configMapRef in envFrom." .configMapRef.name) $root) }} - configMapRef: name: {{ $configName | quote }} - {{- include "ix.v1.common.util.storeEnvFromVarsForCheck" (dict "root" $root "container" $container "name" $configName "type" "configmap") -}} + {{- include "ix.v1.common.util.storeEnvFromVarsForCheck" (dict "root" $root "containers" (list $containerName) "name" $configName "type" "configmap") -}} {{- else -}} {{- fail "Not valid Ref or key is missing in envFrom." -}} {{- end -}} diff --git a/library/common/1.0.0/templates/lib/container/_env_vars.tpl b/library/common/1.0.0/templates/lib/container/_env_vars.tpl index 535a8ef1bf..768658412e 100644 --- a/library/common/1.0.0/templates/lib/container/_env_vars.tpl +++ b/library/common/1.0.0/templates/lib/container/_env_vars.tpl @@ -9,12 +9,12 @@ That's why the custom dict is expected. {{- define "ix.v1.common.container.envVars" -}} {{- $envs := .envs -}} {{- $envList := .envList -}} - {{- $container := .container -}} + {{- $containerName := .containerName -}} {{- $root := .root -}} {{- $fixedEnv := list -}} {{- if $root.Values.injectFixedEnvs -}} - {{- $fixedEnv = (include "ix.v1.common.container.fixedEnvs" (dict "root" $root "fixedEnv" $fixedEnv "container" $container)) -}} + {{- $fixedEnv = (include "ix.v1.common.container.fixedEnvs" (dict "root" $root "fixedEnv" $fixedEnv "containerName" $containerName)) -}} {{- end -}} {{/* Finish fixedEnv */}} {{- with $fixedEnv -}} {{- range $fixedEnv | fromJsonArray }} {{/* "fromJsonArray" parses stringified output and convet to list */}} @@ -22,8 +22,8 @@ That's why the custom dict is expected. value: {{ .value | quote }} {{- end -}} {{- end -}} - {{- include "ix.v1.common.container.env" (dict "envs" $envs "root" $root "fixedEnv" $fixedEnv "container" $container) -}} - {{- include "ix.v1.common.container.envList" (dict "envList" $envList "envs" $envs "root" $root "fixedEnv" $fixedEnv "container" $container) -}} + {{- include "ix.v1.common.container.env" (dict "envs" $envs "root" $root "fixedEnv" $fixedEnv "containerName" $containerName) -}} + {{- include "ix.v1.common.container.envList" (dict "envList" $envList "envs" $envs "root" $root "fixedEnv" $fixedEnv "containerName" $containerName) -}} {{- end -}} {{/* Note: TODO: Check for dupes in configmap/secrets. */}} diff --git a/library/common/1.0.0/templates/lib/container/env/_checkDuplicates.tpl b/library/common/1.0.0/templates/lib/container/env/_checkDuplicates.tpl index 931c1eb4ce..139794dbaf 100644 --- a/library/common/1.0.0/templates/lib/container/env/_checkDuplicates.tpl +++ b/library/common/1.0.0/templates/lib/container/env/_checkDuplicates.tpl @@ -4,9 +4,9 @@ {{- range $kOut, $vOut := $root.Values.envsForDupeCheck -}} {{- range $kIn, $vIn := $root.Values.envsForDupeCheck -}} {{- if and (ne $vOut.source $vIn.source) (eq $vOut.key $vIn.key) -}} - {{- range $container := $vOut.containers -}} - {{- if (mustHas $container $vIn.containers) -}} - {{- fail (printf "Environment Variable (%s) on container (%s) is set more than once. [to (%s) on (%s)] and [to (%s) on (%s)]" $vOut.key $container $vOut.value $vOut.source $vIn.value $vIn.source) -}} + {{- range $containerName := $vOut.containers -}} + {{- if (mustHas $containerName $vIn.containers) -}} + {{- fail (printf "Environment Variable (%s) on container (%s) is set more than once. [to (%s) on (%s)] and [to (%s) on (%s)]" $vOut.key $containerName $vOut.value $vOut.source $vIn.value $vIn.source) -}} {{- end -}} {{- end -}} {{- end -}} @@ -42,6 +42,9 @@ {{- $k = $v.name -}} {{- $v = $v.value -}} {{- end -}} + {{- if not (kindIs "slice" $containers) -}} + {{- fail "Something went wront $containers are not a list." -}} + {{- end -}} {{- $tmpList = mustAppend $tmpList (dict "key" $k "value" $v "source" $source "containers" $containers) -}} {{- end -}} {{- $_ := set $root.Values "envsForDupeCheck" $tmpList -}} @@ -50,15 +53,15 @@ {{- define "ix.v1.common.util.storeEnvFromVarsForCheck" -}} {{- $root := .root -}} {{- $name := .name -}} - {{- $container := .container -}} + {{- $containerName := .containerName -}} {{- $type := .type -}} {{- $dupes := $root.Values.envsForDupeCheck -}} {{- range $item := $dupes -}} {{- if eq $item.source (printf "%s-%s" (camelcase $type) $name) -}} - {{- if not (mustHas $container $item.containers) -}} + {{- if not (mustHas $containerName $item.containers) -}} {{- $dupes = without $dupes $item -}} - {{- $_ := set $item "containers" (mustAppend $item.containers $container) -}} + {{- $_ := set $item "containers" (mustAppend $item.containers $containerName) -}} {{- $dupes = mustAppend $dupes $item -}} {{- $_ := set $root.Values "envsForDupeCheck" $dupes -}} {{- end -}} diff --git a/library/common/1.0.0/templates/lib/container/env/_env.tpl b/library/common/1.0.0/templates/lib/container/env/_env.tpl index 648c265b5b..ac701c8cd6 100644 --- a/library/common/1.0.0/templates/lib/container/env/_env.tpl +++ b/library/common/1.0.0/templates/lib/container/env/_env.tpl @@ -1,7 +1,7 @@ {{- define "ix.v1.common.container.env" -}} {{- $envs := .envs -}} {{- $root := .root -}} - {{- $container := .container -}} + {{- $containerName := .containerName -}} {{- $fixedEnv := .fixedEnv -}} {{- $dupeCheck := dict -}} @@ -51,6 +51,6 @@ key: {{ tpl (required (printf " for the keyRef is not defined in (%s)" $name) $value.key) $root }} {{- end -}} {{- end -}} - {{- include "ix.v1.common.util.storeEnvsForDupeCheck" (dict "root" $root "source" "env" "data" $dupeCheck "containers" (list $container)) -}} + {{- include "ix.v1.common.util.storeEnvsForDupeCheck" (dict "root" $root "source" "env" "data" $dupeCheck "containers" (list $containerName)) -}} {{- end -}} {{/* Finish env */}} {{- end -}} diff --git a/library/common/1.0.0/templates/lib/container/env/_envList.tpl b/library/common/1.0.0/templates/lib/container/env/_envList.tpl index c83689a749..70cf7f7abc 100644 --- a/library/common/1.0.0/templates/lib/container/env/_envList.tpl +++ b/library/common/1.0.0/templates/lib/container/env/_envList.tpl @@ -1,7 +1,7 @@ {{- define "ix.v1.common.container.envList" -}} {{- $envList := .envList -}} {{- $envs := .envs -}} - {{- $container := .container -}} + {{- $containerName := .containerName -}} {{- $root := .root -}} {{- $fixedEnv := .fixedEnv -}} @@ -24,6 +24,6 @@ {{- fail "Please specify both name and value for environment variable" -}} {{- end -}} {{- end -}} - {{- include "ix.v1.common.util.storeEnvsForDupeCheck" (dict "root" $root "source" "envList" "data" $dupeCheck "containers" (list $container)) -}} + {{- include "ix.v1.common.util.storeEnvsForDupeCheck" (dict "root" $root "source" "envList" "data" $dupeCheck "containers" (list $containerName)) -}} {{- end -}} {{/* Finish envList */}} {{- end -}} diff --git a/library/common/1.0.0/templates/lib/container/env/_fixedEnvs.tpl b/library/common/1.0.0/templates/lib/container/env/_fixedEnvs.tpl index 8e70a72e50..356e35aa8c 100644 --- a/library/common/1.0.0/templates/lib/container/env/_fixedEnvs.tpl +++ b/library/common/1.0.0/templates/lib/container/env/_fixedEnvs.tpl @@ -4,7 +4,7 @@ will be parsed correctly without causing errors. */}} {{- define "ix.v1.common.container.fixedEnvs" -}} {{- $root := .root -}} - {{- $container := .container -}} + {{- $containerName := .containerName -}} {{- $vars := list -}} {{- $vars = mustAppend $vars (dict "name" "TZ" "value" (tpl (toYaml $root.Values.TZ) $root)) -}} @@ -26,6 +26,6 @@ will be parsed correctly without causing errors. {{- if or ($root.Values.securityContext.readOnlyRootFilesystem) ($root.Values.securityContext.runAsNonRoot) -}} {{/* Mainly for LSIO containers, tell S6 to avoid using rootfs */}} {{- $vars = mustAppend $vars (dict "name" "S6_READ_ONLY_ROOT" "value" "1") -}} {{- end -}} - {{- include "ix.v1.common.util.storeEnvsForDupeCheck" (dict "root" $root "source" "fixedEnv" "data" (toJson $vars) "containers" (list $container)) -}} + {{- include "ix.v1.common.util.storeEnvsForDupeCheck" (dict "root" $root "source" "fixedEnv" "data" (toJson $vars) "containers" (list $containerName)) -}} {{- toJson $vars -}} {{/* Helm can only return "string", so we stringify the output */}} {{- end -}} diff --git a/library/common/1.0.0/templates/lib/pod/_container.tpl b/library/common/1.0.0/templates/lib/pod/_container.tpl index 28b86fa142..03b94ac774 100644 --- a/library/common/1.0.0/templates/lib/pod/_container.tpl +++ b/library/common/1.0.0/templates/lib/pod/_container.tpl @@ -36,11 +36,11 @@ So it can work on multiple places, like additional containers and not only the m {{- with (include "ix.v1.common.container.termination.messagePolicy" (dict "msgPolicy" .Values.termination.messagePolicy "root" $)) | trim }} terminationMessagePolicy: {{ . }} {{- end -}} - {{- with (include "ix.v1.common.container.envVars" (dict "envs" .Values.env "envList" .Values.envList "container" $name "root" $) | trim) }} + {{- with (include "ix.v1.common.container.envVars" (dict "envs" .Values.env "envList" .Values.envList "containerName" $name "root" $) | trim) }} env: {{- . | nindent 4 }} {{/* env, fixedEnvs and envList */}} {{- end -}} - {{- with (include "ix.v1.common.container.envFrom" (dict "envFrom" .Values.envFrom "container" $name "root" $) | trim) }} + {{- with (include "ix.v1.common.container.envFrom" (dict "envFrom" .Values.envFrom "containerName" $name "root" $) | trim) }} envFrom: {{- . | nindent 4 }} {{- end -}} diff --git a/library/common/1.0.0/templates/lib/pod/_initContainers.tpl b/library/common/1.0.0/templates/lib/pod/_initContainers.tpl index e4555e37f7..521fd3e651 100644 --- a/library/common/1.0.0/templates/lib/pod/_initContainers.tpl +++ b/library/common/1.0.0/templates/lib/pod/_initContainers.tpl @@ -23,11 +23,11 @@ args: {{- . | nindent 4 }} {{- end -}} - {{- with (include "ix.v1.common.container.envVars" (dict "envs" $container.env "envList" $container.envList "container" $name "root" $root) | trim) }} + {{- with (include "ix.v1.common.container.envVars" (dict "envs" $container.env "envList" $container.envList "containerName" $name "root" $root) | trim) }} env: {{- . | nindent 4 }} {{/* env, fixedEnvs and envList */}} {{- end -}} - {{- with (include "ix.v1.common.container.envFrom" (dict "envFrom" $container.envFrom "container" $name "root" $root) | trim) }} + {{- with (include "ix.v1.common.container.envFrom" (dict "envFrom" $container.envFrom "containerName" $name "root" $root) | trim) }} envFrom: {{- . | nindent 4 }} {{- end -}}