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:
-
kubetrim
is great, how can I support you?You can sponsor me via GitHub - the more sponsors, the more time I'll spend on tools like kubetrim, Have a arkade and k3sup.
-
What if I like doing things the long way?
You can combine
kubectl config get-clusters
withkubectl config use-context
andkubectl get nodes
, followed bykubectl config delete-cluster
andkubectl config delete-context
for each.kubectx -d
will 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
kubectx
-
What if I want to keep a cluster that is unreachable?
You can add each context name to the keep file at
~/.local/kubetrim/keep.txt
. You can also use comments in this file by prefixing the comment with# Needs work VPN etc
-
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.
-
How can I run kubetrim daily?
Create a crontab with the following expression:
0 0 * * * kubetrim
-
Can I run kubetrim every time I open a terminal?
Yes, you can add
kubetrim &
to your.bashrc
,.bash_profile
or.zshrc
file, 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
--force
to the command.
$ 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
Getting kubetrim
with arkade:
curl -sfLS https://get.arkade.dev | sh
arkade get kubetrim
Or download it manually via the Releases page
MIT license