From f0befb97d93ef32bc54f10a5e3c25fa67c76bb87 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 Please enter the commit message for your changes. Lines starting --- backend/src/apiserver/template/v2_template.go | 26 +++++++++++++++++-- backend/src/v2/compiler/argocompiler/argo.go | 13 ++++++++++ backend/src/v2/compiler/visitor.go | 2 +- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/backend/src/apiserver/template/v2_template.go b/backend/src/apiserver/template/v2_template.go index d14ddffdaeb..f2e555b4250 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 PipelineOptions argocompiler.Options + for _, platform := range t.platformSpec.Platforms { + if platform.PipelineConfig.PipelineTtl != 0 { + PipelineOptions = 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, &PipelineOptions) } else if util.CurrentExecutionType() == util.TektonPipelineRun { obj, err = tektoncompiler.Compile(job, kubernetesSpec, &tektoncompiler.Options{LauncherImage: Launcher}) } @@ -300,9 +310,21 @@ func (t *V2Spec) RunWorkflow(modelRun *model.Run, options RunWorkflowOptions) (u } } + var PipelineOptions *argocompiler.Options + if t.platformSpec != nil && t.platformSpec.Platforms != nil { + for _, platform := range t.platformSpec.Platforms { + if platform.PipelineConfig.PipelineTtl != 0 { + PipelineOptions = &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, PipelineOptions) } 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..7413cf5ca1d 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 ( + pipelineDefaultTtlSeconds = 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 } } + pipelineTtlSeconds := pipelineDefaultTtlSeconds + if &opts.TtlSeconds != nil { + pipelineTtlSeconds = 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: &pipelineTtlSeconds, + }, }, } 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)