The following command generates a secrets in the templates directory
kubectl create secret generic --dry-run=client eu-secret --from-literal=secret='myEuropeanSecret' -o yaml | kubectl neat > templates/secret.yaml
Then we'll enclose the environment variable definition with a condition depending on the region in the deployment template.
{{- if eq .Values.global.region "eu-west-1" }}
- name: eu-secret
valueFrom:
secretKeyRef:
name: eu-secret
key: secret
{{- end }}
First of all we need to add a new value
printIP:
enabled: True
Then this command will generate a job yaml
kubectl create job my-ip --dry-run=client --image=busybox -o yaml -- wget -qO - http://ipinfo.io/ip | kubectl neat > templates/job.yaml
If we enclose the whole yaml, it won't be created if the boolean is False. With the hook annotation here, the job will be created before any other resource will be applied. We defined a delete policy "hook-failed" in order to keep the job, otherwise it would have been deleted.
{{- if .Values.printIP.enabled -}}
apiVersion: batch/v1
kind: Job
metadata:
name: my-ip
annotations:
"helm.sh/hook": pre-install,pre-upgrade
"helm.sh/hook-weight": "1"
"helm.sh/hook-delete-policy": hook-failed
...
{{- end -}}
If we want to keep the deployment easy to read, we would prefer adding the code in the _helpers.tpl
{{/*
Environment variables
*/}}
{{- define "web.envVars" -}}
{{- range $key, $value := .Values.envVars }}
- name: {{ $key }}
value: {{ $value }}
{{- end }}
{{- end -}}
Then this new variable could be used in the deployment as follows
env:
{{- include "web.envVars" . | nindent 12 }}
The common labels can be changed in the file templates/_helpers.tpl. Here's a proposal This one is tricky, I needed to dig back into the charts available in the stable github repository.
codename: {{ printf "%s %s %s" (.Release.Name | trunc 3) .Chart.Name (now | date "20060102") | snakecase }}
Here's an option to achieve the expected results.
env:
- name: ETCD_HOSTS
value: "{{ range $index, $e := until (.Values.etcd.count|int) }}{{- if $index }},{{end}}etcd-{{ $index }}{{- end }}"