从舵配置获取字符串数组 [英] Get array of strings from helm config

查看:88
本文介绍了从舵配置获取字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最终,我正在尝试获取字符串数组,例如通过头盔配置在我的js应用中['foo', 'bar'].

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

./vars/dev/organizations.yaml

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

./templates/configmap.yaml

./templates/configmap.yaml

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

./templates/deployment.yaml

./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)
...

上面的输出是:

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

如何修改头盔配置,使organizationsConfig.organizations成为['foo', 'bar']

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

推荐答案

获取所需输出的一种方法是更改​​:

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

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

收件人:

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

因此helm将其视为单个字符串.我们碰巧知道它包含数组内容,但是舵只认为它是一个字符串.然后我们可以直接在configmap中设置该字符串:

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

这是格拉那娜图所做的,因为它强制用户首先以所需格式指定列表.也许您希望从helm值中获取一个数组并将其转换为所需的格式,在我看来,该格式为json格式.为此,您可以按照保险库示例图表.因此,configmap行变为:

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

然后,用户输入的yaml可以与您最初拥有的一样,即真正的yaml数组.我尝试了此方法,但它确实有效,但我注意到它提供了双引号内容,例如["foo","bar"]

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

另一种方法是:

organizations:
  {{- range .Values.organizations }}
    - {{ . }}
  {{- end }}

这篇关于从舵配置获取字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆