_helpers.tpl

_helpers.tpl holds reusable named templates so you don't repeat logic across manifest files.

Syntax{{- define "mychart.name" -}} ... {{- end -}}

As charts grow, the same snippets — a name, a label block — appear in many files. Rather than copy them, you define them once in templates/_helpers.tpl.

Why the underscore

Files whose names start with _ are never rendered into Kubernetes manifests. They exist purely to hold named templates that other files pull in.

Defining a named template

Use define to declare one and end to close it. Naming convention is <chart>.<purpose>.

Example

Example · yaml
{{/* templates/_helpers.tpl */}}
{{- define "mychart.fullname" -}}
{{- printf "%s-%s" .Release.Name .Chart.Name | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{- define "mychart.labels" -}}
app.kubernetes.io/name: {{ .Chart.Name }}
app.kubernetes.io/instance: {{ .Release.Name }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end -}}

When to use it

  • A chart author defines the application full name once in _helpers.tpl and includes it in every template file, so renaming the chart requires changing only one place.
  • A developer moves the common label block into _helpers.tpl to remove copy-pasted label sections from the Deployment, Service, and Ingress templates.
  • A platform team shares a _helpers.tpl across multiple charts via a library chart, ensuring consistent naming conventions across all services without duplication.

More examples

Define a name helper

Defines two reusable named templates in _helpers.tpl: a short chart name and a full name combining release and chart.

Example · yaml
{{/* _helpers.tpl */}}
{{- define "myapp.name" -}}
{{- .Chart.Name | trunc 63 | trimSuffix "-" }}
{{- end }}

{{- define "myapp.fullname" -}}
{{- printf "%s-%s" .Release.Name .Chart.Name | trunc 63 | trimSuffix "-" }}
{{- end }}

Include helper in a template

Calls the named templates from _helpers.tpl using include so the Deployment reuses the same name logic as every other resource.

Example · yaml
# templates/deployment.yaml
metadata:
  name: {{ include "myapp.fullname" . }}
  labels:
    app: {{ include "myapp.name" . }}

Helper with complex logic

Encapsulates a conditional name-resolution rule in a helper so all templates that reference a ServiceAccount use identical logic.

Example · yaml
{{- define "myapp.serviceAccountName" -}}
{{- if .Values.serviceAccount.create }}
{{- .Values.serviceAccount.name | default (include "myapp.fullname" .) }}
{{- else }}
{{- .Values.serviceAccount.name | default "default" }}
{{- end }}
{{- end }}

Discussion

  • Be the first to comment on this lesson.