I have a list of 50 cities, trying to reuse the same deployment template to spin up 5 pods each containing 10 containers (representing 10 cities ) in it.
I managed to create a pod (50 pods) deployment per city. Couldn't figure out how to split 10 cities per pod.
sample code
{{ if .Values.enabled }}
{{- range $i, $value := .Values.cities }}
kind: Pod
apiVersion: v1
metadata:
name: "nginx-blob-{{ . }}"
spec:
nodeSelector:
"kubernetes.io/os": linux
containers:
- image: mcr.microsoft.com/oss/nginx/nginx:1.17.3-alpine
name: "nginx-blob-{{ $value }}"
---
{{- end }}
{{ end }}
For starters, you're going to need a nested loop -- one to create multiple pods, and then within each pod you'll need a loop to create multiple containers.
For the outer loop, we can use
untilStepto loop over the number of cities in steps of 10, like this:Assuming there are 50 cities in our list, that gets us:
For the inner loop, we need to go from
$beginningOfRangeto$beginningOfRange + 10, which we can do like this:Which gets us:
As you can see, we now have the basic structure for our template. If we modify your original template, we get something like this:
Assuming that
.Values.citieshas a list of US state capitals, this will generate output like: