0

In helm I have the following .Values

mqtt:
  topics:
    - /tenant/+/#
    - /otherStuff/
    - /importantStuff/

tenants:
  - id: tenantA
    name: Tenant A
  - id: tenantB
    name: Tenant B

Instead of the wildcard topic /tenant/+/# I reather would like to have the topics tenant specific:

  • /tenant/tenantA/#
  • /tenant/tenantB/#

So I tried what would make sense to me should work:

mqtt:
  topics:
{{- range .Values.tenants }}
    - /tenant/{{ .id }}/#
{{ end }}
    - /otherStuff/
    - /importantStuff/

This leads to multiple errors

A block sequence may not be used as an implicit map key
Implicit keys need to be on a single line
Implicit map keys need to be followed by map values

While not completly understanding the errors, it seems to me that this approach does not work or I am doing something completely wrong.

How can I loop in my .Values over a range and generate to generate a list?

Helm Playground (Actually shows a different error as visual studio code does, but probably the same lead)

Herr Derb
  • 4,977
  • 5
  • 34
  • 62

2 Answers2

1

Helm doesn't allow this. The contents of values.yaml must be a flat YAML file, and no templating is applied to it.

Helm - Templating variables in values.yaml addresses a simpler case of this for simple strings using the Helm tpl function to render a string. You could do that here, but it's trickier: the values.yaml value must be a string, and you'll have to ask Helm to parse the result of tpl with fromYaml.

# values.yaml
mqtt:
  #       v YAML "block scalar" marker to create a multiline string
  topics: |
    {{- range .Values.tenants }}
    - /tenant/{{ .id }}/#
    {{ end }}
    - /otherStuff/
    - /importantStuff/
#^^^ also note all contents are indented
{{- $topics := tpl .Values.mqtt.topics . | fromYaml }}
{{- range $topic := $topics }}...{{ end }}
David Maze
  • 130,717
  • 29
  • 175
  • 215
  • I use [Helmfile](https://helmfile.readthedocs.io/) for some multi-chart scenarios. It's notable here because it _does_ allow applying a `values.yaml.gotmpl` file; this also gets confusing because Helmfile has its own notion of `.Values` that are used to render the file that will become `.Values` when the chart is installed. – David Maze May 23 '23 at 11:00
0

Helm doc

The curly brace syntax of template declarations can be modified with special characters to tell the template engine to chomp whitespace. {{- (with the dash and space added) indicates that whitespace should be chomped left, while -}} means whitespace to the right should be consumed. Be careful! Newlines are whitespace!

mqtt: 
  topics:
    {{- range .Values.tenants }}
    - /tenant/{{ .id }}/#
    {{- end }}
    - /otherStuff/
    - /importantStuff/

values.yaml

tenants:
  - id: tenantA
    name: Tenant A
  - id: tenantB
    name: Tenant B

output

mqtt: 
  topics:
    - /tenant/tenantA/#
    - /tenant/tenantB/#
    - /otherStuff/
    - /importantStuff/

helm-playground

z.x
  • 1,905
  • 7
  • 11