Home > Software engineering >  Helm configmap error. expected string; got bool
Helm configmap error. expected string; got bool

Time:06-14

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

CodePudding user response:

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 -}}

CodePudding user response:

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 }}
  • Related