File tree Expand file tree Collapse file tree 6 files changed +53
-32
lines changed Expand file tree Collapse file tree 6 files changed +53
-32
lines changed Original file line number Diff line number Diff line change 1818from kubernetes import client
1919from kubernetes .client .exceptions import ApiException
2020
21+ from ...common .utils import get_current_namespace
22+
2123
2224def 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 ()
Original file line number Diff line number Diff line change 1+ """
2+ Common utilities for the CodeFlare SDK.
3+ """
4+
5+ from .k8s_utils import get_current_namespace
6+
7+ __all__ = ["get_current_namespace" ]
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff 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 (
Original file line number Diff line number Diff line change 2626import ipywidgets as widgets
2727from IPython .display import display , HTML , Javascript
2828import pandas as pd
29+
30+ from ...common .utils import get_current_namespace
2931from ...ray .cluster .config import ClusterConfiguration
3032from ...ray .cluster .status import RayClusterStatus
3133from ..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 ()
Original file line number Diff line number Diff line change 2727import uuid
2828import warnings
2929
30+ from ...common .utils import get_current_namespace
31+
3032from ...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-
667643def get_cluster (
668644 cluster_name : str ,
669645 namespace : str = "default" ,
You can’t perform that action at this time.
0 commit comments