Search code examples
kubernetes-helm

In Helm, can I loop over a set of manifests in my Helm chart?


I am developing a Helm chart for an application that I've developed. One of the components requires multiple manifests that each need to be deployed multiple times. So far, I have taken care of that by putting all of those manifests into a single file, then looping at the top. Something like this:

{{- range .Values.thingies }}
---
apiVersion: v1
kind: ConfigMap
...

---
apiVersion: v1
kind: PersistentVolume
...

---
apiVersion: v1
kind: PersistentVolumeClaim
...

As the list of manifests grows, though, this is starting to feel a little cumbersome. Is there a way to loop over a directory of manifests rather than needing to put everything into the same file?

Another option that I'm not fond of is to implement the same loop in each of the individual manifests.


Solution

  • You can use "Named Templates".

    Put this in one file (i.e. _cm.tpl):

    {{- define "mychart.configmap" }}
    apiVersion: v1
    kind: ConfigMap
    ...
    {{- end }}
    

    and use it in another (i.e. thingies.yaml):

    {{- range .Values.thingies }}
    {{- template "mychart.configmap" }}
    {{- }}
    

    Documentation for Named Templates: https://helm.sh/docs/chart_template_guide/named_templates/