From 9ddc536f9252118d4d0cbbec24284dc1a983871d Mon Sep 17 00:00:00 2001 From: Mustafa Eyceoz Date: Fri, 13 Oct 2023 15:51:48 -0400 Subject: [PATCH] Review feedback applied, auto-select --- src/codeflare_sdk/cluster/cluster.py | 22 ++++++++++++++++++++- src/codeflare_sdk/utils/kube_api_helpers.py | 10 +++++++--- tests/test-case-no-mcad.yamls | 6 +++--- 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/src/codeflare_sdk/cluster/cluster.py b/src/codeflare_sdk/cluster/cluster.py index 8646d0b65..e13c635fd 100644 --- a/src/codeflare_sdk/cluster/cluster.py +++ b/src/codeflare_sdk/cluster/cluster.py @@ -611,7 +611,7 @@ def get_current_namespace(): # pragma: no cover return None -def get_cluster(cluster_name: str, namespace: str = "default", mcad=True): +def get_cluster(cluster_name: str, namespace: str = "default"): try: config_check() api_instance = client.CustomObjectsApi(api_config_handler()) @@ -626,6 +626,7 @@ def get_cluster(cluster_name: str, namespace: str = "default", mcad=True): for rc in rcs["items"]: if rc["metadata"]["name"] == cluster_name: + mcad = _check_aw_exists(cluster_name, namespace) return Cluster.from_k8_cluster_object(rc, mcad=mcad) raise FileNotFoundError( f"Cluster {cluster_name} is not found in {namespace} namespace" @@ -633,6 +634,25 @@ def get_cluster(cluster_name: str, namespace: str = "default", mcad=True): # private methods +def _check_aw_exists(name: str, namespace: str) -> bool: + try: + config_check() + api_instance = client.CustomObjectsApi(api_config_handler()) + aws = api_instance.list_namespaced_custom_object( + group="workload.codeflare.dev", + version="v1beta1", + namespace=namespace, + plural="appwrappers", + ) + except Exception as e: # pragma: no cover + return _kube_api_error_handling(e, print_error=False) + + for aw in aws["items"]: + if aw["metadata"]["name"] == name: + return True + return False + + def _get_ingress_domain(): try: config_check() diff --git a/src/codeflare_sdk/utils/kube_api_helpers.py b/src/codeflare_sdk/utils/kube_api_helpers.py index 8f8180b97..01a93ef5c 100644 --- a/src/codeflare_sdk/utils/kube_api_helpers.py +++ b/src/codeflare_sdk/utils/kube_api_helpers.py @@ -23,7 +23,9 @@ # private methods -def _kube_api_error_handling(e: Exception): # pragma: no cover +def _kube_api_error_handling( + e: Exception, print_error: bool = True +): # pragma: no cover perm_msg = ( "Action not permitted, have you put in correct/up-to-date auth credentials?" ) @@ -32,11 +34,13 @@ def _kube_api_error_handling(e: Exception): # pragma: no cover if type(e) == config.ConfigException: raise PermissionError(perm_msg) if type(e) == executing.executing.NotOneValueFound: - print(nf_msg) + if print_error: + print(nf_msg) return if type(e) == client.ApiException: if e.reason == "Not Found": - print(nf_msg) + if print_error: + print(nf_msg) return elif e.reason == "Unauthorized" or e.reason == "Forbidden": raise PermissionError(perm_msg) diff --git a/tests/test-case-no-mcad.yamls b/tests/test-case-no-mcad.yamls index 87c1c5c4a..6d905566d 100644 --- a/tests/test-case-no-mcad.yamls +++ b/tests/test-case-no-mcad.yamls @@ -51,7 +51,7 @@ spec: value: /home/ray/workspace/tls/server.key - name: RAY_TLS_CA_CERT value: /home/ray/workspace/tls/ca.crt - image: quay.io/project-codeflare/ray:2.5.0-py38-cu116 + image: quay.io/project-codeflare/ray:latest-py39-cu118 imagePullPolicy: Always lifecycle: preStop: @@ -79,7 +79,7 @@ spec: nvidia.com/gpu: 0 imagePullSecrets: - name: unit-test-pull-secret - rayVersion: 2.5.0 + rayVersion: 2.7.0 workerGroupSpecs: - groupName: small-group-unit-test-cluster-ray maxReplicas: 2 @@ -118,7 +118,7 @@ spec: value: /home/ray/workspace/tls/server.key - name: RAY_TLS_CA_CERT value: /home/ray/workspace/tls/ca.crt - image: quay.io/project-codeflare/ray:2.5.0-py38-cu116 + image: quay.io/project-codeflare/ray:latest-py39-cu118 lifecycle: preStop: exec: