1+ #! /usr/bin/env bash
2+ # Copyright 2025 Google LLC
3+ #
4+ # Licensed under the Apache License, Version 2.0 (the "License");
5+ # you may not use this file except in compliance with the License.
6+ # You may obtain a copy of the License at
7+ #
8+ # http://www.apache.org/licenses/LICENSE-2.0
9+ #
10+ # Unless required by applicable law or agreed to in writing, software
11+ # distributed under the License is distributed on an "AS IS" BASIS,
12+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ # See the License for the specific language governing permissions and
14+ # limitations under the License.
15+
16+ set -euxo pipefail
17+ KUBECTL=${KUBECTL:- bin/ kubectl}
18+ export USE_GKE_E2E_AUTH_PLUGIN=True
19+ export KUBECONFIG=${KUBECONFIG:- bin/ e2e-kubeconfig.yaml}
20+
21+ mkdir -p bin/ns
22+ function remove_ns(){
23+ # Check that the namespace exists, return if not.
24+ if ! $KUBECTL get namespace " $1 " ; then
25+ return
26+ fi
27+
28+ # Tell kubernetes to delete the namespace, If it times out, force delete.
29+ if ! $KUBECTL delete namespace " $1 " --timeout=10s ; then
30+
31+ # Get the namespace, remove finalizers from the namespace spec.
32+ $KUBECTL get namespace " $1 " -o json | \
33+ jq ' .spec.finalizers = []' > " bin/ns/$1 .json"
34+
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"
38+ fi
39+
40+ }
41+
42+
43+ if [[ ${#@ } -gt 0 ]] ; then
44+ remove_ns " $1 "
45+ else
46+ ( $KUBECTL get ns -o=name | grep namespace/test > bin/ns/list.txt ) || true
47+ namespaces=( $( cat bin/ns/list.txt ) )
48+ for ns in ${namespaces[*]} ; do
49+ ns=" ${ns#*/ } " # remove "namespace/" from the beginning of the string
50+ echo " Deleting $ns "
51+ remove_ns " $ns "
52+ done
53+ fi
0 commit comments