2021-11-29 08:07:17 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
2022-03-11 18:38:32 +00:00
|
|
|
source .utils
|
2021-11-29 08:07:17 +00:00
|
|
|
|
2022-03-11 18:38:32 +00:00
|
|
|
NAMESPACE="$(kubectl config view --minify --output 'jsonpath={..namespace}' &>/dev/null)"
|
|
|
|
set -e
|
|
|
|
NAMESPACE=${NAMESPACE:-default}
|
2021-11-29 08:07:17 +00:00
|
|
|
LABEL=""
|
|
|
|
ALL_NAMESPACES=false
|
|
|
|
|
|
|
|
print_usage() {
|
2022-03-11 18:38:32 +00:00
|
|
|
blue "kube-clean-replicasets - Remove all olf ReplicaSets with 0 desired pods"
|
2021-11-29 08:07:17 +00:00
|
|
|
echo " "
|
2022-03-11 18:38:32 +00:00
|
|
|
underline "Usage:"
|
2021-11-29 08:07:17 +00:00
|
|
|
echo "kube-clean-replicasets [options]"
|
|
|
|
echo " "
|
2022-03-11 18:38:32 +00:00
|
|
|
underline "Options:"
|
2021-11-29 08:07:17 +00:00
|
|
|
echo "-h, --help show this help text"
|
|
|
|
echo "-n, --namespace the namespace the to search in"
|
|
|
|
echo "-l, --selector the label selector to match on"
|
|
|
|
echo "-A, --all-namespaces match resources in all namespaces (default: false)"
|
|
|
|
}
|
|
|
|
|
|
|
|
while test $# -gt 0; do
|
|
|
|
case "$1" in
|
|
|
|
-n|--namespace)
|
|
|
|
shift
|
|
|
|
NAMESPACE=$1
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
-l|--selector)
|
|
|
|
shift
|
|
|
|
LABEL=$1
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
-A|--all-namespaces)
|
|
|
|
ALL_NAMESPACES=true
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
-h|--help)
|
|
|
|
print_usage
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
break
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
if [[ "${LABEL}" != "" ]]; then
|
|
|
|
LABEL="-l ${LABEL}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ $ALL_NAMESPACES ]; then
|
|
|
|
kubectl get replicasets --all-namespaces $LABEL -o jsonpath='{range .items[?(@.status.replicas==0)]}--namespace {@.metadata.namespace} {@.metadata.name};{end}' | \
|
|
|
|
tr ";" "\n" | \
|
|
|
|
xargs -I {} sh -c "kubectl delete rs {}"
|
|
|
|
else
|
|
|
|
kubectl get replicasets --namespace $NAMESPACE $LABEL -o jsonpath='{range .items[?(@.status.replicas==0)]}--namespace {@.metadata.namespace} {@.metadata.name};{end}' | \
|
|
|
|
tr ";" "\n" | \
|
|
|
|
xargs -I {} sh -c "kubectl delete rs {}"
|
|
|
|
fi
|