diff --git a/goldens/Basic_cluster_create.txt b/goldens/Basic_cluster_create.txt index 5b2657e3e..e994df8f4 100644 --- a/goldens/Basic_cluster_create.txt +++ b/goldens/Basic_cluster_create.txt @@ -16,13 +16,10 @@ gcloud container clusters list --project=golden-project --filter=name=golden-clu gcloud container clusters describe golden-cluster --project=golden-project --location=us-central1 --format="value(privateClusterConfig.enablePrivateNodes)" [XPK] Private Nodes is not enabled on the cluster. [XPK] Cluster is public and no need to authorize networks. -[XPK] Try 1: get-credentials-dns-endpoint to cluster golden-cluster [XPK] Task: `get-credentials-dns-endpoint to cluster golden-cluster` is implemented by the following command not running since it is a dry run. gcloud container clusters get-credentials golden-cluster --location=us-central1 --dns-endpoint --project=golden-project && kubectl config view && kubectl config set-context --current --namespace=default -[XPK] Testing credentials with kubectl... -[XPK] Task: `kubectl get pods` is implemented by the following command not running since it is a dry run. +[XPK] Task: `Test kubectl credentials` is implemented by the following command not running since it is a dry run. kubectl get pods -[XPK] Credentials test succeeded. [XPK] Finished get-credentials and kubectl setup. [XPK] Task: 'Checking CoreDNS deployment existence' in progress for namespace: kube-system [XPK] Task: `Check CoreDNS deployment in kube-system` is implemented by the following command not running since it is a dry run. diff --git a/goldens/Batch.txt b/goldens/Batch.txt index 5b5b303cf..8f327e2ac 100644 --- a/goldens/Batch.txt +++ b/goldens/Batch.txt @@ -3,13 +3,10 @@ $ xpk batch --project=golden-project --zone=us-central1-a --cluster=golden-clust [XPK] Working on golden-project and us-central1-a [XPK] Task: `Find cluster region or zone` is implemented by the following command not running since it is a dry run. gcloud container clusters list --project=golden-project --filter=name=golden-cluster --format="value(location)" -[XPK] Try 1: get-credentials-dns-endpoint to cluster golden-cluster [XPK] Task: `get-credentials-dns-endpoint to cluster golden-cluster` is implemented by the following command not running since it is a dry run. gcloud container clusters get-credentials golden-cluster --location=us-central1 --dns-endpoint --project=golden-project && kubectl config view && kubectl config set-context --current --namespace=default -[XPK] Testing credentials with kubectl... -[XPK] Task: `kubectl get pods` is implemented by the following command not running since it is a dry run. +[XPK] Task: `Test kubectl credentials` is implemented by the following command not running since it is a dry run. kubectl get pods -[XPK] Credentials test succeeded. [XPK] Finished get-credentials and kubectl setup. [XPK] Task: `GKE Cluster Get ConfigMap` is implemented by the following command not running since it is a dry run. kubectl get configmap golden-cluster-resources-configmap -o=custom-columns="ConfigData:data" --no-headers=true diff --git a/goldens/Cluster_create_for_multi-host_nodepool.txt b/goldens/Cluster_create_for_multi-host_nodepool.txt index eae794c59..8342cd965 100644 --- a/goldens/Cluster_create_for_multi-host_nodepool.txt +++ b/goldens/Cluster_create_for_multi-host_nodepool.txt @@ -16,13 +16,10 @@ gcloud container clusters list --project=golden-project --filter=name=golden-clu gcloud container clusters describe golden-cluster --project=golden-project --location=us-central1 --format="value(privateClusterConfig.enablePrivateNodes)" [XPK] Private Nodes is not enabled on the cluster. [XPK] Cluster is public and no need to authorize networks. -[XPK] Try 1: get-credentials-dns-endpoint to cluster golden-cluster [XPK] Task: `get-credentials-dns-endpoint to cluster golden-cluster` is implemented by the following command not running since it is a dry run. gcloud container clusters get-credentials golden-cluster --location=us-central1 --dns-endpoint --project=golden-project && kubectl config view && kubectl config set-context --current --namespace=default -[XPK] Testing credentials with kubectl... -[XPK] Task: `kubectl get pods` is implemented by the following command not running since it is a dry run. +[XPK] Task: `Test kubectl credentials` is implemented by the following command not running since it is a dry run. kubectl get pods -[XPK] Credentials test succeeded. [XPK] Finished get-credentials and kubectl setup. [XPK] Task: 'Checking CoreDNS deployment existence' in progress for namespace: kube-system [XPK] Task: `Check CoreDNS deployment in kube-system` is implemented by the following command not running since it is a dry run. diff --git a/goldens/Cluster_create_private.txt b/goldens/Cluster_create_private.txt index 7902db28f..720d7d40a 100644 --- a/goldens/Cluster_create_private.txt +++ b/goldens/Cluster_create_private.txt @@ -18,13 +18,10 @@ gcloud container clusters describe golden-cluster-private --project=golden-proje [XPK] Task: `Fetching the list of authorized network from cluster describe.` is implemented by the following command not running since it is a dry run. gcloud container clusters describe golden-cluster-private --project=golden-project --location=us-central1 --format="value(masterAuthorizedNetworksConfig.cidrBlocks[].cidrBlock)" [XPK] Current machine's IP adrress is already authorized. -[XPK] Try 1: get-credentials-dns-endpoint to cluster golden-cluster-private [XPK] Task: `get-credentials-dns-endpoint to cluster golden-cluster-private` is implemented by the following command not running since it is a dry run. gcloud container clusters get-credentials golden-cluster-private --location=us-central1 --dns-endpoint --project=golden-project && kubectl config view && kubectl config set-context --current --namespace=default -[XPK] Testing credentials with kubectl... -[XPK] Task: `kubectl get pods` is implemented by the following command not running since it is a dry run. +[XPK] Task: `Test kubectl credentials` is implemented by the following command not running since it is a dry run. kubectl get pods -[XPK] Credentials test succeeded. [XPK] Finished get-credentials and kubectl setup. [XPK] Task: 'Checking CoreDNS deployment existence' in progress for namespace: kube-system [XPK] Task: `Check CoreDNS deployment in kube-system` is implemented by the following command not running since it is a dry run. diff --git a/goldens/Cluster_create_with_CPU_and_memory_limits_above_capacity.txt b/goldens/Cluster_create_with_CPU_and_memory_limits_above_capacity.txt index fa3b63ae5..623646ced 100644 --- a/goldens/Cluster_create_with_CPU_and_memory_limits_above_capacity.txt +++ b/goldens/Cluster_create_with_CPU_and_memory_limits_above_capacity.txt @@ -16,13 +16,10 @@ gcloud container clusters list --project=golden-project --filter=name=golden-clu gcloud container clusters describe golden-cluster --project=golden-project --location=us-central1 --format="value(privateClusterConfig.enablePrivateNodes)" [XPK] Private Nodes is not enabled on the cluster. [XPK] Cluster is public and no need to authorize networks. -[XPK] Try 1: get-credentials-dns-endpoint to cluster golden-cluster [XPK] Task: `get-credentials-dns-endpoint to cluster golden-cluster` is implemented by the following command not running since it is a dry run. gcloud container clusters get-credentials golden-cluster --location=us-central1 --dns-endpoint --project=golden-project && kubectl config view && kubectl config set-context --current --namespace=default -[XPK] Testing credentials with kubectl... -[XPK] Task: `kubectl get pods` is implemented by the following command not running since it is a dry run. +[XPK] Task: `Test kubectl credentials` is implemented by the following command not running since it is a dry run. kubectl get pods -[XPK] Credentials test succeeded. [XPK] Finished get-credentials and kubectl setup. [XPK] Task: 'Checking CoreDNS deployment existence' in progress for namespace: kube-system [XPK] Task: `Check CoreDNS deployment in kube-system` is implemented by the following command not running since it is a dry run. diff --git a/goldens/Cluster_create_with_CPU_and_memory_limits_below_capacity.txt b/goldens/Cluster_create_with_CPU_and_memory_limits_below_capacity.txt index b7ce142c8..c628158bc 100644 --- a/goldens/Cluster_create_with_CPU_and_memory_limits_below_capacity.txt +++ b/goldens/Cluster_create_with_CPU_and_memory_limits_below_capacity.txt @@ -16,13 +16,10 @@ gcloud container clusters list --project=golden-project --filter=name=golden-clu gcloud container clusters describe golden-cluster --project=golden-project --location=us-central1 --format="value(privateClusterConfig.enablePrivateNodes)" [XPK] Private Nodes is not enabled on the cluster. [XPK] Cluster is public and no need to authorize networks. -[XPK] Try 1: get-credentials-dns-endpoint to cluster golden-cluster [XPK] Task: `get-credentials-dns-endpoint to cluster golden-cluster` is implemented by the following command not running since it is a dry run. gcloud container clusters get-credentials golden-cluster --location=us-central1 --dns-endpoint --project=golden-project && kubectl config view && kubectl config set-context --current --namespace=default -[XPK] Testing credentials with kubectl... -[XPK] Task: `kubectl get pods` is implemented by the following command not running since it is a dry run. +[XPK] Task: `Test kubectl credentials` is implemented by the following command not running since it is a dry run. kubectl get pods -[XPK] Credentials test succeeded. [XPK] Finished get-credentials and kubectl setup. [XPK] Task: 'Checking CoreDNS deployment existence' in progress for namespace: kube-system [XPK] Task: `Check CoreDNS deployment in kube-system` is implemented by the following command not running since it is a dry run. diff --git a/goldens/Cluster_create_with_gb200-4.txt b/goldens/Cluster_create_with_gb200-4.txt index 0cee708f9..8a9d674c0 100644 --- a/goldens/Cluster_create_with_gb200-4.txt +++ b/goldens/Cluster_create_with_gb200-4.txt @@ -16,13 +16,10 @@ gcloud container clusters list --project=golden-project --filter=name=golden-clu gcloud container clusters describe golden-cluster --project=golden-project --location=us-central1 --format="value(privateClusterConfig.enablePrivateNodes)" [XPK] Private Nodes is not enabled on the cluster. [XPK] Cluster is public and no need to authorize networks. -[XPK] Try 1: get-credentials-dns-endpoint to cluster golden-cluster [XPK] Task: `get-credentials-dns-endpoint to cluster golden-cluster` is implemented by the following command not running since it is a dry run. gcloud container clusters get-credentials golden-cluster --location=us-central1 --dns-endpoint --project=golden-project && kubectl config view && kubectl config set-context --current --namespace=default -[XPK] Testing credentials with kubectl... -[XPK] Task: `kubectl get pods` is implemented by the following command not running since it is a dry run. +[XPK] Task: `Test kubectl credentials` is implemented by the following command not running since it is a dry run. kubectl get pods -[XPK] Credentials test succeeded. [XPK] Finished get-credentials and kubectl setup. [XPK] Task: 'Checking CoreDNS deployment existence' in progress for namespace: kube-system [XPK] Task: `Check CoreDNS deployment in kube-system` is implemented by the following command not running since it is a dry run. diff --git a/goldens/Cluster_create_with_shared_reservation.txt b/goldens/Cluster_create_with_shared_reservation.txt index 5a9555228..c880a29e7 100644 --- a/goldens/Cluster_create_with_shared_reservation.txt +++ b/goldens/Cluster_create_with_shared_reservation.txt @@ -16,13 +16,10 @@ gcloud container clusters list --project=golden-project --filter=name=golden-clu gcloud container clusters describe golden-cluster --project=golden-project --location=us-central1 --format="value(privateClusterConfig.enablePrivateNodes)" [XPK] Private Nodes is not enabled on the cluster. [XPK] Cluster is public and no need to authorize networks. -[XPK] Try 1: get-credentials-dns-endpoint to cluster golden-cluster [XPK] Task: `get-credentials-dns-endpoint to cluster golden-cluster` is implemented by the following command not running since it is a dry run. gcloud container clusters get-credentials golden-cluster --location=us-central1 --dns-endpoint --project=golden-project && kubectl config view && kubectl config set-context --current --namespace=default -[XPK] Testing credentials with kubectl... -[XPK] Task: `kubectl get pods` is implemented by the following command not running since it is a dry run. +[XPK] Task: `Test kubectl credentials` is implemented by the following command not running since it is a dry run. kubectl get pods -[XPK] Credentials test succeeded. [XPK] Finished get-credentials and kubectl setup. [XPK] Task: 'Checking CoreDNS deployment existence' in progress for namespace: kube-system [XPK] Task: `Check CoreDNS deployment in kube-system` is implemented by the following command not running since it is a dry run. diff --git a/goldens/Job_cancel.txt b/goldens/Job_cancel.txt index e8931dc9d..ec8946781 100644 --- a/goldens/Job_cancel.txt +++ b/goldens/Job_cancel.txt @@ -4,13 +4,10 @@ $ xpk job cancel golden-job --project=golden-project --zone=us-central1-a --clus [XPK] Working on golden-project and us-central1-a [XPK] Task: `Find cluster region or zone` is implemented by the following command not running since it is a dry run. gcloud container clusters list --project=golden-project --filter=name=golden-cluster --format="value(location)" -[XPK] Try 1: get-credentials-dns-endpoint to cluster golden-cluster [XPK] Task: `get-credentials-dns-endpoint to cluster golden-cluster` is implemented by the following command not running since it is a dry run. gcloud container clusters get-credentials golden-cluster --location=us-central1 --dns-endpoint --project=golden-project && kubectl config view && kubectl config set-context --current --namespace=default -[XPK] Testing credentials with kubectl... -[XPK] Task: `kubectl get pods` is implemented by the following command not running since it is a dry run. +[XPK] Task: `Test kubectl credentials` is implemented by the following command not running since it is a dry run. kubectl get pods -[XPK] Credentials test succeeded. [XPK] Finished get-credentials and kubectl setup. [XPK] Task: `delete job` is implemented by the following command not running since it is a dry run. kubectl-kjob delete slurm golden-job diff --git a/goldens/Job_list.txt b/goldens/Job_list.txt index 53e6dadfd..9c86c3955 100644 --- a/goldens/Job_list.txt +++ b/goldens/Job_list.txt @@ -3,13 +3,10 @@ $ xpk job ls --project=golden-project --zone=us-central1-a --cluster=golden-clus [XPK] Working on golden-project and us-central1-a [XPK] Task: `Find cluster region or zone` is implemented by the following command not running since it is a dry run. gcloud container clusters list --project=golden-project --filter=name=golden-cluster --format="value(location)" -[XPK] Try 1: get-credentials-dns-endpoint to cluster golden-cluster [XPK] Task: `get-credentials-dns-endpoint to cluster golden-cluster` is implemented by the following command not running since it is a dry run. gcloud container clusters get-credentials golden-cluster --location=us-central1 --dns-endpoint --project=golden-project && kubectl config view && kubectl config set-context --current --namespace=default -[XPK] Testing credentials with kubectl... -[XPK] Task: `kubectl get pods` is implemented by the following command not running since it is a dry run. +[XPK] Task: `Test kubectl credentials` is implemented by the following command not running since it is a dry run. kubectl get pods -[XPK] Credentials test succeeded. [XPK] Finished get-credentials and kubectl setup. [XPK] Listing jobs for project golden-project and zone us-central1-a: [XPK] Task: `list jobs` is implemented by the following command not running since it is a dry run. diff --git a/goldens/NAP_cluster-create.txt b/goldens/NAP_cluster-create.txt index df6d80416..8bb28a4f3 100644 --- a/goldens/NAP_cluster-create.txt +++ b/goldens/NAP_cluster-create.txt @@ -16,13 +16,10 @@ gcloud container clusters list --project=golden-project --filter=name=golden-clu gcloud container clusters describe golden-cluster --project=golden-project --location=us-central1 --format="value(privateClusterConfig.enablePrivateNodes)" [XPK] Private Nodes is not enabled on the cluster. [XPK] Cluster is public and no need to authorize networks. -[XPK] Try 1: get-credentials-dns-endpoint to cluster golden-cluster [XPK] Task: `get-credentials-dns-endpoint to cluster golden-cluster` is implemented by the following command not running since it is a dry run. gcloud container clusters get-credentials golden-cluster --location=us-central1 --dns-endpoint --project=golden-project && kubectl config view && kubectl config set-context --current --namespace=default -[XPK] Testing credentials with kubectl... -[XPK] Task: `kubectl get pods` is implemented by the following command not running since it is a dry run. +[XPK] Task: `Test kubectl credentials` is implemented by the following command not running since it is a dry run. kubectl get pods -[XPK] Credentials test succeeded. [XPK] Finished get-credentials and kubectl setup. [XPK] Task: 'Checking CoreDNS deployment existence' in progress for namespace: kube-system [XPK] Task: `Check CoreDNS deployment in kube-system` is implemented by the following command not running since it is a dry run. diff --git a/goldens/NAP_cluster-create_with_pathways.txt b/goldens/NAP_cluster-create_with_pathways.txt index 2d7166f9b..ca028dc1a 100644 --- a/goldens/NAP_cluster-create_with_pathways.txt +++ b/goldens/NAP_cluster-create_with_pathways.txt @@ -16,13 +16,10 @@ gcloud container clusters list --project=golden-project --filter=name=golden-clu gcloud container clusters describe golden-cluster --project=golden-project --location=us-central1 --format="value(privateClusterConfig.enablePrivateNodes)" [XPK] Private Nodes is not enabled on the cluster. [XPK] Cluster is public and no need to authorize networks. -[XPK] Try 1: get-credentials-dns-endpoint to cluster golden-cluster [XPK] Task: `get-credentials-dns-endpoint to cluster golden-cluster` is implemented by the following command not running since it is a dry run. gcloud container clusters get-credentials golden-cluster --location=us-central1 --dns-endpoint --project=golden-project && kubectl config view && kubectl config set-context --current --namespace=default -[XPK] Testing credentials with kubectl... -[XPK] Task: `kubectl get pods` is implemented by the following command not running since it is a dry run. +[XPK] Task: `Test kubectl credentials` is implemented by the following command not running since it is a dry run. kubectl get pods -[XPK] Credentials test succeeded. [XPK] Finished get-credentials and kubectl setup. [XPK] Task: 'Checking CoreDNS deployment existence' in progress for namespace: kube-system [XPK] Task: `Check CoreDNS deployment in kube-system` is implemented by the following command not running since it is a dry run. diff --git a/goldens/Workload_delete.txt b/goldens/Workload_delete.txt index 0d0ff55a9..e9e70492f 100644 --- a/goldens/Workload_delete.txt +++ b/goldens/Workload_delete.txt @@ -4,13 +4,10 @@ $ xpk workload delete --project=golden-project --zone=us-central1-a --cluster=go [XPK] Working on golden-project and us-central1-a [XPK] Task: `Find cluster region or zone` is implemented by the following command not running since it is a dry run. gcloud container clusters list --project=golden-project --filter=name=golden-cluster --format="value(location)" -[XPK] Try 1: get-credentials-dns-endpoint to cluster golden-cluster [XPK] Task: `get-credentials-dns-endpoint to cluster golden-cluster` is implemented by the following command not running since it is a dry run. gcloud container clusters get-credentials golden-cluster --location=us-central1 --dns-endpoint --project=golden-project && kubectl config view && kubectl config set-context --current --namespace=default -[XPK] Testing credentials with kubectl... -[XPK] Task: `kubectl get pods` is implemented by the following command not running since it is a dry run. +[XPK] Task: `Test kubectl credentials` is implemented by the following command not running since it is a dry run. kubectl get pods -[XPK] Credentials test succeeded. [XPK] Finished get-credentials and kubectl setup. [XPK] Task: `Check if PathwaysJob is installed on golden-cluster` is implemented by the following command not running since it is a dry run. kubectl get pods -n pathways-job-system --no-headers -o custom-columns=NAME:.metadata.name diff --git a/goldens/Workload_list.txt b/goldens/Workload_list.txt index c96ae2f10..f3762652d 100644 --- a/goldens/Workload_list.txt +++ b/goldens/Workload_list.txt @@ -4,13 +4,10 @@ $ xpk workload list --project=golden-project --zone=us-central1-a --cluster=gold [XPK] Working on golden-project and us-central1-a [XPK] Task: `Find cluster region or zone` is implemented by the following command not running since it is a dry run. gcloud container clusters list --project=golden-project --filter=name=golden-cluster --format="value(location)" -[XPK] Try 1: get-credentials-dns-endpoint to cluster golden-cluster [XPK] Task: `get-credentials-dns-endpoint to cluster golden-cluster` is implemented by the following command not running since it is a dry run. gcloud container clusters get-credentials golden-cluster --location=us-central1 --dns-endpoint --project=golden-project && kubectl config view && kubectl config set-context --current --namespace=default -[XPK] Testing credentials with kubectl... -[XPK] Task: `kubectl get pods` is implemented by the following command not running since it is a dry run. +[XPK] Task: `Test kubectl credentials` is implemented by the following command not running since it is a dry run. kubectl get pods -[XPK] Credentials test succeeded. [XPK] Finished get-credentials and kubectl setup. [XPK] Task: `List Jobs with filter-by-status=EVERYTHING with filter-by-job=None` is implemented by the following command not running since it is a dry run. kubectl get workloads --ignore-not-found -o=custom-columns="Jobset Name:.metadata.ownerReferences[0].name,Created Time:.metadata.creationTimestamp,Priority:.spec.priorityClassName,TPU VMs Needed:.spec.podSets[0].count,TPU VMs Running/Ran:.status.admission.podSetAssignments[-1].count,TPU VMs Done:.status.reclaimablePods[0].count,Status:.status.conditions[-1].type,Status Message:.status.conditions[-1].message,Status Time:.status.conditions[-1].lastTransitionTime" diff --git a/src/xpk/core/cluster.py b/src/xpk/core/cluster.py index 55848adc3..5f520fc89 100644 --- a/src/xpk/core/cluster.py +++ b/src/xpk/core/cluster.py @@ -729,78 +729,58 @@ def update_cluster_with_gcsfuse_driver_if_necessary(args) -> int: return 0 -def test_and_retry_credentials_with_dns_logic(args) -> int: - """Tests kubectl credentials and retries with default settings if a DNS error is found. +def get_cluster_credentials(args) -> int: + """Run cluster configuration command to set the kubectl config. Args: args: user provided arguments for running the command. Returns: - 0 if credentials are valid after retrying, 1 otherwise. + 0 if successful and 1 otherwise. """ - - xpk_print('Testing credentials with kubectl...') - kubectl_command = 'kubectl get pods' - kubectl_return_code, kubectl_output = run_command_for_value( - kubectl_command, 'kubectl get pods' - ) - if kubectl_return_code == 0: - xpk_print('Credentials test succeeded.') - return 0 - - dns_endpoint_error = ( - 'control_plane_endpoints_config.dns_endpoint_config.allow_external_traffic' - ' is disabled' - ) - if dns_endpoint_error not in kubectl_output: - xpk_print(f'kubectl failed with an unhandled error: {kubectl_output}') - xpk_exit(kubectl_return_code) - xpk_print( - 'Detected DNS endpoint-related error. Retrying without --dns-endpoint' - ' flag...' - ) - location = get_cluster_location(args.project, args.cluster, args.zone) - without_dns_command = ( - 'gcloud container clusters get-credentials' - f' {args.cluster} --location={location}' - f' --project={args.project} &&' - ' kubectl config view && kubectl config set-context --current' - ' --namespace=default' - ) - return_code = run_command_with_updates( - without_dns_command, 'get-credentials to cluster', verbose=False + + return_code = _get_credentials( + project=args.project, + cluster=args.cluster, + location=location, + dns_endpoint=True, ) if return_code != 0: - xpk_print('Failed to get credentials even without --dns-endpoint. Exiting.') - xpk_exit(return_code) - return 0 - + return return_code + + if not _are_credentials_valid(): + xpk_print('Detected error. Retrying without --dns-endpoint flag...') + return_code = _get_credentials( + project=args.project, + cluster=args.cluster, + location=location, + dns_endpoint=False, + ) + if return_code != 0: + return return_code -def get_cluster_credentials(args) -> int: - """Run cluster configuration command to set the kubectl config. + xpk_print('Finished get-credentials and kubectl setup.') + return 0 - Args: - args: user provided arguments for running the command. - Returns: - 0 if successful and 1 otherwise. - """ - location = get_cluster_location(args.project, args.cluster, args.zone) +def _get_credentials( + project: str, cluster: str, location: str, dns_endpoint: bool +) -> int: + dns_endpoint_arg = '--dns-endpoint' if dns_endpoint else '' command = ( 'gcloud container clusters get-credentials' - f' {args.cluster} --location={location} --dns-endpoint' - f' --project={args.project} && kubectl config view && kubectl config' + f' {cluster} --location={location} {dns_endpoint_arg}' + f' --project={project} && kubectl config view && kubectl config' ' set-context --current --namespace=default' ) - task = f'get-credentials-dns-endpoint to cluster {args.cluster}' - return_code = run_command_with_updates_retry(command, task, verbose=False) + task = f'get-credentials-dns-endpoint to cluster {cluster}' + return run_command_with_updates(command, task, verbose=False) - if return_code != 0: - xpk_print(f'{task} returned ERROR {return_code}') - xpk_exit(return_code) - return_code = test_and_retry_credentials_with_dns_logic(args) - xpk_print('Finished get-credentials and kubectl setup.') - - return return_code +def _are_credentials_valid() -> bool: + kubectl_command = 'kubectl get pods' + kubectl_return_code = run_command_with_updates( + kubectl_command, 'Test kubectl credentials' + ) + return kubectl_return_code == 0 diff --git a/src/xpk/core/cluster_test.py b/src/xpk/core/cluster_test.py new file mode 100644 index 000000000..2d87ba9bd --- /dev/null +++ b/src/xpk/core/cluster_test.py @@ -0,0 +1,95 @@ +""" +Copyright 2025 Google LLC + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + https://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +""" + +import pytest +from .testing.commands_tester import CommandsTester +from .cluster import get_cluster_credentials +from pytest_mock import MockerFixture + + +@pytest.fixture(autouse=True) +def commands_tester(mocker: MockerFixture) -> CommandsTester: + return CommandsTester( + mocker=mocker, + run_command_for_value_path="xpk.core.cluster.run_command_for_value", + run_command_with_updates_path="xpk.core.cluster.run_command_with_updates", + ) + + +@pytest.fixture(autouse=True) +def mock_location(mocker: MockerFixture): + mocker.patch( + "xpk.core.cluster.get_cluster_location", return_value="us-central1" + ) + + +@pytest.fixture(autouse=True) +def command_args(mocker: MockerFixture): + return mocker.Mock(cluster="cluster", project="project", zone="zone") + + +def test_get_cluster_credentials_returns_1_when_retrieval_command_fails( + commands_tester: CommandsTester, command_args +): + commands_tester.set_result_for_command( + (1, ""), "gcloud container clusters get-credentials" + ) + assert get_cluster_credentials(command_args) == 1 + + +def test_get_cluster_credentials_returns_0_when_retrieval_succeeds( + commands_tester: CommandsTester, command_args +): + commands_tester.set_result_for_command( + (0, ""), "gcloud container clusters get-credentials" + ) + assert get_cluster_credentials(command_args) == 0 + + +def test_get_cluster_credentials_does_not_retry_with_dns_when_retrieval_succeeds( + commands_tester: CommandsTester, command_args +): + commands_tester.set_result_for_command( + (0, ""), "gcloud container clusters get-credentials --dns-endpoint" + ) + commands_tester.set_result_for_command((0, ""), "kubectl get pods") + get_cluster_credentials(command_args) + non_dns_endpoint_commands = [ + c + for c in commands_tester.get_matching_commands( + "gcloud container clusters get-credentials" + ) + if "dns-endpoint" not in c + ] + assert len(non_dns_endpoint_commands) == 0 + + +def test_get_cluster_credentials_retries_without_dns_when_dns_retrieval_fails( + commands_tester: CommandsTester, command_args +): + commands_tester.set_result_for_command( + (0, ""), "gcloud container clusters get-credentials --dns-endpoint" + ) + commands_tester.set_result_for_command((1, ""), "kubectl get pods") + get_cluster_credentials(command_args) + non_dns_endpoint_commands = [ + c + for c in commands_tester.get_matching_commands( + "gcloud container clusters get-credentials" + ) + if "dns-endpoint" not in c + ] + assert len(non_dns_endpoint_commands) == 1