Kubernetes: Deleting Namespace Stuck at Terminating State

Problem

有時候會發現 namespace 一直在 teminating,不會結束。

Error from server (Conflict): Operation cannot be fulfilled on namespaces "demo":
The system is ensuring all content is removed from this namespace. Upon completion,
this namespace will automatically be purged by the system.

Solution

開啟一個 terminal 執行:

~ kubectl proxy
Starting to serve on 127.0.0.1:8001

另一個 terminal 執行:

把 ns=demo 換成你想要刪除的 namespace

ns=demo
kubectl get namespace $ns -o json |sed 's/"kubernetes"//g' > tmp.json
curl -k -H "Content-Type: application/json" -X PUT --data-binary @tmp.json http://127.0.0.1:8001/api/v1/namespaces/$ns/finalize

or

ns=demo
curl -X PUT \
--data-binary @<(kubectl get namespace $ns -o json | sed 's/"kubernetes"//g') \
-H "Content-Type: application/json" \
http://127.0.0.1:8001/api/v1/namespaces/$ns/finalize

Inspired by

https://github.com/kubernetes/kubernetes/issues/60807#issuecomment-408599873