Helm配置映射错误。预期为字符串;实际为布尔值。

huangapple go评论76阅读模式
英文:

Helm configmap error. expected string; got bool

问题

我有一个Helm图表,其中包含多个模板。其中一个是配置映射(configmap),之前一直工作正常。但是当我想要添加enabled部分时,出现了错误消息。

执行"base-helm-chart/templates/configmap.yaml"时,在<$config>处:值的类型错误;期望的是字符串,但得到的是布尔值。

这是我使用的文件:

{{- if .Values.configMap.enabled }}
apiVersion: v1
kind: ConfigMap
metadata:
  namespace: {{ .Release.Namespace }}
  name: {{include "chart.fullname" .}}
  labels: {{ include "chart.labels" . | nindent 4 }}
data:
{{- range $name, $config := .Values.configMap }}
  {{ $name }}: |
{{ tpl $config $ | indent 4 }}
{{- end }}
{{- end -}}

values.yaml

configMap:
  enabled: true
  config.json: |
    food = pizza
    drink = soda    

我希望用户可以从values.yaml中启用/禁用是否添加configmap。

英文:

I have a Helm chart with has multiple templates. One is the configmap which was working fine.
But when I want to add the enabled part I´m getting the error message.

executing &quot;base-helm-chart/templates/configmap.yaml&quot; at &lt;$config&gt;: wrong type for value; expected string; got bool

This are the files I´m using:

{{- if .Values.configMap.enabled }}
apiVersion: v1
kind: ConfigMap
metadata:
  namespace: {{ .Release.Namespace }}
  name: {{include &quot;chart.fullname&quot; .}}
  labels: {{ include &quot;chart.labels&quot; . | nindent 4 }}
data:
{{- range $name, $config := .Values.configMap }}
  {{ $name }}: |
{{ tpl $config $ | indent 4 }}
  {{- end }}
  {{- end -}}

values.yaml

configMap:
  enabled: true
  config.json: |
    food = pizza
    drink = soda

I want user to enable/disable if he wants to add configmap or not from the values.yaml

答案1

得分: 1

你可以在tpl函数中添加条件来跳过另一种类型的值,然后传递字符串。

{{- range $name, $config := .Values.configMap -}}
{{ if typeOf $config | eq "string" }}
{{ $name }}: |
{{- tpl $config $ | nindent 12 }}
{{ end }}
{{ end }}
{{ end }}

如果你想在输出中打印另一个键值对,你可以使用printprintfprintln或任何其他打印选项。

{{- range $name, $config := .Values.configMap -}}
{{ if typeOf $config | eq "string" }}
{{ $name }}: |
{{- tpl $config $ | nindent 12 }}
{{- else -}}
{{ printf "%v: %v" $name $config }}
{{ end }}
{{ end }}
{{ end }}
英文:

You can add condition to skip value of another type then string to be passed in tpl function

{{- range $name, $config := .Values.configMap -}}
{{ if typeOf $config | eq &quot;string&quot; }}
{{ $name }}: |
{{- tpl $config $ | nindent 12 }}
{{ end }}
{{ end }}
{{ end }}

If you want to also print another key value in output then you can use print, printf, println or any other print option.

{{- range $name, $config := .Values.configMap -}}
{{ if typeOf $config | eq &quot;string&quot; }}
{{ $name }}: |
{{- tpl $config $ | nindent 12 }}
{{- else -}}
{{ printf &quot;%v: %v&quot; $name $config }}
{{ end }}
{{ end }}
{{ end }}

答案2

得分: 0

我帮你翻译一下:

这是否意味着默认情况下 configMap 是空的?
如果是这样的话,你可以检查是否为空值:

{{- if .Values.configMap }}
apiVersion: v1
kind: ConfigMap
metadata:
  namespace: {{ .Release.Namespace }}
  name: {{include "chart.fullname" .}}
  labels: {{ include "chart.labels" . | nindent 4 }}
data:
{{- range $name, $config := .Values.configMap }}
  {{ $name }}: |
{{ tpl $config $ | indent 4 }}
  {{- end }}

{{- end -}}

values.yaml 中,默认是一个空字典:

configMap: {}

这样,只有当用户填写了 configMap 时,才会生成清单文件。

除了这个可选的激活之外,你似乎在遍历值时遇到了问题,因为它们的类型不同。

你可以使用更简单的 toYaml 过滤器(参见这里)。

最终结果可能是这样的:

{{- if .Values.configMap }}
apiVersion: v1
kind: ConfigMap
metadata:
  namespace: {{ .Release.Namespace }}
  name: {{include "chart.fullname" .}}
  labels: {{ include "chart.labels" . | nindent 4 }}
data:
{{- toYaml .Values.configMap | nindent 2 -}}
{{- end -}}
英文:

> I want user to enable/disable if he wants to add configmap or not from the values.yaml

Does this mean that by default the configMap is empty?
If that's the case, you can check for the empty value

{{- if .Values.configMap }}
apiVersion: v1
kind: ConfigMap
metadata:
  namespace: {{ .Release.Namespace }}
  name: {{include &quot;chart.fullname&quot; .}}
  labels: {{ include &quot;chart.labels&quot; . | nindent 4 }}
data:
{{- range $name, $config := .Values.configMap }}
  {{ $name }}: |
{{ tpl $config $ | indent 4 }}
  {{- end }}

{{- end -}}

and in values.yaml the default is an empty dictionary:

configMap: {}

In this way, only when a user fills in configMap, the manifest will be generated.

Aside from this optional activation, you seem to have a problem in the iteration over the values, because they differ types.

You can use the much simpler toYaml filter (see here)

The end result could be something like this:

{{- if .Values.configMap }}
apiVersion: v1
kind: ConfigMap
metadata:
  namespace: {{ .Release.Namespace }}
  name: {{include &quot;chart.fullname&quot; .}}
  labels: {{ include &quot;chart.labels&quot; . | nindent 4 }}
data:
{{- toYaml .Values.configMap | nindent 2 -}}
{{- end -}}

huangapple
  • 本文由 发表于 2022年6月10日 21:18:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/72574996.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定