forked from kubevirt/kubevirt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcluster-clean.sh
executable file
·119 lines (98 loc) · 4.3 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
#!/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
function patch_remove_finalizers() {
_kubectl patch --type=json -p '[{ "op": "remove", "path": "/metadata/finalizers" }]' $@
}
function delete_kubevirt_cr() {
# Delete KubeVirt CR, timeout after 10 seconds
set +e
_kubectl -n ${namespace} delete kv kubevirt --timeout=10s --ignore-not-found || true
_kubectl -n cdi delete service cdi-uploadproxy-nodeport || true
patch_remove_finalizers -n ${namespace} kv kubevirt
set -e
}
function remove_finalizers() {
_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
local arr=($p)
local name="${arr[0]}"
local ns="${arr[1]}"
patch_remove_finalizers -n $ns vmsnapshots $name
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
local arr=($p)
local name="${arr[0]}"
local ns="${arr[1]}"
patch_remove_finalizers -n $ns vmsnapshotcontents $name
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
local arr=($p)
local name="${arr[0]}"
local ns="${arr[1]}"
patch_remove_finalizers -n $ns vmi $name
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
local arr=($p)
local name="${arr[0]}"
local ns="${arr[1]}"
patch_remove_finalizers -n $ns vm $name
done
}
function delete_resources() {
local managed_namespaces=("$@")
# Delete all traces of kubevirt
local namespaces=(default ${managed_namespaces[@]})
local labels=("operator.kubevirt.io" "kubevirt.io")
# Namespaced resources
for i in ${namespaces[@]}; do
for label in ${labels[@]}; do
_kubectl -n ${i} delete deployment,ds,rs,pods,services,pvc,rolebinding,role,serviceaccounts,configmaps,secrets,jobs -l ${label}
done
done
# Not namespaced resources
for label in ${labels[@]}; do
# Remove the finalizers added by virt-operator from CRDs
_kubectl get customresourcedefinitions --no-headers -o=custom-columns=NAME:.metadata.name,FINALIZERS:.metadata.finalizers -l ${label} | grep -e "kubevirt.io/virtOperatorFinalizer" | while read p; do
local arr=($p)
local name="${arr[0]}"
patch_remove_finalizers customresourcedefinitions ${name}
done
_kubectl delete apiservices,clusterroles,clusterrolebinding,customresourcedefinitions,pv,validatingwebhookconfiguration -l ${label}
done
}
function delete_namespaces() {
local managed_namespaces=("$@")
_kubectl delete ns ${managed_namespaces[@]} --timeout=180s --ignore-not-found
}
function main() {
echo "Cleaning up ..."
local kubevirt_managed_namespaces=(${namespace})
delete_kubevirt_cr
remove_finalizers
delete_resources "${kubevirt_managed_namespaces[@]}"
delete_namespaces "${kubevirt_managed_namespaces[@]}"
sleep 2
echo "Done $0"
}
main "$@"