Skip to content

Commit

Permalink
fix 6093 task results not replaced with their values in childReferences
Browse files Browse the repository at this point in the history
used to only when the tasks are in NextSchedulableTask, the reference values in whenexpressions will be replaced. now try to replace result value for whenexpression
  • Loading branch information
chengjoey authored and tekton-robot committed Sep 20, 2023
1 parent 3835c75 commit 3076240
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pkg/reconciler/pipelinerun/pipelinerun.go
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ func (c *Reconciler) reconcile(ctx context.Context, pr *v1.PipelineRun, getPipel
after = pr.Status.GetCondition(apis.ConditionSucceeded)
pr.Status.StartTime = pipelineRunFacts.State.AdjustStartTime(pr.Status.StartTime)

pr.Status.ChildReferences = pipelineRunFacts.State.GetChildReferences()
pr.Status.ChildReferences = pipelineRunFacts.GetChildReferences()

pr.Status.SkippedTasks = pipelineRunFacts.GetSkippedTasks()
if after.Status == corev1.ConditionTrue || after.Status == corev1.ConditionFalse {
Expand Down
12 changes: 10 additions & 2 deletions pkg/reconciler/pipelinerun/resources/pipelinerunstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,18 @@ func (state PipelineRunState) GetRunsResults() map[string][]v1beta1.CustomRunRes

// 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() []v1.ChildStatusReference {
func (facts *PipelineRunFacts) GetChildReferences() []v1.ChildStatusReference {
var childRefs []v1.ChildStatusReference

for _, rpt := range state {
for _, rpt := range facts.State {
// try to replace the parameters of the reference result of whenexpression in the taskrun that has ended
if rpt.isDone(facts) {
resolvedResultRefs, _, err := ResolveResultRefs(facts.State, PipelineRunState{rpt})
if err == nil {
ApplyTaskResults(facts.State, resolvedResultRefs)
}
}

switch {
case len(rpt.TaskRuns) != 0:
for _, taskRun := range rpt.TaskRuns {
Expand Down
121 changes: 119 additions & 2 deletions pkg/reconciler/pipelinerun/resources/pipelinerunstate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3058,7 +3058,7 @@ func TestPipelineRunState_GetChildReferences(t *testing.T) {
Value: v1.ParamValue{Type: v1.ParamTypeArray, ArrayVal: []string{"qux", "baz"}},
}}},
},
TaskRuns: []*v1.TaskRun{nil, nil, nil, nil},
TaskRuns: []*v1.TaskRun{},
}},
childRefs: nil,
},
Expand Down Expand Up @@ -3259,10 +3259,127 @@ func TestPipelineRunState_GetChildReferences(t *testing.T) {
}},
}},
},
{
name: "whenexpressions-reference-task-results",
state: PipelineRunState{
{
TaskRunNames: []string{"task-generate-results"},
PipelineTask: &v1.PipelineTask{
Name: "task1",
},
TaskRuns: []*v1.TaskRun{
{
ObjectMeta: metav1.ObjectMeta{
Name: "task-generate-results",
},
Status: v1.TaskRunStatus{
Status: duckv1.Status{Conditions: []apis.Condition{{
Type: apis.ConditionSucceeded,
Status: corev1.ConditionTrue,
}}},
TaskRunStatusFields: v1.TaskRunStatusFields{
Results: []v1.TaskRunResult{
{
Name: "string-result",
Value: v1.ResultValue{
StringVal: "value1",
Type: v1.ParamTypeString,
},
},
{
Name: "array-result",
Value: v1.ResultValue{
ArrayVal: []string{
"array-value1",
"array-value2",
},
Type: v1.ParamTypeArray,
},
},
},
},
},
},
},
},
{
TaskRunNames: []string{"task-reference-previous-result"},
PipelineTask: &v1.PipelineTask{
Name: "task2",
When: []v1.WhenExpression{
{
Input: "$(tasks.task1.results.string-result)",
Operator: selection.In,
Values: []string{"value1"},
},
{
Input: "$(tasks.task1.results.array-result[1])",
Operator: selection.In,
Values: []string{"array-value2"},
},
},
},
TaskRuns: []*v1.TaskRun{
{
ObjectMeta: metav1.ObjectMeta{
Name: "task-reference-previous-result",
},
Status: v1.TaskRunStatus{
Status: duckv1.Status{Conditions: []apis.Condition{{
Type: apis.ConditionSucceeded,
Status: corev1.ConditionFalse,
}}},
},
},
},
},
},
childRefs: []v1.ChildStatusReference{
{
TypeMeta: runtime.TypeMeta{
APIVersion: "tekton.dev/v1",
Kind: "TaskRun",
},
Name: "task-generate-results",
PipelineTaskName: "task1",
},
{
TypeMeta: runtime.TypeMeta{
APIVersion: "tekton.dev/v1",
Kind: "TaskRun",
},
Name: "task-reference-previous-result",
PipelineTaskName: "task2",
WhenExpressions: []v1.WhenExpression{
{
Input: "value1",
Operator: selection.In,
Values: []string{"value1"},
},
{
Input: "array-value2",
Operator: selection.In,
Values: []string{"array-value2"},
},
},
},
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
childRefs := tc.state.GetChildReferences()
d, err := dagFromState(tc.state)
if err != nil {
t.Fatalf("Unexpected error while building DAG for state %v: %v", tc.state, err)
}
childRefs := (&PipelineRunFacts{
State: tc.state,
TasksGraph: d,
FinalTasksGraph: &dag.Graph{},
TimeoutsState: PipelineRunTimeoutsState{
Clock: testClock,
},
}).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 3076240

Please sign in to comment.