Skip to content

Commit bd329c3

Browse files
committed
Don't remove if there are zero or no reachable contexts
When run on start-up, if there is no WiFi available, we want to preserve the clusters, or allow the user to --force the removal. When there are zero changes to make, don't write out the file. Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
1 parent 8ed8704 commit bd329c3

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

README.md

+19-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Q&A:
99

1010
* What if I like doing things the long way?
1111

12-
You can combine `kubectl config get-clusters` with `kubectl config use-context` and `kubectl get nodes`, followed by `kubectl config delete-cluster` and `kubectl config delete-context` for each.
12+
You can combine `kubectl config get-clusters` with `kubectl config use-context` and `kubectl get nodes`, followed by `kubectl config delete-cluster` and `kubectl config delete-context` for each. `kubectx -d` will remove a context, but leaves the cluster in your kubeconfig file, so requires additional steps.
1313

1414
* Doesn't [my favourite tool] have an option to do this?
1515

@@ -35,6 +35,10 @@ Q&A:
3535

3636
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.
3737

38+
* What if my WiFi is down and I run this tool?
39+
40+
If all clusters show as unavailable, kubetrim will not delete any clusters, in this case add `--force` to the command.
41+
3842
## Usage
3943

4044
```bash
@@ -62,6 +66,20 @@ default
6266
kind-2
6367
```
6468

69+
What if the Internet is unavailable, and all clusters report as unavailable?
70+
71+
```bash
72+
# Take down WiFi/Ethernet
73+
74+
$ kubetrim
75+
76+
No contexts are working, the Internet may be down, use --force to delete all contexts anyway.
77+
78+
# Force the deletion, even if all clusters are unavailable.
79+
80+
$ kubetrim --force
81+
```
82+
6583
Try out kubetrim without writing changes to the kubeconfig file:
6684

6785
```bash

main.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424

2525
var (
2626
writeFile bool
27+
force bool
2728
)
2829

2930
func main() {
@@ -44,6 +45,7 @@ func main() {
4445
}
4546

4647
flag.BoolVar(&writeFile, "write", true, "Write changes to the kubeconfig file")
48+
flag.BoolVar(&force, "force", false, "Force delete all contexts, even if all are unreachable")
4749
flag.Parse()
4850

4951
// Load the kubeconfig file
@@ -102,11 +104,18 @@ func main() {
102104
}
103105

104106
if writeFile {
105-
// Save the modified kubeconfig
106-
if err = clientcmd.WriteToFile(*config, kubeconfig); err != nil {
107-
fmt.Printf("Error saving updated kubeconfig: %v\n", err)
107+
108+
if len(contextsToDelete) == len(config.Contexts) && !force {
109+
fmt.Println("No contexts are working, the Internet may be down, use --force to delete all contexts anyway.")
108110
os.Exit(1)
109111
}
112+
if len(contextsToDelete) > 0 {
113+
// Save the modified kubeconfig
114+
if err = clientcmd.WriteToFile(*config, kubeconfig); err != nil {
115+
fmt.Printf("Error saving updated kubeconfig: %v\n", err)
116+
os.Exit(1)
117+
}
118+
}
110119
fmt.Printf("Updated: %s (in %s).\n", kubeconfig, time.Since(st).Round(time.Millisecond))
111120
}
112121
}

0 commit comments

Comments
 (0)