简体   繁体   中英

Helm configmap error. expected string; got bool

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 "base-helm-chart/templates/configmap.yaml" at <$config>: 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 "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

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

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 "string" }}
{{ $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 "string" }}
{{ $name }}: |
{{- tpl $config $ | nindent 12 }}
{{- else -}}
{{ printf "%v: %v" $name $config }}
{{ end }}
{{ end }}
{{ 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 "chart.fullname" .}}
  labels: {{ include "chart.labels" . | 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 "chart.fullname" .}}
  labels: {{ include "chart.labels" . | nindent 4 }}
data:
{{- toYaml .Values.configMap | nindent 2 -}}
{{- end -}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM