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 :
@@ -123,13 +123,8 @@ def up(self):
123
123
plural = "appwrappers" ,
124
124
body = aw ,
125
125
)
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 )
133
128
134
129
def down (self ):
135
130
"""
@@ -147,21 +142,10 @@ def down(self):
147
142
plural = "appwrappers" ,
148
143
name = self .app_wrapper_name ,
149
144
)
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?")
165
149
166
150
def status (
167
151
self , print_to_console : bool = True
@@ -271,8 +255,8 @@ def cluster_dashboard_uri(self) -> str:
271
255
namespace = self .config .namespace ,
272
256
plural = "routes" ,
273
257
)
274
- except :
275
- pass
258
+ except Exception as e :
259
+ return _kube_api_error_handling ( e )
276
260
277
261
for route in routes ["items" ]:
278
262
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):
343
327
344
328
def get_current_namespace ():
345
329
try :
330
+ config .load_kube_config ()
346
331
_ , 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 )
351
334
try :
352
335
return active_context ["context" ]["namespace" ]
353
336
except KeyError :
@@ -357,6 +340,25 @@ def get_current_namespace():
357
340
# private methods
358
341
359
342
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
+
360
362
def _app_wrapper_status (name , namespace = "default" ) -> Optional [AppWrapper ]:
361
363
try :
362
364
config .load_kube_config ()
@@ -367,15 +369,8 @@ def _app_wrapper_status(name, namespace="default") -> Optional[AppWrapper]:
367
369
namespace = namespace ,
368
370
plural = "appwrappers" ,
369
371
)
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 )
379
374
380
375
for aw in aws ["items" ]:
381
376
if aw ["metadata" ]["name" ] == name :
@@ -393,15 +388,8 @@ def _ray_cluster_status(name, namespace="default") -> Optional[RayCluster]:
393
388
namespace = namespace ,
394
389
plural = "rayclusters" ,
395
390
)
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 )
405
393
406
394
for rc in rcs ["items" ]:
407
395
if rc ["metadata" ]["name" ] == name :
@@ -420,19 +408,8 @@ def _get_ray_clusters(namespace="default") -> List[RayCluster]:
420
408
namespace = namespace ,
421
409
plural = "rayclusters" ,
422
410
)
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 )
436
413
437
414
for rc in rcs ["items" ]:
438
415
list_of_clusters .append (_map_to_ray_cluster (rc ))
@@ -453,19 +430,8 @@ def _get_app_wrappers(
453
430
namespace = namespace ,
454
431
plural = "appwrappers" ,
455
432
)
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 )
469
435
470
436
for item in aws ["items" ]:
471
437
app_wrapper = _map_to_app_wrapper (item )
@@ -496,13 +462,6 @@ def _map_to_ray_cluster(rc) -> Optional[RayCluster]:
496
462
if route ["metadata" ]["name" ] == f"ray-dashboard-{ rc ['metadata' ]['name' ]} " :
497
463
ray_route = route ["spec" ]["host" ]
498
464
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
-
506
465
return RayCluster (
507
466
name = rc ["metadata" ]["name" ],
508
467
status = status ,
0 commit comments