Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(matrix): Fix matrix param type mismatch problem for ref array result from customrun scenario #8024

Merged
merged 1 commit into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -164,13 +165,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 @@ -841,3 +841,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))
}
})
}
}