Sub topic 10
Helper templates: define, include and _helpers.tpl
This chapter assumes you can read Go template actions, use .Values, .Release and .Chart, and apply pipelines such as default and trunc. You should also know how {{- trims whitespace.
Why helpers exist
Every manifest in a chart needs the same resource names and labels. Copying that text into each file invites drift. A named template is a reusable block of template text. You declare it once with define and call it from anywhere in the chart.
Files in templates/ whose names start with an underscore are not rendered as manifests. By convention, _helpers.tpl holds the named templates.
Defining helpers
Prefix each name with the chart name, such as webapp.. Named templates are global across the chart and its subcharts, so a generic name like labels can collide with another chart's template.
{{- define "webapp.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}}
{{- end }}
{{- define "webapp.fullname" -}}
{{- printf "%s-%s" .Release.Name .Chart.Name | trunc 63 | trimSuffix "-" -}}
{{- end }}
{{- define "webapp.selectorLabels" -}}
app.kubernetes.io/name: {{ include "webapp.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}
{{- define "webapp.labels" -}}
helm.sh/chart: {{ printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{ include "webapp.selectorLabels" . }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}The trunc 63 calls matter because Kubernetes limits many names and label values to 63 characters. webapp.labels also calls webapp.selectorLabels, so helpers can build on each other.
Calling helpers with include
Pass the context as the last argument. The dot . hands the helper the current root context, so it can read .Chart, .Values and .Release.
metadata:
name: {{ include "webapp.fullname" . }}
labels:
{{- include "webapp.labels" . | nindent 4 }}Inside a with or range block, . changes meaning. To call a helper from there, pass the root context saved in $, as in include "webapp.name" $. Passing the wrong context is the most common cause of errors such as nil pointer evaluating interface {}.Chart.
include versus template
Helm also has a built-in template action. It prints directly and cannot feed its output into a pipeline. include returns a string, so you can pipe it into nindent, quote or sha256sum. Use include in charts.
{{ template "webapp.name" . }}
{{ include "webapp.name" . | upper }}The first line works but cannot be piped. The second can.
Indentation
A helper that returns several lines does not know where it will be placed. Leave helper output unindented. Let the caller add indentation with nindent N, which adds a leading newline and indents every line by N spaces. That is why the call above is written {{- include ... | nindent 4 }}. The {{- removes the newline and spaces before the action, and nindent supplies its own.
How a call flows
Common errors
| Symptom | Likely cause |
|---|---|
| `template: no template "webapp.name" associated` | Name misspelled, or the helper is defined in a file that failed to parse |
| `nil pointer evaluating interface {}` | `.` was changed by `with` or `range`, or you passed no context |
| Labels misaligned, or `mapping values are not allowed` | Missing `nindent`, or the wrong indentation number |
| Extra blank lines in output | Missing `-` trimming in `define` |
Check results with helm template my-release ./webapp.
Exercise
A helper webapp.labels is called inside a range block with {{ include "webapp.labels" . | nindent 4 }}. It fails with a nil pointer error on .Chart.Name. Why, and how do you fix it?
Show answer
Inside range, . is the current list or map item, not the root context, so .Chart does not exist. Pass the root context instead: {{ include "webapp.labels" $ | nindent 4 }}.