Sub topic 7
Reusing snippets with named templates
This chapter assumes you can write a basic Helm template with .Values, .Release, and .Chart, and that you know pipelines such as | quote. If {{ .Values.replicaCount }} is new to you, review the templates chapter first.
Why named templates
A chart usually has several manifests that need the same labels or the same resource name. Copying that text into every file works until one copy changes. A named template is a snippet you define once and call from any manifest in the chart.
Defining templates in _helpers.tpl
Helm renders every file in templates/ as a manifest, except files whose names start with an underscore. By convention, _helpers.tpl holds definitions only. It produces no Kubernetes object.
{{- define "webapp.fullname" -}}
{{- printf "%s-%s" .Release.Name .Chart.Name | trunc 63 | trimSuffix "-" -}}
{{- end -}}
{{- define "webapp.labels" -}}
app.kubernetes.io/name: {{ .Chart.Name }}
app.kubernetes.io/instance: {{ .Release.Name }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end -}}The trunc 63 call matters because Kubernetes limits many resource names to 63 characters. The {{- and -}} markers trim surrounding whitespace so the output has no stray blank lines.
Calling a template with include
Use include with the template name and a context. The context is the value the snippet sees as .. Usually you pass the current . so the snippet can read .Release, .Chart, and .Values.
metadata:
name: {{ include "webapp.fullname" . }}
labels:
{{- include "webapp.labels" . | nindent 4 }}nindent 4 adds a newline and indents every line by four spaces. YAML depends on indentation, so the number must match the nesting at the call site.
include versus template
Helm also has a template action. It inserts the output directly, so you cannot pipe it into another function. include returns the output as a string, which lets you write | nindent 4. Prefer include in nearly all cases.
Passing a custom context
A snippet receives only what you pass it. To give it several values, build a map with dict.
{{- define "webapp.envVar" -}}
- name: {{ .name }}
value: {{ .value | quote }}
{{- end -}}env:
{{- include "webapp.envVar" (dict "name" "LOG_LEVEL" "value" .Values.env.LOG_LEVEL) | nindent 2 }}Inside webapp.envVar, . is the map, so you read .name and .value. Chart-wide objects such as .Release are not available there unless you add them to the map.
Common errors
- Forgetting the dot.
include "webapp.labels"with no context fails or renders empty values, because.Chart.Namehas nothing to read from. - Wrong indentation. If a rendered label block lands at the wrong depth, check the number in
nindent. - Name collisions. Template names are global across the chart and its subcharts. A bare name like
labelscan clash with a subchart's template. Prefix every name with the chart name, as inwebapp.labels. - Misusing
$. Insiderangeorwith,.changes. Pass$(the root context) when you need the top-level objects, for exampleinclude "webapp.labels" $.
Summary
Define a snippet once with define in _helpers.tpl. Call it with include, passing . or a dict. Pipe the result through nindent to fix its indentation, and prefix names with the chart name to avoid collisions.
Exercise
A named template "webapp.labels" renders correctly in deployment.yaml, but inside a range loop in the same file, include "webapp.labels" . fails because .Chart is missing. Why does this happen, and how do you fix it?
Show answer
Inside range, . is bound to the current loop item, not the root context, so .Chart does not exist. Pass the root context instead: include "webapp.labels" $.
Exercise
Why does {{ include "webapp.labels" . | nindent 4 }} work, while {{ template "webapp.labels" . | nindent 4 }} does not?
Show answer
include returns its output as a string, so it can be piped to nindent. template writes its output directly and cannot be used in a pipeline.