Tuesday, 7 July 2020

Kubeval, a tool for validating K8S configuration files

Well, you have prepared you kubernetes manifests and the next step is to apply them to create all the resources you need. But how to know if your YAML file is correct?

Using kubeval :)

kubeval is a tool for validating a Kubernetes YAML or JSON configuration file. It does so using schemas generated from the Kubernetes OpenAPI specification, and therefore can validate schemas for multiple versions of Kubernetes.


Installation

Download kubeval here: https://github.com/instrumenta/kubeval/releases


Example.

Suppose we have the following k8s-example.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80


We can check it with the following command:

$ kubeval k8s-example.yaml                
PASS - k8s-example.yaml contains a valid Deployment (nginx-deployment)


Let's introduce an indentation error:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
  metadata:
    labels:
      app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80

If we run the same command as before we get:

$ kubeval k8s-example.yaml
WARN - k8s-example.yaml contains an invalid Deployment (nginx-deployment) - spec.template: Invalid type. Expected: object, given: null






No comments:

Post a Comment