Skip to content

Commit 083928c

Browse files
committed
Added exception handling
1 parent aea6ccd commit 083928c

File tree

1 file changed

+39
-80
lines changed

1 file changed

+39
-80
lines changed

src/codeflare_sdk/cluster/cluster.py

Lines changed: 39 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
from time import sleep
2323
from typing import List, Optional, Tuple, Dict
2424

25-
import openshift as oc
2625
from ray.job_submission import JobSubmissionClient
2726

2827
from ..utils import pretty_print
@@ -39,6 +38,7 @@
3938
from kubernetes import client, config
4039

4140
import yaml
41+
import executing
4242

4343

4444
class Cluster:
@@ -127,13 +127,8 @@ def up(self):
127127
plural="appwrappers",
128128
body=aw,
129129
)
130-
except oc.OpenShiftPythonException as osp: # pragma: no cover
131-
error_msg = osp.result.err()
132-
if "Unauthorized" in error_msg:
133-
raise PermissionError(
134-
"Action not permitted, have you put in correct/up-to-date auth credentials?"
135-
)
136-
raise osp
130+
except Exception as e:
131+
return _kube_api_error_handling(e)
137132

138133
def down(self):
139134
"""
@@ -151,21 +146,10 @@ def down(self):
151146
plural="appwrappers",
152147
name=self.app_wrapper_name,
153148
)
154-
except oc.OpenShiftPythonException as osp: # pragma: no cover
155-
error_msg = osp.result.err()
156-
if (
157-
'the server doesn\'t have a resource type "AppWrapper"' in error_msg
158-
or "forbidden" in error_msg
159-
or "Unauthorized" in error_msg
160-
or "Missing or incomplete configuration" in error_msg
161-
):
162-
raise PermissionError(
163-
"Action not permitted, have you run auth.login()/cluster.up() yet?"
164-
)
165-
elif "not found" in error_msg:
166-
print("Cluster not found, have you run cluster.up() yet?")
167-
else:
168-
raise osp
149+
except Exception as e:
150+
return _kube_api_error_handling(e)
151+
# elif "not found" in error_msg:
152+
# print("Cluster not found, have you run cluster.up() yet?")
169153

170154
def status(
171155
self, print_to_console: bool = True
@@ -275,8 +259,8 @@ def cluster_dashboard_uri(self) -> str:
275259
namespace=self.config.namespace,
276260
plural="routes",
277261
)
278-
except:
279-
pass
262+
except Exception as e:
263+
return _kube_api_error_handling(e)
280264

281265
for route in routes["items"]:
282266
if route["metadata"]["name"] == f"ray-dashboard-{self.config.name}":
@@ -347,11 +331,10 @@ def list_all_queued(namespace: str, print_to_console: bool = True):
347331

348332
def get_current_namespace():
349333
try:
334+
config.load_kube_config()
350335
_, active_context = config.list_kube_config_contexts()
351-
except config.ConfigException:
352-
raise PermissionError(
353-
"Retrieving current namespace not permitted, have you put in correct/up-to-date auth credentials?"
354-
)
336+
except Exception as e:
337+
return _kube_api_error_handling(e)
355338
try:
356339
return active_context["context"]["namespace"]
357340
except KeyError:
@@ -361,6 +344,25 @@ def get_current_namespace():
361344
# private methods
362345

363346

347+
def _kube_api_error_handling(e: Exception):
348+
perm_msg = (
349+
"Action not permitted, have you put in correct/up-to-date auth credentials?"
350+
)
351+
nf_msg = "No instances found, nothing to be done."
352+
if type(e) == config.ConfigException:
353+
raise PermissionError(perm_msg)
354+
if type(e) == executing.executing.NotOneValueFound:
355+
print(nf_msg)
356+
return
357+
if type(e) == client.ApiException:
358+
if e.reason == "Not Found":
359+
print(nf_msg)
360+
return
361+
elif e.reason == "Unauthorized" or e.reason == "Forbidden":
362+
raise PermissionError(perm_msg)
363+
raise e
364+
365+
364366
def _app_wrapper_status(name, namespace="default") -> Optional[AppWrapper]:
365367
try:
366368
config.load_kube_config()
@@ -371,15 +373,8 @@ def _app_wrapper_status(name, namespace="default") -> Optional[AppWrapper]:
371373
namespace=namespace,
372374
plural="appwrappers",
373375
)
374-
except oc.OpenShiftPythonException as osp: # pragma: no cover
375-
error_msg = osp.result.err()
376-
if not (
377-
'the server doesn\'t have a resource type "appwrapper"' in error_msg
378-
or "forbidden" in error_msg
379-
or "Unauthorized" in error_msg
380-
or "Missing or incomplete configuration" in error_msg
381-
):
382-
raise osp
376+
except Exception as e:
377+
return _kube_api_error_handling(e)
383378

384379
for aw in aws["items"]:
385380
if aw["metadata"]["name"] == name:
@@ -397,15 +392,8 @@ def _ray_cluster_status(name, namespace="default") -> Optional[RayCluster]:
397392
namespace=namespace,
398393
plural="rayclusters",
399394
)
400-
except oc.OpenShiftPythonException as osp: # pragma: no cover
401-
error_msg = osp.result.err()
402-
if not (
403-
'the server doesn\'t have a resource type "rayclusters"' in error_msg
404-
or "forbidden" in error_msg
405-
or "Unauthorized" in error_msg
406-
or "Missing or incomplete configuration" in error_msg
407-
):
408-
raise osp
395+
except Exception as e:
396+
return _kube_api_error_handling(e)
409397

410398
for rc in rcs["items"]:
411399
if rc["metadata"]["name"] == name:
@@ -424,19 +412,8 @@ def _get_ray_clusters(namespace="default") -> List[RayCluster]:
424412
namespace=namespace,
425413
plural="rayclusters",
426414
)
427-
except oc.OpenShiftPythonException as osp: # pragma: no cover
428-
error_msg = osp.result.err()
429-
if (
430-
'the server doesn\'t have a resource type "rayclusters"' in error_msg
431-
or "forbidden" in error_msg
432-
or "Unauthorized" in error_msg
433-
or "Missing or incomplete configuration" in error_msg
434-
):
435-
raise PermissionError(
436-
"Action not permitted, have you put in correct/up-to-date auth credentials?"
437-
)
438-
else:
439-
raise osp
415+
except Exception as e:
416+
return _kube_api_error_handling(e)
440417

441418
for rc in rcs["items"]:
442419
list_of_clusters.append(_map_to_ray_cluster(rc))
@@ -457,19 +434,8 @@ def _get_app_wrappers(
457434
namespace=namespace,
458435
plural="appwrappers",
459436
)
460-
except oc.OpenShiftPythonException as osp: # pragma: no cover
461-
error_msg = osp.result.err()
462-
if (
463-
'the server doesn\'t have a resource type "appwrappers"' in error_msg
464-
or "forbidden" in error_msg
465-
or "Unauthorized" in error_msg
466-
or "Missing or incomplete configuration" in error_msg
467-
):
468-
raise PermissionError(
469-
"Action not permitted, have you put in correct/up-to-date auth credentials?"
470-
)
471-
else:
472-
raise osp
437+
except Exception as e:
438+
return _kube_api_error_handling(e)
473439

474440
for item in aws["items"]:
475441
app_wrapper = _map_to_app_wrapper(item)
@@ -500,13 +466,6 @@ def _map_to_ray_cluster(rc) -> Optional[RayCluster]:
500466
if route["metadata"]["name"] == f"ray-dashboard-{rc['metadata']['name']}":
501467
ray_route = route["spec"]["host"]
502468

503-
# with oc.project(rc["metadata"]["namespace"]), oc.timeout(10 * 60):
504-
# route = (
505-
# oc.selector(f"route/ray-dashboard-{rc['metadata']['name']}")
506-
# .object()
507-
# .model.spec.host
508-
# )
509-
510469
return RayCluster(
511470
name=rc["metadata"]["name"],
512471
status=status,

0 commit comments

Comments
 (0)