From 618f91022cc0003bbd6700cdcd7be53e6038f121 Mon Sep 17 00:00:00 2001 From: Stavros kois Date: Tue, 15 Nov 2022 18:45:17 +0200 Subject: [PATCH] cleanup a bit container tpl from envs --- .../templates/lib/container/_env_from.tpl | 10 +- .../templates/lib/container/_env_list.tpl | 29 ------ .../templates/lib/container/_env_vars.tpl | 91 ++++++++++++------- .../1.0.0/templates/lib/container/_ports.tpl | 47 +++++----- .../templates/lib/controller/_container.tpl | 16 ++-- library/common/1.0.0/values.yaml | 22 +++-- 6 files changed, 105 insertions(+), 110 deletions(-) delete mode 100644 library/common/1.0.0/templates/lib/container/_env_list.tpl 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 fd4edae9..715f4ba7 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 @@ -2,12 +2,13 @@ {{- define "ix.v1.common.container.envFrom" -}} {{- $envFrom := .envFrom -}} {{- $root := .root -}} -{{- if or $root.secretEnv $envFrom -}} - envFrom: - {{- if $root.secretEnv }} {{/* TODO: Write unittest once _secret.tpl is completed */}} +{{- if or $root.Values.secretEnv $envFrom }} +envFrom: +{{- end -}} +{{- if $root.Values.secretEnv }} {{/* TODO: Write unittest once _secret.tpl is completed */}} - secretRef: name: {{ include "ix.v1.common.names.fullname" . }} - {{- end -}} +{{- end -}} {{- range $envFrom -}} {{- if .secretRef }} - secretRef: @@ -20,7 +21,6 @@ {{- end -}} {{- end -}} {{- end -}} -{{- end -}} {{/* A custom dict is expected with envList and root. diff --git a/library/common/1.0.0/templates/lib/container/_env_list.tpl b/library/common/1.0.0/templates/lib/container/_env_list.tpl deleted file mode 100644 index 7b455a31..00000000 --- a/library/common/1.0.0/templates/lib/container/_env_list.tpl +++ /dev/null @@ -1,29 +0,0 @@ -{{/* Environment Variables List included by the container */}} -{{- define "ix.v1.common.container.envList" -}} -{{- $envList := .envList -}} -{{- $root := .root -}} -{{- with $envList -}} - {{- range $envList -}} - {{- if and .name .value -}} - {{- if or (kindIs "map" .name) (kindIs "slice" .name) -}} - {{- fail "Name in envList cannot be a map or slice" -}} - {{- end -}} - {{- if or (kindIs "map" .value) (kindIs "slice" .value) -}} - {{- fail "Value in envList cannot be a map or slice" -}} - {{- end }} -- name: {{ tpl .name $root }} - value: {{ tpl .value $root | quote }} - {{- else -}} - {{- fail "Please specify both name and value for environment variable" -}} - {{- end -}} - {{- end -}} -{{- end -}} -{{- end -}} - -{{/* -A custom dict is expected with envList and root. -It's designed to work for mainContainer AND initContainers. -Calling this from an initContainer, wouldn't work, as it would have a different "root" context, -and "tpl" on "$" would cause erors. -That's why the custom dict is expected. -*/}} 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 77a02a1d..62005f28 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 @@ -1,51 +1,74 @@ {{/* Environment Variables included by the container */}} {{- define "ix.v1.common.container.envVars" -}} {{- $envs := .envs -}} +{{- $envList := .envList -}} {{- $root := .root -}} -{{- with $envs -}} +{{- if or $envs $root.Values.injectFixedEnvs }} +env: +{{- end -}} +{{- if $root.Values.injectFixedEnvs -}} + {{- include "ix.v1.common.container.fixedEnvs" $root | nindent 2 -}} +{{- end -}} {{/* Finish fixedEnv */}} +{{- with $envs }} {{- range $k, $v := . -}} {{- $name := $k -}} {{- $value := $v -}} {{- if kindIs "int" $name -}} {{- fail "Environment Variables as a list is not supported. Use key-value format." -}} {{- end }} -- name: {{ $name | quote }} - {{- if not (kindIs "map" $value) -}} - {{- if or (kindIs "string" $value) -}} {{/* Single values are parsed as string (eg. int, bool) */}} - {{- $value = tpl $value $root -}} {{/* Expand Value */}} - {{- end }} - value: {{ quote $value }} - {{- else if kindIs "map" $value -}} {{/* If value is a dict... */}} - {{- if hasKey $value "valueFrom" -}} - {{- fail "Please remove and use directly configMapKeyRef or secretKeyRef" -}} - {{- end }} - valueFrom: - {{- if hasKey $value "configMapKeyRef" }} {{/* And contains configMapRef... */}} - configMapKeyRef: - {{- $_ := set $value "name" $value.configMapKeyRef.name -}} {{/* Extract name and key */}} - {{- $_ := set $value "key" $value.configMapKeyRef.key -}} - {{- if hasKey $value.configMapKeyRef "optional" -}} - {{- fail " is not supported in configMapRefKey" -}} - {{- end -}} - {{- else if hasKey $value "secretKeyRef" }} {{/* And contains secretpRef... */}} - secretKeyRef: - {{- $_ := set $value "name" $value.secretKeyRef.name -}} {{/* Extract name and key */}} - {{- $_ := set $value "key" $value.secretKeyRef.key -}} - {{- if (hasKey $value.secretKeyRef "optional") -}} - {{- if (kindIs "bool" $value.secretKeyRef.optional) }} - optional: {{ $value.secretKeyRef.optional }} - {{- else -}} - {{- fail (printf " in secretKeyRef must be a boolean on Environment Variable (%s)" $name) -}} + - name: {{ $name | quote }} + {{- if not (kindIs "map" $value) -}} + {{- if or (kindIs "string" $value) -}} {{/* Single values are parsed as string (eg. int, bool) */}} + {{- $value = tpl $value $root -}} {{/* Expand Value */}} + {{- end }} + value: {{ quote $value }} + {{- else if kindIs "map" $value -}} {{/* If value is a dict... */}} + {{- if hasKey $value "valueFrom" -}} + {{- fail "Please remove and use directly configMapKeyRef or secretKeyRef" -}} + {{- end }} + valueFrom: + {{- if hasKey $value "configMapKeyRef" }} {{/* And contains configMapRef... */}} + configMapKeyRef: + {{- $_ := set $value "name" $value.configMapKeyRef.name -}} {{/* Extract name and key */}} + {{- $_ := set $value "key" $value.configMapKeyRef.key -}} + {{- if hasKey $value.configMapKeyRef "optional" -}} + {{- fail " is not supported in configMapRefKey" -}} {{- end -}} - {{- end -}} - {{- else -}} - {{- fail "Not a valid valueFrom reference. Valid options are (configMapKeyRef and secretKeyRef)" -}} + {{- else if hasKey $value "secretKeyRef" }} {{/* And contains secretpRef... */}} + secretKeyRef: + {{- $_ := set $value "name" $value.secretKeyRef.name -}} {{/* Extract name and key */}} + {{- $_ := set $value "key" $value.secretKeyRef.key -}} + {{- if (hasKey $value.secretKeyRef "optional") -}} + {{- if (kindIs "bool" $value.secretKeyRef.optional) }} + optional: {{ $value.secretKeyRef.optional }} + {{- else -}} + {{- fail (printf " in secretKeyRef must be a boolean on Environment Variable (%s)" $name) -}} + {{- end -}} + {{- end -}} + {{- else -}} + {{- fail "Not a valid valueFrom reference. Valid options are (configMapKeyRef and secretKeyRef)" -}} + {{- end }} + name: {{ tpl (required (printf " for the keyRef is not defined in (%s)" $name) $value.name) $root }} {{/* Expand name and key */}} + key: {{ tpl (required (printf " for the keyRef is not defined in (%s)" $name) $value.key) $root }} + {{- end -}} + {{- end -}} +{{- end -}} {{/* Finish env */}} +{{- with $envList -}} + {{- range $envList -}} + {{- if and .name .value -}} + {{- if or (kindIs "map" .name) (kindIs "slice" .name) -}} + {{- fail "Name in envList cannot be a map or slice" -}} + {{- end -}} + {{- if or (kindIs "map" .value) (kindIs "slice" .value) -}} + {{- fail "Value in envList cannot be a map or slice" -}} {{- end }} - name: {{ tpl (required (printf " for the keyRef is not defined in (%s)" $name) $value.name) $root }} {{/* Expand name and key */}} - key: {{ tpl (required (printf " for the keyRef is not defined in (%s)" $name) $value.key) $root }} + - name: {{ tpl .name $root }} + value: {{ tpl .value $root | quote }} + {{- else -}} + {{- fail "Please specify both name and value for environment variable" -}} {{- end -}} {{- end -}} -{{- end -}} +{{- end -}} {{/* FInish envList */}} {{- end -}} {{/* diff --git a/library/common/1.0.0/templates/lib/container/_ports.tpl b/library/common/1.0.0/templates/lib/container/_ports.tpl index 29565b34..00e92938 100644 --- a/library/common/1.0.0/templates/lib/container/_ports.tpl +++ b/library/common/1.0.0/templates/lib/container/_ports.tpl @@ -11,30 +11,31 @@ {{- end -}} {{/* Render the list of ports */}} -{{- if $ports -}} -{{- range $_ := $ports }} - {{- if .enabled }} -- name: {{ tpl .name $ }} - {{- if and .targetPort (kindIs "string" .targetPort) }} - {{- fail (printf "This common library does not support named ports for targetPort. (port name: %s, targetPort: %s)" .name .targetPort) }} +{{- if $ports }} +ports: + {{- range $_ := $ports }} + {{- if .enabled }} + - name: {{ tpl .name $ }} + {{- if and .targetPort (kindIs "string" .targetPort) }} + {{- fail (printf "This common library does not support named ports for targetPort. (port name: %s, targetPort: %s)" .name .targetPort) }} + {{- end }} + {{- if not .port }} + {{- fail (printf "Port is required on enabled services. Service: (%s)" .name) }} + {{- end }} + containerPort: {{ default .port .targetPort }} + {{- with .protocol }} + {{- if or (eq (. | upper) "HTTP") (eq (. | upper) "HTTPS") (eq (. | upper) "TCP") }} + protocol: TCP + {{- else if (eq (. | upper) "UDP") }} + protocol: {{ . }} + {{- else }} + {{- fail (printf "Not valid (protocl: %s)" .) }} + {{- end }} + {{- else }} {{/* If no protocol is given, default to TCP */}} + protocol: TCP + {{- end }} + {{- end }} {{- end }} - {{- if not .port }} - {{- fail (printf "Port is required on enabled services. Service: (%s)" .name) }} - {{- end }} - containerPort: {{ default .port .targetPort }} - {{- with .protocol }} - {{- if or (eq (. | upper) "HTTP") (eq (. | upper) "HTTPS") (eq (. | upper) "TCP") }} - protocol: TCP - {{- else if (eq (. | upper) "UDP") }} - protocol: {{ . }} - {{- else }} - {{- fail (printf "Not valid (protocl: %s)" .) }} - {{- end }} - {{- else }} {{/* If no protocol is given, default to TCP */}} - protocol: TCP - {{- end }} - {{- end }} -{{- end }} {{- end -}} {{- end -}} diff --git a/library/common/1.0.0/templates/lib/controller/_container.tpl b/library/common/1.0.0/templates/lib/controller/_container.tpl index 48a7c5e2..d1f3a8a9 100644 --- a/library/common/1.0.0/templates/lib/controller/_container.tpl +++ b/library/common/1.0.0/templates/lib/controller/_container.tpl @@ -40,23 +40,19 @@ {{- with .Values.termination.messagePolicy }} terminationMessagePolicy: {{ tpl . $ }} {{- end }} - env: - {{- if .Values.injectFixedEnvs -}} - {{- include "ix.v1.common.container.fixedEnvs" . | nindent 4 -}} - {{- end -}} {{- $context := dict -}} {{/* Create a new context and pass it to envVars include, so tpl can work. */}} {{- $_ := set $context "envs" .Values.env -}} - {{- $_ := set $context "root" $ -}} - {{- include "ix.v1.common.container.envVars" $context | nindent 4 -}} - {{- $context := dict -}} {{/* Create a new context and pass it to envList include, so tpl can work. */}} {{- $_ := set $context "envList" .Values.envList -}} {{- $_ := set $context "root" $ -}} - {{- include "ix.v1.common.container.envList" $context | nindent 4 -}} + {{/* env and envList */}} + {{- include "ix.v1.common.container.envVars" $context | indent 2 -}} {{- $context := dict -}} {{/* Create a new context and pass it to envFrom include, so tpl can work. */}} {{- $_ := set $context "envFrom" .Values.envFrom -}} {{- $_ := set $context "root" $ -}} - {{- include "ix.v1.common.container.envFrom" $context | nindent 2 -}} - {{/* TODO: On helm template there are some empty lines after fixedEnvs. See if there is a quick way to prune them */}} + {{/* envFrom */}} + {{- include "ix.v1.common.container.envFrom" $context | indent 2 -}} + {{/* Ports */}} + {{- include "ix.v1.common.container.ports" . | indent 2 -}} {{- end -}} {{/* diff --git a/library/common/1.0.0/values.yaml b/library/common/1.0.0/values.yaml index 0a750d6e..8b638c5d 100644 --- a/library/common/1.0.0/values.yaml +++ b/library/common/1.0.0/values.yaml @@ -106,20 +106,22 @@ security: # key: key-name | "{{ .Values.some_key }}" # optional: false (ony with secretKeyRef) env: {} + # some_env: some_value + # some_env2: some_value2 # -- Mainly used in the SCALE UI. Helm Template enabled. envList: [] -# - name: SOME_ENV_VAR -# value: SOME_VALUE -# - name: SOME_ENV_VAR2 -# value: SOME_VALUE2 + # - name: SOME_ENV_VAR + # value: SOME_VALUE + # - name: SOME_ENV_VAR2 + # value: SOME_VALUE2 # Load envs from secret or configMap. Helm template enabled. envFrom: [] - # - configMapRef: - # name: config-map-name - # - secretRef: - # name: secret-name +# - configMapRef: +# name: config-map-name +# - secretRef: +# name: secret-name controller: # -- Enable the controller. @@ -156,6 +158,7 @@ service: main: # -- Enables or disables the service enabled: true + # enabled: false # -- Configure the Service port information here. # Additional ports can be added by adding a dictionary key similar to the 'main' port. # @default -- See below @@ -163,8 +166,9 @@ service: main: # -- Enables or disables the port enabled: true + # enabled: false # -- The port number (Default port is required if enabled: true) - port: + port: 2 # -- Port protocol. # Support values are `HTTP`, `HTTPS`, `TCP` and `UDP`. # HTTPS and HTTPS spawn a TCP service and get used for internal URL and name generation