Lesson 02 of 10 · How Helm works
Templates and helpers
Write Helm templates that stay readable and fail clearly: named templates in _helpers.tpl, include instead of template, scope with . and $, whitespace control, required and default, tpl for templated values, and why lookup and random functions surprise people.
The template language in one minute
Helm templates are Go templates plus the Sprig function library. The root context . gives you .Values, .Release (name, namespace, revision), .Chart, .Capabilities (cluster version and APIs) and .Files.
A template is a form letter: "Dear , your class is ". Named templates are stamps you made once ("school address block") and press wherever needed, at the right place on the page (indentation).
Named templates and helpers
Put reusable pieces in templates/_helpers.tpl (files starting with _ are not rendered as manifests):
{{- define "app.labels" -}}
app.kubernetes.io/name: {{ .Values.name }}
app.kubernetes.io/instance: {{ .Release.Name }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
metadata:
labels:
{{- include "app.labels" . | nindent 4 }}
- Use
include, nottemplate: onlyincludecan be piped intonindent,quote,trim. nindent Nadds a newline and indents by N, which is the usual source of "works on my machine" YAML errors when wrong.- Pass the right context:
include "app.labels" .(the whole root), or adictwhen a helper needs specific inputs.
Scope: . and $
Inside range and with, . becomes the current item. Use $ for the root:
{{- range .Values.extraPorts }}
- name: {{ .name }}
port: {{ .port }}
# a top-level value, reached through the root context:
targetPort: {{ $.Values.defaultTargetPort }}
{{- end }}
Fail clearly, default sensibly
required "message" .Values.xstops rendering with your message, which is much better than an invalid manifest that fails at apply time.defaultfor optional values; but put real defaults invalues.yaml, where users can see them.- A
values.schema.json(next lesson) catches wrong types and unknown values before templates run.
Whitespace
{{- trims whitespace (including newlines) before the action; -}} after. Most rendering bugs are blank lines or wrong indentation from missing or extra dashes. Render often with helm template . --debug.
Values that are templates: tpl
Sometimes a value must contain template syntax (for example a hostname built from the release name). tpl renders a string with the given context:
host: {{ tpl .Values.hostTemplate . }}
# values.yaml: hostTemplate: "{{ .Release.Name }}.apps.example.com"
Use sparingly: it makes values harder to reason about.
Functions that surprise
lookupreads live objects from the cluster. Withhelm template, client-side dry runs and Argo CD rendering it returns nothing, so output differs between environments.randAlphaNumand other random functions produce a different value on every render: a password in a Secret changes on every upgrade, and GitOps tools see permanent drift. Generate secrets outside the chart (secret store, External Secrets) or reference an existing Secret.nowand dates: same problem.
Try it
Create a chart with helm create demo, open templates/_helpers.tpl and templates/deployment.yaml, and trace how labels are produced. Remove one - in a {{- and render with helm template demo ./demo: see the blank line appear. Add required around the image tag and render with the tag unset.
Recap
- Put shared pieces in named templates; call them with
include … | nindent N. .changes insiderange/with;$is the root.- Fail early with
requiredand schemas; control whitespace with dashes. - Avoid
lookupand random or time functions in rendered output.
This site is a public version of my personal engineering knowledge hub. It intentionally excludes confidential company information and internal operational details.