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

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

问题描述

最终我试图获得一个字符串数组,例如['foo', 'bar'] 在我的 helm 配置中的 js 应用程序中.

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

如何修改我的 helm 配置,使 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 将其视为单个字符串.我们碰巧知道它包含数组内容,但 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:

组织:{{ .Values.organizations |缩进 4 }}

它的作用是grafana图表的作用/a> 因为它首先强制用户以所需的格式指定列表.也许您更喜欢从 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:

组织:{{ .Values.organizations |toJson |缩进 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 }}

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

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