@@ -57,17 +57,17 @@ JOB_NAME="weblogic-command-job"
57
57
58
58
# function genericDelete
59
59
#
60
- # This function is a 'generic kubernetes delete' that takes two arguments:
60
+ # This function is a 'generic kubernetes delete' that takes three arguments:
61
61
#
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"
63
64
#
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"
67
67
#
68
+ # arg3: '|' (pipe) separated list of keywords.
68
69
# Artifacts with a label or name that contains one
69
70
# or more of the keywords are delete candidates.
70
- #
71
71
# example: "logstash|kibana|elastisearch|weblogic|elk|domain"
72
72
#
73
73
# It runs in two stages:
@@ -81,66 +81,65 @@ function genericDelete {
81
81
# in the second iteration, we try to delete any leftovers.
82
82
83
83
if [ " $iteration " = " first" ]; then
84
- maxwaitsecs=15
84
+ local maxwaitsecs=15
85
85
else
86
- maxwaitsecs=60
86
+ local maxwaitsecs=60
87
87
fi
88
88
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
+
92
99
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` "
97
106
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` "
99
110
100
- egrep -e " ($2 )" $resfile1 | grep -v ' secrets.*traefik.token' > $resfile2
101
- artcount=" ` cat $resfile2 | wc -l` "
111
+ artcount_total=$(( artcount_yes + artcount_no))
102
112
103
113
mnow=` date +%s`
104
114
105
- if [ $(( artcount )) -eq 0 ]; then
115
+ if [ $(( artcount_total )) -eq 0 ]; then
106
116
echo " @@ No artifacts found."
107
117
return 0
108
118
fi
109
119
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
-
120
120
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
122
122
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:"
124
124
125
- echo " $unexpected_nonamespace "
126
- echo " $unexpected_yesnamespace "
125
+ cat $resfile_yes | awk ' { print "n=" $1 " " $2 } '
126
+ cat $resfile_no | awk ' { print $1 } '
127
127
128
128
else
129
129
# in the second thirty seconds we try to delete remaining artifacts
130
130
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 )."
132
132
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
139
138
done
140
139
fi
141
140
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
144
143
echo " kubectl delete $line --ignore-not-found"
145
144
kubectl delete $line --ignore-not-found
146
145
done
@@ -150,9 +149,9 @@ function genericDelete {
150
149
151
150
if [ $(( mnow - mstart)) -gt $(( maxwaitsecs)) ]; then
152
151
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."
154
153
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."
156
155
fi
157
156
break
158
157
fi
@@ -303,13 +302,18 @@ mkdir -p $TMP_DIR || fail No permision to create directory $TMP_DIR
303
302
304
303
# try an ordered/controlled delete first
305
304
305
+ # Comment out orderlyDelete so we can fully test generic delete (do not merge this change!)
306
306
orderlyDelete
307
307
308
308
# 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
310
310
# 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
311
315
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"
313
317
SUCCESS=" $? "
314
318
315
319
# Delete pv directories using a job (/scratch maps to PV_ROOT on the k8s cluster machines).
0 commit comments