helm iterate over range

5/20/2019

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!

-- BigBoss
kubernetes
kubernetes-helm
templates

1 Answer

5/20/2019

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 }}
-- BigBoss
Source: StackOverflow