Skip to content

Instantly share code, notes, and snippets.

@rammanokar
Created September 23, 2024 04:22
Show Gist options
  • Save rammanokar/fb100263f51b943c7356692811032f5e to your computer and use it in GitHub Desktop.
Save rammanokar/fb100263f51b943c7356692811032f5e to your computer and use it in GitHub Desktop.
finalize_and_delete_namespace.sh
#!/bin/bash
# ----------------------------------------------------------------------------
# Script: finalize_and_delete_namespace.sh
# Description: This script resolves a stuck Kubernetes namespace that is in
# "Terminating" state by removing the finalizers from the namespace,
# and then optionally deletes the namespace upon user confirmation.
#
# Created based on the steps provided in:
# https://repost.aws/knowledge-center/eks-terminated-namespaces
#
# Usage: ./finalize_and_delete_namespace.sh <namespace>
#
# Requirements:
# - kubectl: Kubernetes command-line tool
# - jq: Command-line JSON processor for manipulating JSON data
#
# Parameters:
# <namespace> : The name of the stuck Kubernetes namespace that needs to be finalized and deleted.
#
# Steps:
# 1. Fetch the namespace details in JSON format and store it in a temporary file.
# 2. Remove the 'finalizers' block from the JSON file to allow namespace finalization.
# 3. Replace the namespace spec with the modified JSON to complete the finalization.
# 4. Ask for user confirmation to delete the namespace.
# 5. If approved, delete the namespace using 'kubectl delete namespace'.
# 6. Clean up temporary files created during the process.
#
# ----------------------------------------------------------------------------
# Step 1: Validate input parameters
# Check if a namespace argument is provided by the user
if [ -z "$1" ]; then
echo "Usage: $0 <namespace>"
echo "Error: Namespace argument missing."
exit 1
fi
# Assign the provided namespace to a variable
NAMESPACE=$1
# Step 2: Fetch namespace details in JSON format
# The 'kubectl get namespace' command fetches the namespace details and outputs them in JSON format.
# The output is redirected to a temporary file named 'tempfile.json'.
echo "Fetching namespace details for: $NAMESPACE"
kubectl get namespace "$NAMESPACE" -o json > tempfile.json
# Step 3: Verify if the tempfile.json was created successfully
# If the file does not exist, the command might have failed, so we exit the script with an error message.
if [ ! -f tempfile.json ]; then
echo "Error: Failed to fetch namespace details. Exiting."
exit 1
fi
# Step 4: Remove the 'finalizers' block from the JSON
# We use 'jq', a command-line JSON processor, to modify the JSON file.
# The 'del(.spec.finalizers)' command removes the 'finalizers' field from the 'spec' section of the JSON.
# The modified JSON is saved in a new file 'tempfile_cleaned.json'.
echo "Removing finalizers from the namespace JSON"
jq 'del(.spec.finalizers)' tempfile.json > tempfile_cleaned.json
# Step 5: Verify if the cleaned JSON file was created successfully
# If the 'tempfile_cleaned.json' is not created, we exit the script with an error message.
if [ ! -f tempfile_cleaned.json ]; then
echo "Error: Failed to modify the namespace JSON. Exiting."
exit 1
fi
# Step 6: Apply the changes to finalize the namespace deletion
# The 'kubectl replace --raw' command is used to apply the changes in the cleaned JSON file.
# This command finalizes the namespace deletion process.
echo "Applying the changes to finalize the deletion of the namespace: $NAMESPACE"
kubectl replace --raw "/api/v1/namespaces/$NAMESPACE/finalize" -f ./tempfile_cleaned.json
# Step 7: Ask for user confirmation to delete the namespace
# After finalization, ask the user if they would like to proceed with deleting the namespace.
read -p "Do you want to delete the namespace '$NAMESPACE'? (y/n): " CONFIRM
if [[ "$CONFIRM" =~ ^[Yy]$ ]]; then
# Step 8: Proceed to delete the namespace if the user confirms
echo "Deleting namespace: $NAMESPACE"
kubectl delete namespace "$NAMESPACE"
else
echo "Namespace deletion skipped."
fi
# Step 9: Clean up temporary files
# Once the namespace finalization is complete, we remove the temporary files 'tempfile.json' and 'tempfile_cleaned.json'.
rm -f tempfile.json tempfile_cleaned.json
# Step 10: Confirm the namespace finalization and deletion process is complete
echo "Namespace $NAMESPACE finalization process complete."
if [[ "$CONFIRM" =~ ^[Yy]$ ]]; then
echo "Namespace $NAMESPACE has been deleted."
else
echo "Namespace $NAMESPACE was not deleted."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment