How to loop different templates in Helm for Kubernetes?

3/14/2019

I want to deploy multiple Deployments of Pods with different images, ports, etc. but with very similar other properties. So I want to declare a single deployment.yaml file that looks something like this

{{- range .Values.types }}
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
...
{{- end }}

Where my values.yaml is

types:
  - foo
  - bar
  - baz

However, this only spins up a single Kubernetes Deployment when I helm install because everything is in one template file. Any ideas on how to do this?

-- Math is Hard
kubernetes
kubernetes-helm

1 Answer

3/14/2019

Kubernetes generally uses YAML syntax, and that allows multiple "documents" to be in a single physical file with a --- delimiter before each one. Helm in turn generally operates by applying the templating to produce a plain-text file and in effect feeding it to kubectl apply.

The upshot of this is that if you start each Kubernetes object description with the --- start-of-document delimiter, it should work:

{{- range .Values.types }}
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
...
{{- end }}
-- David Maze
Source: StackOverflow