Search code examples
kubernetes-helm

How to render render code block from values file in helm charts


I've a block in values.yaml like

extraHosts:
- hosts:
  - domain1.tld
  port:
    name: sftp
    number: 22
    protocol: TCP
- hosts:
  - domain2.tld
  port:
    name: sftp
    number: 2222
    protocol: TCP

I want to template this whole block like

servers:
- hosts:
  - domain1.tld
  port:
    name: sftp
    number: 22
    protocol: TCP
- hosts:
  - domain2.tld
  port:
    name: sftp
    number: 2222
    protocol: TCP

I tried

servers:
{{- range .Values.extraHosts }}
- {{ . }}
{{- end }}

Solution

  • Helm has an undocumented toYaml function that can dump out an object from .Values as valid YAML.

    servers:
    {{ toYaml .Values.extraHosts | indent 2 }}
    

    (In this example the indent isn't strictly necessary; if the object was a map rather than a list then it would be.)