在 Helm 的 v3 文档中:Accessing Files Inside Templates,作者给出了 3 个属性(toml)文件的示例;其中每个文件只有一个键/值对。
configmap.yaml 看起来像这样。为了简单起见,我只添加一个 config.toml。
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-config
data:
{{- $files := .Files }}
{{- range tuple "config.toml" }}
{{ . }}: |-
{{ $files.Get . }}
{{- end }}
这工作得很好,直到我在 config.toml 文件中添加了 second 行。
配置.toml
replicaCount=1
foo=bar
然后我收到错误:
INSTALLATION FAILED: YAML parse error on deploy/templates/configmap.yaml: error converting YAML to JSON: yaml: line 9: could not find expected ':'
任何想法将不胜感激。 谢谢
Helm 将读取该文件,但它(无论好坏)是一个 text 模板引擎。它不明白您正在尝试编写 YAML 文件,因此它不会为您提供帮助。这实际上就是为什么您会在野外看到这么多带有
{{ .thing | indent 8 }}
或 {{ .otherThing | toYaml }}
的模板 - 因为 您需要帮助 Helm 了解它在什么上下文中发出 text
indent
过滤器,因为您当前的模板有两个空格用于键缩进级别,还有两个空格用于值块标量
data:
{{- $files := .Files }}
{{- range tuple "config.toml" }}
{{ . }}: |-
{{ $files.Get . | indent 4 }}
{{/* notice this ^^^ template expression is flush left,
because the 'indent' is handling whitespace, not the golang template itself */}}
{{- end }}
.AsConfig
部分,这似乎更有可能是您真正想要发生的事情,并且需要更少的indent
数学