@@ -424,117 +424,85 @@ jobs:
424424 run : |
425425 #!/bin/bash
426426
427- echo "🗑️ Starting enhanced namespace deletion process..."
427+ echo "Starting namespace deletion process..."
428428
429- # Function to completely force delete a namespace
430- force_delete_namespace () {
429+ # Function to delete a namespace with proper error handling
430+ delete_namespace () {
431431 local ns=$1
432- echo "========== Processing namespace: $ns ========== "
432+ echo "Processing namespace: $ns"
433433
434+ # Check if namespace exists
434435 if ! kubectl get namespace "$ns" &>/dev/null; then
435- echo "✅ Namespace $ns does not exist, skipping..."
436+ echo "Namespace $ns does not exist, skipping..."
436437 return 0
437438 fi
438439
439- echo "📋 Current namespace status:"
440- kubectl get namespace $ns -o wide || true
440+ echo "Namespace $ns exists, proceeding with deletion..."
441441
442- # Step 1: Final resource cleanup in the namespace
443- echo "🧹 Final cleanup of all resources in namespace $ns..."
442+ # Step 1: Remove finalizers from all resources
443+ echo "Removing finalizers from resources in $ns..."
444+ kubectl api-resources --verbs=list --namespaced -o name 2>/dev/null | \
445+ grep -v events | \
446+ xargs -I {} bash -c "kubectl get {} -n $ns -o name 2>/dev/null | xargs -I {} kubectl patch {} -n $ns -p '{\"metadata\":{\"finalizers\":[]}}' --type=merge 2>/dev/null || true"
444447
445- # Remove finalizers from all resources in the namespace
446- for resource_type in $(kubectl api-resources --verbs=list --namespaced -o name 2>/dev/null | grep -v events); do
447- kubectl get $resource_type -n $ns -o json 2>/dev/null | \
448- jq -r '.items[]? | select(.metadata.finalizers) | .metadata.name' 2>/dev/null | \
449- while read resource_name; do
450- if [[ -n "$resource_name" ]]; then
451- echo " Removing finalizers from $resource_type/$resource_name"
452- kubectl patch $resource_type $resource_name -n $ns -p '{"metadata":{"finalizers":[]}}' --type=merge 2>/dev/null || true
453- fi
454- done
455- done
448+ # Step 2: Delete the namespace with extended timeout
449+ echo "Deleting namespace $ns..."
450+ if kubectl delete namespace "$ns" --timeout=180s --ignore-not-found; then
451+ echo "Successfully deleted namespace $ns"
452+ return 0
453+ fi
456454
457- # Step 2: Try graceful deletion first
458- echo "🔄 Attempting graceful namespace deletion..."
459- kubectl delete namespace $ns --timeout=60s --ignore-not-found &
460- DELETE_PID=$!
455+ # Step 3: Force deletion if graceful deletion failed
456+ echo "Graceful deletion failed, attempting force deletion..."
461457
462- # Wait for graceful deletion
463- sleep 30
458+ # Remove namespace finalizers
459+ kubectl patch namespace "$ns" -p '{"metadata":{"finalizers":[]}}' --type=merge 2>/dev/null || true
464460
465- # Step 3: If still exists, force delete
466- if kubectl get namespace $ns --ignore-not-found 2>/dev/null; then
467- echo "⚡ Graceful deletion failed, forcing deletion..."
468-
469- # Kill the background delete process
470- kill $DELETE_PID 2>/dev/null || true
471-
472- # Get current namespace JSON and remove finalizers
473- kubectl get namespace $ns -o json | \
474- jq 'del(.spec.finalizers[])' | \
475- kubectl replace --raw "/api/v1/namespaces/$ns/finalize" -f - 2>/dev/null || true
476-
477- # Alternative approach - patch the namespace directly
478- kubectl patch namespace $ns -p '{"metadata":{"finalizers":[]}}' --type=merge 2>/dev/null || true
479-
480- # Wait a bit more
481- sleep 15
482-
483- # Final check and force if needed
484- if kubectl get namespace $ns --ignore-not-found 2>/dev/null; then
485- echo "🚨 Trying nuclear option - direct deletion..."
486-
487- # Delete the namespace object directly
488- kubectl delete namespace $ns --force --grace-period=0 2>/dev/null || true
489-
490- # Patch with empty spec
491- kubectl patch namespace $ns -p '{"spec":{"finalizers":[]}}' --type=merge 2>/dev/null || true
492- kubectl patch namespace $ns -p '{"metadata":{"finalizers":[]}}' --type=merge 2>/dev/null || true
493-
494- sleep 10
495- fi
496- fi
461+ # Force delete with zero grace period
462+ kubectl delete namespace "$ns" --force --grace-period=0 2>/dev/null || true
497463
498- # Final verification
499- if kubectl get namespace $ns --ignore-not-found 2>/dev/null; then
500- echo "❌ WARNING: Namespace $ns still exists after all attempts"
501- echo "📋 Final namespace details:"
502- kubectl get namespace $ns -o yaml || true
464+ # Wait a moment and check
465+ sleep 10
466+
467+ if kubectl get namespace "$ns" &>/dev/null; then
468+ echo "WARNING: Namespace $ns still exists after force deletion attempts"
503469 return 1
504470 else
505- echo "✅ Successfully deleted namespace $ns"
471+ echo "Successfully force- deleted namespace $ns"
506472 return 0
507473 fi
508474 }
509475
510476 # Array of namespaces to delete
511- NAMESPACES=("${{ vars.APP_NAMESPACE }} " "${{ vars.MONITORING_NAMESPACE }} " "${{ vars.ARGOCD_NAMESPACE }} " "ingress-nginx" "${{ vars.KARPENTER_NAMESPACE }} ")
477+ NAMESPACES=("my-solar-system-app-namespace " "my-solar-system-app-monitoring " "my-solar-system-app-argocd " "ingress-nginx" "karpenter ")
512478
513- # Delete each namespace
479+ # Track failed deletions
514480 FAILED_NAMESPACES=()
481+
482+ # Delete each namespace
515483 for ns in "${NAMESPACES[@]}"; do
516484 if [[ -n "$ns" ]]; then
517- if ! force_delete_namespace "$ns"; then
485+ if ! delete_namespace "$ns"; then
518486 FAILED_NAMESPACES+=("$ns")
519487 fi
520- echo ""
488+ echo "---------------------------------------- "
521489 fi
522490 done
523491
524492 # Summary
525- echo "========== NAMESPACE CLEANUP SUMMARY ========== "
526- echo "📊 Remaining namespaces:"
527- kubectl get namespaces || true
493+ echo "CLEANUP SUMMARY"
494+ echo "Remaining namespaces:"
495+ kubectl get namespaces 2>/dev/null || echo "Could not list namespaces"
528496
529497 if [[ ${#FAILED_NAMESPACES[@]} -eq 0 ]]; then
530- echo "✅ All target namespaces successfully deleted!"
498+ echo "All target namespaces successfully deleted"
499+ exit 0
531500 else
532- echo "❌ Failed to delete namespaces: ${FAILED_NAMESPACES[*]}"
533- echo "⚠️ You may need to check these manually after terraform destroy completes"
534- # Don't fail the workflow for namespace cleanup issues
501+ echo "Failed to delete namespaces: ${FAILED_NAMESPACES[*]}"
502+ echo "These may need manual cleanup"
503+ # Don't fail the workflow - this is cleanup, not critical infrastructure
504+ exit 0
535505 fi
536-
537- echo "✅ Namespace deletion process completed"
538506 continue-on-error : true
539507
540508 # ==================================================
0 commit comments