Skip to content

Commit bb021ce

Browse files
committed
Added exception handling
1 parent 6aedea2 commit bb021ce

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

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

166150
def status(
167151
self, print_to_console: bool = True
@@ -271,8 +255,8 @@ def cluster_dashboard_uri(self) -> str:
271255
namespace=self.config.namespace,
272256
plural="routes",
273257
)
274-
except:
275-
pass
258+
except Exception as e:
259+
return _kube_api_error_handling(e)
276260

277261
for route in routes["items"]:
278262
if route["metadata"]["name"] == f"ray-dashboard-{self.config.name}":
@@ -343,11 +327,10 @@ def list_all_queued(namespace: str, print_to_console: bool = True):
343327

344328
def get_current_namespace():
345329
try:
330+
config.load_kube_config()
346331
_, active_context = config.list_kube_config_contexts()
347-
except config.ConfigException:
348-
raise PermissionError(
349-
"Retrieving current namespace not permitted, have you put in correct/up-to-date auth credentials?"
350-
)
332+
except Exception as e:
333+
return _kube_api_error_handling(e)
351334
try:
352335
return active_context["context"]["namespace"]
353336
except KeyError:
@@ -357,6 +340,25 @@ def get_current_namespace():
357340
# private methods
358341

359342

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

380375
for aw in aws["items"]:
381376
if aw["metadata"]["name"] == name:
@@ -393,15 +388,8 @@ def _ray_cluster_status(name, namespace="default") -> Optional[RayCluster]:
393388
namespace=namespace,
394389
plural="rayclusters",
395390
)
396-
except oc.OpenShiftPythonException as osp: # pragma: no cover
397-
error_msg = osp.result.err()
398-
if not (
399-
'the server doesn\'t have a resource type "rayclusters"' in error_msg
400-
or "forbidden" in error_msg
401-
or "Unauthorized" in error_msg
402-
or "Missing or incomplete configuration" in error_msg
403-
):
404-
raise osp
391+
except Exception as e:
392+
return _kube_api_error_handling(e)
405393

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

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

470436
for item in aws["items"]:
471437
app_wrapper = _map_to_app_wrapper(item)
@@ -496,13 +462,6 @@ def _map_to_ray_cluster(rc) -> Optional[RayCluster]:
496462
if route["metadata"]["name"] == f"ray-dashboard-{rc['metadata']['name']}":
497463
ray_route = route["spec"]["host"]
498464

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

0 commit comments

Comments
 (0)