Skip to content

Commit

Permalink
Don't remove if there are zero or no reachable contexts
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
alexellis committed Aug 19, 2024
1 parent 8ed8704 commit bd329c3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Q&A:

* What if I like doing things the long way?

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

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

Expand All @@ -35,6 +35,10 @@ Q&A:

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.

## Usage

```bash
Expand Down Expand Up @@ -62,6 +66,20 @@ default
kind-2
```

What if the Internet is unavailable, and all clusters report as unavailable?

```bash
# 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
```

Try out kubetrim without writing changes to the kubeconfig file:

```bash
Expand Down
15 changes: 12 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

var (
writeFile bool
force bool
)

func main() {
Expand All @@ -44,6 +45,7 @@ func main() {
}

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

// Load the kubeconfig file
Expand Down Expand Up @@ -102,11 +104,18 @@ func main() {
}

if writeFile {
// Save the modified kubeconfig
if err = clientcmd.WriteToFile(*config, kubeconfig); err != nil {
fmt.Printf("Error saving updated kubeconfig: %v\n", err)

if len(contextsToDelete) == len(config.Contexts) && !force {
fmt.Println("No contexts are working, the Internet may be down, use --force to delete all contexts anyway.")
os.Exit(1)
}
if len(contextsToDelete) > 0 {
// Save the modified kubeconfig
if err = clientcmd.WriteToFile(*config, kubeconfig); err != nil {
fmt.Printf("Error saving updated kubeconfig: %v\n", err)
os.Exit(1)
}
}
fmt.Printf("Updated: %s (in %s).\n", kubeconfig, time.Since(st).Round(time.Millisecond))
}
}
Expand Down

0 comments on commit bd329c3

Please sign in to comment.