@@ -19,22 +19,55 @@ export USE_GKE_E2E_AUTH_PLUGIN=True
1919export KUBECONFIG=${KUBECONFIG:- bin/ e2e-kubeconfig.yaml}
2020
2121mkdir -p bin/ns
22+
23+ function remove_pod() {
24+ ns=" $1 "
25+ pod_name=" $2 "
26+
27+ # Gracefully delete in 10 seconds, then force delete.
28+ if ! kubectl -n " $ns " delete pod --grace-period=10 " $pod_name " ; then
29+ kubectl -n " $ns " delete pod --grace-period=0 --force " $pod_name "
30+ fi
31+ }
32+
2233function remove_ns(){
2334 # Check that the namespace exists, return if not.
2435 if ! $KUBECTL get namespace " $1 " ; then
2536 return
2637 fi
2738
2839 # Tell kubernetes to delete the namespace, If it times out, force delete.
29- if ! $KUBECTL delete namespace " $1 " --timeout=10s ; then
40+ if ! $KUBECTL delete namespace " $1 " --timeout=30s ; then
41+
42+ # Attempt to delete all the pods in the namespace
43+ for pod_name in $( kubectl -n " $1 " get pods -o json | jq -r .items[].metadata.name) ; do
44+ remove_pod " $1 " " $pod_name "
45+ done
46+
47+ # Check if the namespace was deleted. If so, return
48+ if ! $KUBECTL get namespace " $1 " ; then
49+ return
50+ fi
3051
3152 # Get the namespace, remove finalizers from the namespace spec.
53+ # Force update the namespace resource, removing finalizers.
54+ # This will allow Kubernetes to continue the deletion of the resource.
55+
3256 $KUBECTL get namespace " $1 " -o json | \
3357 jq ' .spec.finalizers = []' > " bin/ns/$1 .json"
3458
35- # Force update the namespace resource, removing finalizers.
36- # This will allow Kubernetes to continue the deletion of the resource.
37- $KUBECTL replace --raw " /api/v1/namespaces/$1 /finalize" -f " bin/ns/$1 .json"
59+ if ! $KUBECTL replace --raw " /api/v1/namespaces/$1 /finalize" -f " bin/ns/$1 .json" ; then
60+ echo " Update finalizers failed. Will force delete"
61+ fi
62+ sleep 5
63+
64+ # Check if the namespace was deleted. If so, return
65+ if ! $KUBECTL get namespace " $1 " ; then
66+ return
67+ fi
68+
69+ # Attempt to delete the namespace again
70+ $KUBECTL delete namespace " $1 " --force || true # ignore failure
3871 fi
3972
4073}
0 commit comments