登录
首页 >  Golang >  Go问答

解析 Helm 包中 configmap 时遇到渲染问题

来源:stackoverflow

时间:2024-03-25 19:42:35 186浏览 收藏

在 Helm 包中使用 configmap 时,用户遇到渲染问题。该问题源于 value.yaml 中值的名称包含连字符,而 go 名称中不允许出现连字符。此外,用户使用不必要的模板嵌套和 range 循环,导致模板渲染错误。

问题内容

我有一个 helm 包,其 value.yaml 包含类似以下内容:

ere:
  adm-users:
  - username: test1
    password: test2
  - usename: user3
    password: user4

我想将这些值动态地包含在一个 xml 文件中,我在 deployment.yaml 中包含了一个配置映射。我的配置映射包含类似以下内容:

data:
  poolserver.xml: {{ tpl (.files.get "files/config/poolserver.xml") . | nindent 4 }}

在我的 poolserver.xml 中我有以下内容:

{{- range $index, $val := .values.ere.adm-users }}

{{- end }

这是我做了很多更改后的样子,但似乎不起作用。

当我 lint 时,它失败并出现错误:

[ERROR] templates/: render error in "eric-em-ere/templates/deployment-ere.yaml": template: eric-em-ere/templates/deployment-ere.yaml:25:28: executing "eric-em-ere/templates/deployment-ere.yaml" at : error calling include: template: eric-em-ere/templates/configmap.yaml:14:21: executing "eric-em-ere/templates/configmap.yaml" at : error calling tpl: Error during tpl function execution for "\r\n": parse error in "eric-em-ere/templates/deployment-ere.yaml": template: eric-em-ere/templates/deployment-ere.yaml:1: bad character U+002D '-'

请帮我指出我在这里做错了什么。


解决方案


Go text/template documentation 暗示了您的实际错误消息:在 .values.foo.bar 表达式中,每个部分都需要是有效的 go 名称,不能包含连字符。通常,值名称采用驼峰命名法,因此您可以将变量引用和值名称更改为 admusers(大写“u”,无连字符)。

如果您的 usernamepassword 始终是简单字符串,那么 tpl 的多层会令人困惑,并且可能没有必要。您也不需要将 range 的迭代器分配给任何东西,并且(与标准 go 不同)如果您只分配一个变量,它将是值。

我可能会将此循环写为:

{{/* protect against .values.ere being missing entirely */}}
{{- $ere := .values.ere | default dict -}}
{{- range $ere.admusers }}

{{- end }}

如果您希望能够将用户名和密码的模板语法放入 values.yaml 文件中,则需要为 tpl 构造适当的表达式;您无法按照所示方式嵌套模板语法。

{{- $top := . -}}
{{- $ere := .Values.ere | default dict -}}
{{- range $ere.admUsers }}

{{- end }}

如果您想构造一个像 {{ .username }} 这样的字符串并评估它,您可以,但是在模板中放置花括号会很棘手。我认为循环的内部主体相当于我的第一种形式(它最终打印出循环中每个元素的 usernamepassword )。

以上就是《解析 Helm 包中 configmap 时遇到渲染问题》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>