27
27
#
28
28
# The test runs in 4 phases:
29
29
#
30
- # Phase 1: Delete domain resources with label 'weblogic.domainUID' .
30
+ # Phase 1: Delete test kubernetes artifacts with labels .
31
31
#
32
- # Phase 2: Delete wls operator with lable 'weblogic.operatorName' and
33
- # delete voyager controller.
32
+ # Phase 2: Wait 15 seconds to see if stage 1 succeeded, and
33
+ # if not, repeatedly search for all test related kubectl
34
+ # artifacts and try delete them directly for up to 60 more
35
+ # seconds. This phase has no dependency on the
36
+ # previous test run's yaml files. It makes no
37
+ # attempt to delete artifacts in a particular order.
34
38
#
35
39
# Phase 3: Use a kubernetes job to delete the PV directories
36
40
# on the kubernetes cluster.
@@ -57,6 +61,7 @@ function fail {
57
61
58
62
#! /bin/bash
59
63
#
64
+ # Usage:
60
65
# getResWithLabel outfilename
61
66
#
62
67
function getResWithLabel {
@@ -75,15 +80,17 @@ function getResWithLabel {
75
80
}
76
81
77
82
#
83
+ # Usage:
78
84
# deleteResWithLabel outputfile
79
85
#
80
- function deleteResWithLabel {
86
+ function deleteWithOneLabel {
81
87
echo @@ Delete resources with label $LABEL_SELECTOR .
82
88
# clean the output file first
83
89
if [ -e $1 ]; then
84
90
rm $1
85
91
fi
86
92
93
+ echo @@ Deleting resources with label $LABEL_SELECTOR .
87
94
getResWithLabel $1
88
95
# delete namespaced types
89
96
cat $1 | awk ' { print $4 }' | grep -v " ^$" | sort -u | while read line; do
@@ -98,19 +105,23 @@ function deleteResWithLabel {
98
105
99
106
echo " @@ Waiting for pods to stop running."
100
107
local total=0
101
- for count in {1..100}; do
102
- pods=($( kubectl get pods --all-namespaces -l weblogic.domainUID -o jsonpath=' {range .items[*]}{.metadata.name} {end}' ) )
108
+ local mstart=` date +%s`
109
+ local mnow=mstart
110
+ local maxwaitsecs=60
111
+ while [ $(( mnow - mstart)) -lt $maxwaitsecs ]; do
112
+ pods=($( kubectl get pods --all-namespaces -l $LABEL_SELECTOR -o jsonpath=' {range .items[*]}{.metadata.name} {end}' ) )
103
113
total=${# pods[*]}
104
114
if [ $total -eq 0 ] ; then
105
115
break
106
116
else
107
- echo " @@ There are still $total running pods with label $LABEL_SELECTOR ."
117
+ echo " @@ There are $total running pods with label $LABEL_SELECTOR ."
108
118
fi
109
119
sleep 3
120
+ mnow=` date +%s`
110
121
done
111
122
112
123
if [ $total -gt 0 ]; then
113
- echo " Warning: after waiting 300 seconds, there are still $total running pods with label $LABEL_SELECTOR ."
124
+ echo " Warning: after waiting $maxwaitsecs seconds, there are still $total running pods with label $LABEL_SELECTOR ."
114
125
fi
115
126
}
116
127
@@ -122,6 +133,7 @@ function deleteVoyagerController {
122
133
}
123
134
124
135
#
136
+ # Usage:
125
137
# deleteNamespaces outputfile
126
138
#
127
139
function deleteNamespaces {
@@ -132,46 +144,179 @@ function deleteNamespaces {
132
144
done
133
145
134
146
}
135
- echo @@ Starting cleanup.
136
- script=" ${BASH_SOURCE[0]} "
137
- scriptDir=" $( cd " $( dirname " ${script} " ) " > /dev/null 2>&1 ; pwd -P) "
138
147
139
- echo " @@ RESULT_ROOT=$RESULT_ROOT TMP_DIR=$TMP_DIR RESULT_DIR=$RESULT_DIR PROJECT_ROOT=$PROJECT_ROOT "
148
+ function deleteWithLabels {
149
+ NAMESPACED_TYPES=" pod,job,deploy,rs,service,pvc,ingress,cm,serviceaccount,role,rolebinding,secret"
140
150
141
- mkdir -p $TMP_DIR || fail No permision to create directory $TMP_DIR
151
+ HANDLE_VOYAGER=" false"
152
+ VOYAGER_ING_NAME=" ingresses.voyager.appscode.com"
153
+ if [ ` kubectl get crd $VOYAGER_ING_NAME --ignore-not-found | grep $VOYAGER_ING_NAME | wc -l` = 1 ]; then
154
+ NAMESPACED_TYPES=" $VOYAGER_ING_NAME ,$NAMESPACED_TYPES "
155
+ HANDLE_VOYAGER=" true"
156
+ fi
142
157
143
- NAMESPACED_TYPES=" pod,job,deploy,rs,service,pvc,ingress,cm,serviceaccount,role,rolebinding,secret"
158
+ DOMAIN_CRD=" domains.weblogic.oracle"
159
+ if [ ` kubectl get crd $DOMAIN_CRD --ignore-not-found | grep $DOMAIN_CRD | wc -l` = 1 ]; then
160
+ NAMESPACED_TYPES=" $DOMAIN_CRD ,$NAMESPACED_TYPES "
161
+ fi
144
162
145
- HANDLE_VOYAGER=" false"
146
- VOYAGER_ING_NAME=" ingresses.voyager.appscode.com"
147
- if [ ` kubectl get crd $VOYAGER_ING_NAME --ignore-not-found | grep $VOYAGER_ING_NAME | wc -l` = 1 ]; then
148
- NAMESPACED_TYPES=" $VOYAGER_ING_NAME ,$NAMESPACED_TYPES "
149
- HANDLE_VOYAGER=" true"
150
- fi
163
+ NOT_NAMESPACED_TYPES=" pv,crd,clusterroles,clusterrolebindings"
151
164
152
- DOMAIN_CRD=" domains.weblogic.oracle"
153
- if [ ` kubectl get crd $DOMAIN_CRD --ignore-not-found | grep $DOMAIN_CRD | wc -l` = 1 ]; then
154
- NAMESPACED_TYPES=" $DOMAIN_CRD ,$NAMESPACED_TYPES "
155
- fi
165
+ tempfile=" /tmp/$( basename $0 ) .tmp.$$ " # == /tmp/[script-file-name].tmp.[pid]
156
166
157
- NOT_NAMESPACED_TYPES=" pv,crd,clusterroles,clusterrolebindings"
167
+ echo @@ Deleting domain resources.
168
+ LABEL_SELECTOR=" weblogic.domainUID"
169
+ deleteWithOneLabel " $tempfile -0"
158
170
159
- tempfile=" /tmp/$( basename $0 ) .tmp.$$ " # == /tmp/[script-file-name].tmp.[pid]
171
+ echo @@ Deleting wls operator resources.
172
+ LABEL_SELECTOR=" weblogic.operatorName"
173
+ deleteWithOneLabel " $tempfile -1"
160
174
161
- echo @@ Deleting domain resources.
162
- LABEL_SELECTOR=" weblogic.domainUID"
163
- deleteResWithLabel " $tempfile -0"
164
- deleteNamespaces " $tempfile -0"
175
+ deleteNamespaces " $tempfile -0"
176
+ deleteNamespaces " $tempfile -1"
165
177
166
- echo @@ Deleting wls operator resources.
167
- LABEL_SELECTOR=" weblogic.operatorName"
168
- deleteResWithLabel " $tempfile -1"
169
- deleteNamespaces " $tempfile -1"
178
+ echo @@ Deleting voyager controller.
179
+ if [ " $HANDLE_VOYAGER " = " true" ]; then
180
+ deleteVoyagerController
181
+ fi
182
+ }
170
183
171
- echo @@ Deleting voyager controller.
172
- if [ " $HANDLE_VOYAGER " = " true" ]; then
173
- deleteVoyagerController
174
- fi
184
+ # function genericDelete
185
+ #
186
+ # This function is a 'generic kubernetes delete' that takes three arguments:
187
+ #
188
+ # arg1: Comma separated list of types of kubernetes namespaced types to search/delete.
189
+ # example: "all,cm,pvc,ns,roles,rolebindings,secrets"
190
+ #
191
+ # arg2: Comma separated list of types of kubernetes non-namespaced types to search/delete.
192
+ # example: "crd,pv,clusterroles,clusterrolebindings"
193
+ #
194
+ # arg3: '|' (pipe) separated list of keywords.
195
+ # Artifacts with a label or name that contains one
196
+ # or more of the keywords are delete candidates.
197
+ # example: "logstash|kibana|elastisearch|weblogic|elk|domain"
198
+ #
199
+ # It runs in two stages:
200
+ # In the first, wait to see if artifacts delete on their own.
201
+ # In the second, try to delete any leftovers.
202
+ #
203
+ function genericDelete {
204
+
205
+ for iteration in first second; do
206
+ # In the first iteration, we wait to see if artifacts delete.
207
+ # in the second iteration, we try to delete any leftovers.
208
+
209
+ if [ " $iteration " = " first" ]; then
210
+ local maxwaitsecs=15
211
+ else
212
+ local maxwaitsecs=60
213
+ fi
214
+
215
+ echo " @@ Waiting up to $maxwaitsecs seconds for ${1:? } and ${2:? } artifacts that contain string ${3:? } to delete."
216
+
217
+ local artcount_no
218
+ local artcount_yes
219
+ local artcount_total
220
+ local resfile_no
221
+ local resfile_yes
222
+
223
+ local mstart=` date +%s`
224
+
225
+ while : ; do
226
+ resfile_no=" $TMP_DIR /kinv_filtered_nonamespace.out.tmp"
227
+ resfile_yes=" $TMP_DIR /kinv_filtered_yesnamespace.out.tmp"
228
+
229
+ # leftover namespaced artifacts
230
+ kubectl get $1 \
231
+ -o=jsonpath=' {range .items[*]}{.metadata.namespace}{" "}{.kind}{"/"}{.metadata.name}{"\n"}{end}' \
232
+ --all-namespaces=true 2>&1 \
233
+ | egrep -e " ($3 )" | sort > $resfile_yes 2>&1
234
+ artcount_yes=" ` cat $resfile_yes | wc -l` "
235
+
236
+ # leftover non-namespaced artifacts
237
+ kubectl get $2 \
238
+ -o=jsonpath=' {range .items[*]}{.kind}{"/"}{.metadata.name}{"\n"}{end}' \
239
+ --all-namespaces=true 2>&1 \
240
+ | egrep -e " ($3 )" | sort > $resfile_no 2>&1
241
+ artcount_no=" ` cat $resfile_no | wc -l` "
242
+
243
+ artcount_total=$(( artcount_yes + artcount_no))
244
+
245
+ mnow=` date +%s`
246
+
247
+ if [ $(( artcount_total)) -eq 0 ]; then
248
+ echo " @@ No artifacts found."
249
+ return 0
250
+ fi
251
+
252
+ if [ " $iteration " = " first" ]; then
253
+ # in the first iteration we just wait to see if artifacts go away on there own
254
+
255
+ echo " @@ Waiting for $artcount_total artifacts to delete. Wait time $(( mnow - mstart)) seconds (max=$maxwaitsecs ). Waiting for:"
256
+
257
+ cat $resfile_yes | awk ' { print "n=" $1 " " $2 }'
258
+ cat $resfile_no | awk ' { print $1 }'
259
+
260
+ else
261
+ # in the second thirty seconds we try to delete remaining artifacts
262
+
263
+ echo " @@ Trying to delete ${artcount_total} leftover artifacts, including ${artcount_yes} namespaced artifacts and ${artcount_no} non-namespaced artifacts, wait time $(( mnow - mstart)) seconds (max=$maxwaitsecs )."
264
+
265
+ if [ ${artcount_yes} -gt 0 ]; then
266
+ cat " $resfile_yes " | while read line; do
267
+ local args=" ` echo \" $line \" | awk ' { print "-n " $1 " delete " $2 " --ignore-not-found" }' ` "
268
+ echo " kubectl $args "
269
+ kubectl $args
270
+ done
271
+ fi
272
+
273
+ if [ ${artcount_no} -gt 0 ]; then
274
+ cat " $resfile_no " | while read line; do
275
+ echo " kubectl delete $line --ignore-not-found"
276
+ kubectl delete $line --ignore-not-found
277
+ done
278
+ fi
279
+
280
+ fi
281
+
282
+ if [ $(( mnow - mstart)) -gt $(( maxwaitsecs)) ]; then
283
+ if [ " $iteration " = " first" ]; then
284
+ echo " @@ Warning: ${maxwaitsecs} seconds reached. Will try deleting unexpected resources via kubectl delete."
285
+ else
286
+ echo " @@ Error: ${maxwaitsecs} seconds reached and possibly ${artcount_total} artifacts remaining. Giving up."
287
+ fi
288
+ break
289
+ fi
290
+
291
+ sleep 5
292
+ done
293
+ done
294
+ return 1
295
+ }
296
+
297
+ echo @@ Starting cleanup.
298
+ script=" ${BASH_SOURCE[0]} "
299
+ scriptDir=" $( cd " $( dirname " ${script} " ) " > /dev/null 2>&1 ; pwd -P) "
300
+
301
+ echo " @@ RESULT_ROOT=$RESULT_ROOT TMP_DIR=$TMP_DIR RESULT_DIR=$RESULT_DIR PROJECT_ROOT=$PROJECT_ROOT "
302
+
303
+ mkdir -p $TMP_DIR || fail No permision to create directory $TMP_DIR
304
+
305
+ # first, try to delete with labels since the conversion is that all created resources need to
306
+ # have the proper label(s)
307
+ echo @@ Starting deleteWithLabels
308
+ deleteWithLabels
309
+
310
+ # second, try a generic delete in case there are some leftover resources, this runs in two phases:
311
+ # phase 1: wait to see if artifacts dissappear naturally due to the above orderlyDelete
312
+ # phase 2: kubectl delete left over artifacts
313
+ # arguments
314
+ # arg1 - namespaced kubernetes artifacts
315
+ # arg2 - non-namespaced artifacts
316
+ # arg3 - keywords in deletable artificats
317
+ echo @@ Starting genericDelete
318
+ genericDelete " all,cm,pvc,roles,rolebindings,serviceaccount,secrets" " crd,pv,ns,clusterroles,clusterrolebindings" " logstash|kibana|elastisearch|weblogic|elk|domain|traefik|voyager"
319
+ SUCCESS=" $? "
175
320
176
321
# Delete pv directories using a job (/scratch maps to PV_ROOT on the k8s cluster machines).
177
322
0 commit comments