forked from kubevirt/kubevirt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cluster-clean.sh
executable file
·138 lines (118 loc) · 5.09 KB
/
cluster-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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env bash
#
# This file is part of the KubeVirt project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Copyright 2017 Red Hat, Inc.
#
set -ex
DOCKER_TAG=${DOCKER_TAG:-devel}
source hack/common.sh
source cluster-up/cluster/$KUBEVIRT_PROVIDER/provider.sh
source hack/config.sh
echo "Cleaning up ..."
# Delete KubeVirt CR, timeout after 10 seconds
set +e
(
cmdpid=$BASHPID
(
sleep 10
kill $cmdpid
) &
_kubectl -n ${namespace} delete kv kubevirt
)
_kubectl -n ${namespace} patch kv kubevirt --type=json -p '[{ "op": "remove", "path": "/metadata/finalizers" }]'
_kubectl patch cdi cdi --type=json -p '[{ "op": "remove", "path": "/metadata/finalizers" }]'
set -e
kubectl get vmsnapshots --all-namespaces -o=custom-columns=NAME:.metadata.name,NAMESPACE:.metadata.namespace,FINALIZERS:.metadata.finalizers --no-headers | grep vmsnapshot-protection | while read p; do
arr=($p)
name="${arr[0]}"
namespace="${arr[1]}"
_kubectl patch vmsnapshots $name -n $namespace --type=json -p '[{ "op": "remove", "path": "/metadata/finalizers" }]'
done
kubectl get vmsnapshotcontents --all-namespaces -o=custom-columns=NAME:.metadata.name,NAMESPACE:.metadata.namespace,FINALIZERS:.metadata.finalizers --no-headers | grep vmsnapshotcontent-protection | while read p; do
arr=($p)
name="${arr[0]}"
namespace="${arr[1]}"
_kubectl patch vmsnapshotcontents $name -n $namespace --type=json -p '[{ "op": "remove", "path": "/metadata/finalizers" }]'
done
# Remove finalizers from all running vmis, to not block the cleanup
_kubectl get vmis --all-namespaces -o=custom-columns=NAME:.metadata.name,NAMESPACE:.metadata.namespace,FINALIZERS:.metadata.finalizers --no-headers | grep foregroundDeleteVirtualMachine | while read p; do
arr=($p)
name="${arr[0]}"
namespace="${arr[1]}"
_kubectl patch vmi $name -n $namespace --type=json -p '[{ "op": "remove", "path": "/metadata/finalizers" }]'
done
_kubectl get vms --all-namespaces -o=custom-columns=NAME:.metadata.name,NAMESPACE:.metadata.namespace,FINALIZERS:.metadata.finalizers --no-headers | grep -e foregroundDeleteVirtualMachine -e orphan -e snapshot-source-protection | while read p; do
arr=($p)
name="${arr[0]}"
namespace="${arr[1]}"
_kubectl patch vm $name -n $namespace --type=json -p '[{ "op": "remove", "path": "/metadata/finalizers" }]'
done
# Delete Namespaces created by us.
managed_namespaces=(${namespace} ${cdi_namespace})
# Delete all traces of kubevirt
namespaces=(default ${namespace} ${cdi_namespace})
labels=("operator.kubevirt.io" "operator.cdi.kubevirt.io" "kubevirt.io" "cdi.kubevirt.io")
# Namespaced resources
for i in ${namespaces[@]}; do
for label in ${labels[@]}; do
_kubectl -n ${i} delete deployment -l ${label}
_kubectl -n ${i} delete ds -l ${label}
_kubectl -n ${i} delete rs -l ${label}
_kubectl -n ${i} delete pods -l ${label}
_kubectl -n ${i} delete services -l ${label}
_kubectl -n ${i} delete pvc -l ${label}
_kubectl -n ${i} delete rolebinding -l ${label}
_kubectl -n ${i} delete roles -l ${label}
_kubectl -n ${i} delete serviceaccounts -l ${label}
_kubectl -n ${i} delete configmaps -l ${label}
_kubectl -n ${i} delete secrets -l ${label}
_kubectl -n ${i} delete jobs -l ${label}
done
done
# Not namespaced resources
for label in ${labels[@]}; do
_kubectl delete validatingwebhookconfiguration -l ${label}
_kubectl delete pv -l ${label}
_kubectl delete clusterrolebinding -l ${label}
_kubectl delete clusterroles -l ${label}
_kubectl delete customresourcedefinitions -l ${label}
# W/A for https://github.com/kubernetes/kubernetes/issues/65818
_kubectl delete apiservices -l ${label} --wait=false
_kubectl get apiservices -l ${label} -o=custom-columns=NAME:.metadata.name,FINALIZERS:.metadata.finalizers --no-headers | grep foregroundDeletion | while read p; do
arr=($p)
name="${arr[0]}"
_kubectl -n ${i} patch apiservices $name --type=json -p '[{ "op": "remove", "path": "/metadata/finalizers" }]'
done
done
for i in ${managed_namespaces[@]}; do
if [ -n "$(_kubectl get ns | grep "${i} ")" ]; then
echo "Clean ${i} namespace"
_kubectl delete ns ${i}
start_time=0
sample=10
timeout=120
echo "Waiting for ${i} namespace to disappear ..."
while [ -n "$(_kubectl get ns | grep "${i} ")" ]; do
sleep $sample
start_time=$((current_time + sample))
if [[ $current_time -gt $timeout ]]; then
exit 1
fi
done
fi
done
sleep 2
echo "Done"