Skip to content

Commit f3dd478

Browse files
l-qingtekton-robot
authored andcommitted
fix: avoid panic when used pipelineRef or pipelineSpec in pipeline task
fix #7720 Currently, the `pipelineRef` and `pipelineSpec` are only in preview mode and not yet supported. If a user has configured this field and enabled alpha features, it might bypass validation and enter into controller logic. It is now necessary to implement relevant checks within the controller logic to clearly prompt the user, instead of causing the program to panic.
1 parent a40423b commit f3dd478

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

pkg/reconciler/pipelinerun/resources/pipelinerunresolution.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,8 @@ func resolveTask(
630630
pipelineTask v1.PipelineTask,
631631
) (*resources.ResolvedTask, error) {
632632
rt := &resources.ResolvedTask{}
633-
if pipelineTask.TaskRef != nil {
633+
switch {
634+
case pipelineTask.TaskRef != nil:
634635
// If the TaskRun has already a stored TaskSpec in its status, use it as source of truth
635636
if taskRun != nil && taskRun.Status.TaskSpec != nil {
636637
rt.TaskSpec = taskRun.Status.TaskSpec
@@ -655,8 +656,13 @@ func resolveTask(
655656
}
656657
}
657658
rt.Kind = pipelineTask.TaskRef.Kind
658-
} else {
659+
case pipelineTask.TaskSpec != nil:
659660
rt.TaskSpec = &pipelineTask.TaskSpec.TaskSpec
661+
default:
662+
// If the alpha feature is enabled, and the user has configured pipelineSpec or pipelineRef, it will enter here.
663+
// Currently, the controller is not yet adapted, and to avoid a panic, an error message is provided here.
664+
// TODO: Adjust the logic here once the feature is supported in the future.
665+
return nil, fmt.Errorf("Currently, Task %q does not support PipelineRef or PipelineSpec, please use TaskRef or TaskSpec instead", pipelineTask.Name)
660666
}
661667
rt.TaskSpec.SetDefaults(ctx)
662668
return rt, nil

pkg/reconciler/pipelinerun/resources/pipelinerunresolution_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"errors"
2222
"fmt"
2323
"sort"
24+
"strings"
2425
"testing"
2526
"time"
2627

@@ -2434,6 +2435,31 @@ func TestResolvePipelineRun_PipelineTaskHasNoResources(t *testing.T) {
24342435
}
24352436
}
24362437

2438+
func TestResolvePipelineRun_PipelineTaskHasPipelineRef(t *testing.T) {
2439+
pt := v1.PipelineTask{
2440+
Name: "pipeline-in-pipeline",
2441+
PipelineRef: &v1.PipelineRef{Name: "pipeline"},
2442+
}
2443+
2444+
getTask := func(ctx context.Context, name string) (*v1.Task, *v1.RefSource, *trustedresources.VerificationResult, error) {
2445+
return task, nil, nil, nil
2446+
}
2447+
getTaskRun := func(name string) (*v1.TaskRun, error) { return &trs[0], nil }
2448+
pr := v1.PipelineRun{
2449+
ObjectMeta: metav1.ObjectMeta{
2450+
Name: "pipelinerun",
2451+
},
2452+
}
2453+
2454+
_, err := ResolvePipelineTask(context.Background(), pr, getTask, getTaskRun, nopGetCustomRun, pt, nil)
2455+
if err == nil {
2456+
t.Errorf("Error getting tasks for fake pipeline %s, expected an error but got nil.", p.ObjectMeta.Name)
2457+
}
2458+
if !strings.Contains(err.Error(), "does not support PipelineRef or PipelineSpec") {
2459+
t.Errorf("Error getting tasks for fake pipeline %s: expected contains keyword but got %s", p.ObjectMeta.Name, err)
2460+
}
2461+
}
2462+
24372463
func TestResolvePipelineRun_TaskDoesntExist(t *testing.T) {
24382464
pts := []v1.PipelineTask{{
24392465
Name: "mytask1",

0 commit comments

Comments
 (0)