Sub topic 11
Emitting nested YAML with toYaml and nindent
This chapter assumes you can read Go template actions, use pipelines, and control whitespace with {{-. It also assumes you have a chart with a values.yaml and a _helpers.tpl, as in the earlier chapters.
The problem with nested values
Some values are single strings or numbers, and you can print them with {{ .Values.replicaCount }}. Others are whole YAML blocks, such as resources, nodeSelector or tolerations. Printing a map directly gives Go's internal format (map[limits:map[cpu:500m]]), which is not YAML. You need to convert the value to YAML text and indent it to the right column.
Two functions do this work:
toYamlconverts a value (map or list) into a YAML string.nindent Nadds a newline, then indents every line of the text by N spaces.
A worked example
Suppose values.yaml contains:
resources:
limits:
cpu: 500m
memory: 256Mi
requests:
cpu: 100mThe deployment template renders it like this:
{{- with .Values.resources }}
resources:
{{- toYaml . | nindent 12 }}
{{- end }}Inside with, the dot (.) is .Values.resources, so toYaml . converts that map. The output is a multi-line string. nindent 12 puts a newline first, so the text starts on the line after resources:. It then indents each line by 12 spaces, which is two more than the resources: key.
The rendered result:
resources:
limits:
cpu: 500m
memory: 256Mi
requests:
cpu: 100mThe {{- before toYaml trims the whitespace and newline left after resources:. Without it, you would get a blank line or a stray space. The nindent call then supplies the one newline you want.
indent versus nindent
indent adds spaces to the start of each line but does not add a leading newline. nindent is indent with a newline in front. Use nindent when the block goes under a key on its own line. Use indent when the first line must stay on the current line, which is rare in manifests.
The same pattern works for labels from a helper:
labels:
{{- include "webapp.labels" . | nindent 4 }}Skipping empty blocks
In the default values.yaml, resources is {}. An empty map is falsy, so with skips the whole block and no resources: key appears. If you left out with, toYaml would output {} and you would get resources: followed by {}. That is valid YAML, but wrapping it in with keeps the manifest clean.
Common errors
| Symptom | Likely cause |
|---|---|
| `error converting YAML to JSON` | The indent number is wrong, so lines sit at the wrong column. |
| A blank line inside the manifest | A missing `-` on `{{-`, so the newline before the action stays. |
| A key appears twice or is nested wrongly | The `nindent` value does not match the parent key's depth. |
| Output is `map[...]` | You printed the value without `toYaml`. |
To find the right number, count the spaces in front of the parent key and add two. Then run helm template to check the result.
Where the flow fits
Exercise
A template contains nodeSelector: indented 6 spaces, followed by {{- toYaml .Values.nodeSelector | nindent 8 }}. What does nindent 8 do, and why is the number 8?
Show answer
It puts a newline before the YAML text and indents every line by 8 spaces. The number is 8 because the child lines must sit two spaces deeper than the nodeSelector: key, which is at 6 spaces.
Exercise
Why do you wrap toYaml in with .Values.resources when resources defaults to {}?
Show answer
An empty map is falsy, so with skips the block. The manifest then has no resources: key, instead of resources: followed by {}.