Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a yaml array with helm template

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 strings 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?

like image 392
turbulencetoo Avatar asked Sep 07 '26 15:09

turbulencetoo


1 Answers

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 (see docs), 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.

like image 159
flyx Avatar answered Sep 12 '26 15:09

flyx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!