Search code examples
kubernetes-helmgo-templates

Escape dot in Helm template


In values.yaml I have:

key1:
  key2.yaml:
    key3:
    - name
      value
    - name
      value

Inside a template, how can I get to key3? {{ .Values.key1.key2.yaml.key3 }} does not work because key2.yaml in this context corresponds to

key1:
  key2:
    yaml:
      key3:
      ...

Escaping the . with \ results in:

$ helm template .
Error: parse error at (...): bad character U+005C '\'

Solution

  • The core Go text/template language includes an index function, which can look up an arbitrary item in a map by string key (or an array by numeric key). The key can be any valid template expression, including a string literal. index can take multiple parameters to do nested lookups.

    So, you can use index to do that specific lookup like:

    {{ index .Values "key1" "key2.yaml" "key3" }}
    

    If that mapping is supposed to be an arbitrary list of files and their contents, the template range function will return the keys and values, and you don't necessarily need the lookups.

    {{- range $filename, $contents := .Values.key1 }}
    $filename: |-
    {{ $contents | toYaml | indent 2 }}
    {{ end -}}