Skip to content

Commit d9f67aa

Browse files
committed
Added exception handling
1 parent 000b277 commit d9f67aa

File tree

1 file changed

+39
-80
lines changed

1 file changed

+39
-80
lines changed

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

+39-80
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:
@@ -125,13 +125,8 @@ def up(self):
125125
plural="appwrappers",
126126
body=aw,
127127
)
128-
except oc.OpenShiftPythonException as osp: # pragma: no cover
129-
error_msg = osp.result.err()
130-
if "Unauthorized" in error_msg:
131-
raise PermissionError(
132-
"Action not permitted, have you put in correct/up-to-date auth credentials?"
133-
)
134-
raise osp
128+
except Exception as e:
129+
return _kube_api_error_handling(e)
135130

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

168152
def status(
169153
self, print_to_console: bool = True
@@ -273,8 +257,8 @@ def cluster_dashboard_uri(self) -> str:
273257
namespace=self.config.namespace,
274258
plural="routes",
275259
)
276-
except:
277-
pass
260+
except Exception as e:
261+
return _kube_api_error_handling(e)
278262

279263
for route in routes["items"]:
280264
if route["metadata"]["name"] == f"ray-dashboard-{self.config.name}":
@@ -345,11 +329,10 @@ def list_all_queued(namespace: str, print_to_console: bool = True):
345329

346330
def get_current_namespace():
347331
try:
332+
config.load_kube_config()
348333
_, active_context = config.list_kube_config_contexts()
349-
except config.ConfigException:
350-
raise PermissionError(
351-
"Retrieving current namespace not permitted, have you put in correct/up-to-date auth credentials?"
352-
)
334+
except Exception as e:
335+
return _kube_api_error_handling(e)
353336
try:
354337
return active_context["context"]["namespace"]
355338
except KeyError:
@@ -359,6 +342,25 @@ def get_current_namespace():
359342
# private methods
360343

361344

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

382377
for aw in aws["items"]:
383378
if aw["metadata"]["name"] == name:
@@ -395,15 +390,8 @@ def _ray_cluster_status(name, namespace="default") -> Optional[RayCluster]:
395390
namespace=namespace,
396391
plural="rayclusters",
397392
)
398-
except oc.OpenShiftPythonException as osp: # pragma: no cover
399-
error_msg = osp.result.err()
400-
if not (
401-
'the server doesn\'t have a resource type "rayclusters"' in error_msg
402-
or "forbidden" in error_msg
403-
or "Unauthorized" in error_msg
404-
or "Missing or incomplete configuration" in error_msg
405-
):
406-
raise osp
393+
except Exception as e:
394+
return _kube_api_error_handling(e)
407395

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

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

472438
for item in aws["items"]:
473439
app_wrapper = _map_to_app_wrapper(item)
@@ -498,13 +464,6 @@ def _map_to_ray_cluster(rc) -> Optional[RayCluster]:
498464
if route["metadata"]["name"] == f"ray-dashboard-{rc['metadata']['name']}":
499465
ray_route = route["spec"]["host"]
500466

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

0 commit comments

Comments
 (0)