helm - error converting YAML to JSON: yaml: line 29: mapping values are not allowed in this context

6/29/2021

Defined labels in temp with top of the same deployment.yml file-

{{- define "chart.labels" }} 
  version: v1.0
  method: http
  internet: enabled
{{- end }}

I have deployment.yml file in template folder-

apiVersion: apps/v1
kind: Deployment
metadata:
  name: app1-deployment
  namespace: {{ .Values.global.namespace }}
  labels:
    app: app1
    type: microservice1
spec:
  replicas: 3
  selector:
    matchLabels:
      app: app1
      type: microservice1
  strategy:
    type: {{ .Values.global.strategytype }}
  template:
    metadata:
      labels:
        app: app1
        type: microservice1
        {{- template "chart.labels" }}

The Two ways - one from the keyword template (last line of the below code)

And second one from the include keyword I am trying to call the template.

{{include "chart.labels" . | indent 8 }}
  • I am getting error ( when I am using keyword template to call the template).

Error: YAML parse error on chart/templates/deployment.yml: error converting YAML to JSON: yaml: line 27: did not find expected key helm.go:81: debug error converting YAML to JSON: yaml: line 27: did not find expected key YAML parse error on chart/templates/deployment.yml helm.sh/helm/v3/pkg/releaseutil.(manifestFile).sort helm.sh/helm/v3/pkg/releaseutil/manifest_sorter.go:146 helm.sh/helm/v3/pkg/releaseutil.SortManifests helm.sh/helm/v3/pkg/releaseutil/manifest_sorter.go:106 helm.sh/helm/v3/pkg/action.(Configuration).renderResources helm.sh/helm/v3/pkg/action/action.go:165 helm.sh/helm/v3/pkg/action.(*Install).Run helm.sh/helm/v3/pkg/action/install.go:247

  • Getting another error (when I use Include keyword to call the template)

Error: YAML parse error on chart/templates/deployment.yml: error converting YAML to JSON: yaml: line 29: mapping values are not allowed in this context helm.go:81: debug error converting YAML to JSON: yaml: line 29: mapping values are not allowed in this context YAML parse error on chart/templates/deployment.yml helm.sh/helm/v3/pkg/releaseutil.(manifestFile).sort helm.sh/helm/v3/pkg/releaseutil/manifest_sorter.go:146 helm.sh/helm/v3/pkg/releaseutil.SortManifests helm.sh/helm/v3/pkg/releaseutil/manifest_sorter.go:106 helm.sh/helm/v3/pkg/action.(Configuration).renderResources helm.sh/helm/v3/pkg/action/action.go:165 helm.sh/helm/v3/pkg/action.(*Install).Run helm.sh/helm/v3/pkg/action/install.go:247 main.runInstall

What am I missing here ?

-- Darshana Patel
helmfile
kubernetes
kubernetes-helm
yaml

1 Answer

6/29/2021

You need to follow sane indentation. You have:

{{- define "chart.labels" }} 
  version: v1.0
  method: http
  internet: enabled
{{- end }}

Note there is no double space in chart.labels definition below.

The below works:

{{- define "chart.labels" }} 
version: v1.0
method: http
internet: enabled
{{- end }}

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "test.fullname" . }}
  labels:
    {{- include "test.labels" . | nindent 4 }}
spec:
{{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
{{- end }}
  selector:
    matchLabels:
      {{- include "test.selectorLabels" . | nindent 6 }}
  template:
    metadata:
    {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
    {{- end }}
      labels:
      {{- include "test.selectorLabels" . | nindent 8 }}
      {{include "chart.labels" . | nindent 8 }}

Edit: Or only change the nindent to match chart.labels in the template meta as below:

{{include "chart.labels" . | nindent 6 }}
-- GlorifiedTypist
Source: StackOverflow