Get array of strings from helm config

对着背影说爱祢 提交于 2019-12-12 09:57:39

问题


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']


回答1:


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 }}


来源:https://stackoverflow.com/questions/52839920/get-array-of-strings-from-helm-config

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!