Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Helm: generate comma separated list

Using Helm templates, I'm trying to generate a list of server names based on a number in values.yaml. The dot for this template is set to the number (its a float64).

{{- define "zkservers" -}}
{{- $zkservers := list -}}
{{- range int . | until -}}
{{- $zkservers := print "zk-" . ".zookeeper" | append $zkservers -}}
{{- end -}}
{{- join "," $zkservers -}}
{{- end -}}

For an input of, say, 3 I'm expecting this to produce:

zk-0.zookeeper,zk-1.zookeeper,zk-2.zookeeper

It produces nothing.

I understand that the line within the range block is a no-op since the variable $zkservers is a new variable each time the loop iterates. It is not the same variable as the $zkservers in the outer scope.

I hope the intention is clear of what I want to do. I am at a loss how to do it.

Anyone know how to do this with Helm templates?

like image 564
alfred Avatar asked Dec 06 '17 07:12

alfred


4 Answers

For those from 2020+ it now can be achieved as simple as that:

{{ join "," .Values.some.array }}

It produces the correct output: value: "test1,test2,test3"

At least it works with (more or less) recent versions of helm-cli:

Client: &version.Version{SemVer:"v2.16.2", GitCommit:"bbdfe5e7803a12bbdf97e94cd847859890cf4050", GitTreeState:"clean"}
like image 82
Illya Doos Avatar answered Nov 08 '22 04:11

Illya Doos


Another quick way of doing it:

{{- define "helm-toolkit.utils.joinListWithComma" -}}
{{- $local := dict "first" true -}}
{{- range $k, $v := . -}}{{- if not $local.first -}},{{- end -}}{{- $v -}}{{- $_ := set $local "first" false -}}{{- end -}}
{{- end -}}

If you give this input like:

test:
- foo
- bar

And call with:

{{ include "helm-toolkit.utils.joinListWithComma" .Values.test }}

You'll get the following rendered:

foo,bar

This is from OpenStack-Helm's Helm-toolkit chart, which is a collection of utilities for similar purposes.

like image 43
Pete Birley Avatar answered Nov 08 '22 03:11

Pete Birley


I faced with the same problem and your solution with dictionary saved my day. It's a good workaround and it can be just a little simpler:

{{- define "zkservers" -}}
{{- $zk := dict "servers" (list) -}}
{{- range int . | until -}}
{{- $noop := printf "zk-%d.zookeeper" . | append $zk.servers | set $zk "servers" -}}
{{- end -}}
{{- join "," $zk.servers -}}
{{- end -}}
like image 18
DmiBay Avatar answered Nov 08 '22 02:11

DmiBay


You're currently defining a new varialbe in the scope of the list while you want to alter the existing value.

In order to fix your bug, you only need to change the way you assign the value to $zkservers:

    - {{- $zkservers := print "zk-" . ".zookeeper" | append $zkservers -}}
    + {{- $zkservers = print "zk-" . ".zookeeper" | append $zkservers -}}  

which gives you the following script:

{{- define "zkservers" -}}
{{- $zkservers := list -}}
{{- range int . | until -}}
{{- $zkservers = print "zk-" . ".zookeeper" | append $zkservers -}}
{{- end -}}
{{- join "," $zkservers -}}
{{- end -}}

With this slight modification, {{ include "zkservers" 3 }}gives you the output zk-0.zookeeper,zk-1.zookeeper,zk-2.zookeeper

like image 8
Pierre Michard Avatar answered Nov 08 '22 02:11

Pierre Michard