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 :
@@ -127,13 +127,8 @@ def up(self):
127
127
plural = "appwrappers" ,
128
128
body = aw ,
129
129
)
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 )
137
132
138
133
def down (self ):
139
134
"""
@@ -151,21 +146,10 @@ def down(self):
151
146
plural = "appwrappers" ,
152
147
name = self .app_wrapper_name ,
153
148
)
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?")
169
153
170
154
def status (
171
155
self , print_to_console : bool = True
@@ -275,8 +259,8 @@ def cluster_dashboard_uri(self) -> str:
275
259
namespace = self .config .namespace ,
276
260
plural = "routes" ,
277
261
)
278
- except :
279
- pass
262
+ except Exception as e :
263
+ return _kube_api_error_handling ( e )
280
264
281
265
for route in routes ["items" ]:
282
266
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):
347
331
348
332
def get_current_namespace ():
349
333
try :
334
+ config .load_kube_config ()
350
335
_ , 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 )
355
338
try :
356
339
return active_context ["context" ]["namespace" ]
357
340
except KeyError :
@@ -361,6 +344,25 @@ def get_current_namespace():
361
344
# private methods
362
345
363
346
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
+
364
366
def _app_wrapper_status (name , namespace = "default" ) -> Optional [AppWrapper ]:
365
367
try :
366
368
config .load_kube_config ()
@@ -371,15 +373,8 @@ def _app_wrapper_status(name, namespace="default") -> Optional[AppWrapper]:
371
373
namespace = namespace ,
372
374
plural = "appwrappers" ,
373
375
)
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 )
383
378
384
379
for aw in aws ["items" ]:
385
380
if aw ["metadata" ]["name" ] == name :
@@ -397,15 +392,8 @@ def _ray_cluster_status(name, namespace="default") -> Optional[RayCluster]:
397
392
namespace = namespace ,
398
393
plural = "rayclusters" ,
399
394
)
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 )
409
397
410
398
for rc in rcs ["items" ]:
411
399
if rc ["metadata" ]["name" ] == name :
@@ -424,19 +412,8 @@ def _get_ray_clusters(namespace="default") -> List[RayCluster]:
424
412
namespace = namespace ,
425
413
plural = "rayclusters" ,
426
414
)
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 )
440
417
441
418
for rc in rcs ["items" ]:
442
419
list_of_clusters .append (_map_to_ray_cluster (rc ))
@@ -457,19 +434,8 @@ def _get_app_wrappers(
457
434
namespace = namespace ,
458
435
plural = "appwrappers" ,
459
436
)
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 )
473
439
474
440
for item in aws ["items" ]:
475
441
app_wrapper = _map_to_app_wrapper (item )
@@ -500,13 +466,6 @@ def _map_to_ray_cluster(rc) -> Optional[RayCluster]:
500
466
if route ["metadata" ]["name" ] == f"ray-dashboard-{ rc ['metadata' ]['name' ]} " :
501
467
ray_route = route ["spec" ]["host" ]
502
468
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
-
510
469
return RayCluster (
511
470
name = rc ["metadata" ]["name" ],
512
471
status = status ,
0 commit comments