Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get array of strings from helm config

Ultimately i'm trying to get an array of strings e.g. ['foo', 'bar'] in my js app from my helm config.

./vars/dev/organizations.yaml

...
organizations:
  - 'foo'
  - 'bar'
...

./templates/configmap.yaml

...
data:
  organizations.yaml: |
    organizations: "{{ toYaml .Values.organizations | indent 4 }}"
...

./templates/deployment.yaml

...
containers:
    args:
       - "--organizations-config"
       - "/etc/app/cfg/organizations.yaml"
...

index.js

...
const DEFAULT_ORGANIZATIONS_PATH = './vars/local/organizations.yaml'
const program = require('commander')

program
  .option(
    '--organizations-config <file path>',
    'The path to the organizations config file.', DEFAULT_ORGANIZATIONS_PATH)
  .parse(process.argv)

function readConfigs () {
  return Promise.all(configs.map(path => {
    return new Promise((resolve, reject) => {
      fs.readFile(path, (err, data) => {
        err ? reject(err) : resolve(yaml.safeLoad(data))
      })
    })
  }))
}

readConfigs()
  .then(configs => {
    let organizationsConfig = configs[3]

    console.log('organizationsConfig = ', organizationsConfig)
    console.log('organizationsConfig.organizations = ', organizationsConfig.organizations)
...

The output from above is:

organizationsConfig =  { organizations: '    - foo - bar' }
organizationsConfig.organizations =      - foo - bar

How can I modify my helm config so that organizationsConfig.organizations will be ['foo', 'bar']

like image 750
Catfish Avatar asked Oct 16 '18 16:10

Catfish


Video Answer


1 Answers

One way to get the output you're looking for is to change:

...
organizations:
  - 'foo'
  - 'bar'
...

To:

organizations: |
  [ 'foo', 'bar']

So helm treats it as a single string. We happen to know that it contains array content but helm just thinks it's a string. Then we can set that string directly in the configmap:

organizations: {{ .Values.organizations | indent 4 }}

What this does is what the grafana chart does in that it forces the user to specify the list in the desired format in the first place. Perhaps you'd prefer to take an array from the helm values and convert it to your desired format, which appears to me to be json format. To do that you could follow the example of the vault chart. So the configmap line becomes:

organizations: {{ .Values.organizations | toJson | indent 4 }}

Then the yaml that the user puts in can be as you originally had it i.e. a true yaml array. I tried this and it works but I notice that it gives double-quoted content like ["foo","bar"]

The other way you can do it is with:

organizations:
  {{- range .Values.organizations }}
    - {{ . }}
  {{- end }}
like image 183
Ryan Dawson Avatar answered Nov 04 '22 21:11

Ryan Dawson