Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

helm iterate over range

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!

like image 549
BigBoss Avatar asked May 20 '19 16:05

BigBoss


People also ask

What does {{ }} mean in Helm?

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.

What is TPL in Helm?

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.

Does Helm use Jinja?

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.

Can we use and operator in Helm yaml files?

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 ) ).


1 Answers

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 }}
like image 70
BigBoss Avatar answered Sep 29 '22 18:09

BigBoss