且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从 helm 配置中获取字符串数组

更新时间:2023-11-07 08:46:28

获得您正在寻找的输出的一种方法是更改​​:

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