-
Notifications
You must be signed in to change notification settings - Fork 0
/
clean.sh
executable file
·49 lines (40 loc) · 1.12 KB
/
clean.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env bash
K8S_CLI="kubectl"
FILTER=""
error() {
echo "Error: invalid parameters!" >&2
usage >&2
exit 1
}
usage() {
echo "Usage: ./clean.sh -n <NAMESPACE> -c <CRD>"
echo "-n <NAMESPACE>: Kubernetes namespace"
echo "-c <CRD>: Kubernetes custom resource definition"
echo "-f <FILTER>: Prefix filter (perform only on name begining with filter)"
echo "-h: Get the help"
}
main() {
if [[ ! $NS ]] || [[ ! $CRD ]]; then
error
fi
"${K8S_CLI}" -n "${NS}" get "${CRD}" | while read name trash; do
if [[ $name == "NAME" ]] || [[ ! $name =~ "${FILTER}".* ]]; then
continue
fi
echo "[main] deleting ${NS}/${CRD}/${name}"
"${K8S_CLI}" -n "${NS}" get "${CRD}" "${name}" -o yaml | yq e ".metadata.finalizers = []" - | kubectl -n "${NS}" apply -f -
"${K8S_CLI}" -n "${NS}" delete "${CRD}" "${name}" >/dev/null 2>&1 || :
done
}
[[ $# -lt 1 ]] && error
while getopts ":n:c:f:h" option; do
case "$option" in
n) NS="${OPTARG}" ;;
c) CRD="${OPTARG}" ;;
f) FILTER="${OPTARG}" ;;
:) error ;;
h) usage ;;
*) error ;;
esac
done
main