18

I have a values.yaml that has the following:

abc:
  env:
  - name: name01
    value: value01
  - name: name02
    value: value02

and I have another values file values-dev.yaml that I add when installing using -f and it has:

abc:
  env:
  - name: name03
    value: value03

and using range I list them in my template. My hope was that the list would become like this after both files are applied:

abc:
  env:
  - name: name01
    value: value01
  - name: name02
    value: value02
  - name: name03
    value: value03

but the values-dev.yaml values will override the one in the values.yaml and it becomes:

abc:
  env:
  - name: name03
    value: value03

How can I achieve merging these 2 lists with the same field names from different values files?

arash moeen
  • 4,533
  • 9
  • 40
  • 85
  • Does this thread clarify a bit the usage of multiple values files https://stackoverflow.com/a/56653384/11714114 ? Did you try to pass those values files in a different order ? In this case you'll probably get final result containing only name01 and name02. It looks like currently there is no way to pass multiple value files so that everything is merged rather than overridden. – mario Dec 19 '19 at 17:24

1 Answers1

15

Short answer is, you can not merge lists.

In your case abc.env is the key and the value is a list. Let me re-write your first values file in an equivalent notation and it will be more clear:

abc:
  env: [{name: name01, value: value01}, {name: name02, value: value02}]

So Helm is doing what is expected, overriding the key abc.env with the last provided one.

Solution is re-structuring your values files, like this:

abc:
  env:
    name01: value01
    name02: value02

This way, you can merge and override your values files as desired. This way, it's also much easy to override a single value with command line flag, for example:

--set abc.env.name01=different

With some Helm magic, it's easy to pass those values as environment variables to your pods:

...
  containers:
  - name: abc
    image: abc
    env:
    {{- range $key, $value := .Values.abc.env }}
    - name: {{ $key }}
      value: {{ $value | quote }}
    {{- end }}  
r a f t
  • 411
  • 3
  • 9