Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error converting YAML to JSON, did not find expected key kubernetes

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


The overall file looks good. There are some issues with indentation.

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
            # looks like indentation issue here                 
            mountPath: /var/www/data 
        - 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

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:
  tls:
  - 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: