forked from TheRealHaoLiu/hyper-acm-deploy
-
Notifications
You must be signed in to change notification settings - Fork 3
/
detach-cluster.sh
executable file
·108 lines (77 loc) · 3.07 KB
/
detach-cluster.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
#!/bin/bash
source ./helpers.sh
check_dependency
if [ "$#" -lt 1 ]; then
detach_cluster_usage
exit 0
fi
while getopts "c:n:m:k:" arg; do
case $arg in
c)
CLUSTER_NAME="${OPTARG}"
;;
n)
CLUSTER_NAMESPACE="${OPTARG}"
;;
m)
MANAGED_CLUSTER_NAME="${OPTARG}"
;;
k)
MANAGED_KUBECONFIG="${OPTARG}"
;;
:)
detach_cluster_usage
exit 0
;;
*)
detach_cluster_usage
exit 0
;;
esac
done
comment "info" "1. Validating management cluster status"
oc config view --minify=true --raw=true > hub.kubeconfig
export KUBECONFIG=./hub.kubeconfig
oc cluster-info
if [ $? -ne 0 ]; then
comment "error" "Failed to access the management cluster."
exit 1
fi
HOSTED_CLUSTER="${CLUSTER_NAMESPACE}-${CLUSTER_NAME}"
comment "info" "2. Validating hosted cluster status in the hosted cluster namespace: ${HOSTED_CLUSTER}"
HOSTED_CLUSTER_KUBECONFIG=`oc get hostedcluster -n ${CLUSTER_NAMESPACE} ${CLUSTER_NAME} -o jsonpath={.status.kubeconfig.name}`
oc get secret -n ${CLUSTER_NAMESPACE} ${HOSTED_CLUSTER_KUBECONFIG} -o jsonpath={.data.kubeconfig} | base64 -d > ${HOSTED_CLUSTER_KUBECONFIG}.kubeconfig
export KUBECONFIG=${HOSTED_CLUSTER_KUBECONFIG}.kubeconfig
oc cluster-info
if [ $? -ne 0 ]; then
comment "error" "Failed to access the hosted cluster."
exit 1
fi
comment "info" "3. Validating managed cluster status"
export KUBECONFIG=${MANAGED_KUBECONFIG}
oc cluster-info
if [ $? -ne 0 ]; then
comment "error" "Failed to access the hosted cluster."
exit 1
fi
comment "info" "4. Delete all addons from the hosted cluster"
export KUBECONFIG=${HOSTED_CLUSTER_KUBECONFIG}.kubeconfig
oc delete managedclusteraddons -n ${MANAGED_CLUSTER_NAME} application-manager --ignore-not-found
oc delete managedclusteraddons -n ${MANAGED_CLUSTER_NAME} config-policy-controller --ignore-not-found
oc delete managedclusteraddons -n ${MANAGED_CLUSTER_NAME} governance-policy-framework --ignore-not-found
comment "info" "4.1 wait for addons to be removed from the managed cluster"
export KUBECONFIG=${MANAGED_KUBECONFIG}
waitForNoPods "open-cluster-management-agent-addon"
comment "info" "5. Delete klusterlet component from the managed cluster"
export KUBECONFIG=${MANAGED_KUBECONFIG}
oc delete deployments -n open-cluster-management-agent klusterlet
oc delete klusterlet klusterlet --wait=false --ignore-not-found
oc patch klusterlet klusterlet -p '{"metadata":{"finalizers":null}}' --type=merge
waitForNoPods "open-cluster-management-agent"
oc delete namespace open-cluster-management-agent --wait=false --ignore-not-found
oc delete namespace open-cluster-management-agent-addon --wait=false --ignore-not-found
comment "info" "6. Delete the managed cluster from the hosted cluster"
export KUBECONFIG=${HOSTED_CLUSTER_KUBECONFIG}.kubeconfig
oc delete managedclusters ${MANAGED_CLUSTER_NAME} --wait=false --ignore-not-found
oc patch managedclusters ${MANAGED_CLUSTER_NAME} -p '{"metadata":{"finalizers":null}}' --type=merge
oc delete secret -n ${MANAGED_CLUSTER_NAME} ${MANAGED_CLUSTER_NAME}-import --wait=false --ignore-not-found