Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am doing a lab about kubernetes in google cloud.
I have create the YAML file, but when I am trying to deploy it a shell shows me this error:

error converting YAML to JSON: yaml: line 34: did not find expected key

YAML file:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
    spec:
      volumes:
      - name: nginx-config
        configMap:
          name: nginx-config
      - name: php-config
        configMap:
          name: php-config
      containers:
      - image: php-fpm:7.2
        name: php
        ports:
        - containerPort: 9000
        volumeMounts:
        - name: persistent-storage
          mountPath: /var/www/data
        - name: php-config
          mountPath: /usr/local/etc/php-fpm.d/www.conf
          subPath: www.conf
      - image: nginx:latest
        name: nginx
        - containerPort: 80
        volumeMounts:
        - name: persistent-storage
          mountPath: /var/www/data
        - name: nginx-config
          mountPath: /etc/nginx/nginx.conf
          subPath: nginx.conf
      volumes:
        - name: persistent-storage
          persistentVolumeClaim:
            claimName: nfs-pvc

yamllint package is useful to debug and find this kind of errors, just do yamllint filename and it will list the possible problems it finds. Install via your distro package manager (usually recommended if available) or via the below npm install command (it will install globally)

npm install -g yaml-lint

Thanks to Kyle VG for the npm command

- name: php-config # looks like indentation issue here mountPath: /usr/local/etc/php-fpm.d/www.conf subPath: www.conf - image: nginx:latest name: nginx - containerPort: 80 volumeMounts: - name: persistent-storage mountPath: /var/www/data - name: nginx-config mountPath: /etc/nginx/nginx.conf subPath: nginx.conf volumes: - name: persistent-storage persistentVolumeClaim: claimName: nfs-pvc There I was thinking that indentation issues had been left behind with FORTRAN in the punched-card era. If only people would come to their senses and use TABS not spaces! :) – Ed Randall Jul 3, 2020 at 18:01 Tabs are not allowed in YAML - "To maintain portability, tab characters must not be used in indentation, since different systems treat tabs differently. " See spec – boardtc Mar 16, 2021 at 14:37

Following higuita's answer you can lint your yaml and check for errors without installing a module in your machine using npx. I prefer this approach for commands that I do not intend to use often. NPX downloads the package, executes the command and remove the package when finishes.

npx yaml-lint yamllint file_name

I got that error while creating a yaml file for an Ingress using Helm. I had something like this as my Ingress specification

spec:
  - hosts:
    - {{ .Values.ingress.host }}

and in the values.yaml

ingress:
  host: "[NAMESPACE]-example.com"

Turned out that the brackets where causing the error.

The issue could be fixed by putting quotes on the value using the quote function.

- {{ .Values.ingress.host | quote }}

This is also what the Helm doc recommends

The easiest way to avoid type conversion errors is to be explicit about strings, and implicit about everything else. Or, in short, quote all strings.

and here

When you are working with string data, you are always safer quoting the strings than leaving them as bare words:

I had the same problem, but I solve copying the link of the RAW Github file and set it on kubectl

kubectl create -f https://raw.githubusercontent.com/user/project/master/file.yml

Ensure you don't have any invisible characters which are causing you issue.

This error:

error converting YAML to JSON: yaml: line 96: could not find expected '':''

Was because of an invisible break between the : and the C in this line:

- name: CERT_ALIAS

I recently came across this same error when attempting to create a template yaml file for multiple cronjobs where the settings were stored in an array in a values.yaml file.

Here is an example of the values data based on my actual code:

allCronJobs:
  - cron1:
    cronJobName: hello-world
    enabled: true
    cronJob:
      schedule: "2 * * * *"
      backoffLimit: 3
      restartPolicy: OnFailure
  - cron2:
    cronJobName: foo-bar
    enabled: true
    cronJob:
      schedule: "0 11 * * *"
      activeDeadlineSeconds: 10800
      restartPolicy: OnFailure

Here is a snippet from the template file using the range syntax. (This is the code that resulted in the error converting YAML to JSON)

{{ range $index, $currCron := .Values.allCronJobs -}}
{{- if $currCron.enabled -}}
{{- with $ }}
apiVersion: batch/v1
kind: CronJob
metadata:
  name: {{ include "api-payments.fullname" . }}-{{ $currCron.cronJobName }}
  labels:
    {{- include "api-payments.labels" . | nindent 4 }}
spec:
  schedule: {{ $currCron.cronJob.schedule | quote }}
  startingDeadlineSeconds: {{ $currCron.cronJob.startingDeadlineSeconds }}

Thankfully, after much searching, I came across this post that helped me figure it out: https://dev.to/ujwaldhakal/manage-multiple-cron-with-helm-flow-control-32i7

I needed to include "---" between each iteration of cronjob, just like helm does when processing multiple files (which are usually a single resource per file).

Here is the corrected code that now works. The change is on line 4.

{{ range $index, $currCron := .Values.allCronJobs -}}
{{- if $currCron.enabled -}}
{{- with $ }}
apiVersion: batch/v1
kind: CronJob
metadata:
  name: {{ include "api-payments.fullname" . }}-{{ $currCron.cronJobName }}
  labels:
    {{- include "api-payments.labels" . | nindent 4 }}
spec:
  schedule: {{ $currCron.cronJob.schedule | quote }}
  startingDeadlineSeconds: {{ $currCron.cronJob.startingDeadlineSeconds }}
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.