From 75203950b9698ccbb0926e53b6fed2052c3fe396 Mon Sep 17 00:00:00 2001 From: Simon Li Date: Mon, 14 Oct 2024 14:52:31 +0100 Subject: [PATCH 1/2] CI: Cleanup namespaces in parallel Currently if deletion of one namespace fails during cleanup the other namespace won't be deleted. Using asyncio.gather means we can delete in parallel but still receive the first Exception --- tests/conftest.py | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index e35d4efb..9048e7ae 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -236,6 +236,21 @@ async def watch_kubernetes(kube_client, kube_ns): raise exc +async def _delete_namespace(client, namespace): + await client.delete_namespace(namespace, body={}, grace_period_seconds=0) + for _ in range(20): # Usually finishes a good deal faster + try: + await client.read_namespace(namespace) + except ApiException as e: + if e.status == 404: + return + else: + raise + else: + print("waiting for %s to delete" % namespace) + await asyncio.sleep(1) + + @pytest_asyncio.fixture(scope="session") async def kube_client(request, kube_ns, kube_another_ns): """fixture for the Kubernetes client object. @@ -279,19 +294,11 @@ async def kube_client(request, kube_ns, kube_another_ns): # allow opting out of namespace cleanup, for post-mortem debugging if not os.environ.get("KUBESPAWNER_DEBUG_NAMESPACE"): - for namespace in expected_namespaces: - await client.delete_namespace(namespace, body={}, grace_period_seconds=0) - for _ in range(20): # Usually finishes a good deal faster - try: - await client.read_namespace(namespace) - except ApiException as e: - if e.status == 404: - return - else: - raise - else: - print("waiting for %s to delete" % namespace) - await asyncio.sleep(1) + # Delete in parallel so that if one deletion fails we still clean up the others + ns_deletions = asyncio.gather( + *[_delete_namespace(client, ns) for ns in expected_namespaces] + ) + await ns_deletions async def wait_for_pod(kube_client, kube_ns, pod_name, timeout=90): From 263bf894e368f5fb026835a2b23a00426e82b1d7 Mon Sep 17 00:00:00 2001 From: Simon Li Date: Mon, 14 Oct 2024 14:57:53 +0100 Subject: [PATCH 2/2] CI: Throw Exception if namespace not deleted after 20s --- tests/conftest.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/conftest.py b/tests/conftest.py index 9048e7ae..a92b9b77 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -249,6 +249,7 @@ async def _delete_namespace(client, namespace): else: print("waiting for %s to delete" % namespace) await asyncio.sleep(1) + raise Exception(f"Namespace {namespace} not deleted after 20 s") @pytest_asyncio.fixture(scope="session")