'Loop through comma separated variable

I have a shell script that triggers a helm upgrade. I have a comma-separated variable in the script that I want to use in help to loop through via range. I tried a comma separate variable but it does not work.

secretkey="aws.id,aws.pwd"
helm upgrade <chartName> ./charts/xxx -f values.yaml --install --set a.secretlist=$secretkey

And I want to loop through this variable in helm. Something like this

{{- range $v := .Values.a.secretlist }}
- name: {{ $v }}
  valueFrom:
    secretKeyRef:
      name: secretname
      key: {{ $v }}
      optional: true
{{ end }}

I know I can loop through a yaml, but is there any way to loop through a shell variable?



Solution 1:[1]

Helm does not support setting non-existent array elements, you can assign values by passing the entire array or set the corresponding number of elements first, and then modify it by --set a.secretlist[i]=xxx.

If you want to set by --set, you need to pass array values you can use curly braces (unix shell require quotes):

--set a.secretlist={"3"\,"4"\,"5"}

Notice!!!

The separator of the array elements , you must remember to add a \

Demo

values.yaml

a:
  secretlist:
    - "1"
    - "2"

tpl.yaml

  {{- range $v := .Values.a.secretlist }}
  - name: {{ $v }}
    valueFrom:
      secretKeyRef:
        name: secretname
        key: {{ $v }}
        optional: true
  {{ end }}

cmd

secretkey={"aws.id"\,"aws.pwd"} 
helm upgrade <chartName> ./charts/xxx -f values.yaml --install --set a.secretlist=$secretkey

output

  - name: aws.id
    valueFrom:
      secretKeyRef:
        name: secretname
        key: aws.id
        optional: true
  
  - name: aws.pwd
    valueFrom:
      secretKeyRef:
        name: secretname
        key: aws.pwd
        optional: true

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 z.x