Skip to content

Commit

Permalink
Handle error conditions in CheckMissingResultReferences
Browse files Browse the repository at this point in the history
Before this change, it was possible for a PipelineRun to exist which
would cause the controller to crashloop.
  • Loading branch information
ralphbean authored and tekton-robot committed Jul 3, 2024
1 parent c56e0da commit fddbbbe
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
11 changes: 10 additions & 1 deletion pkg/reconciler/pipelinerun/resources/pipelinerunresolution.go
Original file line number Diff line number Diff line change
Expand Up @@ -797,14 +797,23 @@ func isCustomRunCancelledByPipelineRunTimeout(cr *v1beta1.CustomRun) bool {
func CheckMissingResultReferences(pipelineRunState PipelineRunState, targets PipelineRunState) error {
for _, target := range targets {
for _, resultRef := range v1.PipelineTaskResultRefs(target.PipelineTask) {
referencedPipelineTask := pipelineRunState.ToMap()[resultRef.PipelineTask]
referencedPipelineTask, ok := pipelineRunState.ToMap()[resultRef.PipelineTask]
if !ok {
return fmt.Errorf("Result reference error: Could not find ref \"%s\" in internal pipelineRunState", resultRef.PipelineTask)
}
if referencedPipelineTask.IsCustomTask() {
if len(referencedPipelineTask.CustomRuns) == 0 {
return fmt.Errorf("Result reference error: Internal result ref \"%s\" has zero-length CustomRuns", resultRef.PipelineTask)
}
customRun := referencedPipelineTask.CustomRuns[0]
_, err := findRunResultForParam(customRun, resultRef)
if err != nil {
return err
}
} else {
if len(referencedPipelineTask.TaskRuns) == 0 {
return fmt.Errorf("Result reference error: Internal result ref \"%s\" has zero-length TaskRuns", resultRef.PipelineTask)
}
taskRun := referencedPipelineTask.TaskRuns[0]
_, err := findTaskResultForParam(taskRun, resultRef)
if err != nil {
Expand Down
36 changes: 36 additions & 0 deletions pkg/reconciler/pipelinerun/resources/resultrefresolution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,28 @@ var pipelineRunState = PipelineRunState{{
Value: *v1.NewStructuredValues("$(tasks.kTask.results.I-DO-NOT-EXIST)[*]"),
}},
},
}, {
TaskRunNames: []string{"lTaskRun"},
TaskRuns: []*v1.TaskRun{},
PipelineTask: &v1.PipelineTask{
Name: "lTask",
TaskRef: &v1.TaskRef{Name: "lTask"},
Params: []v1.Param{{
Name: "jParam",
Value: *v1.NewStructuredValues("$(tasks.does-not-exist.results.some-result)"),
}},
},
}, {
TaskRunNames: []string{"mTaskRun"},
TaskRuns: []*v1.TaskRun{},
PipelineTask: &v1.PipelineTask{
Name: "mTask",
TaskRef: &v1.TaskRef{Name: "mTask"},
Params: []v1.Param{{
Name: "mParam",
Value: *v1.NewStructuredValues("$(tasks.lTask.results.aResult)"),
}},
},
}}

func TestResolveResultRefs(t *testing.T) {
Expand Down Expand Up @@ -724,6 +746,20 @@ func TestCheckMissingResultReferences(t *testing.T) {
pipelineRunState[14],
},
wantErr: "Invalid task result reference: Could not find result with name iDoNotExist for task aCustomPipelineTask",
}, {
name: "Invalid: Test result references where ref does not exist in pipelineRunState map",
pipelineRunState: pipelineRunState,
targets: PipelineRunState{
pipelineRunState[18],
},
wantErr: "Result reference error: Could not find ref \"does-not-exist\" in internal pipelineRunState",
}, {
name: "Invalid: Test result references where referencedPipelineTask has no TaskRuns",
pipelineRunState: pipelineRunState,
targets: PipelineRunState{
pipelineRunState[19],
},
wantErr: "Result reference error: Internal result ref \"lTask\" has zero-length TaskRuns",
}} {
t.Run(tt.name, func(t *testing.T) {
err := CheckMissingResultReferences(tt.pipelineRunState, tt.targets)
Expand Down

0 comments on commit fddbbbe

Please sign in to comment.