Search code examples
yamlkubernetes-helm

Helm split global section


I have a helm values.yaml file like below

global:
  foo: bar
  foo1: bar1

random-chart:
  fooo: baar

My use case is to append values in both global and random-chart during run time. After appending values, the chart looks like this.

global:
  foo: bar
  foo1: bar1

random-chart:
  fooo: baar

global:
  secret: password

random-chart:
  secret1: password1

Since there's 2 different global and random-chart keys. Will it work as intended and is it a good practice to do that?


Solution

  • This probably won't work as intended.

    The YAML 1.2.2 spec notes (emphasis from original):

    The content of a mapping node is an unordered set of key/value node pairs, with the restriction that each of the keys is unique.

    And in discussing loading errors continues:

    ... mapping keys may not be unique ....

    So the YAML file you show has a mapping with two keys both named global and two keys both named random-chart, and that's not valid. Depending on the specific YAML library that's being used, this might be interpreted as a loading error, or the library might just pick the last value of global.

    In general, it's hard to work with YAML files using line-oriented shell tools, since there are so many syntactic variations. A dedicated library in a higher-level language will usually work better. For example, using the Python PyYAML library:

    import yaml
    with open('values.in.yaml', 'r') as f:
      values = yaml.safe_load(f)
    values['global']['secret'] = 'password'
    values['random-chart']['secret-1'] = 'password1'
    with open('values.out.yaml', 'w') as f:
      yaml.dump(values. f)
    

    Two other possibilities to consider: you can have multiple helm install -f options, so it's possible to write out a file with just the values you're adding, and those will be merged with other settings (you do not need to repeat the values from the chart's values.yaml file). Depending on your environment, you also may find it easier to dynamically write out JSON files, particularly if you don't need to re-read the base chart; setups like Jenkins or Javascript applications will have built-in JSON support, and valid JSON turns out to be valid YAML.