Skip to content

Instantly share code, notes, and snippets.

@sudomann
Created August 26, 2019 07:20
Show Gist options
  • Save sudomann/09cdd1137b1d1a535f8453cfff7f4f8f to your computer and use it in GitHub Desktop.
Save sudomann/09cdd1137b1d1a535f8453cfff7f4f8f to your computer and use it in GitHub Desktop.
So I don't have to keep looking for this and redoing it: https://github.com/kubernetes/kubernetes/issues/60807#issuecomment-524653983
#!/bin/bash
NS=$1
NS_CHECK=$(kubectl get ns | grep $NS | awk '{print $1}')
if [[ $NS_CHECK == $NS ]];
then
echo "Namespace $NS will be deleted"
else
echo "No such namespace named $NS"
exit 1
fi
kubectl delete ns $NS --force=true --grace-period=0 > /dev/null 2>&1 &
echo "Start kubectl proxy on port 8080"
kubectl proxy --port=8080 &
PROXY_PID=$!
sleep 1
kubectl get namespace $NS -o json > /tmp/ns.json
jq 'del(.spec.finalizers[])' /tmp/ns.json > /tmp/new-ns.json
echo "Delete namespace $NS"
sleep 1
curl -k -H "Content-Type: application/json" -X PUT --data-binary @/tmp/new-ns.json http://127.0.0.1:8080/api/v1/namespaces/$NS/finalize > /dev/null 2>&1
rm -rf /tmp/ns.json /tmp/new-ns.json
echo "Namespace $NS deleted"
echo "Stop kubectl proxy"
kill $PROXY_PID
wait $PROXY_PID 2>/dev/null
sleep 1
echo "Kubectl proxy stoped"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment