Skip to content

Instantly share code, notes, and snippets.

@slotlocker2
Created September 5, 2019 09:03
Show Gist options
  • Save slotlocker2/4118523b7481669f17902b92f753797e to your computer and use it in GitHub Desktop.
Save slotlocker2/4118523b7481669f17902b92f753797e to your computer and use it in GitHub Desktop.
Terminate stuck namespaces
#!/bin/bash
# Based off of https://www.ibm.com/support/knowledgecenter/en/SSBS6K_3.1.1/troubleshoot/ns_terminating.html
# Check for jq
command -v jq 2>&1 > /dev/null
if [ $? -ne 0 ]
then
echo "Either jq is not installed or not in PATH. Exiting."
exit 1
fi
# Check for namespace
if [ $# -ne 1 ]
then
echo "Please specify the name of the namespace to be terminated"
exit 1
fi
NAMESPACE="$1"
# Namespace manifest file
NAMESPACE_MANIFEST_FILE="/tmp/$NAMESPACE.json"
rm $NAMESPACE_MANIFEST_FILE 2>/dev/null
kubectl get namespace $NAMESPACE -o json | jq '.spec.finalizers = []' > $NAMESPACE_MANIFEST_FILE
# Check if kube-proxy is running
nc -w 2 -z localhost 8001
if [ $? -ne 0 ]
then
kubectl proxy &
sleep 3
fi
# Finalize the termination
curl -k -H "Content-Type: application/json" -X PUT --data-binary @$NAMESPACE_MANIFEST_FILE "http://127.0.0.1:8001/api/v1/namespaces/$NAMESPACE/finalize"
killall kubectl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment