Let's say I'm creating a helm template in which I want the eventual deployments to be able to execute a command to their liking. For this example, I'll use use the touch
command.
In my templates file, I'd write it down like this:
templates.yaml
...
foo:
{{- if .Values.foo.command }}
exec:
command: {{ .Values.foo.command }}
{{- end }}
...
Then following that, I'd have my values file:
values.yaml
...
foo:
command: ["touch", "bar.txt"]
...
When I'm checking the resulting template of the above, the command gets templated as:
foo:
exec:
command: [touch bar.txt]
Thus, there are no quotes/commas in its command. Would this still execute as intended? Or are commas and quotes necessary in order for this to execute.
Thanks!
CodePudding user response:
This will not work as expected. The output syntax you show is a valid YAML list, but only containing a single shell word, and so it is looking for a command named touch bar.txt
, where the space would be part of the filename in /usr/bin
.
The Go text/template engine that Helm uses isn't especially aware of YAML. When you directly write out a value like this, it uses a default formatting that's not especially useful. Helm has a couple of lightly-documented extensions to write out values in a more useful format. For your example I might use:
foo:
{{- with .Values.foo.command }}{{/* like "if", but also rebinds `.` */}}
exec:
command:
{{ toYaml . | indent 6 }}
{{- end }}
This should write out:
foo:
exec:
command:
- touch
- bar.txt
###### note: 6 spaces here matches `indent 6`
You may be able to fit command: {{ toJson .Values.foo.command }}
into a single line. In either case it may not have the exact formatting that's in the values.yaml
file, since Helm parses the YAML at startup time and then writes out the equivalent structure.