Skip to content

Instantly share code, notes, and snippets.

@pavelanni
Created October 30, 2024 16:53
Show Gist options
  • Save pavelanni/7a6c9dcca0b18c088264bec2160b3b58 to your computer and use it in GitHub Desktop.
Save pavelanni/7a6c9dcca0b18c088264bec2160b3b58 to your computer and use it in GitHub Desktop.
Remove finalizers from PVCs and Secrets
#!/bin/bash
namespace=$1
if [ -z "$namespace" ]; then
echo "Usage: $0 <namespace>"
exit 1
fi
# Check if kubectl is available
if ! command -v kubectl &>/dev/null; then
echo "kubectl not found"
exit 1
fi
# Check if jq is available
if ! command -v jq &>/dev/null; then
echo "jq not found"
exit 1
fi
# Function to remove finalizers
remove_finalizers() {
resource_type=$1
resource_name=$2
echo "Removing finalizers from $resource_type: $resource_name"
kubectl patch $resource_type $resource_name -n $namespace \
--type=json \
-p='[{"op": "remove", "path": "/metadata/finalizers"}]' 2>/dev/null
}
# Process PVCs
echo "Processing PVCs..."
# Get all PVCs with finalizers
pvcs=$(kubectl get pvc -n $namespace -o json | jq -r '.items[] | select(.metadata.finalizers) | .metadata.name')
for pvc in $pvcs; do
remove_finalizers pvc $pvc
done
# Process Secrets
echo "Processing Secrets..."
# Get all Secrets with finalizers
secrets=$(kubectl get secrets -n $namespace -o json | jq -r '.items[] | select(.metadata.finalizers) | .metadata.name')
for secret in $secrets; do
remove_finalizers secret $secret
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment