Helm chart incorrectly array render

743 Views Asked by At

I did not figured out how to proper render nested template array of objects

values.yaml


persistence:
  volumes:
    - name: api-manutencao-env2
      configMap:
        name: api-manutencao-env2
    - name: api-manutencao-env2
      configMap:
        name: api-manutencao-env2

deployment.yaml (template)

      {{- if .Values.persistence }}
      volumes:
      {{- range .Values.persistence.volumes }}
      {{ . | toYaml | nindent 8 }}
      {{- end }}
      {{- end }}

Result:

      volumes:
      
        configMap:
          name: api-manutencao-env2
        name: api-manutencao-env2
      
        configMap:
          name: api-manutencao-env2
        name: api-manutencao-env2
---

I render its content reversed and break in the array structure not being add the - and the correct ident.

I've tried https://kb.novaordis.com/index.php/Helm_Template_range and it did not work either.

Thanks

1

There are 1 best solutions below

0
On

The easiest way to do this is to call toYaml on the entire list, rather than iterating through it on your own.

{{- with .Values.persistence }}
      volumes:
{{ toYaml .volumes | indent 8 }}
{{- end }}

If you iterate through the list with range, and you want the YAML list markers, you need to emit them yourself.

{{- with .Values.persistence }}
      volumes:
{{- range .volumes }}
        - {{ toYaml . | indent 10 | trim }}
{{- end }}
{{- end }}

indent puts spaces in front of every line including the first one, so in this construct you need to trim leading and trailing whitespace, causing the first line of the rendered output to appear immediately after the hyphen.

The order of items in a YAML mapping isn't important, and at the underlying Go map[string]interface{} layer it isn't preserved. In the Go text/template language range is documented as iterating over maps in sorted key order. Helm's toYaml extension isn't especially well-documented; toJson at least appears in the documentation but it doesn't say anything about key order. It doesn't matter whether name: or configMap: comes first and Helm won't remember the ordering from values.yaml; there's nothing you can do to control this.