☸️ kcleaner — A CLI for cleaning up kubernetes config files
I have been working with k8s (Kubernetes) for the most significant part of the last two years. As a result, my config files keep growing with contexts, clusters and users that might not be relevant or even exist anymore. Cleaning up a large YAML file by hand would not do (we are in 2019 😄). So I created a tool to automate this process. It’s called kcleaner, and I’m making it available as open source on GitHub. In this post, I’ll walk you through the process I followed and code I wrote to create kcleaner.
If the code and release information is all you want, there is a link for the GitHub repo at the end of this story.
Now that I know a CLI tool is the way to go for me, I had to lay out the features I wanted and adjust my priorities.
- Remove an entry of the YAML file interactively;
- Remove from a YAML file other than the default;
- Remove a resource that I already know the name of;
- Remove multiple entries in the YAML file;
“Cool, that is exactly what I need for now! Where do I want to run this? It would be nice to run this on Windows, Linux and MacOS… That is an excellent opportunity to stretch my Python muscles here!” — Me when I realized I was really going to do this.
The compatibility of this tool is essential. You can operate on K8s from virtually any platform, so should this software. I have not honestly spent much time on the decision of the tools, Python was already in my mind, and I already knew a great python package called Click to help me create CLI tools. That would be the fastest path for creating my software.
Let’s check what we have thus far:
- Platforms where this is going to run;
- Language and packages I’m going to use;
- Primary functionalities of the application;
That seems like a good start! Let's create our GitHub repo:
Looking good! Now it's time to get this working. This part is going to be coding mostly, you can skip it if it's not something you want to see.
Still here? Cool, you're in for a treat!
(Not really, it's a straight forward code ¯\_(ツ)_/¯)
Let's add the boilerplate for the CLI tool with Click:
Click makes things easier by creating the invoke code for us, all we need to do is add the decoration
@click.command(), and Click takes care of the rest by treating it as the actual command. This automagically (yes, I use this kind of words) generates our help command:
Great! We have something to work with already, Let's add some options and arguments to the tool:
All I am doing here is making use of click to add an argument and two options to my CLI, let's check how it looks like now:
Now that we have the base of the CLI, it is time to add some functionality to the tool, which can be challenging to find time nowadays.
Well, it did not take as long as I thought it would! We have a working prototype!
Here is how it works:
- Get the config file — Selected by the
--kubeconfigoption or the default;
- Trigger selection of the current resources for removal;
- Remove selected resources;
- Write the new config file with removed entries;
Now that we have a working prototype, it would be nice to build it into a CLI tool itself, not only a python script. The first step is creating a pretty basic setup file:
Adding this to the root of my project and running
pip install . seems to do the trick! But how about my tests? Code coverage? That is important, you know? Where am I going to run these releases? My tool of choice here is Azure DevOps, so I need to come up with the yaml file for the pipeline. First, I need to see what needs to be done:
- Get the version of the Application;
- Ensure Python is installed;
- Install the tools I need to test and build the application;
- Run tests and Code coverage;
- Publish Test Results;
- Publish Code Coverage;
- Stage the files for the release;
- Archive the files to tar.gz to use later;
- Publish build artifacts to be used in the release;
Putting this together, this is what I came up with:
Trying to push some code to the master branch has these results:
None of this would make sense if I did not publish this somewhere/somehow. I am planning on adding support for Brew, Chocolatey and PyPI, but for now, I only have the GitHub Release and the skeleton for PyPI:
Those tasks are relatively simple, I just need to make sure I know the version of the application and use the built-in Azure DevOps task for GitHub release:
I have created a CLI tool to cleanup k8s config files, it is written in python and has a nice CI/CD pipeline going to keep things running.
You can find the source code and release of kcleaner here: