Validating k8s deployments using Sparrow

Alexey Melezhik - Jul 26 '21 - - Dev Community

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

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
Enter fullscreen mode Exit fullscreen mode

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:

k8s apply -f nginx.yaml
deployment.apps/nginx-deployment created
Enter fullscreen mode Exit fullscreen mode

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

tomty --edit verify

#!raku

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

tomty verify

[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
Enter fullscreen mode Exit fullscreen mode

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

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"
  },
);
Enter fullscreen mode Exit fullscreen mode

And now run:

tomty verify

[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
Enter fullscreen mode Exit fullscreen mode

Conclusion

Sparrow k8s-deployment-check plugin allows one to test k8s infrastructure by just writing a simple piece of Raku code. The full documentation 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"
Enter fullscreen mode Exit fullscreen mode

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


Alexey

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player