11#! /usr/bin/env sh
22
3+ touch error.log
4+ tail -F error.log &
5+
36delete_resources_safely () {
47 resource_type=" $1 "
58 namespace=" $2 "
69
710 echo " Attempting normal deletion of $resource_type in $namespace ..."
8- kubectl delete " ${resource_type} " --all -n " ${namespace} " --wait=true --timeout=10s || true
11+ kubectl delete " ${resource_type} " --all -n " ${namespace} " --wait=true --timeout=10s 2> error.log || true
912
1013 # Check if any resources are still stuck
1114 # Let's not fail here and continue deletion
12- resources=$( kubectl get " $resource_type " -n " ${namespace} " --no-headers -o custom-columns=" :metadata.name" 2> /dev/null || true)
15+ resources=$( kubectl get " $resource_type " -n " ${namespace} " --no-headers -o custom-columns=" :metadata.name" 2> error.log || true)
1316
1417 for resource in ${resources} ; do
1518 echo " ${resource_type} /${resource} is still present, force deleting..."
1619
17- kubectl patch " ${resource_type} " " ${resource} " -n " ${namespace} " -p ' {"metadata":{"finalizers":null}}' --type=merge || true
18- kubectl delete " ${resource_type} " " ${resource} " -n " ${namespace} " --force --grace-period=0 || true
20+ kubectl patch " ${resource_type} " " ${resource} " -n " ${namespace} " -p ' {"metadata":{"finalizers":null}}' --type=merge 2> error.log || true
21+ kubectl delete " ${resource_type} " " ${resource} " -n " ${namespace} " --force --grace-period=0 2> error.log || true
1922 done
2023}
2124
@@ -29,11 +32,12 @@ if [ -z ${LABELS+x} ]; then
2932 exit 1
3033fi
3134
35+
3236echo " Deleting namespaces for evg tasks that are older than ${DELETE_OLDER_THAN_AMOUNT} ${DELETE_OLDER_THAN_UNIT} with label ${LABELS} "
3337echo " Which are:"
3438kubectl get namespace -l " ${LABELS} " -o name
35- for namespace in $( kubectl get namespace -l " ${LABELS} " -o name) ; do
36- creation_time=$( kubectl get " ${namespace} " -o jsonpath=' {.metadata.creationTimestamp}' 2> /dev/null || echo " " )
39+ for namespace in $( kubectl get namespace -l " ${LABELS} " -o name 2> error.log ) ; do
40+ creation_time=$( kubectl get " ${namespace} " -o jsonpath=' {.metadata.creationTimestamp}' 2> error.log || echo " " )
3741
3842 if [ -z " $creation_time " ]; then
3943 echo " Namespace ${namespace} does not exist or has no creation timestamp, skipping."
@@ -49,14 +53,17 @@ for namespace in $(kubectl get namespace -l "${LABELS}" -o name); do
4953
5054 echo " Deleting ${namespace_name} "
5155
52- csrs_in_namespace=$( kubectl get csr -o name | grep " ${namespace_name} " || true)
56+ csrs_in_namespace=$( kubectl get csr -o name 2> error.log | grep " ${namespace_name} " 2> /dev/null || true)
5357 if [ -n " ${csrs_in_namespace} " ]; then
54- kubectl delete " ${csrs_in_namespace} "
58+ kubectl delete " ${csrs_in_namespace} " 2> error.log
5559 fi
5660
5761 delete_resources_safely " mdb" " ${namespace_name} "
5862 delete_resources_safely " mdbu" " ${namespace_name} "
63+ delete_resources_safely " mdbc" " ${namespace_name} "
64+ delete_resources_safely " mdbmc" " ${namespace_name} "
5965 delete_resources_safely " om" " ${namespace_name} "
66+ delete_resources_safely " clustermongodbroles" " ${namespace_name} "
6067
6168 echo " Attempting to delete namespace: ${namespace_name} "
6269
0 commit comments