22
22
from time import sleep
23
23
from typing import List , Optional , Tuple , Dict
24
24
25
- import openshift as oc
26
25
from ray .job_submission import JobSubmissionClient
27
26
28
27
from ..utils import pretty_print
39
38
from kubernetes import client , config
40
39
41
40
import yaml
41
+ import executing
42
42
43
43
44
44
class Cluster :
@@ -125,13 +125,8 @@ def up(self):
125
125
plural = "appwrappers" ,
126
126
body = aw ,
127
127
)
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 )
135
130
136
131
def down (self ):
137
132
"""
@@ -149,21 +144,10 @@ def down(self):
149
144
plural = "appwrappers" ,
150
145
name = self .app_wrapper_name ,
151
146
)
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?")
167
151
168
152
def status (
169
153
self , print_to_console : bool = True
@@ -273,8 +257,8 @@ def cluster_dashboard_uri(self) -> str:
273
257
namespace = self .config .namespace ,
274
258
plural = "routes" ,
275
259
)
276
- except :
277
- pass
260
+ except Exception as e :
261
+ return _kube_api_error_handling ( e )
278
262
279
263
for route in routes ["items" ]:
280
264
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):
345
329
346
330
def get_current_namespace ():
347
331
try :
332
+ config .load_kube_config ()
348
333
_ , 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 )
353
336
try :
354
337
return active_context ["context" ]["namespace" ]
355
338
except KeyError :
@@ -359,6 +342,25 @@ def get_current_namespace():
359
342
# private methods
360
343
361
344
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
+
362
364
def _app_wrapper_status (name , namespace = "default" ) -> Optional [AppWrapper ]:
363
365
try :
364
366
config .load_kube_config ()
@@ -369,15 +371,8 @@ def _app_wrapper_status(name, namespace="default") -> Optional[AppWrapper]:
369
371
namespace = namespace ,
370
372
plural = "appwrappers" ,
371
373
)
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 )
381
376
382
377
for aw in aws ["items" ]:
383
378
if aw ["metadata" ]["name" ] == name :
@@ -395,15 +390,8 @@ def _ray_cluster_status(name, namespace="default") -> Optional[RayCluster]:
395
390
namespace = namespace ,
396
391
plural = "rayclusters" ,
397
392
)
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 )
407
395
408
396
for rc in rcs ["items" ]:
409
397
if rc ["metadata" ]["name" ] == name :
@@ -422,19 +410,8 @@ def _get_ray_clusters(namespace="default") -> List[RayCluster]:
422
410
namespace = namespace ,
423
411
plural = "rayclusters" ,
424
412
)
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 )
438
415
439
416
for rc in rcs ["items" ]:
440
417
list_of_clusters .append (_map_to_ray_cluster (rc ))
@@ -455,19 +432,8 @@ def _get_app_wrappers(
455
432
namespace = namespace ,
456
433
plural = "appwrappers" ,
457
434
)
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 )
471
437
472
438
for item in aws ["items" ]:
473
439
app_wrapper = _map_to_app_wrapper (item )
@@ -498,13 +464,6 @@ def _map_to_ray_cluster(rc) -> Optional[RayCluster]:
498
464
if route ["metadata" ]["name" ] == f"ray-dashboard-{ rc ['metadata' ]['name' ]} " :
499
465
ray_route = route ["spec" ]["host" ]
500
466
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
-
508
467
return RayCluster (
509
468
name = rc ["metadata" ]["name" ],
510
469
status = status ,
0 commit comments