☸️ kcleaner — A CLI for cleaning up kubernetes config files

Gui Martins
May 28, 2019 · 5 min read

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.

  1. Remove an entry of the YAML file interactively;
  2. Remove from a YAML file other than the default;
  3. Remove a resource that I already know the name of;
  4. 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:

My Baby 👶


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 Command boilerplate

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:

Whoops, forgot to make the file executable.

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:

Demonstration of Click's behaviour

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:

  1. Get the config file — Selected by the --kubeconfig option or the default;
  2. Trigger selection of the current resources for removal;
  3. Remove selected resources;
  4. 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:

  1. Get the version of the Application;
  2. Ensure Python is installed;
  3. Install the tools I need to test and build the application;
  4. Run tests and Code coverage;
  5. Publish Test Results;
  6. Publish Code Coverage;
  7. Stage the files for the release;
  8. Archive the files to tar.gz to use later;
  9. 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:

kcleaner build pipeline


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:

kcleaner release pipeline

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:

Release to GitHub job


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:


Toronto’s Premier Cloud-First Digital Transformation…

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store