@@ -19,22 +19,55 @@ export USE_GKE_E2E_AUTH_PLUGIN=True
19
19
export KUBECONFIG=${KUBECONFIG:- bin/ e2e-kubeconfig.yaml}
20
20
21
21
mkdir -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
+
22
33
function remove_ns(){
23
34
# Check that the namespace exists, return if not.
24
35
if ! $KUBECTL get namespace " $1 " ; then
25
36
return
26
37
fi
27
38
28
39
# 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
30
51
31
52
# 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
+
32
56
$KUBECTL get namespace " $1 " -o json | \
33
57
jq ' .spec.finalizers = []' > " bin/ns/$1 .json"
34
58
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
38
71
fi
39
72
40
73
}
0 commit comments