From f20924db248be505da091ff7fad84729d6e70240 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Bombo?= Date: Tue, 2 Jul 2024 22:23:54 +0000 Subject: [PATCH 1/2] ci: cleanup: Ignore nonexisting resources MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Some resource names seem to be lingering in Azure limbo but do not map to any actual resources, so we ignore those. Signed-off-by: Aurélien Bombo --- tests/cleanup_resources.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/tests/cleanup_resources.py b/tests/cleanup_resources.py index cdb58a37a3..46d91c80c3 100644 --- a/tests/cleanup_resources.py +++ b/tests/cleanup_resources.py @@ -3,6 +3,7 @@ from datetime import datetime, timedelta, timezone import os +from azure.core.exceptions import ResourceNotFoundError from azure.identity import AzureCliCredential from azure.mgmt.resource import ResourceManagementClient @@ -44,12 +45,19 @@ for resource in resources: # /subscriptions/(subscriptionId)/resourceGroups/(resourceGroupName)/providers/(resourceProviderNamespace)/(resourceType)/(resourceName) rg_id, _, _ = resource.id.partition("/providers/") _, _, rg_name = rg_id.partition("/resourceGroups/") - rg_resources = client.resources.list_by_resource_group(rg_name) + + try: + num_rg_resources = len(list(client.resources.list_by_resource_group(rg_name))) + except ResourceNotFoundError: + # Some resource names seem to be lingering in Azure limbo but do + # not map to any actual resources, so we ignore those. + print(f"{resource.name}: not found, ignored") + continue # XXX DANGER ZONE: Delete the resource. If it's the only resource # in its resource group, the entire resource group is deleted. - if len(list(rg_resources)) == 1: + if num_rg_resources == 1: client.resource_groups.begin_delete(rg_name) print(f"{resource.name}: deleted resource group") else: From eda5d2c623a16ec9774e5f61fb7329307f0a7493 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Bombo?= Date: Tue, 2 Jul 2024 22:25:01 +0000 Subject: [PATCH 2/2] ci: cleanup: Run every 24 hours instead of 6 hours MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Resources don't fail to get deleted as often to need to run every 6 hours. Signed-off-by: Aurélien Bombo --- .github/workflows/cleanup-resources.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/cleanup-resources.yaml b/.github/workflows/cleanup-resources.yaml index 52cca124e4..52017a5871 100644 --- a/.github/workflows/cleanup-resources.yaml +++ b/.github/workflows/cleanup-resources.yaml @@ -1,7 +1,7 @@ name: Cleanup dangling Azure resources on: schedule: - - cron: "0 */6 * * *" + - cron: "0 0 * * *" workflow_dispatch: jobs: @@ -27,5 +27,5 @@ jobs: - name: Cleanup resources env: AZ_SUBSCRIPTION_ID: ${{ secrets.AZ_SUBSCRIPTION_ID }} - CLEANUP_AFTER_HOURS: 6 # Clean up resources created more than this many hours ago. + CLEANUP_AFTER_HOURS: 24 # Clean up resources created more than this many hours ago. run: python3 tests/cleanup_resources.py