Templates turn one chart into configurable manifests; most chart bugs are template bugs.
Templates use Go's text/template syntax plus the Sprig function library and Helm extras. Expressions live in {{ }}; {{- and -}} trim whitespace on that side.
Built-in objects:
.Values: merged values..Release.Name,.Release.Namespace,.Release.IsUpgrade,.Release.Revision..Chart.Name,.Chart.Version,.Chart.AppVersion..Capabilities.KubeVersion,.Capabilities.APIVersions.Has "policy/v1"..Files.Get "conf/app.conf"to read non-template files from the chart.
Pipelines pass a value through functions left to right: {{ .Values.name | default "web" | quote }}. Useful functions: default, quote, upper, toYaml, indent/nindent, required "msg", b64enc, sha256sum, printf.
Base names on the release ({{ .Release.Name }}-web) so several releases can coexist in one namespace.
YAML is whitespace-sensitive and templates are plain text, so indentation is the classic problem: when inserting a block, use toYaml .Values.resources | nindent 12. nindent adds a newline first, which keeps the template readable.
A good trick: put a checksum of the rendered ConfigMap in a pod annotation (checksum/config) so config changes roll the pods.
Example
# templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ .Release.Name }}-web
labels:
app.kubernetes.io/instance: {{ .Release.Name }}
spec:
replicas: {{ .Values.replicaCount | default 1 }}
selector:
matchLabels: {app.kubernetes.io/instance: {{ .Release.Name }}}
template:
metadata:
labels: {app.kubernetes.io/instance: {{ .Release.Name }}}
annotations:
checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
spec:
containers:
- name: web
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
resources:
{{- toYaml .Values.resources | nindent 12 }}Remember
- .Values, .Release, .Chart, .Capabilities, .Files are the built-ins
- Pipelines: value | function | function
- toYaml | nindent N inserts blocks with correct indentation
Watch out for
- Forgetting quote on values like "yes" or "1.10" that YAML would turn into a boolean or a number.