@@ -48,6 +48,7 @@ def __init__(self, app, nworkers, **kwargs):
4848 k8s_galaxy_instance_id = dict (map = str ),
4949 k8s_timeout_seconds_job_deletion = dict (map = int , valid = lambda x : int > 0 , default = 30 ),
5050 k8s_job_api_version = dict (map = str , default = DEFAULT_JOB_API_VERSION ),
51+ k8s_job_ttl_secs_after_finished = dict (map = int , valid = lambda x : x is None or int (x ) >= 0 , default = 300 ),
5152 k8s_supplemental_group_id = dict (map = str ),
5253 k8s_pull_policy = dict (map = str , default = "Default" ),
5354 k8s_run_as_user_id = dict (map = str , valid = lambda s : s == "$uid" or s .isdigit ()),
@@ -226,6 +227,9 @@ def __get_k8s_job_spec(self, ajs):
226227 If the job hangs around unlimited it will be ended after k8s wall time limit, which sets activeDeadlineSeconds"""
227228 k8s_job_spec = {"template" : self .__get_k8s_job_spec_template (ajs ),
228229 "activeDeadlineSeconds" : int (self .runner_params ['k8s_walltime_limit' ])}
230+ job_ttl = self .runner_params ["k8s_job_ttl_secs_after_finished" ]
231+ if self .runner_params ["k8s_cleanup_job" ] != "never" and job_ttl is not None :
232+ k8s_job_spec ["ttlSecondsAfterFinished" ] = job_ttl
229233 return k8s_job_spec
230234
231235 def __get_k8s_job_spec_template (self , ajs ):
@@ -494,13 +498,18 @@ def __cleanup_k8s_job(self, job):
494498 job .scale (replicas = 0 )
495499 if (k8s_cleanup_job == "always" or
496500 (k8s_cleanup_job == "onsuccess" and not job_failed )):
497- delete_options = {
498- "apiVersion" : "v1" ,
499- "kind" : "DeleteOptions" ,
500- "propagationPolicy" : "Background"
501- }
502- r = job .api .delete (json = delete_options , ** job .api_kwargs ())
503- job .api .raise_for_status (r )
501+ job_ttl = self .runner_params ["k8s_job_ttl_secs_after_finished" ]
502+ if job_ttl is None :
503+ delete_options = {
504+ "apiVersion" : "v1" ,
505+ "kind" : "DeleteOptions" ,
506+ "propagationPolicy" : "Background"
507+ }
508+ r = job .api .delete (json = delete_options , ** job .api_kwargs ())
509+ job .api .raise_for_status (r )
510+ else :
511+ # Let the k8s ttl take care of deletion
512+ pass
504513
505514 def __job_failed_due_to_walltime_limit (self , job ):
506515 conditions = job .obj ['status' ]['conditions' ]
0 commit comments