k8s helm examples script pieces
Add extraEnv values to chart #1 with
{{- range $key, $value := .Values.extraEnv -}} - name: {{ $key }} - value: {{ $value -}} {{- end }}
Add extraEnv values to chart #2 as string and then parse through tpl(template)
# in values.yaml extraEnv: | - name: KEYCLOAK_LOGLEVEL value: DEBUG - name: HOSTNAME value: {{ .Release.Name }}-keycloak # and in template {{- with .Values.keycloak.extraEnv }}{{ tpl . $ | indent 12 }}{{- end }}
Referencing Mutually-deployed Resources e.g. db password
# use trick above passing string that can be parsed in deployment and vars replaced extraEnv: |. #String to be parsed through tpl - name: POSTGRES_USER value: {{ .Values.postgresql.postgresUser }} - name: POSTGRESS_PASSWORD valueFrom: secretKeyRef: name: {{ .Release.Name }}-postgresql key: postgres-password
parse vars into file imported into ConfigMap or Secret
# https://stackoverflow.com/questions/47595295/how-do-i-load-multiple-templated-config-files-into-a-helm-chart/52009992#52009992 conf_file1: {{ tpl (.Files.Get "files/conf_file1") . | quote }} #load into a Secret we need to encode the content in base64: conf_file1: {{ tpl (.Files.Get "files/conf_file1") . | b64enc | quote }} #load a set of files into a ConfigMap using .Files.Glob : {{ (tpl (.Files.Glob "files/*").AsConfig . ) | indent 2 }}
- To update deployment when a config, changes, add the sha1 of the config as annotation to deployment.
- A change in the annotation will force a redeploy
e.g. deployment.yaml
{{/* 1. Generate checksums for config changes once, then loop through deployments */}} {{- define "config-checksums" }} {{- range $path, $bytes := $.Files.Glob "files/**" }} {{- base $path }}={{ tpl ( $.Files.Get $path ) $ | sha1sum }} {{- end }} {{- end }} {{ $chk := include "config-checksums" . | sha1sum }} --- kind: Deployment spec: template: metadata: annotations: config/checksums: {{ $chk }} ...
}}}