Skip to content

Commit 64ff5a9

Browse files
authored
cleanup enhancement (#2451)
Signed-off-by: Allen Li <liyuchen223@gmail.com>
1 parent 49d3f23 commit 64ff5a9

File tree

1 file changed

+51
-56
lines changed

1 file changed

+51
-56
lines changed

preload_data.sh

Lines changed: 51 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ function main() {
5757
if [[ $RERUN == "true" ]]; then
5858
info "Rerun specified..."
5959
cleanup
60-
deletemongocopy
6160
fi
6261
# run backup preload
6362
backup_preload_mongo
@@ -77,7 +76,6 @@ function main() {
7776
else
7877
info "Cleanup selected. Cleaning MongoDB in services namespace $TO_NAMESPACE"
7978
cleanup
80-
deletemongocopy
8179
fi
8280
}
8381

@@ -257,7 +255,7 @@ function backup_preload_mongo() {
257255
dumpmongo
258256
swapmongopvc
259257
loadmongo
260-
deletemongocopy
258+
cleanup
261259
provision_external_connection
262260
} # backup_preload_mongo
263261

@@ -327,6 +325,10 @@ function patch_cm() {
327325
local deployments=$(${OC} get deployments ibm-mongodb-operator -n $FROM_NAMESPACE -o=jsonpath='{.spec.replicas}')
328326
local replicas=$(${OC} get statefulSet icp-mongodb -n $FROM_NAMESPACE -o=jsonpath='{.spec.replicas}')
329327
${OC} scale deployment -n $FROM_NAMESPACE ibm-mongodb-operator --replicas=0
328+
# Scale down ibm-mongodb-operator in CSV level
329+
local mongo_csv=$("${OC}" get -n "${FROM_NAMESPACE}" csv | (grep ibm-mongodb-operator || echo "fail") | awk '{print $1}')
330+
${OC} patch csv ${mongo_csv} -n ${FROM_NAMESPACE} --type='json' -p='[{"op": "replace", "path": "/spec/install/spec/deployments/0/spec/replicas", "value": '0'}]'
331+
330332

331333
migration=$(${OC} get statefulset icp-mongodb -n $FROM_NAMESPACE -o=jsonpath='{.spec.template.metadata.labels.migrating}')
332334
if [[ $migration != "" ]]; then
@@ -759,8 +761,53 @@ function cleanup() {
759761
fi
760762
fi
761763
success "Previous run cleaned up."
762-
} # cleanup
763764

765+
title "Deleting the stand up mongodb statefulset in $TO_NAMESPACE"
766+
msg "-----------------------------------------------------------------------"
767+
768+
currentns=$(${OC} project $TO_NAMESPACE -q)
769+
if [[ "$currentns" -ne "$TO_NAMESPACE" ]]; then
770+
error "Cannot switch to $TO_NAMESPACE"
771+
fi
772+
773+
#delete all other resources EXCEPT icp-mongodb-admin
774+
${OC} delete statefulset icp-mongodb --ignore-not-found
775+
${OC} delete service icp-mongodb --ignore-not-found
776+
${OC} delete issuer god-issuer --ignore-not-found
777+
${OC} delete cm ibm-cpp-config --ignore-not-found
778+
${OC} delete certificate icp-mongodb-client-cert --ignore-not-found
779+
${OC} delete cm icp-mongodb --ignore-not-found
780+
${OC} delete cm icp-mongodb-init --ignore-not-found
781+
${OC} delete cm icp-mongodb-install --ignore-not-found
782+
${OC} delete secret icp-mongodb-keyfile --ignore-not-found
783+
${OC} delete secret icp-mongodb-metrics --ignore-not-found
784+
${OC} delete sa ibm-mongodb-operand --ignore-not-found
785+
${OC} delete service mongodb --ignore-not-found
786+
${OC} delete certificate mongodb-root-ca-cert --ignore-not-found
787+
${OC} delete issuer mongodb-root-ca-issuer --ignore-not-found
788+
${OC} delete cm namespace-scope --ignore-not-found
789+
790+
#delete mongodump pvc and pv
791+
VOL=$(${OC} get pvc cs-mongodump -o=jsonpath='{.spec.volumeName}' --ignore-not-found)
792+
if [[ -z "$VOL" ]]; then
793+
warning "Volume for pvc cs-mongodump not found in $TO_NAMESPACE. It may have already been deleted."
794+
else
795+
${OC} patch pv $VOL -p '{"spec": { "persistentVolumeReclaimPolicy" : "Delete" }}'
796+
${OC} delete pvc cs-mongodump -n $TO_NAMESPACE --ignore-not-found --timeout=30s
797+
if [ $? -ne 0 ]; then
798+
info "Failed to delete pvc cs-mongodump, patching its finalizer to null..."
799+
${OC} patch pvc cs-mongodump -n $TO_NAMESPACE --type="json" -p '[{"op": "remove", "path":"/metadata/finalizers"}]' --ignore-not-found
800+
fi
801+
${OC} delete pv $VOL --ignore-not-found --timeout=30s
802+
if [ $? -ne 0 ]; then
803+
info "Failed to delete pv $VOL, patching its finalizer to null..."
804+
${OC} patch pv $VOL --type="json" -p '[{"op": "remove", "path":"/metadata/finalizers"}]'
805+
fi
806+
fi
807+
808+
success "MongoDB removed from services namespace $TO_NAMESPACE"
809+
810+
} # cleanup
764811

765812
#
766813
# Create the dump PVC
@@ -2355,58 +2402,6 @@ EOF
23552402

23562403
} # deploymongocopy
23572404

2358-
2359-
#
2360-
# Delete the mongo copy
2361-
#
2362-
function deletemongocopy {
2363-
title "Deleting the stand up mongodb statefulset in $TO_NAMESPACE"
2364-
msg "-----------------------------------------------------------------------"
2365-
2366-
currentns=$(${OC} project $TO_NAMESPACE -q)
2367-
if [[ "$currentns" -ne "$TO_NAMESPACE" ]]; then
2368-
error "Cannot switch to $TO_NAMESPACE"
2369-
fi
2370-
2371-
#delete all other resources EXCEPT icp-mongodb-admin
2372-
${OC} delete statefulset icp-mongodb --ignore-not-found
2373-
${OC} delete service icp-mongodb --ignore-not-found
2374-
${OC} delete issuer god-issuer --ignore-not-found
2375-
${OC} delete cm ibm-cpp-config --ignore-not-found
2376-
${OC} delete certificate icp-mongodb-client-cert --ignore-not-found
2377-
${OC} delete cm icp-mongodb --ignore-not-found
2378-
${OC} delete cm icp-mongodb-init --ignore-not-found
2379-
${OC} delete cm icp-mongodb-install --ignore-not-found
2380-
${OC} delete secret icp-mongodb-keyfile --ignore-not-found
2381-
${OC} delete secret icp-mongodb-metrics --ignore-not-found
2382-
${OC} delete sa ibm-mongodb-operand --ignore-not-found
2383-
${OC} delete service mongodb --ignore-not-found
2384-
${OC} delete certificate mongodb-root-ca-cert --ignore-not-found
2385-
${OC} delete issuer mongodb-root-ca-issuer --ignore-not-found
2386-
${OC} delete cm namespace-scope --ignore-not-found
2387-
2388-
#delete mongodump pvc and pv
2389-
VOL=$(${OC} get pvc cs-mongodump -o=jsonpath='{.spec.volumeName}' --ignore-not-found)
2390-
if [[ -z "$VOL" ]]; then
2391-
warning "Volume for pvc cs-mongodump not found in $TO_NAMESPACE. It may have already been deleted."
2392-
else
2393-
${OC} patch pv $VOL -p '{"spec": { "persistentVolumeReclaimPolicy" : "Delete" }}'
2394-
${OC} delete pvc cs-mongodump -n $TO_NAMESPACE --ignore-not-found --timeout=30s
2395-
if [ $? -ne 0 ]; then
2396-
info "Failed to delete pvc cs-mongodump, patching its finalizer to null..."
2397-
${OC} patch pvc cs-mongodump -n $TO_NAMESPACE --type="json" -p '[{"op": "remove", "path":"/metadata/finalizers"}]' --ignore-not-found
2398-
fi
2399-
${OC} delete pv $VOL --ignore-not-found --timeout=30s
2400-
if [ $? -ne 0 ]; then
2401-
info "Failed to delete pv $VOL, patching its finalizer to null..."
2402-
${OC} patch pv $VOL --type="json" -p '[{"op": "remove", "path":"/metadata/finalizers"}]'
2403-
fi
2404-
fi
2405-
2406-
success "MongoDB removed from services namespace $TO_NAMESPACE"
2407-
2408-
} # deletemongocopy
2409-
24102405
function delete_mongo_pods() {
24112406
local namespace=$1
24122407
local pods=$(${OC} get pods -n $namespace | grep icp-mongodb | awk '{print $1}' | tr "\n" " ")

0 commit comments

Comments
 (0)