Helm - toYaml indenting dictionary/map correctly

Viewed 21

My values.yaml file:

podSelector:
  app: myApp
  app.kubernetes.io/instance: myApp

My templates/temp.yaml file

apiVersion: something/v1beta
kind: SomeResource
metadata:
  name: someName
spec:  
  selection:
    labels:
  {{ .Values.podSelector | toYaml | indent 4 }}

helm template . gives me :-

apiVersion: something/v1beta
kind: SomeResource
metadata:
  name: someName
spec:
  selection:
    labels:
        app: myApp
  app.kubernetes.io/instance: myApp   #This is not indented

As you can see, the second key/value pair is not indented. How can I achieve correct indentation? The labels must all be indented the same way

1 Answers

In your output you want:

spec:
  selector:
    labels:
      app: ...
#^^^^^
# there are exactly 6 spaces here

In your Helm chart, the {{ ... | indent ... }} line needs to start at the first column, and the indentation value needs to match the number of spaces.

spec:
  selector:
    labels:
{{ .Values.podSelector | toYaml | indent 6 }}
{{/* no spaces at the start of the line; indent 6, not 4 */}}

If you're using ... | toYaml | indent ..., the indent applies to every line of the YAML. If there are spaces before the {{ ... }}, these effectively indent only the first line extra, which will cause YAML parse errors.

Related