Last active
July 29, 2023 20:57
-
-
Save codesenju/e08998e8da083524f5e77993ed46728c to your computer and use it in GitHub Desktop.
Removes a namespace stuck in terminating status
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
# Check if the NAMESPACE argument is provided | |
if [ -z "$1" ]; then | |
echo "Error: NAMESPACE argument not provided" | |
echo "Usage: $0 NAMESPACE" | |
exit 1 | |
fi | |
# Check if more than one argument is provided | |
if [ $# -gt 1 ]; then | |
echo "Error: Too many arguments provided" | |
echo "Usage: $0 NAMESPACE" | |
exit 1 | |
fi | |
# Get the namespace from the first argument | |
NAMESPACE=$1 | |
echo "Getting namespace ${NAMESPACE}..." | |
# Save the output of the command in a variable | |
output=$(kubectl get namespace ${NAMESPACE} -o json) | |
if [[ $(echo $output | jq '.spec.finalizers') == "null" ]]; then | |
# Run kubectl delete ns $NAMESPACE in the background | |
kubectl delete ns $NAMESPACE & | |
# Get the process ID of kubectl delete ns $NAMESPACE | |
pid=$! | |
# Set a timeout of 8 seconds | |
timeout=8 | |
# Wait for kubectl delete ns $NAMESPACE to complete or until the timeout is reached | |
while kill -0 $pid 2> /dev/null && ((timeout-- > 0)); do sleep 1; done | |
# If kubectl delete ns $NAMESPACE is still running after the timeout, terminate it and rerun the script from the top | |
if kill -0 $pid 2> /dev/null; then | |
kill $pid | |
exec "$0" "$@" | |
fi | |
else | |
echo "Editing JSON response to remove finalizer..." | |
# Edit the JSON response to remove the finalizer | |
edited_output=$(echo $output | jq 'del(.spec.finalizers[])') | |
TEMPDIR=$(mktemp -d) | |
# Save the edited JSON to a temporary file | |
echo $edited_output > $TEMPDIR/tmp.json | |
echo "Running kubectl proxy in the background..." | |
# Run kubectl proxy in the background | |
kubectl proxy & | |
sleep 5 | |
# Get the process ID of kubectl proxy | |
proxy_pid=$! | |
echo "Executing curl command..." | |
# Execute the curl command and retry up to 3 times if it fails | |
retries=0 | |
max_retries=3 | |
until [ $retries -ge $max_retries ] | |
do | |
curl -k -H "Content-Type: application/json" -X PUT --data-binary @$TEMPDIR/tmp.json http://127.0.0.1:8001/api/v1/namespaces/${NAMESPACE}/finalize && break | |
retries=$[$retries+1] | |
echo "Curl command failed, retrying... ($retries/$max_retries)" | |
done | |
if [ $retries -ge $max_retries ]; then | |
echo "Curl command failed after $max_retries attempts, exiting." | |
exit 1 | |
fi | |
echo "Ending kubectl proxy process..." | |
# End the kubectl proxy process | |
kill $proxy_pid | |
echo "Removing temporary file..." | |
# Remove tmp file | |
rm -rf $TEMPDIR/tmp.json | |
fi | |
echo "Done." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
curl -sL https://gist.githubusercontent.com/codesenju/e08998e8da083524f5e77993ed46728c/raw/c1cbb3bfacd76b1d4778de1ec84be2a36b1a4587/force-remove-namespace.sh | bash -s -- [NAMESPACE]