Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move current namespace logic to config #675

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/codeflare_sdk/cluster/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
36 changes: 0 additions & 36 deletions src/codeflare_sdk/cluster/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=<your_current_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
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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():
Expand Down
9 changes: 9 additions & 0 deletions src/codeflare_sdk/cluster/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions tests/unit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
Loading