1

Is it possible, within a chart to create a single string which is a comma-separated representation (similar to using the ",".join() command in Python) of strings with a common prefix and a variable suffix?

For example, I have a CLI application that requires an argument like so via the extraArgs parameter in a pod definition:

extraArgs: >-
  -M {{ $.Values.global.hostname }}/100

I now have to modify this value to be over a range (i.e. from {{$.Values.global.minval}} to {{$.Values.global.maxval}}, inclusive). So, for a minval=100 and maxval=105, my chart needs to now become (note the lack of a trailing comma, and no spaces other than the space after -M):

extraArgs: >-
  -M {{ $.Values.global.hostname }}/100,{{ $.Values.global.hostname }}/101,{{ $.Values.global.hostname }}/102,{{ $.Values.global.hostname }}/103,{{ $.Values.global.hostname }}/104,{{ $.Values.global.hostname }}/105

Is there some way I can execute this in a range/loop in my chart? I have several instances of this chart that will use different min/max values, and I'd like to automate this tedious task as much as I can (additionally, I do not have access to the app's source, so I can't change the CLI interface to the application).

In Python, I could accomplish this roughly by:

minval = 100
minval = 105
s = "-M "
L = []
for i in range(minval, maxval+1):
    L.append("{{{{ $.Values.global.hostname }}}}/{}".format(i))
s = s + ",".join(L)
# print(s)

I'm not sure where to begin doing this in a Helm template beyond starting with the range() function.

Uther
  • 13
  • 1
  • 4
  • You could use [`range untilstep`](https://github.com/helm/helm/issues/1055) for part of this, and [this](https://stackoverflow.com/questions/47668793/helm-generate-comma-separated-list) to generate the comma separated list. Not sure where you'll dump your intermediate list so it doesn't end up needlessly in the final chart. – Cloud Mar 23 '20 at 21:34
  • Does this answer your question? [Helm: generate comma separated list](https://stackoverflow.com/questions/47668793/helm-generate-comma-separated-list) – coderanger Mar 23 '20 at 21:42

1 Answers1

2

Helm includes the sprig library of template functions which contains untilStep and join.

There is no concept of a map or each operator in sprig yet so you can construct the list in a range loop to be joined later (from here)

{{- $minval :=  int .Values.minval -}}
{{- $maxval :=  int .Values.maxval | add1 | int -}}
{{- $args := list -}}
{{- range untilStep $minval $maxval 1 -}}
{{-   $args = printf "%s/%d" $hostname . | append $args -}}
{{- end }}
extraArgs: '-M {{ $args | join "," }}'
Matt
  • 68,711
  • 7
  • 155
  • 158