Skip to content

Commit 3ba4b93

Browse files
committed
Made handler functions generic and altered get_current_namespace() functionality
1 parent 8048948 commit 3ba4b93

File tree

4 files changed

+59
-71
lines changed

4 files changed

+59
-71
lines changed

src/codeflare_sdk/cluster/auth.py

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,6 @@ def load_kube_config(self):
5959
"""
6060
pass
6161

62-
def config_check(self):
63-
"""
64-
Method for checking if a user is authenticated via token and server or with their own config file
65-
"""
66-
pass
67-
6862
def logout(self):
6963
"""
7064
Method for logging out of the remote cluster
@@ -118,15 +112,6 @@ def login(self) -> str:
118112
except client.ApiException as exception:
119113
return exception
120114

121-
def api_config_handler() -> str:
122-
"""
123-
This function is used to load the api client if the user has logged in
124-
"""
125-
if api_client != None and config_path == None:
126-
return api_client
127-
else:
128-
return None
129-
130115
def logout(self) -> str:
131116
"""
132117
This function is used to logout of a Kubernetes cluster.
@@ -165,14 +150,25 @@ def load_kube_config(self):
165150
raise Exception("Please specify a config file path")
166151
return response
167152

168-
def config_check() -> str:
169-
"""
170-
Function for loading the config file at the default config location ~/.kube/config if the user has not
171-
specified their own config file or has logged in with their token and server.
172-
"""
173-
global config_path
174-
global api_client
175-
if config_path == None and api_client == None:
176-
config.load_kube_config()
177-
if config_path != None and api_client == None:
178-
return config_path
153+
154+
def config_check() -> str:
155+
"""
156+
Function for loading the config file at the default config location ~/.kube/config if the user has not
157+
specified their own config file or has logged in with their token and server.
158+
"""
159+
global config_path
160+
global api_client
161+
if config_path == None and api_client == None:
162+
config.load_kube_config()
163+
if config_path != None and api_client == None:
164+
return config_path
165+
166+
167+
def api_config_handler() -> str:
168+
"""
169+
This function is used to load the api client if the user has logged in
170+
"""
171+
if api_client != None and config_path == None:
172+
return api_client
173+
else:
174+
return None

src/codeflare_sdk/cluster/awload.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
from kubernetes import client, config
2626
from ..utils.kube_api_helpers import _kube_api_error_handling
27-
from .auth import KubeConfigFileAuthentication, TokenAuthentication
27+
from .auth import config_check, api_config_handler
2828

2929

3030
class AWManager:
@@ -58,10 +58,8 @@ def submit(self) -> None:
5858
Attempts to create the AppWrapper custom resource using the yaml file
5959
"""
6060
try:
61-
KubeConfigFileAuthentication.config_check()
62-
api_instance = client.CustomObjectsApi(
63-
TokenAuthentication.api_config_handler()
64-
)
61+
config_check()
62+
api_instance = client.CustomObjectsApi(api_config_handler())
6563
api_instance.create_namespaced_custom_object(
6664
group="mcad.ibm.com",
6765
version="v1beta1",
@@ -85,10 +83,8 @@ def remove(self) -> None:
8583
return
8684

8785
try:
88-
KubeConfigFileAuthentication.config_check()
89-
api_instance = client.CustomObjectsApi(
90-
TokenAuthentication.api_config_handler()
91-
)
86+
config_check()
87+
api_instance = client.CustomObjectsApi(api_config_handler())
9288
api_instance.delete_namespaced_custom_object(
9389
group="mcad.ibm.com",
9490
version="v1beta1",

src/codeflare_sdk/cluster/cluster.py

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
from ray.job_submission import JobSubmissionClient
2525

26-
from .auth import KubeConfigFileAuthentication, TokenAuthentication
26+
from .auth import config_check, api_config_handler
2727
from ..utils import pretty_print
2828
from ..utils.generate_yaml import generate_appwrapper
2929
from ..utils.kube_api_helpers import _kube_api_error_handling
@@ -69,7 +69,11 @@ def create_app_wrapper(self):
6969

7070
if self.config.namespace is None:
7171
self.config.namespace = get_current_namespace()
72-
if type(self.config.namespace) is not str:
72+
if self.config.namespace is None:
73+
print(
74+
"Unable to find current namespace please specify with namespace=<your_current_namespace>"
75+
)
76+
elif type(self.config.namespace) is not str:
7377
raise TypeError(
7478
f"Namespace {self.config.namespace} is of type {type(self.config.namespace)}. Check your Kubernetes Authentication."
7579
)
@@ -115,10 +119,8 @@ def up(self):
115119
"""
116120
namespace = self.config.namespace
117121
try:
118-
KubeConfigFileAuthentication.config_check()
119-
api_instance = client.CustomObjectsApi(
120-
TokenAuthentication.api_config_handler()
121-
)
122+
config_check()
123+
api_instance = client.CustomObjectsApi(api_config_handler())
122124
with open(self.app_wrapper_yaml) as f:
123125
aw = yaml.load(f, Loader=yaml.FullLoader)
124126
api_instance.create_namespaced_custom_object(
@@ -138,10 +140,8 @@ def down(self):
138140
"""
139141
namespace = self.config.namespace
140142
try:
141-
KubeConfigFileAuthentication.config_check()
142-
api_instance = client.CustomObjectsApi(
143-
TokenAuthentication.api_config_handler()
144-
)
143+
config_check()
144+
api_instance = client.CustomObjectsApi(api_config_handler())
145145
api_instance.delete_namespaced_custom_object(
146146
group="mcad.ibm.com",
147147
version="v1beta1",
@@ -252,10 +252,8 @@ def cluster_dashboard_uri(self) -> str:
252252
Returns a string containing the cluster's dashboard URI.
253253
"""
254254
try:
255-
KubeConfigFileAuthentication.config_check()
256-
api_instance = client.CustomObjectsApi(
257-
TokenAuthentication.api_config_handler()
258-
)
255+
config_check()
256+
api_instance = client.CustomObjectsApi(api_config_handler())
259257
routes = api_instance.list_namespaced_custom_object(
260258
group="route.openshift.io",
261259
version="v1",
@@ -383,8 +381,7 @@ def list_all_queued(namespace: str, print_to_console: bool = True):
383381

384382

385383
def get_current_namespace(): # pragma: no cover
386-
namespace_error = "Unable to find current namespace please specify with namespace=<your_current_namespace>"
387-
if TokenAuthentication.api_config_handler() != None:
384+
if api_config_handler() != None:
388385
if os.path.isfile("/var/run/secrets/kubernetes.io/serviceaccount/namespace"):
389386
try:
390387
file = open(
@@ -393,21 +390,20 @@ def get_current_namespace(): # pragma: no cover
393390
active_context = file.readline().strip("\n")
394391
return active_context
395392
except Exception as e:
396-
return namespace_error
393+
print("Unable to find current namespace")
394+
return None
397395
else:
398-
return namespace_error
396+
print("Unable to find current namespace")
397+
return None
399398
else:
400399
try:
401-
# KubeConfigFileAuthentication.config_check()
402-
_, active_context = config.list_kube_config_contexts(
403-
KubeConfigFileAuthentication.config_check()
404-
)
400+
_, active_context = config.list_kube_config_contexts(config_check())
405401
except Exception as e:
406402
return _kube_api_error_handling(e)
407403
try:
408404
return active_context["context"]["namespace"]
409405
except KeyError:
410-
return namespace_error
406+
return None
411407

412408

413409
def get_cluster(cluster_name: str, namespace: str = "default"):
@@ -446,8 +442,8 @@ def _get_ingress_domain():
446442

447443
def _app_wrapper_status(name, namespace="default") -> Optional[AppWrapper]:
448444
try:
449-
KubeConfigFileAuthentication.config_check()
450-
api_instance = client.CustomObjectsApi(TokenAuthentication.api_config_handler())
445+
config_check()
446+
api_instance = client.CustomObjectsApi(api_config_handler())
451447
aws = api_instance.list_namespaced_custom_object(
452448
group="mcad.ibm.com",
453449
version="v1beta1",
@@ -465,8 +461,8 @@ def _app_wrapper_status(name, namespace="default") -> Optional[AppWrapper]:
465461

466462
def _ray_cluster_status(name, namespace="default") -> Optional[RayCluster]:
467463
try:
468-
KubeConfigFileAuthentication.config_check()
469-
api_instance = client.CustomObjectsApi(TokenAuthentication.api_config_handler())
464+
config_check()
465+
api_instance = client.CustomObjectsApi(api_config_handler())
470466
rcs = api_instance.list_namespaced_custom_object(
471467
group="ray.io",
472468
version="v1alpha1",
@@ -485,8 +481,8 @@ def _ray_cluster_status(name, namespace="default") -> Optional[RayCluster]:
485481
def _get_ray_clusters(namespace="default") -> List[RayCluster]:
486482
list_of_clusters = []
487483
try:
488-
KubeConfigFileAuthentication.config_check()
489-
api_instance = client.CustomObjectsApi(TokenAuthentication.api_config_handler())
484+
config_check()
485+
api_instance = client.CustomObjectsApi(api_config_handler())
490486
rcs = api_instance.list_namespaced_custom_object(
491487
group="ray.io",
492488
version="v1alpha1",
@@ -507,8 +503,8 @@ def _get_app_wrappers(
507503
list_of_app_wrappers = []
508504

509505
try:
510-
KubeConfigFileAuthentication.config_check()
511-
api_instance = client.CustomObjectsApi(TokenAuthentication.api_config_handler())
506+
config_check()
507+
api_instance = client.CustomObjectsApi(api_config_handler())
512508
aws = api_instance.list_namespaced_custom_object(
513509
group="mcad.ibm.com",
514510
version="v1beta1",
@@ -534,8 +530,8 @@ def _map_to_ray_cluster(rc) -> Optional[RayCluster]:
534530
else:
535531
status = RayClusterStatus.UNKNOWN
536532

537-
KubeConfigFileAuthentication.config_check()
538-
api_instance = client.CustomObjectsApi(TokenAuthentication.api_config_handler())
533+
config_check()
534+
api_instance = client.CustomObjectsApi(api_config_handler())
539535
routes = api_instance.list_namespaced_custom_object(
540536
group="route.openshift.io",
541537
version="v1",

src/codeflare_sdk/utils/generate_cert.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from cryptography import x509
2020
from cryptography.x509.oid import NameOID
2121
import datetime
22-
from ..cluster.auth import KubeConfigFileAuthentication, TokenAuthentication
22+
from ..cluster.auth import config_check, api_config_handler
2323
from kubernetes import client, config
2424

2525

@@ -83,8 +83,8 @@ def generate_tls_cert(cluster_name, namespace, days=30):
8383
# Similar to:
8484
# oc get secret ca-secret-<cluster-name> -o template='{{index .data "ca.key"}}'
8585
# oc get secret ca-secret-<cluster-name> -o template='{{index .data "ca.crt"}}'|base64 -d > ${TLSDIR}/ca.crt
86-
KubeConfigFileAuthentication.config_check()
87-
v1 = client.CoreV1Api(TokenAuthentication.api_config_handler())
86+
config_check()
87+
v1 = client.CoreV1Api(api_config_handler())
8888
secret = v1.read_namespaced_secret(f"ca-secret-{cluster_name}", namespace).data
8989
ca_cert = secret.get("ca.crt")
9090
ca_key = secret.get("ca.key")

0 commit comments

Comments
 (0)