【问题标题】:Helm template doesnt work when binded to configmapHelm 模板在绑定到 configmap 时不起作用
【发布时间】:2020-04-08 09:27:41
【问题描述】:

我有一个将绑定到配置映射的文件。一旦我将一个 tpl 函数放入其中,当文件中包含其他行时,它就会停止工作。

我使用这个助手 tpl:

{{- define "call-nested" }}
{{- $dot := index . 0 }}
{{- $subchart := index . 1 | splitList "." }}
{{- $template := index . 2 }}
{{- $values := $dot.Values }}
{{- range $subchart }}
{{- $values = index $values . }}
{{- end }}
{{- include $template (dict "Chart" (dict "Name" (last $subchart)) "Values" $values "Release" $dot.Release "Capabilities" $dot.Capabilities) }}
{{- end }}

在 some.yaml 中测试:

psqlhost:  {{include "call-nested" (list . "postgresql" "postgresql.fullname")}}
newlinekey: value

如果 some.yaml 是独立文件,它会很好地工作。但是一旦我将它绑定到一个 configmap,它就会给出这个错误:

executing "mytestchart/templates/my-configmap.yaml" at <tpl (.Files.Glob "config/*").AsConfig .>: error calling tpl: Error during tpl function execution for "some.yaml:<br>\"name:  {{include \\\"call-nested\\\" (list . \\\"postgresql\\\" \\\"postgresql.fullname\\\")}}\\r\\nnewlinekey:\n  value\"\n": parse error in "mytestchart/templates/my-configmap.yaml": template: mytestchart/templates/my-configmap.yam:1: unexpected "\\" in operand

一旦我删除了新行,它也可以正常工作。

编辑:配置图:

apiVersion: v1
kind: ConfigMap
metadata:
  name: somename
data:
{{ tpl (.Files.Glob "config/*").AsConfig .| indent 2 }}

【问题讨论】:

  • 我正在调查您的问题。您能否尝试将 ConfigMap 的第 6 行更改为:{{ (.Files.Glob "confs/*").AsConfig | indent 2 }} 并告诉我是否可行?

标签: kubernetes kubernetes-helm


【解决方案1】:

您的模板未正确呈现。为了使其工作,您需要更改以下内容:

  1. .tmpl 后缀添加到要放入 ConfigMap 的文件中,例如:some.yaml.tmpl

  2. 将 ConfigMap 的数据:设置为:{{- tpl ((.Files.Glob "config/*.tmpl").AsConfig) . | indent 2 }}。也为该行添加缩进。

您的 ConfigMap 看起来应该是这样的:

apiVersion: v1
kind: ConfigMap
metadata:
  name: somename
data:
  {{- tpl ((.Files.Glob "config/*.tmpl").AsConfig) . | indent 2 }}

您可以通过一些示例找到更多信息here

如果有帮助,请告诉我。

【讨论】:

  • 好吧..在这种情况下,错误消失了,但模板没有被替换为值。所以生成的 yml 包含原始模板:psqlhost: {{include "call-nested" (list . "postgresql" "postgresql.fullname")}}。我想要的只是将文件放入配置映射中。该文件将包含上面指定的包含命令
  • 我正在更深入地分析您的问题,我有了另一个想法。首先:将.tmpl 后缀添加到您要放入ConfigMap 的文件中,例如:some.yaml.tmpl。第二:将您的 ConfigMap 的data: 设置为:{{- tpl ((.Files.Glob "config/*.tmpl").AsConfig) . | indent 2 }}。也为该行添加一个缩进。
猜你喜欢
  • 2019-06-25
  • 2022-08-03
  • 1970-01-01
  • 2016-10-02
  • 1970-01-01
  • 2017-07-14
  • 1970-01-01
  • 2014-07-06
  • 1970-01-01
相关资源
最近更新 更多