Skip to content

Commit f69c792

Browse files
committed
fix: move get_current_namespace to utils dir
1 parent 8acc56e commit f69c792

File tree

6 files changed

+53
-32
lines changed

6 files changed

+53
-32
lines changed

src/codeflare_sdk/common/kueue/kueue.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
from kubernetes import client
1919
from kubernetes.client.exceptions import ApiException
2020

21+
from ...common.utils import get_current_namespace
22+
2123

2224
def get_default_kueue_name(namespace: str) -> Optional[str]:
2325
"""
@@ -81,7 +83,6 @@ def list_local_queues(
8183
List[dict]:
8284
A list of dictionaries containing the name of the local queue and the available flavors
8385
"""
84-
from ...ray.cluster.cluster import get_current_namespace
8586

8687
if namespace is None: # pragma: no cover
8788
namespace = get_current_namespace()
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"""
2+
Common utilities for the CodeFlare SDK.
3+
"""
4+
5+
from .k8s_utils import get_current_namespace
6+
7+
__all__ = ["get_current_namespace"]
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""
2+
Kubernetes utility functions for the CodeFlare SDK.
3+
"""
4+
5+
import os
6+
from kubernetes import config
7+
from ..kubernetes_cluster import config_check, _kube_api_error_handling
8+
9+
10+
def get_current_namespace():
11+
"""
12+
Retrieves the current Kubernetes namespace.
13+
14+
This function attempts to detect the current namespace by:
15+
1. First checking if running inside a pod (reading from service account namespace file)
16+
2. Falling back to reading from the current kubeconfig context
17+
18+
Returns:
19+
str:
20+
The current namespace or None if not found.
21+
"""
22+
if os.path.isfile("/var/run/secrets/kubernetes.io/serviceaccount/namespace"):
23+
try:
24+
file = open("/var/run/secrets/kubernetes.io/serviceaccount/namespace", "r")
25+
active_context = file.readline().strip("\n")
26+
return active_context
27+
except Exception as e:
28+
print("Unable to find current namespace")
29+
print("trying to gather from current context")
30+
try:
31+
_, active_context = config.list_kube_config_contexts(config_check())
32+
except Exception as e:
33+
return _kube_api_error_handling(e)
34+
try:
35+
return active_context["context"]["namespace"]
36+
except KeyError:
37+
return None

src/codeflare_sdk/common/widgets/test_widgets.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def test_view_clusters(mocker, capsys):
106106
# Prepare to run view_clusters when notebook environment is detected
107107
mocker.patch("codeflare_sdk.common.widgets.widgets.is_notebook", return_value=True)
108108
mock_get_current_namespace = mocker.patch(
109-
"codeflare_sdk.ray.cluster.cluster.get_current_namespace",
109+
"codeflare_sdk.common.utils.get_current_namespace",
110110
return_value="default",
111111
)
112112
namespace = mock_get_current_namespace.return_value
@@ -250,7 +250,7 @@ def test_ray_cluster_manager_widgets_init(mocker, capsys):
250250
return_value=test_ray_clusters_df,
251251
)
252252
mocker.patch(
253-
"codeflare_sdk.ray.cluster.cluster.get_current_namespace",
253+
"codeflare_sdk.common.utils.get_current_namespace",
254254
return_value=namespace,
255255
)
256256
mock_delete_cluster = mocker.patch(

src/codeflare_sdk/common/widgets/widgets.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import ipywidgets as widgets
2727
from IPython.display import display, HTML, Javascript
2828
import pandas as pd
29+
30+
from ...common.utils import get_current_namespace
2931
from ...ray.cluster.config import ClusterConfiguration
3032
from ...ray.cluster.status import RayClusterStatus
3133
from ..kubernetes_cluster import _kube_api_error_handling
@@ -43,8 +45,6 @@ class RayClusterManagerWidgets:
4345
"""
4446

4547
def __init__(self, ray_clusters_df: pd.DataFrame, namespace: str = None):
46-
from ...ray.cluster.cluster import get_current_namespace
47-
4848
# Data
4949
self.ray_clusters_df = ray_clusters_df
5050
self.namespace = get_current_namespace() if not namespace else namespace
@@ -353,7 +353,7 @@ def view_clusters(namespace: str = None):
353353
)
354354
return # Exit function if not in Jupyter Notebook
355355

356-
from ...ray.cluster.cluster import get_current_namespace
356+
from ...common.utils import get_current_namespace
357357

358358
if not namespace:
359359
namespace = get_current_namespace()

src/codeflare_sdk/ray/cluster/cluster.py

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import uuid
2828
import warnings
2929

30+
from ...common.utils import get_current_namespace
31+
3032
from ...common.kubernetes_cluster.auth import (
3133
config_check,
3234
get_api_client,
@@ -638,32 +640,6 @@ def list_all_queued(
638640
return resources
639641

640642

641-
def get_current_namespace(): # pragma: no cover
642-
"""
643-
Retrieves the current Kubernetes namespace.
644-
645-
Returns:
646-
str:
647-
The current namespace or None if not found.
648-
"""
649-
if os.path.isfile("/var/run/secrets/kubernetes.io/serviceaccount/namespace"):
650-
try:
651-
file = open("/var/run/secrets/kubernetes.io/serviceaccount/namespace", "r")
652-
active_context = file.readline().strip("\n")
653-
return active_context
654-
except Exception as e:
655-
print("Unable to find current namespace")
656-
print("trying to gather from current context")
657-
try:
658-
_, active_context = config.list_kube_config_contexts(config_check())
659-
except Exception as e:
660-
return _kube_api_error_handling(e)
661-
try:
662-
return active_context["context"]["namespace"]
663-
except KeyError:
664-
return None
665-
666-
667643
def get_cluster(
668644
cluster_name: str,
669645
namespace: str = "default",

0 commit comments

Comments
 (0)