Skip to content

Commit 7abfb77

Browse files
authored
Merge pull request #184 from oracle/delete-script-speedup
Delete script speedup
2 parents 48e6904 + 0802e3f commit 7abfb77

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

kubernetes/delete-weblogic-domain-resources.sh

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,30 +70,32 @@ EOF
7070
#
7171
function getDomainResources {
7272
if [ "$1" = "all" ]; then
73-
local label_selector="weblogic.domainUID"
73+
LABEL_SELECTOR="weblogic.domainUID"
7474
else
75-
local label_selector="weblogic.domainUID in ($1)"
75+
LABEL_SELECTOR="weblogic.domainUID in ($1)"
7676
fi
7777

78-
# first, let's get all namespaced types with -l $label_selector
78+
# first, let's get all namespaced types with -l $LABEL_SELECTOR
7979

80-
local namespaced_types="pod,job,deploy,rs,service,pvc,ingress,cm,serviceaccount,role,rolebinding,secret"
80+
NAMESPACED_TYPES="pod,job,deploy,rs,service,pvc,ingress,cm,serviceaccount,role,rolebinding,secret"
8181

8282
# if domain crd exists, look for domains too:
8383
kubectl get crd domains.weblogic.oracle > /dev/null 2>&1
8484
if [ $? -eq 0 ]; then
85-
namespaced_types="domain,$namespaced_types"
85+
NAMESPACED_TYPES="domain,$NAMESPACED_TYPES"
8686
fi
8787

88-
kubectl get $namespaced_types \
89-
-l "$label_selector" \
88+
kubectl get $NAMESPACED_TYPES \
89+
-l "$LABEL_SELECTOR" \
9090
-o=jsonpath='{range .items[*]}{.kind}{" "}{.metadata.name}{" -n "}{.metadata.namespace}{"\n"}{end}' \
9191
--all-namespaces=true > $2
9292

93-
# now, get all non-namespaced types with -l $label_selector
93+
# now, get all non-namespaced types with -l $LABEL_SELECTOR
9494

95-
kubectl get pv,crd,clusterroles,clusterrolebindings \
96-
-l "$label_selector" \
95+
NOT_NAMESPACED_TYPES="pv,crd,clusterroles,clusterrolebindings"
96+
97+
kubectl get $NOT_NAMESPACED_TYPES \
98+
-l "$LABEL_SELECTOR" \
9799
-o=jsonpath='{range .items[*]}{.kind}{" "}{.metadata.name}{"\n"}{end}' \
98100
--all-namespaces=true >> $2
99101
}
@@ -191,13 +193,25 @@ function deleteDomains {
191193
# In phase 3, directly delete all k8s resources for the given domainUids
192194
# (including any leftover WLS pods from phases 1 & 2).
193195

194-
cat $tempfile | while read line; do
196+
# for each namespace with leftover resources, try delete them
197+
cat $tempfile | awk '{ print $4 }' | grep -v "^$" | sort -u | while read line; do
195198
if [ "$test_mode" = "true" ]; then
196-
echo kubectl delete $line --ignore-not-found
199+
echo kubectl -n $line delete $NAMESPACED_TYPES -l "$LABEL_SELECTOR"
197200
else
198-
kubectl delete $line --ignore-not-found
201+
kubectl -n $line delete $NAMESPACED_TYPES -l "$LABEL_SELECTOR"
199202
fi
200203
done
204+
205+
# if there are any non-namespaced types left, try delete them
206+
local no_namespace_count=`grep -c -v " -n " $tempfile`
207+
if [ ! "$no_namespace_count" = "0" ]; then
208+
if [ "$test_mode" = "true" ]; then
209+
echo kubectl delete $NOT_NAMESPACED_TYPES -l "$LABEL_SELECTOR"
210+
else
211+
kubectl delete $NOT_NAMESPACED_TYPES -l "$LABEL_SELECTOR"
212+
fi
213+
fi
214+
201215
sleep 3
202216
done
203217
}

0 commit comments

Comments
 (0)