Last active
May 10, 2023 08:32
-
-
Save syedmhashim/2c8f8ebdefdf47dcfd450c7bbcc26f69 to your computer and use it in GitHub Desktop.
Delete kubernetes resources based on grep patterns
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 " | |
/////////////////////////////////////////////////////// | |
* Delete kubernetes resources based on grep patterns * | |
////////////////////////////////////////////////////// | |
Usage: | |
kube_delete --resource=RESOURCE_TYPE --pattern=PATTERN | |
Example | |
kube_delete --resource=pod --pattern=nginx | |
Required Parameters: | |
--resource: Type of resource. Example: pod, deployment etc. | |
Optional Parameters: | |
--pattern: A pattern suitable for grep command. If no pattern is provided, delete_resources | |
will try to delete all the instances of that resource type in the current namespace. | |
" | |
} | |
while [ $# -gt 0 ]; do | |
case "$1" in | |
-h|--help*) | |
print_usage | |
exit 0 | |
;; | |
--resource=*) | |
resource="${1#*=}" | |
;; | |
--pattern=*) | |
pattern="${1#*=}" | |
;; | |
*) | |
printf "\nError: Invalid argument: $1\n" | |
print_usage | |
exit 1 | |
esac | |
shift | |
done | |
if [[ -z $resource ]]; then | |
echo "--resource flag is required" | |
exit 1 | |
fi | |
kubectl get $resource --no-headers | grep "$pattern" | awk '{print$1}' | xargs kubectl delete $resource |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment