-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete-masterkube.sh
executable file
·233 lines (193 loc) · 5.8 KB
/
delete-masterkube.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#!/bin/bash
set -eu
CURDIR=$(dirname $0)
FORCE=NO
TRACE=NO
OVERRIDE_PLATEFORMDEFS=
OVERRIDE_AWS_PROFILE=
OVERRIDE_AWS_REGION=
OVERRIDE_NODEGROUP_NAME=
OVERRIDE_CONFIGURATION_LOCATION=
OVERRIDE_KUBERNETES_DISTRO=
PLATEFORM=${PLATEFORM:=multipass}
pushd ${CURDIR}/../ &>/dev/null
OPTIONS=(
"configuration-location:"
"defs:"
"force"
"kube-engine:"
"node-group:"
"plateform:"
"profile:"
"region:"
"trace"
)
PARAMS=$(echo ${OPTIONS[@]} | tr ' ' ',')
TEMP=$(getopt -o ftg:p:r: --long "${PARAMS}" -n "$0" -- "$@")
eval set -- "${TEMP}"
while true; do
case "$1" in
--defs)
OVERRIDE_PLATEFORMDEFS=$2
if [ ! -f ${OVERRIDE_PLATEFORMDEFS} ]; then
echo_red "definitions: ${OVERRIDE_PLATEFORMDEFS} not found"
exit 1
fi
shift 2
;;
--plateform)
PLATEFORM="$2"
shift 2
;;
--kube-engine)
case "$2" in
kubeadm|k3s|rke2|microk8s)
OVERRIDE_KUBERNETES_DISTRO=$2
;;
*)
echo "Unsupported kubernetes distribution: $2"
exit 1
;;
esac
shift 2
;;
-f|--force)
FORCE=YES
shift 1
;;
-t|--trace)
TRACE=YES
shift 1
;;
-p|--profile)
OVERRIDE_AWS_PROFILE="$2"
shift 2
;;
-r|--region)
OVERRIDE_AWS_REGION="$2"
shift 2
;;
-g|--node-group)
OVERRIDE_NODEGROUP_NAME=$2
shift 2
;;
--configuration-location)
OVERRIDE_CONFIGURATION_LOCATION=$2
if [ ! -d ${OVERRIDE_CONFIGURATION_LOCATION} ]; then
echo_red_bold "kubernetes output : ${OVERRIDE_CONFIGURATION_LOCATION} not found"
exit 1
fi
shift 2
;;
--)
shift
break
;;
*)
echo_red_bold "$1 - Internal error!"
exit 1
;;
esac
done
if [ "${TRACE}" = "YES" ]; then
set -x
fi
source "${CURDIR}/common.sh"
if [ -n "${OVERRIDE_KUBERNETES_DISTRO}" ]; then
KUBERNETES_DISTRO=${OVERRIDE_KUBERNETES_DISTRO}
fi
if [ -n "${OVERRIDE_AWS_PROFILE}" ]; then
AWS_PROFILE=${OVERRIDE_AWS_PROFILE}
fi
if [ -n "${OVERRIDE_AWS_REGION}" ]; then
AWS_REGION=${OVERRIDE_AWS_REGION}
fi
if [ -n "${OVERRIDE_NODEGROUP_NAME}" ]; then
NODEGROUP_NAME=${OVERRIDE_NODEGROUP_NAME}
fi
if [ -n "${OVERRIDE_CONFIGURATION_LOCATION}" ]; then
CONFIGURATION_LOCATION=${OVERRIDE_CONFIGURATION_LOCATION}
fi
if [ -n "${OVERRIDE_PLATEFORMDEFS}" ]; then
PLATEFORMDEFS=${OVERRIDE_PLATEFORMDEFS}
source ${PLATEFORMDEFS}
fi
prepare_environment
echo_blue_bold "Delete masterkube ${MASTERKUBE} previous instance"
#===========================================================================================================================================
#
#===========================================================================================================================================
function all_vm_names() {
local NAMES=()
for NODEINDEX in $(seq ${FIRSTNODE} ${LASTNODE_INDEX})
do
NAMES+=($(get_vm_name ${NODEINDEX}))
done
echo ${NAMES[@]}
}
#===========================================================================================================================================
#
#===========================================================================================================================================
function delete_all_vms() {
for NAME in $@
do
delete_vm_by_name ${NAME} || true
done
}
#===========================================================================================================================================
#
#===========================================================================================================================================
function list_nodes() {
local ALLNODES=$(kubectl get node -o json --kubeconfig ${TARGET_CLUSTER_LOCATION}/config)
echo ${ALLNODES} | jq -r '.items | .[] | .metadata.annotations["cluster.autoscaler.nodegroup/instance-name"]' | sort -r
}
#===========================================================================================================================================
#
#===========================================================================================================================================
function delete_nodes() {
local ROLE=
for ROLE in "node-role.kubernetes.io/worker" "node-role.kubernetes.io/master"
do
local ALLNODES=$(kubectl get node -o json --kubeconfig ${TARGET_CLUSTER_LOCATION}/config)
local NODES=$(echo ${ALLNODES} | jq -r --arg LABEL ${ROLE} '.items | .[] | select(.metadata.labels[$LABEL]) | .metadata.name' | sort -r)
for NODE in ${NODES}
do
kubectl drain --force --ignore-daemonsets --delete-emptydir-data --kubeconfig ${TARGET_CLUSTER_LOCATION}/config ${NODE}
#kubectl delete no --force --kubeconfig ${TARGET_CLUSTER_LOCATION}/config ${NODE}
done
done
INSTANCENAMES=$(echo ${ALLNODES} | jq -r '.items | .[] | .metadata.annotations["cluster.autoscaler.nodegroup/instance-name"]' | sort -r)
delete_all_vms ${MASTERKUBE} ${INSTANCENAMES}
wait_jobs_finish
rm ${TARGET_CLUSTER_LOCATION}/config
}
#===========================================================================================================================================
#
#===========================================================================================================================================
if [ ! -f ${TARGET_CLUSTER_LOCATION}/config ]; then
VMNAMES=$(all_vm_names)
FORCE=YES
else
VMNAMES=$(list_nodes)
if [ -f ${TARGET_CONFIG_LOCATION}/buildenv ]; then
source ${TARGET_CONFIG_LOCATION}/buildenv
else
FORCE=YES
fi
fi
if [ "${FORCE}" = "YES" ]; then
delete_all_vms ${VMNAMES}
elif [ -f ${TARGET_CLUSTER_LOCATION}/config ]; then
delete_nodes
fi
wait_jobs_finish
unregister_dns
delete_load_balancers
./bin/kubeconfig-delete.sh ${NODEGROUP_NAME} &> /dev/null || true
if [ -f ${TARGET_CONFIG_LOCATION}/autoscaler.pid ]; then
kill ${TARGET_CONFIG_LOCATION}/autoscaler.pid
fi
delete_host "${MASTERKUBE}"
popd &>/dev/null
echo_blue_bold "Delete ${CONFIGURATION_LOCATION}/config/${NODEGROUP_NAME}"
rm -rf "${CONFIGURATION_LOCATION}/config/${NODEGROUP_NAME}"