Helm in Depth — From Charts to a Platform Library›02 · Templates and helpers

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.

Practitioner → Advanced
Key wordsGo templatesnamed templates_helpers.tpldefineincludetemplatetplnindenttoYamlrequireddefaultscope$withrangelookupwhitespace control

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, not template: only include can be piped into nindent, quote, trim.
  • nindent N adds 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 a dict when 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.x stops rendering with your message, which is much better than an invalid manifest that fails at apply time.
  • default for optional values; but put real defaults in values.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

  • lookup reads live objects from the cluster. With helm template, client-side dry runs and Argo CD rendering it returns nothing, so output differs between environments.
  • randAlphaNum and 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.
  • now and 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 inside range/with; $ is the root.
  • Fail early with required and schemas; control whitespace with dashes.
  • Avoid lookup and 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.