From 723aabd5d215ea732942d4ef0ac02053d40ac0d3 Mon Sep 17 00:00:00 2001 From: VaniHaripriya Date: Mon, 4 Nov 2024 10:12:45 -0600 Subject: [PATCH] Add TTL Strategy in Workflow Spec Signed-off-by: VaniHaripriya --- backend/src/apiserver/template/v2_template.go | 24 +++++++++++++++++-- backend/src/v2/compiler/argocompiler/argo.go | 13 ++++++++++ backend/src/v2/compiler/visitor.go | 2 +- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/backend/src/apiserver/template/v2_template.go b/backend/src/apiserver/template/v2_template.go index d14ddffdaeb..36c9baeeab6 100644 --- a/backend/src/apiserver/template/v2_template.go +++ b/backend/src/apiserver/template/v2_template.go @@ -77,9 +77,19 @@ func (t *V2Spec) ScheduledWorkflow(modelJob *model.Job) (*scheduledworkflow.Sche } } + var pipeline_options argocompiler.Options + for _, platform := range t.platformSpec.Platforms { + if platform.PipelineConfig.PipelineTtl != 0 { + pipeline_options = argocompiler.Options{ + TtlSeconds: platform.PipelineConfig.PipelineTtl, + } + break + } + } + var obj interface{} if util.CurrentExecutionType() == util.ArgoWorkflow { - obj, err = argocompiler.Compile(job, kubernetesSpec, nil) + obj, err = argocompiler.Compile(job, kubernetesSpec, &pipeline_options) } else if util.CurrentExecutionType() == util.TektonPipelineRun { obj, err = tektoncompiler.Compile(job, kubernetesSpec, &tektoncompiler.Options{LauncherImage: Launcher}) } @@ -300,9 +310,19 @@ func (t *V2Spec) RunWorkflow(modelRun *model.Run, options RunWorkflowOptions) (u } } + var pipeline_options *argocompiler.Options + for _, platform := range t.platformSpec.Platforms { + if platform.PipelineConfig.PipelineTtl != 0 { + pipeline_options = &argocompiler.Options{ + TtlSeconds: platform.PipelineConfig.PipelineTtl, + } + break + } + } + var obj interface{} if util.CurrentExecutionType() == util.ArgoWorkflow { - obj, err = argocompiler.Compile(job, kubernetesSpec, nil) + obj, err = argocompiler.Compile(job, kubernetesSpec, pipeline_options) } else if util.CurrentExecutionType() == util.TektonPipelineRun { obj, err = tektoncompiler.Compile(job, kubernetesSpec, nil) } diff --git a/backend/src/v2/compiler/argocompiler/argo.go b/backend/src/v2/compiler/argocompiler/argo.go index 1f1c19ed3ec..4c7aa8e560a 100644 --- a/backend/src/v2/compiler/argocompiler/argo.go +++ b/backend/src/v2/compiler/argocompiler/argo.go @@ -40,8 +40,13 @@ type Options struct { // optional PipelineRoot string // TODO(Bobgy): add an option -- dev mode, ImagePullPolicy should only be Always in dev mode. + TtlSeconds int32 } +const ( + pipeline_default_ttlSeconds = int32(30) +) + func Compile(jobArg *pipelinespec.PipelineJob, kubernetesSpecArg *pipelinespec.SinglePlatformSpec, opts *Options) (*wfapi.Workflow, error) { // clone jobArg, because we don't want to change it jobMsg := proto.Clone(jobArg) @@ -86,6 +91,11 @@ func Compile(jobArg *pipelinespec.PipelineJob, kubernetesSpecArg *pipelinespec.S } } + pipeline_ttlseconds := pipeline_default_ttlSeconds + if &opts.TtlSeconds != nil { + pipeline_ttlseconds = opts.TtlSeconds + } + // initialization wf := &wfapi.Workflow{ TypeMeta: k8smeta.TypeMeta{ @@ -117,6 +127,9 @@ func Compile(jobArg *pipelinespec.PipelineJob, kubernetesSpecArg *pipelinespec.S }, ServiceAccountName: "pipeline-runner", Entrypoint: tmplEntrypoint, + TTLStrategy: &wfapi.TTLStrategy{ + SecondsAfterCompletion: &pipeline_ttlseconds, + }, }, } c := &workflowCompiler{ diff --git a/backend/src/v2/compiler/visitor.go b/backend/src/v2/compiler/visitor.go index f6b7204a45c..277fadcbec8 100644 --- a/backend/src/v2/compiler/visitor.go +++ b/backend/src/v2/compiler/visitor.go @@ -109,7 +109,7 @@ func (state *pipelineDFS) dfs(name string, component *pipelinespec.ComponentSpec } // Add kubernetes spec to annotation - if state.kubernetesSpec != nil { + if state.kubernetesSpec != nil && state.kubernetesSpec.DeploymentSpec != nil { kubernetesExecSpec, ok := state.kubernetesSpec.DeploymentSpec.Executors[executorLabel] if ok { state.visitor.AddKubernetesSpec(name, kubernetesExecSpec)