Validating k8s deployments using Sparrow
Tags: kubernetes, k8s, raku, testing
Published: 2021-07-26 01:10:03

---
title: Validating k8s deployments using Sparrow
published: true
description: How to test your k8s infrastructure using Sparrow 
tags: kubernetes, k8s, raku, testing
cover_image: https://raw.githubusercontent.com/melezhik/images/master/k8s-deployment-check2.png
---


[Sparrow](https://github.com/melezhik/Sparrow6) is a wonderful tool to automate @daily devops tasks. Recently I've dropped a new plugin called `k8s-deployment-check` to verify k8s deployments. It lets you with a little bit of Raku code effectively test entire k8s infrastructure, including k8s deployments.

--- 

Let's create a sample nginx deployment to show how it works:


```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  # replicas: 1 # tells deployment to run 2 pods matching the template
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80
        volumeMounts:
        - mountPath: /var/www
          name: www-data
        env:
          - name: DEMO_GREETING
            value: "Hello from the environment"
          - name: DEMO_FAREWELL
            value: "Such a sweet sorrow"
      volumes:
      - name: www-data
        persistentVolumeClaim:
          claimName: nginx-example
```

In this example we run simple nginx application with local folder mounted as `/var/www` and consuming it's data throughout a volume named `www-data`:


```bash
k8s apply -f nginx.yaml
deployment.apps/nginx-deployment created
```

---


Now when our deployment is up and running, let's verify it, using Raku:


`tomty --edit verify`

```perl
#!raku

task-run "chk-dpl", "k8s-deployment-check", %(
  name => "nginx-deployment",
  namespace => "melezhik-sandbox",
  volume-mounts => %(
    www-data => "/var/www"
  ),
);
```

`tomty verify`

```log
[repository] :: index updated from file:///root/repo/api/v1/index
[chk-dpl] :: >>> verify deployment. name=nginx-deployment,namespace=melezhik-sandbox,container=nginx
[chk-dpl] :: [env_start]
[chk-dpl] :: [DEMO_GREETING=Hello from the environment]
[chk-dpl] :: [DEMO_FAREWELL=Such a sweet sorrow]
[chk-dpl] :: [env_end]
[chk-dpl] :: [volume_mounts_start]
[chk-dpl] :: [www-data /var/www]
[chk-dpl] :: [volume_mounts_end]
[chk-dpl] :: ==================================================================
[task check] >>> check volume mounts
[task check] stdout match (r) <[www-data /var/www]> True
```

As we could see Sparrow has successfully verified that 

* k8s deployment resource it exists
* it has volume `www-data` mounted as `/var/www` folder

We can alter the scenario a bit, adding environment variable check as well:

`tomty --edit verify`

```perl
task-run "chk-dpl", "k8s-deployment-check", %(
  name => "nginx-deployment",
  namespace => "melezhik-sandbox",
  volume-mounts => %(
    www-data => "/var/www"
  ),
  env => {
    DEMO_GREETING => "Hello from the environment",
    DEMO_FAREWELL => "Such a sweet sorrow"
  },
);
```

And now run:

`tomty verify`

```log
[repository] :: index updated from file:///root/repo/api/v1/index
[chk-dpl] :: >>> verify deployment. name=nginx-deployment,namespace=melezhik-sandbox,container=nginx
[chk-dpl] :: [env_start]
[chk-dpl] :: [DEMO_GREETING=Hello from the environment]
[chk-dpl] :: [DEMO_FAREWELL=Such a sweet sorrow]
[chk-dpl] :: [env_end]
[chk-dpl] :: [volume_mounts_start]
[chk-dpl] :: [www-data /var/www]
[chk-dpl] :: [volume_mounts_end]
[chk-dpl] :: ==================================================================
[task check] >>> check env
[task check] stdout match (r) <[DEMO_FAREWELL=Such a sweet sorrow]> True
[task check] stdout match (r) <[DEMO_GREETING=Hello from the environment]> True
[task check] >>> check volume mounts
[task check] stdout match (r) <[www-data /var/www]> True
```

---

# Conclusion

Sparrow `k8s-deployment-check` plugin allows one to test k8s infrastructure by just writing a simple piece of Raku code. The full [documentation](https://sparrowhub.io/plugin/k8s-deployment-check/0.000001) is available at SparrowHub site. 

I am going to add more features eventually. 

Under the hood Sparrow uses Raku regular expressions to verify resources structure, this allows to write even more sophisticated checks. 

For example, to check that a container run command has python 2nd or 3rd version one can write:

```
  command => "regexp: '/usr/bin/python' 2|3"
```

Stay tuned and as usual I'd like to hear your feedback

---

Alexey