Skip to content

Instantly share code, notes, and snippets.

@siredmar
Last active September 26, 2023 12:45
Show Gist options
  • Save siredmar/29bdea7015259165863c5eccedf55877 to your computer and use it in GitHub Desktop.
Save siredmar/29bdea7015259165863c5eccedf55877 to your computer and use it in GitHub Desktop.
kubectl remove finalizers plugin
#!/bin/bash
# Check if at least the resource type is provided
if [ "$#" -lt 1 ]; then
echo "Usage: $0 <resource_type> [<resource name>] [kubectl get args ...]"
exit 1
fi
RESOURCE_TYPE=$1
shift 1 # Shift the arguments to remove the first
# Get the resources using kubectl get
RESOURCES=$(kubectl get "$RESOURCE_TYPE" "$@" -o json)
# Try to extract resources using jq .items[]
RESOURCE_ITEMS=$(echo "$RESOURCES" | jq -c '.items // [.]')
# If the resource is still empty, exit with a message
if [ -z "$RESOURCE_ITEMS" ]; then
echo "No resources found for $RESOURCE_TYPE."
exit 0
fi
echo "$RESOURCE_ITEMS" | jq -c '.[]' | while read -r RESOURCE; do
NAME=$(echo "$RESOURCE" | jq -r '.metadata.name')
NAMESPACE=$(echo "$RESOURCE" | jq -r '.metadata.namespace' 2>/dev/null)
FINALIZERS=$(echo "$RESOURCE" | jq -r '.metadata.finalizers')
if [ "$FINALIZERS" != "null" ]; then
kubectl patch "$RESOURCE_TYPE" -n "$NAMESPACE" "$NAME" --type='json' -p='[{"op": "remove", "path": "/metadata/finalizers"}]' 2>/dev/null
fi
done
@siredmar
Copy link
Author

This needs to be placed somewhere in $PATH, e.g. ~/bin or /usr/local/bin
To check if the plugin is loaded use kubectl plugin list

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment