我有一个带有属性 2darrayIPs 的 Helm chart 。该属性从values.yaml 文件中获取值,该文件又通过helm 安装命令给出
helm-chart/templates/main.yaml
2darrayIPs: {{ .Values.2darrayIPs }}
helm-chart/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
我哪里出错了?
最佳答案
如果您的模板尝试写出比简单字符串更复杂的内容,则默认的 {{ .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 }}
关于arrays - YAML 中的二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70093553/