2

我有一个带有属性 2darrayIPs 的舵图。此属性从 values.yaml 文件中获取值,该文件又通过 helm 安装命令给出

掌舵图/模板/main.yaml

2darrayIPs: {{ .Values.2darrayIPs }}

掌舵图/values.yaml

2darrayIPs: [[]]  -- empty array, this value is given via installation command

我在安装 helm 图表时通过 helm 命令传递 2d 数组。

helm install ..... -f val.yaml

val.yaml

2darrayIPs:
  - - 1.1.1.1
    - 2.2.2.2
  - - 3.3.3.3
    - 4.4.4.4

安装 helm chart 时出现此错误:

Error: YAML parse error on templates/main.yaml: error converting YAML to JSON: yaml: did not find expected ',' or ']'

如果我如下给出一个数组,则安装成功,但在我的日志中,我得到一个只有一个值而不是两个值的数组:

[[1.1.1.1 2.2.2.2]]

val.yaml

2darrayIPs:
  - - 1.1.1.1
    - 2.2.2.2

我哪里错了?

4

2 回答 2

2

如果你的模板试图写出比简单字符串更复杂的东西,那么默认的{{ .Values.name }}序列化是 Go-native 的东西,它并不是特别有用。Helm 包括一个toJson模板函数,还有一个未记录的函数,toYaml它可以将这些以更有用的格式写出来。

# as an array of arrays, in JSON syntax
2darrayIPs: {{ .Values.2darrayIPs | toJson }}
# as an array of arrays, in expanded YAML syntax
# (identical to the previous, but `helm template` output will be
# easier to read)
2darrayIPs: {{- .Values.2darrayIPs | toYaml | nindent 2 }}
# as a YAML-encoded string; for example in a ConfigMap
2darrayIPs: |-
{{ .Values.2darrayIPs | toYaml | indent 2 }}
于 2021-11-24T10:57:42.340 回答
0

这是有效的 yaml。如果可能,您应该在此处使用的 yaml 解析器的存储库上提出问题。同时,Yaml 是 JSON 的超集,因此您可以使用纯 JSON 2D 数组:

2darrayIPs: [["1.1.1.1","2.2.2.2"]]
于 2021-11-24T09:38:39.750 回答