kubernetes-helmgo-templates

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


Solution

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