Search code examples
kuberneteskubernetes-helmgo-templates

What is, and what use cases have the dot "." in helm charts?


im currently going through the docs of helm, and there have been at least 3 different uses for the dot (.), is there any specific definition for it? and does it have something in common with the bash use (actual folder/files)?

some cases in the documentation

This print the accesed files in the range called before?

  {{- $files := .Files }}
  {{- range tuple "config1.toml" "config2.toml" "config3.toml" }}
  {{ . }}: |-
    {{ $files.Get . }}
  {{- end }}

This tells "mychart.app" to use the files in the current folder (bash-like behaviour)

{{ include "mychart.app" . | indent 4 }}

and this, i guess it takes the values from the whole folder??? i guess this is not correct since is not working (it has been made by another employee back then and i have to fix it)

{{- define "read.select-annot" -}}
{{- range $key, $value := . }}
{{ $key }}: {{ $value }}
{{- end }}
{{- end }}

thanks for the help


Solution

  • In general, . in Helm templates has nothing to do with files or directories.

    The Helm templating language uses Go's text/template system. There are a couple of different ways a period character can appear there.

    First of all, . can be a character in a string:

    {{- range tuple "config1.toml" "config2.toml" "config3.toml" }}
    {{/*             ^^^^^^^^^^^^
           this is a literal string "config1.toml"             */}}
    ...
    {{- end }}
    

    Secondly, . can be a lookup operator. There aren't any solid examples in your question, but a typical use is looking up in values. If your values.yaml file has

    root:
      key: value
    

    then you can expand

    {{ .Values.root.key }}
    

    and the . before root and key navigates one level down in the dictionary structure.

    The third use, and possibly the one that's confusing you, is that . on its own is a variable.

    {{ . }}
    

    You can do field lookups on it, and you have some examples of that: .Files is the same as index . "Files", and looks up the field "Files" on the object ..

    You use . as a variable in several places:

    {{- $files := .Files }}        {{/* Get "Files" from . */}}
    {{ . }}                        {{/* Write . as a value */}}
    {{ include "mychart.app" . }}  {{/* Pass . as the template parameter */}}
    

    . is tricky in that it has somewhat contextual meaning:

    • At the top level, Helm initializes . to an object with keys Files, Release, Values, and Chart.
    • In a defined template, . is the parameter to the template. (So when you include or template, you need to pass . down as that parameter.)
    • In a range loop, . is the current item being iterated on.
    • In a with block, . is the matched item if it exists.

    In particular, the interaction with range can be tricky. Let's look at a simplified version of your loop:

    # {{ . }}
    {{- range tuple "config1.toml" "config2.toml" "config3.toml" }}
    - {{ . }}
    {{- end }}
    

    Outside the range loop, . is probably the top-level Helm object. But inside the range loop, . is the file name (each value from the tuple in turn). That's where you need to save values from . into local variables:

    {{/* We're about to invalidate ., so save .Files into a variable. */}}
    {{- $files := .Files }}
    
    {{- range tuple "config1.toml" "config2.toml" "config3.toml" }}
    {{/* This . is the filename from the "tuple" call */}}
    {{ . }}: |-
      {{/* Call .Get, from the saved $files, passing the filename .
           as the parameter */}}
      {{ $files.Get . }}
    {{- end }}