2022-03-03 09:22:27 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
2022-03-11 18:38:32 +00:00
|
|
|
source .utils
|
2022-03-03 09:22:27 +00:00
|
|
|
|
2022-03-11 18:38:32 +00:00
|
|
|
NAMESPACE="$(kubectl config view --minify --output 'jsonpath={..namespace}' &>/dev/null)"
|
|
|
|
set -e
|
2022-03-03 09:22:27 +00:00
|
|
|
NAMESPACE=${NAMESPACE:-default}
|
2022-04-05 20:26:27 +00:00
|
|
|
SELECTOR=""
|
2024-09-19 08:54:39 +00:00
|
|
|
CONTEXT="$(kubectl config current-context)"
|
2022-03-03 09:22:27 +00:00
|
|
|
|
|
|
|
print_usage() {
|
2022-03-11 18:38:32 +00:00
|
|
|
blue "kube-force-delete - Force delete resources, even those with finalizers"
|
2022-03-03 09:22:27 +00:00
|
|
|
echo " "
|
2022-03-11 18:38:32 +00:00
|
|
|
underline "Usage:"
|
2022-03-03 09:22:27 +00:00
|
|
|
echo "kube-force-delete [RESOURCE_TYPE] [RESOURCE_NAME]"
|
|
|
|
echo " "
|
2022-03-11 18:38:32 +00:00
|
|
|
underline "Options:"
|
2022-03-03 09:22:27 +00:00
|
|
|
echo "-h, --help show this help text"
|
2024-09-19 08:54:39 +00:00
|
|
|
echo " --context sets the context from the kubeconfig to use"
|
2022-03-03 09:22:27 +00:00
|
|
|
echo "-n, --namespace the namespace the resource is in (default: current namespace)"
|
|
|
|
}
|
|
|
|
|
2024-09-17 11:28:54 +00:00
|
|
|
YOLO="0"
|
2022-03-03 09:22:27 +00:00
|
|
|
POS_ARGS=()
|
|
|
|
|
|
|
|
while test $# -gt 0; do
|
|
|
|
case "$1" in
|
|
|
|
-n|--namespace)
|
|
|
|
shift
|
|
|
|
NAMESPACE=$1
|
|
|
|
shift
|
|
|
|
;;
|
2022-04-05 20:26:27 +00:00
|
|
|
-l|--selector)
|
|
|
|
shift
|
|
|
|
SELECTOR="$1"
|
|
|
|
shift
|
|
|
|
;;
|
2022-03-03 09:22:27 +00:00
|
|
|
-h|--help)
|
|
|
|
print_usage
|
|
|
|
exit 0
|
|
|
|
;;
|
2024-09-19 08:54:39 +00:00
|
|
|
--context)
|
|
|
|
shift
|
|
|
|
CONTEXT="$1"
|
|
|
|
shift
|
|
|
|
;;
|
2024-09-17 11:28:54 +00:00
|
|
|
--yolo)
|
|
|
|
YOLO="1"
|
|
|
|
shift
|
|
|
|
;;
|
2022-03-03 09:22:27 +00:00
|
|
|
/)
|
|
|
|
# We want to ignore slash seperators between resource types and names
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
POS_ARGS+=(`echo $1 | tr '/' ' '`)
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2022-04-05 20:26:27 +00:00
|
|
|
if [ ${#POS_ARGS[@]} -lt 2 ] && [[ "${SELECTOR}" == "" ]]; then
|
2022-03-03 09:22:27 +00:00
|
|
|
echo "Please provide the resource type and name to delete"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2022-04-05 20:26:27 +00:00
|
|
|
function deleteResource() {
|
2022-10-25 08:51:33 +00:00
|
|
|
RES="${1//.v1/}"
|
|
|
|
echo "Deleting ${RES}"
|
2024-09-19 08:54:39 +00:00
|
|
|
kubectl --context "${CONTEXT}" patch -p '{"metadata":{"finalizers":null}}' --type=merge -n ${NAMESPACE} ${RES} 1>/dev/null|| printf ""
|
|
|
|
kubectl --context "${CONTEXT}" delete -n ${NAMESPACE} ${RES} 2>/dev/null || printf ""
|
2022-04-05 20:26:27 +00:00
|
|
|
}
|
|
|
|
|
2022-03-18 07:37:01 +00:00
|
|
|
printf "⚠️ This could leave cloud resources undeleted if finalizers aren't honoured ⚠️\n\n"
|
2022-04-05 20:26:27 +00:00
|
|
|
|
|
|
|
RESOURCES=()
|
|
|
|
|
|
|
|
if [[ "${SELECTOR}" == "" ]]; then
|
|
|
|
printf "Are you sure you want to delete ${POS_ARGS[0]}/${POS_ARGS[1]}? (y/n): "
|
|
|
|
RESOURCES=("${POS_ARGS[0]}/${POS_ARGS[1]}")
|
|
|
|
else
|
|
|
|
printf "Are you sure you want to delete all matching '${SELECTOR}'? (y/n): "
|
|
|
|
fi
|
2024-09-17 11:28:54 +00:00
|
|
|
|
|
|
|
if [[ "${YOLO}" == 0 ]]; then
|
|
|
|
read CONFIRM
|
|
|
|
else
|
|
|
|
echo ""
|
|
|
|
echo ""
|
|
|
|
orange "YOLO MODE!!! (What could go wrong?!)"
|
|
|
|
CONFIRM="y"
|
|
|
|
fi
|
2022-03-03 09:22:27 +00:00
|
|
|
|
|
|
|
if [ "${CONFIRM}" = "y" ]; then
|
2022-03-18 07:37:01 +00:00
|
|
|
echo ""
|
2022-03-03 09:22:27 +00:00
|
|
|
echo "💣 OK, I hope you know what you're doing..."
|
2024-09-17 11:28:54 +00:00
|
|
|
|
|
|
|
if [[ "${SELECTOR}" != "" ]]; then
|
|
|
|
SELECTOR="-l ${SELECTOR}"
|
2024-09-19 08:54:39 +00:00
|
|
|
NAMES="$(kubectl --context "${CONTEXT}" api-resources --verbs list -o name 2>/dev/null | tr '\n' ,)"
|
|
|
|
RESOURCES=$(kubectl --context "${CONTEXT}" get "${NAMES::${#NAMES}-1}" --ignore-not-found ${SELECTOR} -n ${NAMESPACE} -o go-template='{{range.items}}{{.kind}}.{{.apiVersion}}/{{.metadata.name}}{{"\n"}}{{end}}' 2>/dev/null | tr '[:upper:]' '[:lower:]' | sed -r "s|/(v.+)/|/|g")
|
2024-09-17 11:28:54 +00:00
|
|
|
fi
|
|
|
|
|
2022-04-05 20:26:27 +00:00
|
|
|
IFS='
|
|
|
|
'
|
|
|
|
for RESOURCE in ${RESOURCES}
|
|
|
|
do
|
|
|
|
deleteResource ${RESOURCE}
|
|
|
|
done
|
2022-03-03 09:22:27 +00:00
|
|
|
else
|
|
|
|
echo "Aborting..."
|
|
|
|
exit 1
|
|
|
|
fi
|