Search code examples
kuberneteskubernetes-helmgo-templates

Helm default value throws error converting YAML to JSON


I have a basic kubernetes helm template like below

apiVersion: v1
kind: ConfigMap
metadata:
  name: test
  namespace: {{ .Release.Namespace }}
  labels:
    app.kubernetes.io/version: {{ .Chart.AppVersion }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    app.kubernetes.io/instance: {{ .Release.Name }}
data:
  config.tpl: |
{{- default ((tpl .Values.configTpl .) | indent 4)  (tpl (.Files.Get "files/config.tpl") . | indent 4) -}}

and the values.yml file

configTpl: |
   {{ x=8gwifi.org }}

When i apply the helm chart it throw me an error

❯ helm upgrade  mychart . --namespace=test --create-namespace --debug
upgrade.go:142: [debug] preparing upgrade for mychart
Error: UPGRADE FAILED: YAML parse error on 8gwifi.org-test/templates/configmap-logfilegen.yaml: error converting YAML to JSON: yaml: line 11: did not find expected comment or line break
helm.go:84: [debug] error converting YAML to JSON: yaml: line 11: did not find expected comment or line break

I tried different configuration

config.tpl: |
    {{- default (tpl .Values.configTpl . | indent 4) (tpl (.Files.Get "files/config.tpl") . | indent 4) -}}

still resulting in same error, Is there a way to specify a config value if none is passed then used the hardcoded one

I'm sure it's an YAML syntx issue couldn't figure it out checked all cases

Based on David suggestion

Template debug is showing this

data:
  config.tpl: |-
       x := 8gwifi.org
   y := "functions"

I can cleary see y is not indent and throwing YAML syntax error, not sure how to fix this

This is the updated definition

data:
  config.tpl: |-
    {{ (tpl .Values.configTpl . | indent 4)  | default (tpl (.Files.Get "files/config.tpl") . | indent 4) -}}

values.yml

configTpl: |
   x := "8gwifi.org"
   y := "function"

Solution

  • You're hitting problems with whitespace in the first line of the block scalar. You should check two things:

    1. The template block containing indent must not itself be indented, it must start at the first column of its line; and
    2. The template block containing indent must not have a - inside the opening curly braces.
    {{- $configTpl := .Values.configTpl | default (.Files.Get "tiles/config.tpl") }}
      config.tpl: |
    {{ tpl $configTpl . | indent 4 }}
    

    The templating language isn't really aware of YAML syntax as it runs. If you have spaces in front of the indent line, they will get emitted, and then indent adds its own leading space, resulting in the last output you get where the first line is indented extra. The - whitespace control marker will also consume the preceding newline, resulting in the first line of the output being on the same line as the YAML block scalar marker.