Skip to content

Commit 5ff9723

Browse files
committed
move current namespace logic to config
Signed-off-by: Kevin <[email protected]>
1 parent e1c1b97 commit 5ff9723

File tree

4 files changed

+33
-38
lines changed

4 files changed

+33
-38
lines changed

src/codeflare_sdk/cluster/auth.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,3 +219,25 @@ def api_config_handler() -> Optional[client.ApiClient]:
219219
return api_client
220220
else:
221221
return None
222+
223+
224+
def get_current_namespace(): # pragma: no cover
225+
config_check()
226+
if os.path.isfile("/var/run/secrets/kubernetes.io/serviceaccount/namespace"):
227+
try:
228+
file = open("/var/run/secrets/kubernetes.io/serviceaccount/namespace", "r")
229+
active_context = file.readline().strip("\n")
230+
return active_context
231+
except Exception as e:
232+
print("Unable to find current namespace")
233+
if api_config_handler() is not None:
234+
return None
235+
print("trying to gather from current context")
236+
try:
237+
_, active_context = config.list_kube_config_contexts(config_check())
238+
except Exception as e:
239+
return _kube_api_error_handling(e)
240+
try:
241+
return active_context["context"]["namespace"]
242+
except KeyError:
243+
return None

src/codeflare_sdk/cluster/cluster.py

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,6 @@ def create_app_wrapper(self):
110110
the specifications of the ClusterConfiguration.
111111
"""
112112

113-
if self.config.namespace is None:
114-
self.config.namespace = get_current_namespace()
115-
if self.config.namespace is None:
116-
print("Please specify with namespace=<your_current_namespace>")
117-
elif type(self.config.namespace) is not str:
118-
raise TypeError(
119-
f"Namespace {self.config.namespace} is of type {type(self.config.namespace)}. Check your Kubernetes Authentication."
120-
)
121-
122113
return generate_appwrapper(self)
123114

124115
# creates a new cluster with the provided or default spec
@@ -545,28 +536,6 @@ def list_all_queued(
545536
return resources
546537

547538

548-
def get_current_namespace(): # pragma: no cover
549-
if os.path.isfile("/var/run/secrets/kubernetes.io/serviceaccount/namespace"):
550-
try:
551-
file = open("/var/run/secrets/kubernetes.io/serviceaccount/namespace", "r")
552-
active_context = file.readline().strip("\n")
553-
return active_context
554-
except Exception as e:
555-
print("Unable to find current namespace")
556-
557-
if api_config_handler() != None:
558-
return None
559-
print("trying to gather from current context")
560-
try:
561-
_, active_context = config.list_kube_config_contexts(config_check())
562-
except Exception as e:
563-
return _kube_api_error_handling(e)
564-
try:
565-
return active_context["context"]["namespace"]
566-
except KeyError:
567-
return None
568-
569-
570539
def get_cluster(
571540
cluster_name: str,
572541
namespace: str = "default",
@@ -645,14 +614,9 @@ def _check_aw_exists(name: str, namespace: str) -> bool:
645614
return False
646615

647616

648-
# Cant test this until get_current_namespace is fixed and placed in this function over using `self`
649617
def _get_ingress_domain(self): # pragma: no cover
650618
config_check()
651619

652-
if self.config.namespace != None:
653-
namespace = self.config.namespace
654-
else:
655-
namespace = get_current_namespace()
656620
domain = None
657621

658622
if is_openshift_cluster():

src/codeflare_sdk/cluster/config.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
from dataclasses import dataclass, field, fields
2424
from typing import Dict, List, Optional, Union, get_args, get_origin
2525

26+
from .auth import get_current_namespace
27+
2628
dir = pathlib.Path(__file__).parent.parent.resolve()
2729

2830
# https://docs.ray.io/en/latest/ray-core/scheduling/accelerators.html
@@ -122,6 +124,13 @@ def __post_init__(self):
122124
self._validate_extended_resource_requests(
123125
self.worker_extended_resource_requests
124126
)
127+
if self.namespace is None:
128+
self.namespace = get_current_namespace()
129+
print(f"Namespace not provided, using current namespace: {self.namespace}")
130+
if self.namespace is None:
131+
raise ValueError(
132+
"Namespace not provided and unable to determine current namespace"
133+
)
125134

126135
def _combine_extended_resource_mapping(self):
127136
if overwritten := set(self.extended_resource_mapping.keys()).intersection(

tests/unit_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ def test_cluster_creation_no_mcad_local_queue(mocker):
433433
def test_default_cluster_creation(mocker):
434434
mocker.patch("kubernetes.client.ApisApi.get_api_versions")
435435
mocker.patch(
436-
"codeflare_sdk.cluster.cluster.get_current_namespace",
436+
"codeflare_sdk.cluster.auth.get_current_namespace",
437437
return_value="opendatahub",
438438
)
439439
mocker.patch(
@@ -2664,7 +2664,7 @@ def test_export_env():
26642664
def test_cluster_throw_for_no_raycluster(mocker: MockerFixture):
26652665
mocker.patch("kubernetes.client.ApisApi.get_api_versions")
26662666
mocker.patch(
2667-
"codeflare_sdk.cluster.cluster.get_current_namespace",
2667+
"codeflare_sdk.cluster.auth.get_current_namespace",
26682668
return_value="opendatahub",
26692669
)
26702670
mocker.patch(

0 commit comments

Comments
 (0)