From 5ff9723202e0ababd499de2828f0ad221866bdaa Mon Sep 17 00:00:00 2001 From: Kevin Date: Wed, 18 Sep 2024 13:33:11 -0400 Subject: [PATCH] move current namespace logic to config Signed-off-by: Kevin --- src/codeflare_sdk/cluster/auth.py | 22 +++++++++++++++++ src/codeflare_sdk/cluster/cluster.py | 36 ---------------------------- src/codeflare_sdk/cluster/config.py | 9 +++++++ tests/unit_test.py | 4 ++-- 4 files changed, 33 insertions(+), 38 deletions(-) diff --git a/src/codeflare_sdk/cluster/auth.py b/src/codeflare_sdk/cluster/auth.py index c39fe1d4a..b5be9b9fd 100644 --- a/src/codeflare_sdk/cluster/auth.py +++ b/src/codeflare_sdk/cluster/auth.py @@ -219,3 +219,25 @@ def api_config_handler() -> Optional[client.ApiClient]: return api_client else: return None + + +def get_current_namespace(): # pragma: no cover + config_check() + if os.path.isfile("/var/run/secrets/kubernetes.io/serviceaccount/namespace"): + try: + file = open("/var/run/secrets/kubernetes.io/serviceaccount/namespace", "r") + active_context = file.readline().strip("\n") + return active_context + except Exception as e: + print("Unable to find current namespace") + if api_config_handler() is not None: + return None + print("trying to gather from current context") + try: + _, active_context = config.list_kube_config_contexts(config_check()) + except Exception as e: + return _kube_api_error_handling(e) + try: + return active_context["context"]["namespace"] + except KeyError: + return None diff --git a/src/codeflare_sdk/cluster/cluster.py b/src/codeflare_sdk/cluster/cluster.py index 20aa94c90..334e25a70 100644 --- a/src/codeflare_sdk/cluster/cluster.py +++ b/src/codeflare_sdk/cluster/cluster.py @@ -110,15 +110,6 @@ def create_app_wrapper(self): the specifications of the ClusterConfiguration. """ - if self.config.namespace is None: - self.config.namespace = get_current_namespace() - if self.config.namespace is None: - print("Please specify with namespace=") - elif type(self.config.namespace) is not str: - raise TypeError( - f"Namespace {self.config.namespace} is of type {type(self.config.namespace)}. Check your Kubernetes Authentication." - ) - return generate_appwrapper(self) # creates a new cluster with the provided or default spec @@ -545,28 +536,6 @@ def list_all_queued( return resources -def get_current_namespace(): # pragma: no cover - if os.path.isfile("/var/run/secrets/kubernetes.io/serviceaccount/namespace"): - try: - file = open("/var/run/secrets/kubernetes.io/serviceaccount/namespace", "r") - active_context = file.readline().strip("\n") - return active_context - except Exception as e: - print("Unable to find current namespace") - - if api_config_handler() != None: - return None - print("trying to gather from current context") - try: - _, active_context = config.list_kube_config_contexts(config_check()) - except Exception as e: - return _kube_api_error_handling(e) - try: - return active_context["context"]["namespace"] - except KeyError: - return None - - def get_cluster( cluster_name: str, namespace: str = "default", @@ -645,14 +614,9 @@ def _check_aw_exists(name: str, namespace: str) -> bool: return False -# Cant test this until get_current_namespace is fixed and placed in this function over using `self` def _get_ingress_domain(self): # pragma: no cover config_check() - if self.config.namespace != None: - namespace = self.config.namespace - else: - namespace = get_current_namespace() domain = None if is_openshift_cluster(): diff --git a/src/codeflare_sdk/cluster/config.py b/src/codeflare_sdk/cluster/config.py index 610d53c44..394abd36d 100644 --- a/src/codeflare_sdk/cluster/config.py +++ b/src/codeflare_sdk/cluster/config.py @@ -23,6 +23,8 @@ from dataclasses import dataclass, field, fields from typing import Dict, List, Optional, Union, get_args, get_origin +from .auth import get_current_namespace + dir = pathlib.Path(__file__).parent.parent.resolve() # https://docs.ray.io/en/latest/ray-core/scheduling/accelerators.html @@ -122,6 +124,13 @@ def __post_init__(self): self._validate_extended_resource_requests( self.worker_extended_resource_requests ) + if self.namespace is None: + self.namespace = get_current_namespace() + print(f"Namespace not provided, using current namespace: {self.namespace}") + if self.namespace is None: + raise ValueError( + "Namespace not provided and unable to determine current namespace" + ) def _combine_extended_resource_mapping(self): if overwritten := set(self.extended_resource_mapping.keys()).intersection( diff --git a/tests/unit_test.py b/tests/unit_test.py index 8a51c6eb9..99af209d2 100644 --- a/tests/unit_test.py +++ b/tests/unit_test.py @@ -433,7 +433,7 @@ def test_cluster_creation_no_mcad_local_queue(mocker): def test_default_cluster_creation(mocker): mocker.patch("kubernetes.client.ApisApi.get_api_versions") mocker.patch( - "codeflare_sdk.cluster.cluster.get_current_namespace", + "codeflare_sdk.cluster.auth.get_current_namespace", return_value="opendatahub", ) mocker.patch( @@ -2664,7 +2664,7 @@ def test_export_env(): def test_cluster_throw_for_no_raycluster(mocker: MockerFixture): mocker.patch("kubernetes.client.ApisApi.get_api_versions") mocker.patch( - "codeflare_sdk.cluster.cluster.get_current_namespace", + "codeflare_sdk.cluster.auth.get_current_namespace", return_value="opendatahub", ) mocker.patch(