从舵配置获取字符串数组

时间:2018-10-16 16:21:36

标签: kubernetes kubernetes-helm

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

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

上面的输出是:

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

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

2 个答案:

答案 0 :(得分:4)

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

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

收件人:

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

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

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

这是the grafana chart does的作用,它首先迫使用户以所需格式指定列表。也许您希望从helm值中获取一个数组并将其转换为所需的格式,在我看来,这是json格式。为此,您可以遵循example of the vault chart。因此,configmap行变为:

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

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

另一种方法是:

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

答案 1 :(得分:0)

有时候,根本原因是您忘记了用引号将每个项目括起来:

organizations:
  {{- range .Values.organizations }}
    - {{ . | quote }} # <-- SEE Here
  {{- end }}