Right now, I have a values.yaml with a section that looks a bit like this:
...
imageName:
ports:
- containerPort: 7980
name: db0
protocol: TCP
- containerPort: 7981
name: db1
protocol: TCP
- containerPort: 7982
name: db2
protocol: TCP
- containerPort: 7983
name: db3
protocol: TCP
- containerPort: 7984
name: db4
protocol: TCP
- containerPort: 7985
name: db5
protocol: TCP
- containerPort: 7986
name: db6
protocol: TCP
- containerPort: 7987
name: db7
protocol: TCP
- containerPort: 7988
name: db8
protocol: TCP
- containerPort: 7989
name: db9
protocol: TCP
- containerPort: 7990
name: db10
protocol: TCP
...
I'd like to clean this up by creating a function in _helpers.tpl that will take the min port value (7980) and the max port value (7990) and create the structure for each one in that format.
I am wondering: Is this possible? I am having a lot of trouble with this and using the helpers file in general so if anyone can nudge me in the right direction with how to accomplish this, I would appreciate that too!
Thanks :)
This should be possible. Say you configure your chart with the number of ports and the starting port:
# values.yaml (or a `helm install -f` values file)
numberOfPorts: 11
startingPort: 7980
You can use the until
template function to turn that into a list of numbers:
{{- $dbs := until .Values.numberOfPorts }}
Now you can use the standard range
function to loop over that list. Inside the loop body the value will be an integer from 0 to numberOfPorts - 1
and you can produce the list item accordingly. Also note that range
takes over the .
special variable, so you'll need to save anything you need from .Values
outside the range
loop.
imageName:
ports:
{{- $startingPort := .Values.startingPort }}
{{- range $i := until .Values.numberOfPorts }}
- containerPort: {{ add $startingPort $i }}
name: db{{ $i }}
protocol: TCP
{{- end }}