Skip to content

Commit dc4acc7

Browse files
committed
Added logic for handling current namespace when a user authenticates via kube client
1 parent d85cee9 commit dc4acc7

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

Diff for: src/codeflare_sdk/cluster/auth.py

+1
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ def load_kube_config(self):
158158
return "Please specify a config file path"
159159
config_path = self.kube_config_path
160160
api_client = None
161+
config.load_kube_config(config_path)
161162
response = "Loaded user config file at path %s" % self.kube_config_path
162163
except config.ConfigException:
163164
config_path = None

Diff for: src/codeflare_sdk/cluster/cluster.py

+24-12
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
)
3838
from kubernetes import client, config
3939
import yaml
40-
40+
import os
4141

4242
class Cluster:
4343
"""
@@ -382,17 +382,29 @@ def list_all_queued(namespace: str, print_to_console: bool = True):
382382

383383

384384
def get_current_namespace(): # pragma: no cover
385-
try:
386-
# KubeConfigFileAuthentication.config_check()
387-
_, active_context = config.list_kube_config_contexts(
388-
KubeConfigFileAuthentication.config_check()
389-
)
390-
except Exception as e:
391-
return _kube_api_error_handling(e)
392-
try:
393-
return active_context["context"]["namespace"]
394-
except KeyError:
395-
return "default"
385+
namespace_error = "Unable to find current namespace please specify with namespace=<your_current_namespace>"
386+
if TokenAuthentication.api_config_handler() != None:
387+
if os.path.isfile("/var/run/secrets/kubernetes.io/serviceaccount/namespace"):
388+
try:
389+
file = open("/var/run/secrets/kubernetes.io/serviceaccount/namespace", "r")
390+
active_context = file.readline().strip('\n')
391+
return active_context
392+
except Exception as e:
393+
return namespace_error
394+
else:
395+
return namespace_error
396+
else:
397+
try:
398+
# KubeConfigFileAuthentication.config_check()
399+
_, active_context = config.list_kube_config_contexts(
400+
KubeConfigFileAuthentication.config_check()
401+
)
402+
except Exception as e:
403+
return _kube_api_error_handling(e)
404+
try:
405+
return active_context["context"]["namespace"]
406+
except KeyError:
407+
return namespace_error
396408

397409

398410
def get_cluster(cluster_name: str, namespace: str = "default"):

0 commit comments

Comments
 (0)