Skip to content

Commit cafd074

Browse files
author
Tom Barnes
committed
Update comments, usage text, function name, and temp file name.
1 parent 82d4656 commit cafd074

File tree

1 file changed

+47
-45
lines changed

1 file changed

+47
-45
lines changed

kubernetes/delete-domain.sh

Lines changed: 47 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,15 @@
33
# Licensed under the Universal Permissive License v 1.0 as shown at http://oss.oracle.com/licenses/upl.
44
#
55
# Description:
6-
# Use this script to delete a set of given domains, or all domains.
7-
#
8-
# Alternatively, run the script in a test mode to show what would
9-
# be deleted without actually performing the deletes.
6+
# Use this script to delete all kubernetes resources associated
7+
# with a set of given domains. Alternatively, run the script
8+
# in a test mode to show what would be deleted without actually
9+
# performing the deletes.
1010
#
1111
# Usage:
1212
# See "function usage" below or call this script with no parameters.
1313
#
1414

15-
# default when to stop retrying (override via command line)
16-
default_maxwaitsecs=120
17-
18-
# optional test mode that lists what would be deleted without
19-
# actually deleting (override via command line)
20-
test_mode=false
21-
22-
2315
function usage {
2416
cat << EOF
2517
Usage:
@@ -28,9 +20,9 @@ cat << EOF
2820
$(basename $0) -d all [-s max-seconds] [-t]
2921
$(basename $0) -h
3022
31-
Perform a best-effort delete of the k8s artifacts for
23+
Perform a best-effort delete of the kubernetes resources for
3224
the given domain(s), and retry until either max-seconds is reached
33-
or all artifacts were deleted (default $default_maxwaitsecs seconds).
25+
or all resources were deleted (default $default_maxwaitsecs seconds).
3426
3527
The domains can be specified as a comma-separated list of
3628
domain-uids (no spaces), or the keyword 'all'. The domains can be
@@ -39,21 +31,22 @@ cat << EOF
3931
Specify '-t' to run the script in a test mode which will
4032
show kubernetes commands but not actually perform them.
4133
42-
The delete occurs in three phases:
34+
The script runs in three phases:
4335
44-
Phase 1: Set the startupControl of each domain to NONE
45-
if it's not already NONE. This should cause each
46-
domain's operator to initiate a controlled
47-
shutdown of the domain.
36+
Phase 1: Set the startupControl of each domain to NONE if
37+
it's not already NONE. This should cause each
38+
domain's operator to initiate a controlled shutdown
39+
of the domain. Immediately proceed to phase 2.
4840
49-
Phase 2: Wait up to half the max wait seconds
50-
for WebLogic Server pods to exit normally.
41+
Phase 2: Wait up to half of max-seconds for WebLogic
42+
Server pods to exit normally, and then proceed
43+
to phase 3.
5144
52-
Phase 3: Delete all kubernetes objects for the
53-
specified domains, including any pods
54-
leftover from phase 2. Give up if max
55-
seconds is exceeded and there are any
56-
leftover kubernetes objects for the domain(s).
45+
Phase 3: Periodically delete all remaining kubernetes resources
46+
for the specified domains, including any pods
47+
leftover from phase 2. Exit and fail if max-seconds
48+
is exceeded and there are any leftover kubernetes
49+
resources.
5750
5851
This script exits with a zero status on success, and a
5952
non-zero status on failure.
@@ -62,20 +55,20 @@ EOF
6255

6356

6457
#
65-
# getDomains domain(s) outfilename
58+
# getDomainResources domain(s) outfilename
6659
#
6760
# Usage:
68-
# getDomains domainA,domainB,... outfilename
69-
# getDomains all outfilename
61+
# getDomainResources domainA,domainB,... outfilename
62+
# getDomainResources all outfilename
7063
#
7164
# Internal helper function
7265
#
73-
# File output is all domain related artifacts for the given domain uids, one per line,
74-
# in the form: 'kind name [-n namespace]'. For example
66+
# File output is all domain related resources for the given domain uids, one per line,
67+
# in the form: 'kind name [-n namespace]'. For example:
7568
# PersistentVolumeClaim domain1-pv-claim -n default
7669
# PersistentVolume domain1-pv
7770
#
78-
function getDomains {
71+
function getDomainResources {
7972
if [ "$1" = "all" ]; then
8073
local label_selector="weblogic.domainUID"
8174
else
@@ -115,7 +108,7 @@ function getDomains {
115108
# Internal helper function
116109
# This function first sets the startupControl of each Domain to NONE
117110
# and waits up to half of $2 for pods to 'self delete'. It then deletes
118-
# all remaining k8s artifacts for domain $1 (including any remaining pods)
111+
# all remaining k8s resources for domain $1 (including any remaining pods)
119112
# and retries up to $2 seconds.
120113
#
121114
# If $1 has special value "all", it deletes all domains in all namespaces.
@@ -125,38 +118,38 @@ function getDomains {
125118
function deleteDomains {
126119

127120
if [ "$test_mode" = "true" ]; then
128-
echo @@ Test mode! Displaying commands for deleting kubernetes artifacts with label weblogic.domainUID \'$1\' without actually deleting them.
121+
echo @@ Test mode! Displaying commands for deleting kubernetes resources with label weblogic.domainUID \'$1\' without actually deleting them.
129122
else
130-
echo @@ Deleting kubernetes artifacts with label weblogic.domainUID \'$1\'.
123+
echo @@ Deleting kubernetes resources with label weblogic.domainUID \'$1\'.
131124
fi
132125

133126
local maxwaitsecs=${2:-$default_maxwaitsecs}
134-
local tempfile="/tmp/getdomain.tmp.$1.$$"
127+
local tempfile="/tmp/$(basename $0).tmp.$$" # == /tmp/[script-file-name].tmp.[pid]
135128
local mstart=`date +%s`
136129
local phase=1
137130

138131
while : ; do
139-
# get all k8s objects with matching domain-uid labels and put them in $tempfile
140-
getDomains $1 $tempfile
132+
# get all k8s resources with matching domain-uid labels and put them in $tempfile
133+
getDomainResources $1 $tempfile
141134

142-
# get a count of all k8s objects with matching domain-uid labels
135+
# get a count of all k8s resources with matching domain-uid labels
143136
local allcount=`wc -l $tempfile | awk '{ print $1 }'`
144137

145138
# get a count of all WLS pods (any pod with a matching domain-uid label that doesn't have 'traefik' embedded in its name)
146139
local podcount=`grep "^Pod" $tempfile | grep -v traefik | wc -l | awk '{ print $1 }'`
147140

148141
local mnow=`date +%s`
149142

150-
echo @@ $allcount objects remaining after $((mnow - mstart)) seconds, including $podcount WebLogic Server pods. Max wait is $maxwaitsecs seconds.
143+
echo @@ $allcount resources remaining after $((mnow - mstart)) seconds, including $podcount WebLogic Server pods. Max wait is $maxwaitsecs seconds.
151144

152-
# Exit if all k8s objects deleted are max wait seconds exceeded.
145+
# Exit if all k8s resources deleted or max wait seconds exceeded.
153146

154147
if [ $allcount -eq 0 ]; then
155148
echo @@ Success.
156149
rm -f $tempfile
157150
exit 0
158151
elif [ $((mnow - mstart)) -gt $maxwaitsecs ]; then
159-
echo @@ Error! Max wait of $maxwaitsecs seconds exceeded with $allcount objects remaining, including $podcount WebLogic Server pods. Giving up. Remaining objects:
152+
echo @@ Error! Max wait of $maxwaitsecs seconds exceeded with $allcount resources remaining, including $podcount WebLogic Server pods. Giving up. Remaining resources:
160153
cat $tempfile
161154
rm -f $tempfile
162155
exit $allcount
@@ -184,9 +177,9 @@ function deleteDomains {
184177

185178
if [ $phase -eq 2 ]; then
186179
if [ $podcount -eq 0 ]; then
187-
echo @@ All pods shutdown, about to directly delete remaining artifacts.
180+
echo @@ All pods shutdown, about to directly delete remaining resources.
188181
elif [ $((mnow - mstart)) -gt $((maxwaitsecs / 2)) ]; then
189-
echo @@ Warning! $podcount WebLogic Server pods remaining but wait time exceeds half of max wait seconds. About to directly delete all remaining artifacts, including the leftover pods.
182+
echo @@ Warning! $podcount WebLogic Server pods remaining but wait time exceeds half of max wait seconds. About to directly delete all remaining resources, including the leftover pods.
190183
else
191184
echo @@ "Waiting for operator to shutdown pods (will wait for no more than half of max wait seconds before directly deleting them)."
192185
sleep 3
@@ -195,7 +188,7 @@ function deleteDomains {
195188
fi
196189
phase=3
197190

198-
# In phase 3, directly delete all k8s artifacts for the given domainUids
191+
# In phase 3, directly delete all k8s resources for the given domainUids
199192
# (including any leftover WLS pods from phases 1 & 2).
200193

201194
cat $tempfile | while read line; do
@@ -209,6 +202,15 @@ function deleteDomains {
209202
done
210203
}
211204

205+
# main entry point
206+
207+
# default when to stop retrying (override via command line)
208+
default_maxwaitsecs=120
209+
210+
# optional test mode that lists what would be deleted without
211+
# actually deleting (override via command line)
212+
test_mode=false
213+
212214
domains=""
213215

214216
# parse command line options

0 commit comments

Comments
 (0)