Skip to content

Commit

Permalink
feat(matrix): Fix matrix param type mismatch problem for ref array re…
Browse files Browse the repository at this point in the history
…sult from customrun scenario

Adjust `resolveCustomResultRef` implementation in Tekton to handle array
results from CustomRun tasks correctly. This change resolves a type mismatch
issue when using array results as matrix parameters, ensuring compatibility
and proper functioning in scenarios where previous CustomRun emits array results.
  • Loading branch information
hittyt authored and tekton-robot committed Aug 22, 2024
1 parent c2dede8 commit 22e3d0e
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
18 changes: 17 additions & 1 deletion pkg/reconciler/pipelinerun/resources/resultrefresolution.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package resources

import (
"encoding/json"
"errors"
"fmt"
"sort"
Expand Down Expand Up @@ -162,13 +163,28 @@ func resolveCustomResultRef(customRuns []*v1beta1.CustomRun, resultRef *v1.Resul
return nil, err
}
return &ResolvedResultRef{
Value: *v1.NewStructuredValues(runValue),
Value: *paramValueFromCustomRunResult(runValue),
FromTaskRun: "",
FromRun: runName,
ResultReference: *resultRef,
}, nil
}

func paramValueFromCustomRunResult(result string) *v1.ParamValue {
var arrayResult []string
// for fan out array result, which is represented as string, we should make it to array type param value
if err := json.Unmarshal([]byte(result), &arrayResult); err == nil && len(arrayResult) > 0 {
if len(arrayResult) > 1 {
return v1.NewStructuredValues(arrayResult[0], arrayResult[1:]...)
}
return &v1.ParamValue{
Type: v1.ParamTypeArray,
ArrayVal: []string{arrayResult[0]},
}
}
return v1.NewStructuredValues(result)
}

func resolveResultRef(taskRuns []*v1.TaskRun, resultRef *v1.ResultRef) (*ResolvedResultRef, error) {
taskRun := taskRuns[0]
taskRunName := taskRun.Name
Expand Down
50 changes: 50 additions & 0 deletions pkg/reconciler/pipelinerun/resources/resultrefresolution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -930,3 +930,53 @@ func TestValidateArrayResultsIndex(t *testing.T) {
})
}
}

func TestParamValueFromCustomRunResult(t *testing.T) {
type args struct {
result string
}
tests := []struct {
name string
args args
want *v1.ParamValue
}{
{
name: "multiple array elements result",
args: args{
result: `["amd64", "arm64"]`,
},
want: &v1.ParamValue{
Type: "array",
ArrayVal: []string{"amd64", "arm64"},
},
},
{
name: "single array elements result",
args: args{
result: `[ "amd64" ]`,
},
want: &v1.ParamValue{
Type: "array",
ArrayVal: []string{"amd64"},
},
},
{
name: "simple string result",
args: args{
result: "amd64",
},
want: &v1.ParamValue{
Type: "string",
StringVal: "amd64",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := paramValueFromCustomRunResult(tt.args.result)
if d := cmp.Diff(tt.want, got); d != "" {
t.Fatalf("paramValueFromCustomRunResult %s", diff.PrintWantGot(d))
}
})
}
}

0 comments on commit 22e3d0e

Please sign in to comment.