Last active
May 18, 2026 20:11
-
-
Save dudo/fcf7d9ccb14d31072e41f5685933ed30 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| #!/usr/bin/env bash | |
| set -euo pipefail | |
| # Find all namespaces stuck in Terminating and clear known blocking finalizers. | |
| STUCK_NAMESPACES=$(kubectl get ns --field-selector status.phase=Terminating -o json | \ | |
| jq -r '.items[] | select(.metadata.annotations["argocd.argoproj.io/tracking-id"] != null) | .metadata.name') | |
| if [[ -z "$STUCK_NAMESPACES" ]]; then | |
| echo "No terminating namespaces found." | |
| exit 0 | |
| fi | |
| echo "Found terminating namespaces: $STUCK_NAMESPACES" | |
| echo "" | |
| while IFS= read -r NS; do | |
| echo "=== Processing: $NS ===" | |
| # Discover stuck resource types from the namespace's own status condition | |
| REMAINING_RESOURCES=$(kubectl get ns "$NS" -o json | \ | |
| jq -r ' | |
| .status.conditions[]? | | |
| select(.type == "NamespaceContentRemaining") | | |
| .message | | |
| scan("[a-z][a-zA-Z0-9-]+\\.[a-z][a-zA-Z0-9.-]+ has") | | |
| rtrimstr(" has") | |
| ') | |
| if [[ -z "$REMAINING_RESOURCES" ]]; then | |
| echo " No remaining resources found in status, skipping." | |
| echo "" | |
| continue | |
| fi | |
| echo " Stuck resources: $(echo "$REMAINING_RESOURCES" | tr '\n' ' ')" | |
| while IFS= read -r RESOURCE; do | |
| ITEMS=$(kubectl get "$RESOURCE" -n "$NS" -o name 2>/dev/null || true) | |
| if [[ -z "$ITEMS" ]]; then | |
| echo " No instances of $RESOURCE found, skipping." | |
| continue | |
| fi | |
| while IFS= read -r ITEM; do | |
| echo " Clearing finalizers on $ITEM" | |
| kubectl patch "$ITEM" -n "$NS" --type merge -p '{"metadata":{"finalizers":null}}' | |
| done <<< "$ITEMS" | |
| done <<< "$REMAINING_RESOURCES" | |
| echo "" | |
| done <<< "$STUCK_NAMESPACES" | |
| echo "Done. Namespaces should finish terminating shortly." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment