Variables

With functions, pipelines, objects, and control structures under our belts, we can turn to one of the more basic ideas in many programming languages: variables. In templates, they are less frequently used. But we will see how to use them to simplify code, and to make better use of with and range.

In an earlier example, we saw that this code will fail:

  1. {{- with .Values.favorite }}
  2. drink: {{ .drink | default "tea" | quote }}
  3. food: {{ .food | upper | quote }}
  4. release: {{ .Release.Name }}
  5. {{- end }}

Release.Name is not inside of the scope that’s restricted in the with block. One way to work around scoping issues is to assign objects to variables that can be accessed without respect to the present scope.

In Helm templates, a variable is a named reference to another object. It follows the form $name. Variables are assigned with a special assignment operator: :=. We can rewrite the above to use a variable for Release.Name.

  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4. name: {{ .Release.Name }}-configmap
  5. data:
  6. myvalue: "Hello World"
  7. {{- $relname := .Release.Name -}}
  8. {{- with .Values.favorite }}
  9. drink: {{ .drink | default "tea" | quote }}
  10. food: {{ .food | upper | quote }}
  11. release: {{ $relname }}
  12. {{- end }}

Notice that before we start the with block, we assign $relname := .Release.Name. Now inside of the with block, the $relname variable still points to the release name.

Running that will produce this:

  1. # Source: mychart/templates/configmap.yaml
  2. apiVersion: v1
  3. kind: ConfigMap
  4. metadata:
  5. name: viable-badger-configmap
  6. data:
  7. myvalue: "Hello World"
  8. drink: "coffee"
  9. food: "PIZZA"
  10. release: viable-badger

Variables are particularly useful in range loops. They can be used on list-like objects to capture both the index and the value:

  1. toppings: |-
  2. {{- range $index, $topping := .Values.pizzaToppings }}
  3. {{ $index }}: {{ $topping }}
  4. {{- end }}

Note that range comes first, then the variables, then the assignment operator, then the list. This will assign the integer index (starting from zero) to $index and the value to $topping. Running it will produce:

  1. toppings: |-
  2. 0: mushrooms
  3. 1: cheese
  4. 2: peppers
  5. 3: onions

For data structures that have both a key and a value, we can use range to get both. For example, we can loop through .Values.favorite like this:

  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4. name: {{ .Release.Name }}-configmap
  5. data:
  6. myvalue: "Hello World"
  7. {{- range $key, $val := .Values.favorite }}
  8. {{ $key }}: {{ $val | quote }}
  9. {{- end}}

Now on the first iteration, $key will be drink and $val will be coffee, and on the second, $key will be food and $val will be pizza. Running the above will generate this:

  1. # Source: mychart/templates/configmap.yaml
  2. apiVersion: v1
  3. kind: ConfigMap
  4. metadata:
  5. name: eager-rabbit-configmap
  6. data:
  7. myvalue: "Hello World"
  8. drink: "coffee"
  9. food: "pizza"

Variables are normally not “global”. They are scoped to the block in which they are declared. Earlier, we assigned $relname in the top level of the template. That variable will be in scope for the entire template. But in our last example, $key and $val will only be in scope inside of the {{range…}}{{end}} block.

However, there is one variable that is always global - $ - this variable will always point to the root context. This can be very useful when you are looping in a range and need to know the chart’s release name.

An example illustrating this:

  1. {{- range .Values.tlsSecrets }}
  2. apiVersion: v1
  3. kind: Secret
  4. metadata:
  5. name: {{ .name }}
  6. labels:
  7. # Many helm templates would use `.` below, but that will not work,
  8. # however `$` will work here
  9. app.kubernetes.io/name: {{ template "fullname" $ }}
  10. # I cannot reference .Chart.Name, but I can do $.Chart.Name
  11. helm.sh/chart: "{{ $.Chart.Name }}-{{ $.Chart.Version }}"
  12. app.kubernetes.io/instance: "{{ $.Release.Name }}"
  13. # Value from appVersion in Chart.yaml
  14. app.kubernetes.io/version: "{{ $.Chart.AppVersion }}"
  15. app.kubernetes.io/managed-by: "{{ $.Release.Service }}"
  16. type: kubernetes.io/tls
  17. data:
  18. tls.crt: {{ .certificate }}
  19. tls.key: {{ .key }}
  20. ---
  21. {{- end }}

So far we have looked at just one template declared in just one file. But one of the powerful features of the Helm template language is its ability to declare multiple templates and use them together. We’ll turn to that in the next section.