Skip to content

Commit

Permalink
Refactor GetChildReferences
Browse files Browse the repository at this point in the history
Prior to this change, `GetChildReferences` treated `TaskRuns`
and `Runs` differently. It added a`ChildReference` only when
a `TaskRun` was not nil, but did not do the check for `Runs`.

In this change, we consistently add `ChildReference` only when
the `Run` or `TaskRun` is not nil.
  • Loading branch information
jerop authored and tekton-robot committed Jun 21, 2022
1 parent 3147117 commit 09c7ce4
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 28 deletions.
3 changes: 1 addition & 2 deletions pkg/reconciler/pipelinerun/pipelinerun.go
Original file line number Diff line number Diff line change
Expand Up @@ -590,8 +590,7 @@ func (c *Reconciler) reconcile(ctx context.Context, pr *v1beta1.PipelineRun, get
}

if cfg.FeatureFlags.EmbeddedStatus == config.MinimalEmbeddedStatus || cfg.FeatureFlags.EmbeddedStatus == config.BothEmbeddedStatus {
pr.Status.ChildReferences = pipelineRunFacts.State.GetChildReferences(v1beta1.SchemeGroupVersion.String(),
v1alpha1.SchemeGroupVersion.String())
pr.Status.ChildReferences = pipelineRunFacts.State.GetChildReferences()
}

pr.Status.SkippedTasks = pipelineRunFacts.GetSkippedTasks()
Expand Down
27 changes: 10 additions & 17 deletions pkg/reconciler/pipelinerun/resources/pipelinerunstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,27 +238,24 @@ func (state PipelineRunState) GetRunsResults() map[string][]v1alpha1.RunResult {

// GetChildReferences returns a slice of references, including version, kind, name, and pipeline task name, for all
// TaskRuns and Runs in the state.
func (state PipelineRunState) GetChildReferences(taskRunVersion string, runVersion string) []v1beta1.ChildStatusReference {
func (state PipelineRunState) GetChildReferences() []v1beta1.ChildStatusReference {
var childRefs []v1beta1.ChildStatusReference

for _, rprt := range state {
if rprt.CustomTask {
childRefs = append(childRefs, rprt.getChildRefForRun(runVersion))
} else if rprt.TaskRun != nil {
childRefs = append(childRefs, rprt.getChildRefForTaskRun(taskRunVersion))
switch {
case rprt.Run != nil:
childRefs = append(childRefs, rprt.getChildRefForRun())
case rprt.TaskRun != nil:
childRefs = append(childRefs, rprt.getChildRefForTaskRun())
}
}
return childRefs
}

func (rprt *ResolvedPipelineRunTask) getChildRefForRun(runVersion string) v1beta1.ChildStatusReference {
if rprt.Run != nil {
runVersion = rprt.Run.APIVersion
}

func (rprt *ResolvedPipelineRunTask) getChildRefForRun() v1beta1.ChildStatusReference {
return v1beta1.ChildStatusReference{
TypeMeta: runtime.TypeMeta{
APIVersion: runVersion,
APIVersion: rprt.Run.APIVersion,
Kind: pipeline.RunControllerName,
},
Name: rprt.RunName,
Expand All @@ -267,14 +264,10 @@ func (rprt *ResolvedPipelineRunTask) getChildRefForRun(runVersion string) v1beta
}
}

func (rprt *ResolvedPipelineRunTask) getChildRefForTaskRun(taskRunVersion string) v1beta1.ChildStatusReference {
if rprt.TaskRun != nil {
taskRunVersion = rprt.TaskRun.APIVersion
}

func (rprt *ResolvedPipelineRunTask) getChildRefForTaskRun() v1beta1.ChildStatusReference {
return v1beta1.ChildStatusReference{
TypeMeta: runtime.TypeMeta{
APIVersion: taskRunVersion,
APIVersion: rprt.TaskRun.APIVersion,
Kind: pipeline.TaskRunControllerName,
},
Name: rprt.TaskRunName,
Expand Down
11 changes: 2 additions & 9 deletions pkg/reconciler/pipelinerun/resources/pipelinerunstate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2442,14 +2442,7 @@ func TestPipelineRunState_GetChildReferences(t *testing.T) {
},
},
}},
childRefs: []v1beta1.ChildStatusReference{{
TypeMeta: runtime.TypeMeta{
APIVersion: "tekton.dev/v1alpha1",
Kind: "Run",
},
Name: "unresolved-custom-task-run",
PipelineTaskName: "unresolved-custom-task-1",
}},
childRefs: nil,
},
{
name: "single-task",
Expand Down Expand Up @@ -2576,7 +2569,7 @@ func TestPipelineRunState_GetChildReferences(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
childRefs := tc.state.GetChildReferences(v1beta1.SchemeGroupVersion.String(), v1alpha1.SchemeGroupVersion.String())
childRefs := tc.state.GetChildReferences()
if d := cmp.Diff(tc.childRefs, childRefs); d != "" {
t.Errorf("Didn't get expected child references for %s: %s", tc.name, diff.PrintWantGot(d))
}
Expand Down

0 comments on commit 09c7ce4

Please sign in to comment.