From 3076240709fd4ceba13f24ee1eccbee3cb13ca92 Mon Sep 17 00:00:00 2001 From: joey Date: Wed, 8 Feb 2023 23:58:14 +0800 Subject: [PATCH] fix 6093 task results not replaced with their values in childReferences 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 --- pkg/reconciler/pipelinerun/pipelinerun.go | 2 +- .../pipelinerun/resources/pipelinerunstate.go | 12 +- .../resources/pipelinerunstate_test.go | 121 +++++++++++++++++- 3 files changed, 130 insertions(+), 5 deletions(-) diff --git a/pkg/reconciler/pipelinerun/pipelinerun.go b/pkg/reconciler/pipelinerun/pipelinerun.go index 0e7d8bb9588..eef81a2e16c 100644 --- a/pkg/reconciler/pipelinerun/pipelinerun.go +++ b/pkg/reconciler/pipelinerun/pipelinerun.go @@ -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 { diff --git a/pkg/reconciler/pipelinerun/resources/pipelinerunstate.go b/pkg/reconciler/pipelinerun/resources/pipelinerunstate.go index 1c8e9038b28..65882f4c987 100644 --- a/pkg/reconciler/pipelinerun/resources/pipelinerunstate.go +++ b/pkg/reconciler/pipelinerun/resources/pipelinerunstate.go @@ -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 { diff --git a/pkg/reconciler/pipelinerun/resources/pipelinerunstate_test.go b/pkg/reconciler/pipelinerun/resources/pipelinerunstate_test.go index d5e4b918e96..918337be4ae 100644 --- a/pkg/reconciler/pipelinerun/resources/pipelinerunstate_test.go +++ b/pkg/reconciler/pipelinerun/resources/pipelinerunstate_test.go @@ -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, }, @@ -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)) }