Sub topic 6
Pipelines and functions: default, quote, trunc and more
This chapter assumes you can read a chart's layout, know that .Values holds the contents of values.yaml, and can run helm template. Here you will learn how a pipeline passes a value through functions, and which functions you will use most often.
How a pipeline works
A pipeline uses the | character to pass the value on its left as the last argument to the function on its right. These two lines produce the same result:
{{ quote .Values.env.LOG_LEVEL }}
{{ .Values.env.LOG_LEVEL | quote }}Pipelines read left to right, so a chain of functions is easier to follow than nested calls. Helm includes the Sprig function library and adds a few of its own, such as toYaml and include.
The functions you will use most
| Function | What it does | Example | Result | Column |
|---|---|---|---|---|
| `default` | Uses a fallback when the value is empty | `.Values.image.tag \ | default "latest"` | `latest` if the tag is `""` |
| `quote` | Wraps the value in double quotes | `.Values.port \ | quote` | `"80"` |
| `upper` | Converts to upper case | `"web" \ | upper` | `WEB` |
| `trunc` | Cuts a string to at most N characters | `.Release.Name \ | trunc 63` | first 63 characters |
| `trimSuffix` | Removes a trailing string | `"web-" \ | trimSuffix "-"` | `web` |
| `replace` | Replaces text | `"1.0+b" \ | replace "+" "_"` | `1.0_b` |
| `printf` | Formats a string | `printf "%s-%s" "a" "b"` | `a-b` |
default treats "", 0, false, an empty list, an empty map and nil as empty. Keep that in mind for values such as replicaCount: 0, which default 1 would silently replace with 1.
A worked example
The chart's deployment.yaml builds the image reference like this:
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"In values.yaml the tag is an empty string, so default falls back to .Chart.AppVersion, which is 1.27.0. Render it to check:
helm template my-release ./webapp | grep "image:"
helm template my-release ./webapp --set image.tag=1.28.0 | grep "image:"The first command prints image: "nginx:1.27.0". The second prints image: "nginx:1.28.0".
The fullname helper from _helpers.tpl shows a longer chain:
{{- printf "%s-%s" .Release.Name .Chart.Name | trunc 63 | trimSuffix "-" -}}printfjoins the release name and chart name.trunc 63shortens the result, because Kubernetes names are limited to 63 characters in many places.trimSuffix "-"removes a trailing dash that the cut might leave behind.
Argument order and quoting
In a pipeline the piped value becomes the last argument. That is why trunc 63 works in a pipeline: the function signature is trunc length string. If you call the function directly, you must pass the arguments in the same order: trunc 63 .Release.Name.
Use quote when a value must be a YAML string. An environment variable value of true or 80 would otherwise be parsed as a boolean or number, and Kubernetes rejects non-string env values.
- name: {{ $key | quote }}
value: {{ $value | quote }}Note that quote on a missing (nil) value renders an empty string, so pair it with default or required when a value must exist.
Exercise
A values file sets replicaCount: 0 to pause a deployment, and the template uses replicas: {{ .Values.replicaCount | default 2 }}. How many replicas does the rendered Deployment have, and why?
Show answer
It has 2 replicas. default treats 0 as empty and uses the fallback. To honor 0, render {{ .Values.replicaCount }} directly and set the default in values.yaml.
Exercise
Write a pipeline that takes .Values.image.repository, converts it to upper case, and wraps it in double quotes.
Show answer
{{ .Values.image.repository | upper | quote }}