In the previous article, we learned how to create Kubernetes YAML files. Now, let’s see how to ensure that the files we have created are not only valid YAML but more importantly adhere to best practices of Kubernetes development.
This article is intended as a guide for validating Kubernetes manifest files. If you are a developer, software architect, security specialist, or simply interested in learning more about the quality assurance process around creating and managing Kubernetes manifests, this article is for you.
The shift left movement or as I like to call it “extend left” started as an idea of empowering developers to be responsible for and own the end-to-end process of the software lifecycle.
When done correctly, it means creating tools and the supporting organizational structure that allows development teams to succeed by allowing them to take full ownership and control over the software they create and maintain. When done incorrectly, it usually means “dumping” all the responsibility on developers and hoping for the best.
In the spirit of extending left, the process of authoring and managing Kubernetes artifacts should also be the responsibility of development teams.
Kubernetes already has a powerful and sophisticated process of validating resources in the form of the following tools:
policy enforcement via tools like Open Policy Agent, Kyverno or Kubewarden
pod security policies (deprecated in 1.21)
Those tools are great, but they belong to the cluster operator layer and are more suited as a platform rather than a tool that fits neatly in a standard developer workflow.
What we are going to look at next are various tools and methods that shift the starting point of a validation process to a development workflow and thus greatly improve the quality of the whole system.
The holy grail of shifting validation left is to make illegal states irrepresentable. This is difficult to do with a non-statically typed language like YAML, but with the right tools, it can be done.
Quality in a service or product is not what you put into it. It is what the client or customer gets out of it.
— Peter Drucker
After reading this article, you will learn:
If you would like to follow along and get your hands dirty with YAML, you will need:
Please note that the repository uses a devcontainer with all the tools and configuration needed to run the examples built in. Because of this, the image is large, so please modify it accordingly before running the container.
Let’s look a bit closer at the validation process. We can divide the validation process into 3 distinct categories:
Trivy can scan YAML files for security vulnerabilities, misconfiguration and more. Each check has a web page explaining the reason behind it as well as its severity. The only issue here is that the checks are “static”, we cannot influence directly if any of them should be skipped (this is partially possible with setting up severity flag)
Datree takes a different approach, where policies can be switched on and off by the developer.
You can see that datree first checked whether the file is a valid YAML and then validated the content against the Kubernetes schema. This satisfies our Structural -> Semantic -> Pragmatic validation flow!
Each check is associated with a configurable policy set. Policies can be switched on and off to meet personalized validation scheme needs.
This brings us a step closer to shifting the validation process left and empowering developers to take control over the whole process.
The end goal of every repetitive development process is to automate it. Adding validation rules from both datree and trivy to our CI pipeline is easy. They provide rich integration with all major git providers like GitHub, GitLab as well as Helm installation plugins etc.
Kubernetes story for developers still leaves a lot to be desired. I’m happy to see tools created to address some of the pains of developing on Kubernetes. Tools like datree especially focus on and enable the shift left movement.
Developers are fully in control of their software lifecycle, tools, and organizational structures geared towards empowering and supporting development teams in their mission of creating amazing software. This is the future that will benefit all of us.
Also published on: https://piotrzan.medium.com/how-to-validate-kubernetes-yaml-files-9a17b9a30f08