Skip to content

xcoulon/kubectl-terminate

Repository files navigation

kubectl-terminate

GoDoc Go Report Card CI Codecov License

WARNING

Use at your own risk!

Sometimes (hopefully on your dev cluster) you may find yourself with a namespace stuck in Terminating phase because one of its child resource has a finalizer, but the operator (or controller) in charge of dealing with this finalizer is already gone (for example, it has been uninstalled). This is quite annoying and sadly the --force, grace-period, etc. flags won’t help. The only way to get rid of the resource is to PATCH it in order to remove the finalizers, then delete it. Doing it with a good old curl command is possible, but let’s admit it, it’s a bit cumbersome 😬

Say hello to kubectl-terminate 👋

This command takes care of removing the finalizers and deleting the resource on your behalf, using the connection settings of your choice, i.e, using the KUBECONFIG env var if it exists, the --kubeconfig flag if specified or the default location ($HOME on Linux and macOS and %USERPROFILE% on Windows).

And since its name follows the kubectl-* pattern, it also works as a plugin for kubectl and oc (for OpenShift 4 users). Just make sure that the binary is in your $PATH and use it with kubectl terminate pod/cheesecake or oc terminate pod/cheesecake 🎉

Installation

  • run go install github.com/xcoulon/kubectl-terminate/cmd and add $GOPAH/bin to $PATH or move $GOPAtH/bin/kubectl-terminate in one of the directories of $PATH

  • clone this repository, run make install and add $GOPAH/bin to $PATH or move $GOPAtH/bin/kubectl-terminate in one of the directories of $PATH

Demo

On your own Kubernetes or OpenShift cluster, you can try it with the following commands:

# create 2 pods called `keep-me` and `delete-me` that don't do anything
$ kubectl run --image radial/busyboxplus --restart=Never keep-me -- sleep 3600
pod/delete-me created

$ kubectl run --image radial/busyboxplus --restart=Never delete-me -- sleep 3600
pod/delete-me created

# patch the `delete-me` pod by adding a custom finalizer
$ kubectl patch pod/delete-me -p '{"metadata":{"finalizers":["demo/block-me"]}}'
pod/delete-me patched

# try to delete it with the regular `delete` command
$ kubectl delete pod/delete-me
pod "delete-me" deleted
^C

# ... but the command blocks and the pod still exists
$ kubectl get pods
NAME        READY   STATUS        RESTARTS   AGE
delete-me   0/1     Terminating   0          51s
keep-me     1/1     Running       0          54s

# delete it with the `terminate` command
$ kubectl terminate pod/delete-me
pod "delete-me" terminated

# verify
$ kubectl get pods
NAME      READY   STATUS    RESTARTS   AGE
keep-me   1/1     Running   0          82s

Contribution

Feel free to open issues if you find bugs or require more features. Also, PRs are welcome if you’re in the mood for that 🙌

License

This code is licensed under the Apache License, version 2.0.

About

kubectl-terminate, a kubectl plugin to remove finalizers and finally delete k8s resources

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published