TExcelTAD
Home › Kubernetes Packaging: Helm & Kustomize › Helpers & Named Templates

Helpers & Named Templates

Every object in a chart needs the same name and the same labels. Copying them into each file is how charts end up with a Service whose selector doesn't match its Deployment. Named templates (also called partials or helpers) define a snippet once and reuse it everywhere.

define and include

{{ define "name" }}…{{ end }} declares a named template; {{ include "name" context }} renders it and returns the result as a string, which you can pipe on:

bash · try it yourself
mkdir -p linkly/templates && cd linkly
printf 'apiVersion: v2\nname: linkly\nversion: 0.2.0\nappVersion: "1.4.0"\n' > Chart.yaml
printf 'nameOverride: ""\nfullnameOverride: ""\n' > values.yaml
cat > templates/_helpers.tpl <<'EOF'
{{/* Chart name, overridable. */}}
{{- define "linkly.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/* Release-qualified name: "<release>-linkly", or just the release name if it already contains "linkly". */}}
{{- define "linkly.fullname" -}}
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- $name := include "linkly.name" . }}
{{- if contains $name .Release.Name }}
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- end }}
{{- end }}

{{/* Labels that never change for the life of a release: safe for selectors. */}}
{{- define "linkly.selectorLabels" -}}
app.kubernetes.io/name: {{ include "linkly.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}

{{/* All common labels. */}}
{{- define "linkly.labels" -}}
helm.sh/chart: {{ printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" }}
{{ include "linkly.selectorLabels" . }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}
EOF
cat > templates/service.yaml <<'EOF'
apiVersion: v1
kind: Service
metadata:
  name: {{ include "linkly.fullname" . }}
  labels:
    {{- include "linkly.labels" . | nindent 4 }}
spec:
  selector:
    {{- include "linkly.selectorLabels" . | nindent 4 }}
  ports: [{name: http, port: 80, targetPort: http}]
EOF
helm template prod . | tail -n +3
helm template linkly-staging . | grep -m1 'name:'
helm template prod . --set fullnameOverride=shortener | grep -m1 'name:'
apiVersion: v1
kind: Service
metadata:
  name: prod-linkly
  labels:
    helm.sh/chart: linkly-0.2.0
    app.kubernetes.io/name: linkly
    app.kubernetes.io/instance: prod
    app.kubernetes.io/version: "1.4.0"
    app.kubernetes.io/managed-by: Helm
spec:
  selector:
    app.kubernetes.io/name: linkly
    app.kubernetes.io/instance: prod
  ports: [{name: http, port: 80, targetPort: http}]
  name: linkly-staging
  name: shortener

This is the same set of helpers helm create generates, and you'll find it in most charts:

  • fullname gives every object a name unique per release, so two releases of the chart can live in one namespace. Names are truncated to 63 characters because many name fields must be DNS labels.
  • selectorLabels is a separate, smaller set. A Deployment's spec.selector is immutable, so selectors must never include labels that change on upgrade, such as helm.sh/chart or app.kubernetes.io/version. Putting the version in the selector is a classic bug: the next upgrade fails with field is immutable.
  • Names are namespaced by convention: linkly.fullname, not fullname. Named templates are global across the chart and all its subcharts, so two charts defining "fullname" silently overwrite each other.

include vs template

{{ template "name" . }} is the Go built-in; it writes output directly and cannot be piped. include returns a string, so include … | nindent 4 works. Always use include.

Passing more than the dot

A named template receives exactly one argument. To pass several things, build a map with dict and keep the root context in it:

bash · try it yourself
mkdir -p linkly/templates && cd linkly
printf 'apiVersion: v2\nname: linkly\nversion: 0.2.0\n' > Chart.yaml
cat > values.yaml <<'EOF'
ports:
  http: 8080
  metrics: 9090
EOF
cat > templates/_ports.tpl <<'EOF'
{{- define "linkly.containerPort" -}}
- name: {{ .name }}
  containerPort: {{ .port }}
  protocol: TCP
{{- /* .root is the chart's root context, passed in explicitly */}}
{{- if eq .name "metrics" }}
  # scraped by {{ .root.Release.Name }}-monitoring
{{- end }}
{{- end }}
EOF
cat > templates/ports.yaml <<'EOF'
ports:
{{- range $name, $port := .Values.ports }}
  {{- include "linkly.containerPort" (dict "name" $name "port" $port "root" $) | nindent 2 }}
{{- end }}
EOF
helm template demo .
---
# Source: linkly/templates/ports.yaml
ports:
  - name: http
    containerPort: 8080
    protocol: TCP
  - name: metrics
    containerPort: 9090
    protocol: TCP
    # scraped by demo-monitoring

tpl: templates inside values

Sometimes users need to put template expressions into values, for example a hostname that contains the release name. tpl renders a string as a template against a context:

bash · try it yourself
mkdir -p linkly/templates && cd linkly
printf 'apiVersion: v2\nname: linkly\nversion: 0.2.0\n' > Chart.yaml
printf 'host: "{{ .Release.Name }}.preview.example.com"\n' > values.yaml
printf 'hostnames: [{{ tpl .Values.host . | quote }}]\n' > templates/route.yaml
helm template pr-1234 . | tail -1
hostnames: ["pr-1234.preview.example.com"]

Use tpl sparingly: values that are templates are harder to validate and can execute anything the template language can.

Sharing helpers: a library chart

When ten services repeat the same helpers, move them into a type: library chart and add it as a dependency. The session builds a library acme-lib and a chart that uses it through a file:// dependency (dependencies get their own lesson, Chart Dependencies):

bash · try it yourself
mkdir -p acme-lib/templates linkly/templates
printf 'apiVersion: v2\nname: acme-lib\nversion: 1.0.0\ntype: library\n' > acme-lib/Chart.yaml
cat > acme-lib/templates/_labels.tpl <<'EOF'
{{- define "acme.labels" -}}
app.kubernetes.io/name: {{ .Chart.Name }}
app.kubernetes.io/instance: {{ .Release.Name }}
acme.example.com/team: {{ .Values.team | default "unowned" }}
{{- end }}
EOF
cat > linkly/Chart.yaml <<'EOF'
apiVersion: v2
name: linkly
version: 0.2.0
dependencies:
  - name: acme-lib
    version: 1.x.x
    repository: file://../acme-lib
EOF
printf 'team: links\n' > linkly/values.yaml
cat > linkly/templates/sa.yaml <<'EOF'
apiVersion: v1
kind: ServiceAccount
metadata:
  name: {{ .Release.Name }}
  labels:
    {{- include "acme.labels" . | nindent 4 }}
EOF
helm dependency build linkly
helm template demo linkly | tail -n +3
helm template demo acme-lib
Saving 1 charts
Deleting outdated charts
apiVersion: v1
kind: ServiceAccount
metadata:
  name: demo
  labels:
    app.kubernetes.io/name: linkly
    app.kubernetes.io/instance: demo
    acme.example.com/team: links
Error: library charts are not installable

The library's template ran with the parent's context (.Chart.Name is linkly), because named templates are evaluated wherever they are included. Rendering the library on its own is refused.

Test yourself

Why should selector labels exclude app.kubernetes.io/version?

  1. Labels can't contain dots
  2. A Deployment's selector is immutable, so a label that changes on upgrade breaks the next upgrade
  3. Selectors only allow one label
  4. Version labels are reserved by Kubernetes

Why use include instead of template?

  1. include is faster
  2. include returns a string that can be piped to functions like nindent
  3. template is deprecated in Helm 4
  4. include works across charts and template doesn't

How do you pass both a port and the root context to a named template?

  1. Two arguments: include "t" . $port
  2. A dict: include "t" (dict "port" $port "root" $)
  3. Global variables
  4. It's impossible