cleanup a bit container tpl from envs

This commit is contained in:
Stavros kois
2022-11-15 18:45:17 +02:00
parent 6dc188eff3
commit 618f91022c
6 changed files with 105 additions and 110 deletions

View File

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

View File

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

View File

@@ -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 <valueFrom> 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 "<optional> 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 "<optional> 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 <valueFrom> 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 "<optional> 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 "<optional> 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 "<name> for the keyRef is not defined in (%s)" $name) $value.name) $root }} {{/* Expand name and key */}}
key: {{ tpl (required (printf "<key> 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 "<name> for the keyRef is not defined in (%s)" $name) $value.name) $root }} {{/* Expand name and key */}}
key: {{ tpl (required (printf "<key> 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 -}}
{{/*

View File

@@ -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 <protocol> (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 <protocol> (protocl: %s)" .) }}
{{- end }}
{{- else }} {{/* If no protocol is given, default to TCP */}}
protocol: TCP
{{- end }}
{{- end }}
{{- end }}
{{- end -}}
{{- end -}}

View File

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

View File

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