make it more readable abit

This commit is contained in:
Stavros kois
2022-12-22 18:44:05 +02:00
parent acb8b2bfef
commit 542152aaaa
8 changed files with 26 additions and 23 deletions

View File

@@ -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 <name> key is missing in envFrom." -}}
{{- end -}}

View File

@@ -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. */}}

View File

@@ -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 -}}

View File

@@ -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 "<key> 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 -}}

View File

@@ -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 -}}

View File

@@ -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 -}}

View File

@@ -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 -}}

View File

@@ -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 -}}