Variables

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

    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 }}

    Running that will produce this:

    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. {{- 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. 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:

    Variables are normally not “global”. They are scoped to the block in which they are declared. Earlier, we assigned 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.

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

    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.