Skip to content

Commit 1cf3822

Browse files
authored
Merge pull request #96 from oracle/cleanup-cleanup-1
Cleanup cleanup 1
2 parents 099f702 + 89098c2 commit 1cf3822

File tree

1 file changed

+50
-46
lines changed

1 file changed

+50
-46
lines changed

src/integration-tests/bash/cleanup.sh

Lines changed: 50 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,17 @@ JOB_NAME="weblogic-command-job"
5757

5858
# function genericDelete
5959
#
60-
# This function is a 'generic kubernetes delete' that takes two arguments:
60+
# This function is a 'generic kubernetes delete' that takes three arguments:
6161
#
62-
# arg1: Comma separated list of types of kubernetes types to search/delete.
62+
# arg1: Comma separated list of types of kubernetes namespaced types to search/delete.
63+
# example: "all,cm,pvc,ns,roles,rolebindings,secrets"
6364
#
64-
# example: "all,crd,cm,pv,pvc,ns,roles,rolebindings,clusterroles,clusterrolebindings,secrets"
65-
#
66-
# arg2: '|' (pipe) separated list of keywords.
65+
# arg2: Comma separated list of types of kubernetes non-namespaced types to search/delete.
66+
# example: "crd,pv,clusterroles,clusterrolebindings"
6767
#
68+
# arg3: '|' (pipe) separated list of keywords.
6869
# Artifacts with a label or name that contains one
6970
# or more of the keywords are delete candidates.
70-
#
7171
# example: "logstash|kibana|elastisearch|weblogic|elk|domain"
7272
#
7373
# It runs in two stages:
@@ -81,66 +81,65 @@ function genericDelete {
8181
# in the second iteration, we try to delete any leftovers.
8282

8383
if [ "$iteration" = "first" ]; then
84-
maxwaitsecs=15
84+
local maxwaitsecs=15
8585
else
86-
maxwaitsecs=60
86+
local maxwaitsecs=60
8787
fi
8888

89-
echo "@@ Waiting up to $maxwaitsecs seconds for ${1:?} artifacts that contain string ${2:?} to delete."
90-
artcount=1
91-
mstart=`date +%s`
89+
echo "@@ Waiting up to $maxwaitsecs seconds for ${1:?} and ${2:?} artifacts that contain string ${3:?} to delete."
90+
91+
local artcount_no
92+
local artcount_yes
93+
local artcount_total
94+
local resfile_no
95+
local resfile_yes
96+
97+
local mstart=`date +%s`
98+
9299
while : ; do
93-
resfile1="$TMP_DIR/kinv_all.out.tmp"
94-
resfile2="$TMP_DIR/kinv_filtered.out.tmp"
95-
resfile3no="$TMP_DIR/kinv_filtered_nonamespace.out.tmp"
96-
resfile3yes="$TMP_DIR/kinv_filtered_yesnamespace.out.tmp"
100+
resfile_no="$TMP_DIR/kinv_filtered_nonamespace.out.tmp"
101+
resfile_yes="$TMP_DIR/kinv_filtered_yesnamespace.out.tmp"
102+
103+
# leftover namespaced artifacts
104+
kubectl get $1 --show-labels=true --all-namespaces=true 2>&1 | egrep -e "($3)" | awk '{ print $1 " " $2 }' | sort > $resfile_yes 2>&1
105+
artcount_yes="`cat $resfile_yes | wc -l`"
97106

98-
kubectl get $1 --show-labels=true --all-namespaces=true > $resfile1 2>&1
107+
# leftover non-namespaced artifacts
108+
kubectl get $2 --show-labels=true --all-namespaces=true 2>&1 | egrep -e "($3)" | awk '{ print $1 }' | sort > $resfile_no 2>&1
109+
artcount_no="`cat $resfile_no | wc -l`"
99110

100-
egrep -e "($2)" $resfile1 | grep -v 'secrets.*traefik.token' > $resfile2
101-
artcount="`cat $resfile2 | wc -l`"
111+
artcount_total=$((artcount_yes + artcount_no))
102112

103113
mnow=`date +%s`
104114

105-
if [ $((artcount)) -eq 0 ]; then
115+
if [ $((artcount_total)) -eq 0 ]; then
106116
echo "@@ No artifacts found."
107117
return 0
108118
fi
109119

110-
# names of remaining resources that have no namespace in form type/name
111-
cat $resfile2 | grep -v "^ *$" | egrep -e "^ " | awk '{ print $1 }' > $resfile3no
112-
unexpected_nonamespace="`cat $resfile3no | sort`"
113-
unexpected_nonamespace_count="`cat $resfile3no | wc -l`"
114-
115-
# names of remaining resources that have a namespace in form namespace type/name
116-
cat $resfile2 | grep -v "^ *$" | egrep -e "^([a-z]|[A-Z])" | awk '{ print $1 " " $2 }' > $resfile3yes
117-
unexpected_yesnamespace="`cat $resfile3yes | sort`"
118-
unexpected_yesnamespace_count="`cat $resfile3yes | wc -l`"
119-
120120
if [ "$iteration" = "first" ]; then
121-
# in the first thirty seconds we just wait to see if artifacts go away on there own
121+
# in the first iteration we just wait to see if artifacts go away on there own
122122

123-
echo "@@ Waiting for $artcount artifacts to delete. Wait time $((mnow - mstart)) seconds (max=$maxwaitsecs). Waiting for:"
123+
echo "@@ Waiting for $artcount_total artifacts to delete. Wait time $((mnow - mstart)) seconds (max=$maxwaitsecs). Waiting for:"
124124

125-
echo "$unexpected_nonamespace"
126-
echo "$unexpected_yesnamespace"
125+
cat $resfile_yes | awk '{ print "n=" $1 " " $2 }'
126+
cat $resfile_no | awk '{ print $1 }'
127127

128128
else
129129
# in the second thirty seconds we try to delete remaining artifacts
130130

131-
echo "@@ Trying to delete $artcount leftover artifacts, including ${unexpected_yesnamespace_count} namespaced artifacts and ${unexpected_nonamespace_count} non-namespaced artifacts, wait time $((mnow - mstart)) seconds (max=$maxwaitsecs)."
131+
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)."
132132

133-
if [ ${unexpected_yesnamespace_count} -gt 0 ]; then
134-
echo "$unexpected_yesnamespace" | while read line; do
135-
curns="`echo \"$line\" | awk '{ print $1 }'`"
136-
curitem="`echo \"$line\" | awk '{ print $2 }'`"
137-
echo "kubectl -n $curns delete $curitem --ignore_not-found"
138-
kubectl -n $curns delete $curitem --ignore-not-found
133+
if [ ${artcount_yes} -gt 0 ]; then
134+
cat "$resfile_yes" | while read line; do
135+
local args="`echo \"$line\" | awk '{ print "-n " $1 " delete " $2 " --ignore-not-found" }'`"
136+
echo "kubectl $args"
137+
kubectl $args
139138
done
140139
fi
141140

142-
if [ ${unexpected_nonamespace_count} -gt 0 ]; then
143-
echo "$unexpected_nonamespace" | while read line; do
141+
if [ ${artcount_no} -gt 0 ]; then
142+
cat "$resfile_no" | while read line; do
144143
echo "kubectl delete $line --ignore-not-found"
145144
kubectl delete $line --ignore-not-found
146145
done
@@ -150,9 +149,9 @@ function genericDelete {
150149

151150
if [ $((mnow - mstart)) -gt $((maxwaitsecs)) ]; then
152151
if [ "$iteration" = "first" ]; then
153-
echo "@@ Warning: $maxwaitsecs seconds reached. Will try deleting unexpected resources via kubectl delete."
152+
echo "@@ Warning: ${maxwaitsecs} seconds reached. Will try deleting unexpected resources via kubectl delete."
154153
else
155-
echo "@@ Error: $maxwaitsecs seconds reached and possibly $artcount artifacts remaining. Giving up."
154+
echo "@@ Error: ${maxwaitsecs} seconds reached and possibly ${artcount_total} artifacts remaining. Giving up."
156155
fi
157156
break
158157
fi
@@ -303,13 +302,18 @@ mkdir -p $TMP_DIR || fail No permision to create directory $TMP_DIR
303302

304303
# try an ordered/controlled delete first
305304

305+
#Comment out orderlyDelete so we can fully test generic delete (do not merge this change!)
306306
orderlyDelete
307307

308308
# try a generic delete in case the orderly delete missed something, this runs in two phases:
309-
# phase 1: wait to see if artificts dissappear naturally due to the above orderlyDelete
309+
# phase 1: wait to see if artifacts dissappear naturally due to the above orderlyDelete
310310
# phase 2: kubectl delete left over artifacts
311+
# arguments
312+
# arg1 - namespaced kubernetes artifacts
313+
# arg2 - non-namespaced artifacts
314+
# arg3 - keywords in deletable artificats
311315

312-
genericDelete "all,crd,cm,pv,pvc,ns,roles,rolebindings,clusterroles,clusterrolebindings,serviceaccount,secrets" "logstash|kibana|elastisearch|weblogic|elk|domain|traefik"
316+
genericDelete "all,cm,pvc,roles,rolebindings,serviceaccount,secrets" "crd,pv,ns,clusterroles,clusterrolebindings" "logstash|kibana|elastisearch|weblogic|elk|domain|traefik"
313317
SUCCESS="$?"
314318

315319
# Delete pv directories using a job (/scratch maps to PV_ROOT on the k8s cluster machines).

0 commit comments

Comments
 (0)