Skip to content

Commit

Permalink
Fix variable substitution for Task/Pipeline name
Browse files Browse the repository at this point in the history
Previously it would alway use "embedded", it now get the right
value (or embedded if embedded).

Signed-off-by: Vincent Demeester <[email protected]>
  • Loading branch information
vdemeester committed Apr 22, 2022
1 parent e750511 commit 3e991f8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
15 changes: 11 additions & 4 deletions pkg/tekton/pipelinerun.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,29 @@ 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 {
return llb.State{}, errors.Errorf("PipelineRef %s not found in context", pr.Spec.PipelineRef.Name)
}
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")
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}

Expand All @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 7 additions & 3 deletions pkg/tekton/taskrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,29 @@ 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 {
return llb.State{}, errors.Errorf("Taskref %s not found in context", tr.Spec.TaskRef.Name)
}
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")
}
Expand All @@ -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...)
Expand All @@ -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.
Expand Down

0 comments on commit 3e991f8

Please sign in to comment.