TL/DR I made a plugin to clean up your kubeconfig file!

— — —

The other day I opened up my kubeconfig file to find which context I shoule be using for talking to my staging cluster, and there was about 20 contexts and I had no idea what I needed to be using. It was too large, and there was too many entries under each key for me to be able to easily distinguish what user and cluster was matching with which context. The main problem with this was I knew that over half the contexts where pointing to dead clusters I had used for testing and they didn’t need to be there any more.

I started trying to clean up the file using kubectl config delete-context * and kubectl config delete-cluster * commands, having to look at the name of the cluster for each context and decide if it was pointing to one of those dead clusters and no longer needed, or if was to a cluster I still wanted to keep around. Finally, after all that the file was still muddied up with old users.

When it was all said and done I looked at the clock and realized I had spent almost 20 minutes on this trivial task. The worst part about it was knowing that within a week my kubeconfig file would be in that same messy state. I didn’t want to keep spending time on cleaning up this file. Instead I decided to write a kubectl plugin to make this task a lot quicker.

If you also like to keep your kubeconfig maintainable and up to date hopefully my plugin can save you some time. The plugin will go through each of the contexts in your file and check if the cluster associated with it is still responding. If there is an error trying to query the kubernetes api it will give you the option to remove it from the file. Once you decide which contexts and clusters you would like to keep, the plugin will go through and prompt you for every user, not associated with a context, if you would like to remove them. At the very end you have the choice of overwriting your default kubeconfig file located at $HOME/.kube/config , or whatever path you specified with the --kubeconfig flag. If you do not want to overwrite the file though you can also have the new config written to a different location.

Hopefully someone else also finds this useful and can save some time as well. You can find the tool on github here.