Created
May 10, 2023 08:30
-
-
Save syedmhashim/d571393aef1d6bfd65d19f94739720e7 to your computer and use it in GitHub Desktop.
Remove finalizers from kubernetes resources based on grep patterns or name of the resource
This file contains 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
#!/bin/bash | |
print_usage() { | |
echo " | |
////////////////////////////////////////////////////////////////////////////////////////////// | |
* Remove finalizer from kubernetes resources based on grep patterns or name of the resource * | |
////////////////////////////////////////////////////////////////////////////////////////////// | |
Usage: | |
kube_remove_finalizers --type=RESOURCE_TYPE [--pattern=PATTERN | --name=RESOURCE_NAME] | |
Example | |
kube_remove_finalizer --type=pod --pattern=nginx | |
Required Parameters: | |
--type: Type of resource. Example: pod, deployment etc. | |
Optional Parameters: | |
--pattern: A pattern suitable for grep command. | |
--name: Name of the resource. This flag will override --pattern when both are provided. | |
Note: You need to provide atleast one of --pattern or --name flag. | |
" | |
} | |
while [ $# -gt 0 ]; do | |
case "$1" in | |
-h|--help*) | |
print_usage | |
exit 0 | |
;; | |
--type=*) | |
type="${1#*=}" | |
;; | |
--name=*) | |
name="${1#*=}" | |
;; | |
--pattern=*) | |
pattern="${1#*=}" | |
;; | |
*) | |
printf "\nError: Invalid argument: $1\n" | |
print_usage | |
exit 1 | |
esac | |
shift | |
done | |
if [[ -z $type ]]; then | |
echo "--type flag is required" | |
exit 1 | |
fi | |
if [[ -z $name ]]; then | |
for resource in $(kubectl get $type --no-headers | grep "$pattern" | awk '{print$1}'); do | |
kubectl get $type "$resource" -o name | \ | |
xargs -I {} kubectl patch {} --type merge -p '{"metadata":{"finalizers": [null]}}' | |
done | |
else | |
kubectl get $type $name -o name | \ | |
xargs -I {} kubectl patch {} --type merge -p '{"metadata":{"finalizers": [null]}}' | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment