diff --git a/tools/internal_ci/linux/grpc_xds_resource_cleanup.sh b/tools/internal_ci/linux/grpc_xds_resource_cleanup.sh index 4f1921b745b..d0bc3b2e4a2 100644 --- a/tools/internal_ci/linux/grpc_xds_resource_cleanup.sh +++ b/tools/internal_ci/linux/grpc_xds_resource_cleanup.sh @@ -15,25 +15,25 @@ set -ex -cd "$(dirname "$0")/../../.." +# consts +readonly GITHUB_REPOSITORY_NAME="grpc" +readonly TEST_DRIVER_INSTALL_SCRIPT_URL="https://raw.githubusercontent.com/${TEST_DRIVER_REPO_OWNER:-grpc}/grpc/${TEST_DRIVER_BRANCH:-master}/tools/internal_ci/linux/grpc_xds_k8s_install_test_driver.sh" -pyenv local 3.6.1 -gcloud container clusters get-credentials interop-test-psm-sec-v2-us-central1-a --zone us-central1-a --project grpc-testing +cd "$(dirname "$0")/../../.." -cd tools/run_tests/xds_k8s_test_driver -python3 -m pip install -r requirements.txt +# Source the test driver from the master branch. +echo "Sourcing test driver install script from: ${TEST_DRIVER_INSTALL_SCRIPT_URL}" +source /dev/stdin <<< "$(curl -s "${TEST_DRIVER_INSTALL_SCRIPT_URL}")" +activate_gke_cluster GKE_CLUSTER_PSM_SECURITY +kokoro_setup_test_driver "${GITHUB_REPOSITORY_NAME}" -python3 -m grpc_tools.protoc --proto_path=../../../ \ - --python_out=. --grpc_python_out=. \ - src/proto/grpc/testing/empty.proto \ - src/proto/grpc/testing/messages.proto \ - src/proto/grpc/testing/test.proto +cd "${TEST_DRIVER_FULL_DIR}" # flag resource_prefix is required by the gke test framework, but doesn't # matter for the cleanup script. python3 -m bin.cleanup.cleanup \ --project=grpc-testing \ --network=default-vpc \ - --kube_context=gke_grpc-testing_us-central1-a_interop-test-psm-sec-v2-us-central1-a \ + --kube_context="${KUBE_CONTEXT}" \ --resource_prefix='required-but-does-not-matter' \ --td_bootstrap_image='required-but-does-not-matter' --server_image='required-but-does-not-matter' --client_image='required-but-does-not-matter' 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 a2b6ded9db8..74fd6581ce0 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,7 +35,6 @@ from typing import Any, List from absl import app from absl import flags -import pytz from framework import xds_flags from framework import xds_k8s_flags @@ -49,9 +48,8 @@ logger = logging.getLogger(__name__) Json = Any KubernetesClientRunner = client_app.KubernetesClientRunner KubernetesServerRunner = server_app.KubernetesServerRunner -utc = pytz.UTC -KEEP_PERIOD = datetime.timedelta(days=85) +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' @@ -86,8 +84,8 @@ def is_marked_as_keep_gke(suffix: str) -> bool: @functools.lru_cache() -def get_expire_timestamp() -> str: - return datetime.datetime.now() - KEEP_PERIOD +def get_expire_timestamp() -> datetime.datetime: + return datetime.datetime.now(datetime.timezone.utc) - KEEP_PERIOD def exec_gcloud(project: str, *cmds: List[str]) -> Json: @@ -267,7 +265,7 @@ def delete_leaked_td_resources(dry_run, project, network, resources): logging.info('Skipped [keep]:') break # break inner loop, continue outer loop remove(project, network, resource_prefix, result.group(1)) - break + break if not matched: logging.info( '----- Skipped [does not matching resource name templates]') @@ -291,8 +289,7 @@ def delete_k8s_resources(dry_run, project, network, k8s_api_manager, for ns in namespaces: logger.info('-----') logger.info('----- Cleaning up k8s namespaces %s', ns.metadata.name) - if ns.metadata.creation_timestamp <= utc.localize( - get_expire_timestamp()): + if ns.metadata.creation_timestamp <= get_expire_timestamp(): if dry_run: # Skip deletion for dry-runs logging.info('----- Skipped [Dry Run]: %s', ns.metadata.name)