Skip to content

Commit

Permalink
Added Validation: Matrix parameters cannot be empty arrays
Browse files Browse the repository at this point in the history
Added a check in Pipeline validation to verify that matrix parameters are not just arrays, but that they are not empty arrays. This won't change existing functionality/capabilities, but will get rid of the panic in the reconciler.
  • Loading branch information
EmmaMunley committed Feb 9, 2023
1 parent 2da10fc commit 0180ffc
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/apis/pipeline/v1/param_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,9 @@ func validateParametersInTaskMatrix(matrix *Matrix) (errs *apis.FieldError) {
if param.Value.Type != ParamTypeArray {
errs = errs.Also(apis.ErrInvalidValue("parameters of type array only are allowed in matrix", "").ViaFieldKey("matrix", param.Name))
}
if param.Value.Type == ParamTypeArray && len(param.Value.ArrayVal) == 0 {
errs = errs.Also(apis.ErrInvalidValue("empty arrays not allowed as parameters in matrix", "").ViaFieldKey("matrix", param.Name))
}
}
}
return errs
Expand Down
15 changes: 15 additions & 0 deletions pkg/apis/pipeline/v1/pipeline_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,21 @@ func TestPipelineTask_validateMatrix(t *testing.T) {
Name: "barfoo", Value: ParamValue{Type: ParamTypeArray, ArrayVal: []string{"bar", "foo"}},
}}},
},
}, {
name: "parameters in matrix are not empty arrays",
pt: &PipelineTask{
Name: "task",
Matrix: &Matrix{
Params: []Param{{
Name: "foobar", Value: ParamValue{Type: ParamTypeArray, ArrayVal: []string{}},
}, {
Name: "barfoo", Value: ParamValue{Type: ParamTypeArray, ArrayVal: []string{}},
}}},
},
wantErrs: &apis.FieldError{
Message: "invalid value: empty arrays not allowed as parameters in matrix",
Paths: []string{"matrix[barfoo]", "matrix[foobar]"},
},
}, {
name: "parameters in matrix contain results references",
pt: &PipelineTask{
Expand Down
3 changes: 3 additions & 0 deletions pkg/apis/pipeline/v1beta1/param_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,9 @@ func validateParametersInTaskMatrix(matrix *Matrix) (errs *apis.FieldError) {
if param.Value.Type != ParamTypeArray {
errs = errs.Also(apis.ErrInvalidValue("parameters of type array only are allowed in matrix", "").ViaFieldKey("matrix", param.Name))
}
if param.Value.Type == ParamTypeArray && len(param.Value.ArrayVal) == 0 {
errs = errs.Also(apis.ErrInvalidValue("empty arrays not allowed as parameters in matrix", "").ViaFieldKey("matrix", param.Name))
}
}
}
return errs
Expand Down
15 changes: 15 additions & 0 deletions pkg/apis/pipeline/v1beta1/pipeline_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,21 @@ func TestPipelineTask_validateMatrix(t *testing.T) {
Name: "barfoo", Value: ParamValue{Type: ParamTypeArray, ArrayVal: []string{"bar", "foo"}},
}}},
},
}, {
name: "parameters in matrix are not empty arrays",
pt: &PipelineTask{
Name: "task",
Matrix: &Matrix{
Params: []Param{{
Name: "foobar", Value: ParamValue{Type: ParamTypeArray, ArrayVal: []string{}},
}, {
Name: "barfoo", Value: ParamValue{Type: ParamTypeArray, ArrayVal: []string{}},
}}},
},
wantErrs: &apis.FieldError{
Message: "invalid value: empty arrays not allowed as parameters in matrix",
Paths: []string{"matrix[barfoo]", "matrix[foobar]"},
},
}, {
name: "parameters in matrix contain results references",
pt: &PipelineTask{
Expand Down

0 comments on commit 0180ffc

Please sign in to comment.