Sub topic 9
Variables and scope: $ and the root context
This chapter assumes you can write basic Go template actions, read .Values and .Release, and use if, with and range. You also need Helm 3 installed. Here you will learn what the dot (.) refers to at any point in a template, how to keep a handle on the root context with $, and how to store values in variables.
What the dot means
In a template, . is the current context. At the top of a file, . is the root object. It holds .Values, .Release, .Chart and the other built-in objects.
Some actions change the context:
withsets.to the value you pass it.rangesets.to the current item on each pass.definestarts with.set to whatever you pass toincludeortemplate.
Inside those blocks, .Release.Name stops working unless the new . happens to contain a Release key. This is the most common scope error in Helm charts.
Reaching the root with $
$ is set to the root context when template execution starts. It keeps that value inside with and range, so you can always reach the built-in objects.
{{- with .Values.env }}
env:
{{- range $key, $value := . }}
- name: {{ $key | quote }}
value: {{ $value | quote }}
- name: RELEASE_NAME
value: {{ $.Release.Name | quote }}
{{- end }}
{{- end }}Inside range, . is the current value. $.Release.Name still reads from the root. Writing .Release.Name there would fail with an error such as nil pointer evaluating interface {}.Name.
Inside a define block, $ is whatever you passed to include, not the chart root. If you pass . from the top level, it is the same object. If you pass something else, $ is that value.
Declaring variables
A variable starts with $. Use := to declare it and = to change it later.
{{- $name := include "webapp.fullname" . -}}
{{- $port := .Values.service.port -}}
apiVersion: v1
kind: Service
metadata:
name: {{ $name }}
spec:
ports:
- port: {{ $port }}
targetPort: httpVariables are scoped to the block where you declare them, up to its end. A variable declared at the top of the file is visible everywhere in that file, including inside with and range. A variable declared inside a block is gone after end.
Assignment with = can change a variable declared in an outer block. That is how you carry a result out of a loop:
{{- $hosts := "" -}}
{{- range .Values.ingress.hosts }}
{{- $hosts = printf "%s %s" $hosts . -}}
{{- end }}
hosts:{{ $hosts }}If you wrote := inside the loop, you would create a new variable that disappears at end, and the outer $hosts would stay empty.
Saving the root under a name
$ is short, but a named variable can be clearer in long templates. It also survives when you pass a different context to include.
{{- $root := . -}}
{{- range .Values.workers }}
- name: {{ .name }}
image: {{ $root.Values.image.repository }}:{{ .tag | default $root.Chart.AppVersion }}
{{- end }}Passing a custom context to a helper
A helper only sees what you give it. To pass several values, build a dictionary with dict:
{{ include "webapp.labels" (dict "root" $ "component" "worker") }}Inside that helper, you read .root.Release.Name and .component. Plain .Release.Name would fail because . is now the dictionary.
Summary of scope rules
| Construct | Effect on `.` | `$` inside |
|---|---|---|
| Top of file | Root context | Root context |
| `with X` | X | Still the root |
| `range X` | Current item | Still the root |
| `define` | Value passed to `include` | The value passed to `include` |
Exercise
This snippet fails with a nil pointer error. Why, and how do you fix it? {{- range .Values.hosts }}- host: {{ . }}.{{ .Release.Namespace }}{{- end }}
Show answer
Inside range, . is the current host string, so .Release does not exist there. Use the root context instead: {{ . }}.{{ $.Release.Namespace }}.
Exercise
What does $total hold after this runs, and why? {{- $total := 0 -}}{{- range .Values.items }}{{- $total := add $total 1 -}}{{- end }}
Show answer
It stays 0. The := inside the loop declares a new variable that goes out of scope at end. Use = ({{- $total = add $total 1 -}}) to update the outer variable.