From 3e991f8ff249cbc68d2e8d25f43b721ee940dcfe Mon Sep 17 00:00:00 2001 From: Vincent Demeester Date: Fri, 22 Apr 2022 14:14:53 +0200 Subject: [PATCH] Fix variable substitution for Task/Pipeline name Previously it would alway use "embedded", it now get the right value (or embedded if embedded). Signed-off-by: Vincent Demeester --- pkg/tekton/pipelinerun.go | 15 +++++++++++---- pkg/tekton/taskrun.go | 10 +++++++--- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/pkg/tekton/pipelinerun.go b/pkg/tekton/pipelinerun.go index bcc093ec..fd043db8 100644 --- a/pkg/tekton/pipelinerun.go +++ b/pkg/tekton/pipelinerun.go @@ -25,14 +25,17 @@ func PipelineRunToLLB(ctx context.Context, c client.Client, r PipelineRun) (llb. } var ps *v1beta1.PipelineSpec + var name string if pr.Spec.PipelineSpec != nil { ps = pr.Spec.PipelineSpec + name = "embedded" } else if pr.Spec.PipelineRef != nil && pr.Spec.PipelineRef.Bundle != "" { resolvedPipeline, err := resolvePipelineInBundle(ctx, c, *pr.Spec.PipelineRef) if err != nil { return llb.State{}, err } ps = &resolvedPipeline.Spec + name = pr.Spec.PipelineRef.Name } else if pr.Spec.PipelineRef != nil && pr.Spec.PipelineRef.Name != "" { p, ok := r.pipelines[pr.Spec.PipelineRef.Name] if !ok { @@ -40,10 +43,11 @@ func PipelineRunToLLB(ctx context.Context, c client.Client, r PipelineRun) (llb. } p.SetDefaults(ctx) ps = &p.Spec + name = pr.Spec.PipelineRef.Name } // Interpolation - spec, err := applyPipelineRunSubstitution(ctx, pr, ps) + spec, err := applyPipelineRunSubstitution(ctx, pr, ps, name) if err != nil { return llb.State{}, errors.Wrap(err, "variable interpolation failed") } @@ -93,7 +97,9 @@ func PipelineRunToLLB(ctx context.Context, c client.Client, r PipelineRun) (llb. tasks := map[string][]llb.State{} for _, t := range spec.Tasks { var ts v1beta1.TaskSpec + var name string if t.TaskRef != nil { + name = t.TaskRef.Name if t.TaskRef.Bundle != "" { resolvedTask, err := resolveTaskInBundle(ctx, c, *t.TaskRef) if err != nil { @@ -109,6 +115,7 @@ func PipelineRunToLLB(ctx context.Context, c client.Client, r PipelineRun) (llb. ts = task.Spec } } else if t.TaskSpec != nil { + name = "embedded" ts = t.TaskSpec.TaskSpec } @@ -117,7 +124,7 @@ func PipelineRunToLLB(ctx context.Context, c client.Client, r PipelineRun) (llb. Params: t.Params, TaskSpec: &ts, }, - }, &ts) + }, &ts, name) if err != nil { return llb.State{}, errors.Wrapf(err, "variable interpolation failed for %s", t.Name) } @@ -159,9 +166,9 @@ func PipelineRunToLLB(ctx context.Context, c client.Client, r PipelineRun) (llb. return ft.File(fa, llb.WithCustomName("[tekton] buildking image from result (fake)"), llb.IgnoreCache), nil } -func applyPipelineRunSubstitution(ctx context.Context, pr *v1beta1.PipelineRun, ps *v1beta1.PipelineSpec) (v1beta1.PipelineSpec, error) { +func applyPipelineRunSubstitution(ctx context.Context, pr *v1beta1.PipelineRun, ps *v1beta1.PipelineSpec, pipelineName string) (v1beta1.PipelineSpec, error) { ps = resources.ApplyParameters(ps, pr) - ps = resources.ApplyContexts(ps, "embedded", pr) // FIXME(vdemeester) handle this "embedded" better + ps = resources.ApplyContexts(ps, pipelineName, pr) ps = resources.ApplyWorkspaces(ps, pr) if err := ps.Validate(ctx); err != nil { diff --git a/pkg/tekton/taskrun.go b/pkg/tekton/taskrun.go index a7c55d03..5b76d338 100644 --- a/pkg/tekton/taskrun.go +++ b/pkg/tekton/taskrun.go @@ -40,14 +40,17 @@ func TaskRunToLLB(ctx context.Context, c client.Client, r TaskRun) (llb.State, e } var ts *v1beta1.TaskSpec + var name string if tr.Spec.TaskSpec != nil { ts = tr.Spec.TaskSpec + name = "embedded" } else if tr.Spec.TaskRef != nil && tr.Spec.TaskRef.Bundle != "" { resolvedTask, err := resolveTaskInBundle(ctx, c, *tr.Spec.TaskRef) if err != nil { return llb.State{}, err } ts = &resolvedTask.Spec + name = tr.Spec.TaskRef.Name } else if tr.Spec.TaskRef != nil && tr.Spec.TaskRef.Name != "" { t, ok := r.tasks[tr.Spec.TaskRef.Name] if !ok { @@ -55,10 +58,11 @@ func TaskRunToLLB(ctx context.Context, c client.Client, r TaskRun) (llb.State, e } t.SetDefaults(ctx) ts = &t.Spec + name = tr.Spec.TaskRef.Name } // Interpolation - spec, err := applyTaskRunSubstitution(ctx, tr, ts) + spec, err := applyTaskRunSubstitution(ctx, tr, ts, name) if err != nil { return llb.State{}, errors.Wrap(err, "variable interpolation failed") } @@ -85,7 +89,7 @@ func TaskRunToLLB(ctx context.Context, c client.Client, r TaskRun) (llb.State, e return stepStates[len(stepStates)-1], nil } -func applyTaskRunSubstitution(ctx context.Context, tr *v1beta1.TaskRun, ts *v1beta1.TaskSpec) (v1beta1.TaskSpec, error) { +func applyTaskRunSubstitution(ctx context.Context, tr *v1beta1.TaskRun, ts *v1beta1.TaskSpec, taskName string) (v1beta1.TaskSpec, error) { var defaults []v1beta1.ParamSpec if len(ts.Params) > 0 { defaults = append(defaults, ts.Params...) @@ -94,7 +98,7 @@ func applyTaskRunSubstitution(ctx context.Context, tr *v1beta1.TaskRun, ts *v1be ts = resources.ApplyParameters(ts, tr, defaults...) // Apply context substitution from the taskrun - ts = resources.ApplyContexts(ts, "embedded", tr) // FIXME(vdemeester) handle task name here (in case of TaskRef) + ts = resources.ApplyContexts(ts, taskName, tr) // TODO(vdemeester) support PipelineResource ? // Apply bound resource substitution from the taskrun.