I can not find a way to iterate over a range in helm templating. I have the next definition in my values.yaml:
ingress:
app1:
port: 80
hosts:
- example.com
app2:
port: 80
hosts:
- demo.example.com
- test.example.com
- stage.example.com
app3:
port: 80
hosts:
- app3.example.com
And i want to generate the same nginx ingress rule for each mentioned host with:
spec:
rules:
{{- range $key, $value := .Values.global.ingress }}
- host: {{ $value.hosts }}
http:
paths:
- path: /qapi
backend:
serviceName: api-server
servicePort: 80
{{- end }}
But it generates wrong hosts:
- host: [example.com]
- host: [test.example.com demo.example.com test.example.com]
Thanks for the help!
The Helm template syntax is based on the Go programming language's text/template package. The braces {{ and }} are the opening and closing brackets to enter and exit template logic.
Using the 'tpl' Function The tpl function allows developers to evaluate strings as templates inside a template. This is useful to pass a template string as a value to a chart or render external configuration files.
Helm uses Go templates to define Kubernetes (yaml) manifests. We were already unsatisfied by using Jinja and we did not see a huge improvement from our previous system, the main reason being: YAML files are not suitable to be managed by text templating frameworks.
As indicated in the Helm documentation on operators: For templates, the operators ( eq , ne , lt , gt , and , or and so on) are all implemented as functions. In pipelines, operations can be grouped with parentheses ( ( , and ) ).
I've finally got it working using:
spec:
rules:
{{- range $key, $value := .Values.global.ingress }}
{{- range $value.hosts }}
- host: {{ . }}
http:
paths:
- path: /qapi
backend:
serviceName: api-server
servicePort: 80
{{- end }}
{{- end }}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With