Skip to content

Commit cbe1f5a

Browse files
committed
test: Fix kubernetes cleanup in the test environment to avoid hanging tests.
1 parent b607741 commit cbe1f5a

File tree

4 files changed

+55
-8
lines changed

4 files changed

+55
-8
lines changed

Makefile

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -372,17 +372,15 @@ e2e_test_run_gotest: # Run the golang e2e tests
372372

373373
.PHONY: e2e_cleanup_test_namespaces
374374
e2e_cleanup_test_namespaces: e2e_project kustomize kubectl # remove e2e test namespaces named "test*"
375-
( $(E2E_KUBECTL) get ns -o=name | \
376-
grep namespace/test | \
377-
$(E2E_KUBECTL_ENV) xargs $(KUBECTL) delete ) || true
378-
( $(E2E_PRIVATE_KUBECTL) get ns -o=name | \
379-
grep namespace/test | \
380-
$(E2E_PRIVATE_KUBECTL_ENV) xargs $(KUBECTL) delete ) || true
375+
$(E2E_PRIVATE_KUBECTL_ENV) KUBECTL=$(KUBECTL) $(PWD)/tools/delete-test-namespaces.sh
376+
$(E2E_KUBECTL_ENV) KUBECTL=$(KUBECTL) $(PWD)/tools/delete-test-namespaces.sh
381377

382378
.PHONY: e2e_undeploy
383379
e2e_undeploy: e2e_project kustomize kubectl $(E2E_WORK_DIR) # Remove the operator from the GKE cluster
384-
$(E2E_KUBECTL) delete -f $(E2E_WORK_DIR)/operator.yaml
385-
$(E2E_PRIVATE_KUBECTL) delete -f $(E2E_WORK_DIR)/operator.yaml
380+
$(E2E_KUBECTL) delete -f $(E2E_WORK_DIR)/operator.yaml --timeout=30s || true
381+
$(E2E_KUBECTL_ENV) KUBECTL=$(KUBECTL) $(PWD)/tools/delete-test-namespaces.sh namespace/cloud-sql-proxy-operator-system
382+
$(E2E_PRIVATE_KUBECTL) delete -f $(E2E_WORK_DIR)/operator.yaml --timeout=30s || true
383+
$(E2E_PRIVATE_KUBECTL_ENV) KUBECTL=$(KUBECTL) $(PWD)/tools/delete-test-namespaces.sh namespace/cloud-sql-proxy-operator-system
386384

387385
###
388386
# Build the operator docker image and push it to the

infra/permissions/main.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ resource "google_project_iam_member" "allow_image_pull" {
7676
role = "roles/artifactregistry.reader"
7777
member = "serviceAccount:${google_service_account.node_pool.email}"
7878
}
79+
resource "google_project_iam_member" "default_node_service_acct" {
80+
depends_on = [google_project_service.project["iam.googleapis.com"]]
81+
project = var.project_id
82+
role = "roles/container.defaultNodeServiceAccount"
83+
member = "serviceAccount:${google_service_account.node_pool.email}"
84+
}
7985

8086
resource "google_project_iam_binding" "cloud_sql_client" {
8187
depends_on = [google_project_service.project["iam.googleapis.com"]]

tools/delete-test-namespaces.sh

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env bash
2+
set -euxo pipefail
3+
KUBECTL=${KUBECTL:-bin/kubectl}
4+
export USE_GKE_E2E_AUTH_PLUGIN=True
5+
export KUBECONFIG=${KUBECONFIG:-bin/e2e-kubeconfig.yaml}
6+
7+
mkdir -p bin/ns
8+
function remove_ns(){
9+
# Check that the namespace exists, return if not.
10+
if ! $KUBECTL get namespace "$1" ; then
11+
return
12+
fi
13+
14+
# Tell kubernetes to delete the namespace, If it times out, force delete.
15+
if ! $KUBECTL delete namespace "$1" --timeout=10s ; then
16+
17+
# Get the namespace, remove finalizers from the namespace spec.
18+
$KUBECTL get namespace "$1" -o json | \
19+
jq '.spec.finalizers = []' > "bin/ns/$1.json"
20+
21+
# Force update the namespace resource, removing finalizers.
22+
# This will allow Kubernetes to continue the deletion of the resource.
23+
$KUBECTL replace --raw "/api/v1/namespaces/$1/finalize" -f "bin/ns/$1.json"
24+
fi
25+
26+
}
27+
28+
29+
if [[ ${#@} -gt 0 ]] ; then
30+
remove_ns "$1"
31+
else
32+
namespaces=( $( $KUBECTL get ns -o=name | grep namespace/test ) )
33+
for ns in ${namespaces[*]} ; do
34+
ns="${ns#*/}" # remove "namespace/" from the beginning of the string
35+
echo "Deleting $ns"
36+
remove_ns "$ns"
37+
done
38+
fi

tools/e2e_test_job.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ set -x
5858
echo "TIME: $(date) Run Tests"
5959
echo "Running tests on environment ${ENVIRONMENT_NAME:-undefined}"
6060

61+
# Force cleanup of the existing resources before running tests.
62+
echo "Cleaning up the test resources from past runs"
63+
make e2e_test_clean >> bin/e2e_test.log || true
64+
65+
echo "Starting the tests"
6166
# Run e2e test, filtering the stdout so that it only logs go test results.
6267
if make e2e_test_job > bin/e2e_test.log 2>&1 ; then
6368
echo "STATUS: E2E Test Passed"

0 commit comments

Comments
 (0)