From 87068e02c3c4d34f51019b3ccdceea1230c92fea Mon Sep 17 00:00:00 2001 From: Menghan Li Date: Thu, 24 Feb 2022 16:24:59 -0800 Subject: [PATCH] xds/cleanup: fix error datetime fromisoformat not available (#28953) --- .../xds_k8s_test_driver/bin/cleanup/cleanup.py | 13 ++++++++++--- .../run_tests/xds_k8s_test_driver/requirements.txt | 1 + 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/tools/run_tests/xds_k8s_test_driver/bin/cleanup/cleanup.py b/tools/run_tests/xds_k8s_test_driver/bin/cleanup/cleanup.py index 62651091aca..58c17d6d897 100755 --- a/tools/run_tests/xds_k8s_test_driver/bin/cleanup/cleanup.py +++ b/tools/run_tests/xds_k8s_test_driver/bin/cleanup/cleanup.py @@ -35,6 +35,7 @@ from typing import Any, List from absl import app from absl import flags +import dateutil from framework import xds_flags from framework import xds_k8s_flags @@ -49,7 +50,6 @@ Json = Any KubernetesClientRunner = client_app.KubernetesClientRunner KubernetesServerRunner = server_app.KubernetesServerRunner -KEEP_PERIOD = datetime.timedelta(days=7) GCLOUD = os.environ.get('GCLOUD', 'gcloud') GCLOUD_CMD_TIMEOUT_S = datetime.timedelta(seconds=5).total_seconds() ZONE = 'us-central1-a' @@ -58,6 +58,12 @@ SECONDARY_ZONE = 'us-west1-b' PSM_SECURITY_PREFIX = 'xds-k8s-security' # Prefix for gke resources to delete. URL_MAP_TEST_PREFIX = 'interop-psm-url-map' # Prefix for url-map test resources to delete. +KEEP_PERIOD_HOURS = flags.DEFINE_integer( + "keep_hours", + default=168, + help= + "number of hours for a resource to keep. Resources older than this will be deleted. Default is 168 (7 days)" +) DRY_RUN = flags.DEFINE_bool( "dry_run", default=False, @@ -103,7 +109,8 @@ def is_marked_as_keep_gke(suffix: str) -> bool: @functools.lru_cache() def get_expire_timestamp() -> datetime.datetime: - return datetime.datetime.now(datetime.timezone.utc) - KEEP_PERIOD + return datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta( + hours=KEEP_PERIOD_HOURS.value) def exec_gcloud(project: str, *cmds: List[str]) -> Json: @@ -337,7 +344,7 @@ def main(argv): compute = gcp.compute.ComputeV1(gcp.api.GcpApiManager(), project) leakedHealthChecks = [] for item in compute.list_health_check()['items']: - if datetime.datetime.fromisoformat( + if dateutil.parser.isoparse( item['creationTimestamp']) <= get_expire_timestamp(): leakedHealthChecks.append(item) diff --git a/tools/run_tests/xds_k8s_test_driver/requirements.txt b/tools/run_tests/xds_k8s_test_driver/requirements.txt index e45ed47503a..ba785fcb8ab 100644 --- a/tools/run_tests/xds_k8s_test_driver/requirements.txt +++ b/tools/run_tests/xds_k8s_test_driver/requirements.txt @@ -16,5 +16,6 @@ six~=1.13 tenacity~=6.2 packaging~=21.3 Pygments~=2.9 +python-dateutil~=2.8 protobuf~=3.14 xds-protos~=0.0.8