Sub topic 6
Pipelines, functions, conditionals, and loops
This chapter assumes you can read a basic Helm template, know where values.yaml sits in a chart, and can run helm template. Here you will shape values with functions, branch on them, and repeat blocks of YAML.
Pipelines
A pipeline passes the result of one step into the next, like a shell pipe. The value on the left becomes the last argument of the function on the right.
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"This is the same as default .Chart.AppVersion .Values.image.tag. If image.tag is empty, Helm uses the chart's appVersion. Pipelines chain, so you can add more steps:
name: {{ .Values.nameOverride | default .Chart.Name | trunc 63 | trimSuffix "-" }}The diagram shows how a value flows through a typical chain.
Functions you will use most:
| Function | Purpose | Example | Column |
|---|---|---|---|
| `default` | Fallback for empty values | `default "info" .Values.level` | |
| `quote` | Wrap in double quotes | `.Values.port \ | quote` |
| `upper`, `lower` | Change case | `.Values.env \ | upper` |
| `required` | Fail rendering if empty | `required "host is required" .Values.host` | |
| `toYaml` | Convert a map or list to YAML text | `toYaml .Values.resources` | |
| `nindent` | Add a newline, then indent every line | `nindent 12` |
Conditionals
if renders a block only when its value is true. These values count as false: false, 0, an empty string, nil, and an empty list or map. You can add else if and else branches.
{{- if .Values.ingress.enabled }}
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: {{ include "webapp.fullname" . }}
spec:
rules:
- host: {{ .Values.ingress.host | quote }}
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: {{ include "webapp.fullname" . }}
port:
number: {{ .Values.service.port }}
{{- end }}With ingress.enabled: false, the file renders as nothing, so Helm creates no Ingress. Combine tests with and, or, not, eq, and ne, for example {{ if and .Values.ingress.enabled (eq .Values.service.type "ClusterIP") }}.
Whitespace control
{{- removes whitespace before the action, including the newline, and -}} removes it after. Without them, stray blank lines and wrong indentation can produce invalid YAML. Use {{- on lines that only hold control actions such as if, with, range, and end.
Changing scope with with
with runs its block only if the value is non-empty, and it sets . to that value inside the block.
{{- with .Values.resources }}
resources:
{{- toYaml . | nindent 12 }}
{{- end }}If resources is {}, the whole resources: key is left out. Inside the block, .Release.Name no longer works because . now points at resources. Use $, which always refers to the root scope: {{ $.Release.Name }}.
Repeating with range
range loops over a list or a map. For a map, assign the key and value to variables.
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ include "webapp.fullname" . }}-env
data:
{{- range $name, $value := .Values.env }}
{{ $name }}: {{ $value | quote }}
{{- end }}With the default env, this renders LOG_LEVEL: "info". Maps are iterated in sorted key order, so output is stable between renders. For a list, use range $i, $item := .Values.hosts, or range .Values.hosts to set . to each item.
Common errors
- Bad indentation after
toYaml. Always pipe tonindentwith the correct number of spaces, notindent, so the newline is included. - Numbers rendered without quotes. Environment variable values must be strings. Pipe them through
quote. - Empty string treated as unset.
defaultalso replaces"",0, andfalse. A value offalsecannot be kept withdefault true.
Exercise
A values file sets resources: {}. Your template contains {{- with .Values.resources }}resources: ...{{- end }}. What does the rendered Deployment contain for resources, and why?
Show answer
Nothing. An empty map is false, so with skips its block and the resources: key does not appear in the output.
Exercise
Inside {{- range $name, $value := .Values.env }}, how do you print the release name?
Show answer
Use {{ $.Release.Name }}. Inside range, . is the current item, while $ still points at the root scope.