Helm 的 v3 示例不显示多行属性。获取 YAML 到 JSON 解析错误

问题描述 投票:0回答:1

在 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 ':'

任何想法将不胜感激。 谢谢

kubernetes kubernetes-helm
1个回答
4
投票

Helm 将读取该文件,但它(无论好坏)是一个 text 模板引擎。它不明白您正在尝试编写 YAML 文件,因此它不会为您提供帮助。这实际上就是为什么您会在野外看到这么多带有

{{ .thing | indent 8 }}
{{ .otherThing | toYaml }}
的模板 - 因为 您需要帮助 Helm 了解它在什么上下文中发出 text

因此,在您的具体情况下,您需要值为 4 的

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
数学

© www.soinside.com 2019 - 2024. All rights reserved.