Home > Mobile >  Using a yaml array with helm template
Using a yaml array with helm template

Time:02-04

I have a directory structure representing a Helm chart as follows:

Chart.yaml
values.yaml
templates/
  template.tpl

values.yaml:

foo: ["bar", "baz"]

FOO:
 - BAR
 - BAZ

templates/template.tpl:

thing1: {{ .Values.foo }}
thing2: {{ .Values.FOO }}

Output of running helm template . in this directory. (Helm version v3.6.3)

---
# Source: test/templates/template.tpl
thing1: [bar baz]
thing2: [BAR BAZ]

You can see here that both thing1 and thing2 map to YAML arrays containing one string each, namely the strings "bar baz" and "BAR BAZ".

I would like the items in the array to still be separate stings after templating. But the built-in functions that I have found in the helm template language documentation (like {{ list .Values.foo }}) don't do anything productive.

Can someone point me at how to properly template YAML arrays of strings?

CodePudding user response:

Helm uses Go templates and Go templates doesn't know YAML. Thus it will just emit the sequence (YAML doesn't have arrays) in Go's default format, which happens to be [<item> ...].

You need to tell Helm to convert the values to YAML format:

thing1: {{ .Values.foo | toYaml | nindent 2 }}
thing2: {{ .Values.FOO | toYaml | nindent 2 }}

toYaml does the actual conversion (it's kind-a undocumented), nindent 2 adds a line break and then indents every line 2 spaces. This is important when toYaml chooses to render your input as multiple lines, which you don't have any control over.

  •  Tags:  
  • Related