@@ -240,13 +240,13 @@ def dashboard(yes, url):
240240@click .option ("--delete" , "-d" , is_flag = True , default = False )
241241@click .option (
242242 "--uuids" ,
243- "--uids" ,
243+ "-uids" ,
244+ "-ids" ,
244245 type = str ,
245246 help = "List of uuid of operations to clean/delete (comma separated values)." ,
246247)
247248def clean_ops (namespace , in_cluster , delete , uuids ):
248249 """clean-ops command."""
249- # TODO(Operator): Use new correct plural when available
250250 from polyaxon ._k8s .custom_resources import operation
251251 from polyaxon ._k8s .manager .manager import K8sManager
252252
@@ -255,17 +255,17 @@ def clean_ops(namespace, in_cluster, delete, uuids):
255255
256256 manager = K8sManager (namespace = namespace , in_cluster = in_cluster )
257257
258- def _patch_op ():
258+ def _patch_op (name , group , version , plural ):
259259 retry = 0
260260 while retry < 3 :
261261 if retry :
262262 time .sleep (retry * 2 )
263263 try :
264264 manager .update_custom_object (
265- name = op ,
266- group = operation . GROUP ,
267- version = operation . API_VERSION ,
268- plural = operation . PLURAL ,
265+ name = name ,
266+ group = group ,
267+ version = version ,
268+ plural = plural ,
269269 body = {"metadata" : {"finalizers" : None }},
270270 )
271271 return
@@ -274,43 +274,74 @@ def _patch_op():
274274 print ("retrying" )
275275 retry += 1
276276
277- def _delete_op ():
277+ def _delete_op (name , group , version , plural ):
278278 retry = 0
279279 while retry <= 2 :
280280 if retry :
281281 time .sleep (retry )
282282 try :
283283 manager .delete_custom_object (
284- name = op ,
285- group = operation . GROUP ,
286- version = operation . API_VERSION ,
287- plural = operation . PLURAL ,
284+ name = name ,
285+ group = group ,
286+ version = version ,
287+ plural = plural ,
288288 )
289289 return
290290 except Exception as e :
291291 print ("Exception %s" , e )
292292 print ("retrying" )
293293 retry += 1
294294
295+ def _get_op (name , group , version , plural ):
296+ try :
297+ return manager .get_custom_object (
298+ name = name ,
299+ group = group ,
300+ version = version ,
301+ plural = plural ,
302+ )
303+ except Exception :
304+ return None
305+
295306 uuids = validate_tags (uuids , validate_yaml = True )
307+
308+ kinds = [
309+ (operation .JOB_KIND , operation .JOB_PLURAL ),
310+ (operation .SERVICES_KIND , operation .SERVICES_PLURAL ),
311+ (operation .CLUSTER_KIND , operation .CLUSTER_PLURAL ),
312+ (operation .KFJOB_KIND , operation .KFJOB_PLURAL ),
313+ ]
314+
315+ ops = []
316+
296317 if uuids :
297- ops = [o if "plx-operation-" in o else get_resource_name (o ) for o in uuids ]
318+ names = [o if "plx-operation-" in o else get_resource_name (o ) for o in uuids ]
319+ for name in names :
320+ for kind , plural in kinds :
321+ if _get_op (name , operation .GROUP , operation .API_VERSION , plural ):
322+ ops .append ((name , kind , plural ))
323+ break
298324 else :
299- ops = [
300- o ["metadata" ]["name" ]
301- for o in manager .list_custom_objects (
302- group = operation .GROUP ,
303- version = operation .API_VERSION ,
304- plural = operation .PLURAL ,
305- )
306- ]
325+ for kind , plural in kinds :
326+ try :
327+ objs = manager .list_custom_objects (
328+ group = operation .GROUP ,
329+ version = operation .API_VERSION ,
330+ plural = plural ,
331+ )
332+ for o in objs :
333+ ops .append ((o ["metadata" ]["name" ], kind , plural ))
334+ except Exception as e :
335+ Printer .warning (f"Could not list { plural } : { e } " )
336+
307337 if not ops :
338+ Printer .print ("No operations found." )
308339 return
309340
310341 Printer .header (f"Cleaning { len (ops )} ops ..." )
311- for idx , op in enumerate (ops ):
342+ for idx , ( name , kind , plural ) in enumerate (ops ):
312343 with Printer .console .status (f"Cleaning operation { idx + 1 } /{ len (ops )} ..." ):
313- _patch_op ()
344+ _patch_op (name , operation . GROUP , operation . API_VERSION , plural )
314345 if delete :
315- _delete_op ()
316- Printer .success (f"Operation { op } was cleaned" )
346+ _delete_op (name , operation . GROUP , operation . API_VERSION , plural )
347+ Printer .success (f"Operation { name } was cleaned" )
0 commit comments