-
Notifications
You must be signed in to change notification settings - Fork 153
/
clean-clusters.sh
executable file
·131 lines (116 loc) · 4.54 KB
/
clean-clusters.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
#!/bin/bash
# Copyright 2020 Red Hat Inc.
# Parameters
# -k, --keep-providers Keeping all provider connections that are not in Advanced Cluster Management namespaces.
# -t Runs a test, but does not perform any actions
CLEAN_RESOURCES=0
KEEP_PROVIDERS=0
for arg in "$@"
do
case $arg in
-k|--keep-providers)
KEEP_PROVIDERS=1
shift
;;
-t)
CLEAN_RESOURCES=1
shift
;;
*)
echo "Unrecognized argument: $1"
shift
;;
esac
done
echo "Continuing to execute this script will destroy the following \"managed\" Openshift clusters:"
oc get clusterDeployments --all-namespaces
echo
echo "If you would like to proceed with cleanup, type: DESTROY"
read -r DESTROY_YES
if [ "${DESTROY_YES}" != "DESTROY" ]; then
echo "You must type DESTROY to clean up the Hive deployed clusters"
exit 1
fi
for clusterName in `oc get clusterDeployments --all-namespaces --ignore-not-found | grep -v "NAMESPACE" | awk '{ print $1 }'`; do
echo "Destroying ${clusterName}"
if [ $CLEAN_RESOURCES ]; then
oc -n ${clusterName} delete clusterDeployment ${clusterName} --wait=false
sleep 10
podName=`oc -n ${clusterName} get pods | grep uninstall | awk '{ print $1 }'`
oc -n ${clusterName} logs ${podName} -f
fi
done
echo "Detaching imported clusters (rhacm 1.0)"
for clusterName in `oc get clusters --all-namespaces --ignore-not-found | grep -v "NAMESPACE" | awk '{ print $1 }'`; do
printf " Detaching cluster ${clusterName}\n "
if [ $CLEAN_RESOURCES ]; then
oc -n ${clusterName} delete cluster ${clusterName}
printf " " #Spacing
oc delete namespace ${clusterName} --wait=false
fi
done
# Stage2, 2nd pass
echo "Second pass cleaning, by endpointConfig"
for clusterName in `oc get endpointconfig --all-namespaces --ignore-not-found | grep -v "NAMESPACE" | awk '{ print $1 }'`; do
printf " Detaching cluster ${clusterName}\n "
if [ $CLEAN_RESOURCES ]; then
oc -n ${clusterName} delete cluster ${clusterName}
printf " " #Spacing
oc delete namespace ${clusterName} --wait=false
fi
done
echo "Detaching imported clusters (rhacm 2.0+)"
for clusterName in `oc get managedcluster --ignore-not-found | grep -v "NAME" | awk '{ print $1 }'`; do
printf " Detaching cluster ${clusterName}\n "
if [ $CLEAN_RESOURCES ]; then
DELETE_MANAGEDCLUSTER=1
oc delete managedcluster ${clusterName} --wait=false
printf " " #Spacing
oc -n ${clusterName} delete klusterletaddonconfig ${clusterName} --wait=false
fi
done
if [ $DELETE_MANAGEDCLUSTER ] ; then
echo "Wait 100 seconds"
sleep 100
fi
echo "Deleting manifestworks"
for clusterName in `oc get managedcluster --ignore-not-found | grep -v "NAME" | awk '{ print $1 }'`; do
printf " Removing manifestworks in ${clusterName}\n "
if [ $CLEAN_RESOURCES ]; then
oc delete manifestwork -n ${clusterName} --wait=false --all
printf " " #Spacing
oc delete lease -n ${clusterName} cluster-lease-${clusterName}
printf " " #Spacing
oc delete ns ${clusterName} --wait=false
fi
done
if [ $DELETE_MANAGEDCLUSTER ] ; then
echo "Wait 20 seconds"
sleep 20
fi
echo "Force deleting all resources"
for clusterName in `oc get managedcluster --ignore-not-found | grep -v "NAME" | awk '{ print $1 }'`; do
printf " Force removing all manifestwork, klusterletaddonconfig on cluster ${clusterName}\n "
if [ $CLEAN_RESOURCES ]; then
oc get manifestwork -n ${clusterName} | grep -v NAME | awk '{print $1}' | xargs -n1 oc patch manifestwork -n ${clusterName} -p '{"metadata":{"finalizers":[]}}' --type=merge
printf " " #Spacing
oc patch klusterletaddonconfig -n ${clusterName} ${clusterName} -p '{"metadata":{"finalizers":[]}}' --type=merge
printf " " #Spacing
oc patch managedcluster -n ${clusterName} ${clusterName} -p '{"metadata":{"finalizers":[]}}' --type=merge
fi
done
sleep 5
if [ "$KEEP_PROVIDERS" -eq 1 ]; then
echo "Keeping the following provider connections"
# 2.2 and older
oc get secrets -l cluster.open-cluster-management.io/provider --ignore-not-found -A
# 2.3 and newer
oc get secrets -l cluster.open-cluster-management.io/credentials --ignore-not-found -A
else
echo "Deleting provider connections"
# 2.2 and older
oc delete secrets -l cluster.open-cluster-management.io/provider --ignore-not-found -A
# 2.3 and newer
oc delete secrets -l cluster.open-cluster-management.io/credentials --ignore-not-found -A
fi
echo "Done!"