Trim ๐ your KUBECONFIG automatically
kubetrim tidies up old and broken cluster and context entries from your kubeconfig file.
It works like this:
Loads the config specified by KUBECONFIG
Tries to connect to each
Writes a new file with only the ones that it could access
Q&A:
What if I like doing things the long way?
You can combine
kubectl config get-clusterswithkubectl config use-contextandkubectl get nodes, followed bykubectl config delete-clusterandkubectl config delete-contextfor each.kubectx -dwill remove a context, but leaves the cluster in your kubeconfig file, so requires additional steps.Doesn't [my favourite tool] have an option to do this?
Feel free to use your favourite tool instead. kubetrim is a memorable name, and a simple tool that just does one job, similar to
kubectxWhat if I want to keep a cluster that is unreachable?
This is not supported at this time, if you need that feature, open an issue.
What if my cluster is valid, but kubetrim cannot detect it?
Open an issue, and we can look at adding support for your use-case.
kubetrimis great, how can I support you?Have a look at arkade and k3sup, you may like those too. You can also sponsor me via GitHub.
How can I run kubetrim daily?
Create a crontab with the following expression:
0 0 * * * kubetrimCan I run kubetrim every time I open a terminal?
Yes, you can add
kubetrim &to your.bashrc,.bash_profileor.zshrcfile, which will run in the background, and not slow down your terminal session from starting up.What if my WiFi is down and I run this tool?
If all clusters show as unavailable, kubetrim will not delete any clusters, in this case add
--forceto the command.
Usage
$ kubectx
default
do-lon1-openfaas-cluster
kind-2
kind-ingress
$ kubetrim
kubetrim (dev) by Alex Ellis
Loaded: /home/alex/.kube/config. Checking..
- kind-2: โ
- kind-ingress: โ - (failed to connect to cluster: Get "https://127.0.0.1:40349/api/v1/nodes": dial tcp 127.0.0.1:40349: connect: connection refused)
- default: โ
- do-lon1-openfaas-cluster: โ - (failed to connect to cluster: Get "https://da39a3ee5e6b4b0d3255bfef95601890afd80709.k8s.ondigitalocean.co.uk/api/v1/nodes": dial tcp: lookup da39a3ee5e6b4b0d3255bfef95601890afd80709.k8s.ondigitalocean.co.uk on 127.0.0.53:53: no such host)
Updated: /home/alex/.kube/config (in 364ms).
$ kubectx
default
kind-2
Try out kubetrim without writing changes to the kubeconfig file:
$ kubetrim --write=false
Use a different kubeconfig file:
$ KUBECONFIG=$HOME/.kube/config.bak kubetrim
What if the Internet is unavailable, and all clusters report as unavailable?
# Take down WiFi/Ethernet
$ kubetrim
No contexts are working, the Internet may be down, use --force to delete all contexts anyway.
# Force the deletion, even if all clusters are unavailable.
$ kubetrim --force
Installation
Getting kubetrim with arkade:
curl -sfLS https://get.arkade.dev | sh
arkade get kubetrim