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

Cleanup: Move array indexing validation out of apis package #6617

Merged
merged 1 commit into from
May 31, 2023
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
32 changes: 4 additions & 28 deletions pkg/apis/pipeline/v1/param_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,9 @@ func (ps Params) extractParamMapArrVals() map[string][]string {
// Params is a list of Param
type Params []Param

// extractParamArrayLengths extract and return the lengths of all array params
// ExtractParamArrayLengths extract and return the lengths of all array params
// Example of returned value: {"a-array-params": 2,"b-array-params": 2 }
func (ps Params) extractParamArrayLengths() map[string]int {
func (ps Params) ExtractParamArrayLengths() map[string]int {
lbernick marked this conversation as resolved.
Show resolved Hide resolved
// Collect all array params
arrayParamsLengths := make(map[string]int)

Expand Down Expand Up @@ -232,9 +232,9 @@ func (ps Params) ReplaceVariables(stringReplacements map[string]string, arrayRep
return params
}

// extractParamArrayLengths extract and return the lengths of all array params
// ExtractDefaultParamArrayLengths extract and return the lengths of all array params
// Example of returned value: {"a-array-params": 2,"b-array-params": 2 }
func (ps ParamSpecs) extractParamArrayLengths() map[string]int {
func (ps ParamSpecs) ExtractDefaultParamArrayLengths() map[string]int {
lbernick marked this conversation as resolved.
Show resolved Hide resolved
// Collect all array params
arrayParamsLengths := make(map[string]int)

Expand All @@ -249,30 +249,6 @@ func (ps ParamSpecs) extractParamArrayLengths() map[string]int {
return arrayParamsLengths
}

// validateOutofBoundArrayParams validates if the array indexing params are out of bound
// example of arrayIndexingParams: ["$(params.a-array-param[1])", "$(params.b-array-param[2])"]
// example of arrayParamsLengths: {"a-array-params": 2,"b-array-params": 2 }
func validateOutofBoundArrayParams(arrayIndexingParams []string, arrayParamsLengths map[string]int) error {
outofBoundParams := sets.String{}
for _, val := range arrayIndexingParams {
indexString := substitution.ExtractIndexString(val)
idx, _ := substitution.ExtractIndex(indexString)
// this will extract the param name from reference
// e.g. $(params.a-array-param[1]) -> a-array-param
paramName, _, _ := substitution.ExtractVariablesFromString(substitution.TrimArrayIndex(val), "params")

if paramLength, ok := arrayParamsLengths[paramName[0]]; ok {
if idx >= paramLength {
outofBoundParams.Insert(val)
}
}
}
if outofBoundParams.Len() > 0 {
return fmt.Errorf("non-existent param references:%v", outofBoundParams.List())
}
return nil
}

// extractArrayIndexingParamRefs takes a string of the form `foo-$(params.array-param[1])-bar` and extracts the portions of the string that reference an element in an array param.
// For example, for the string “foo-$(params.array-param[1])-bar-$(params.other-array-param[2])-$(params.string-param)`,
// it would return ["$(params.array-param[1])", "$(params.other-array-param[2])"].
Expand Down
89 changes: 89 additions & 0 deletions pkg/apis/pipeline/v1/param_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
"github.com/tektoncd/pipeline/test/diff"
"k8s.io/apimachinery/pkg/util/sets"
Expand Down Expand Up @@ -564,3 +565,91 @@ func TestParams_ReplaceVariables(t *testing.T) {
})
}
}

func TestExtractParamArrayLengths(t *testing.T) {
tcs := []struct {
name string
params v1.Params
want map[string]int
}{{
name: "string params",
params: v1.Params{{Name: "foo", Value: v1.ParamValue{StringVal: "bar", Type: v1.ParamTypeString}}},
want: nil,
}, {
name: "one array param",
params: v1.Params{{Name: "foo", Value: v1.ParamValue{ArrayVal: []string{"bar", "baz"}, Type: v1.ParamTypeArray}}},
want: map[string]int{"foo": 2},
}, {
name: "object params",
params: v1.Params{{Name: "foo", Value: v1.ParamValue{ObjectVal: map[string]string{"bar": "baz"}, Type: v1.ParamTypeObject}}},
want: nil,
}, {
name: "multiple array params",
params: v1.Params{
{Name: "foo", Value: v1.ParamValue{ArrayVal: []string{"bar", "baz"}, Type: v1.ParamTypeArray}},
{Name: "abc", Value: v1.ParamValue{ArrayVal: []string{"123", "456", "789"}, Type: v1.ParamTypeArray}},
{Name: "empty", Value: v1.ParamValue{ArrayVal: []string{}, Type: v1.ParamTypeArray}},
},
want: map[string]int{"foo": 2, "abc": 3, "empty": 0},
}, {
name: "mixed param types",
params: v1.Params{
{Name: "foo", Value: v1.ParamValue{StringVal: "abc", Type: v1.ParamTypeString}},
{Name: "bar", Value: v1.ParamValue{ArrayVal: []string{"def", "ghi"}, Type: v1.ParamTypeArray}},
{Name: "baz", Value: v1.ParamValue{ObjectVal: map[string]string{"jkl": "mno"}, Type: v1.ParamTypeObject}},
},
want: map[string]int{"bar": 2},
}}
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
got := tc.params.ExtractParamArrayLengths()
if d := cmp.Diff(tc.want, got, cmpopts.EquateEmpty()); d != "" {
t.Errorf("wrong param array lengths: %s", d)
}
})
}
}

func TestExtractDefaultParamArrayLengths(t *testing.T) {
tcs := []struct {
name string
params v1.ParamSpecs
want map[string]int
}{{
name: "string params",
params: v1.ParamSpecs{{Name: "foo", Default: &v1.ParamValue{StringVal: "bar", Type: v1.ParamTypeString}}},
want: nil,
}, {
name: "one array param",
params: v1.ParamSpecs{{Name: "foo", Default: &v1.ParamValue{ArrayVal: []string{"bar", "baz"}, Type: v1.ParamTypeArray}}},
want: map[string]int{"foo": 2},
}, {
name: "object params",
params: v1.ParamSpecs{{Name: "foo", Default: &v1.ParamValue{ObjectVal: map[string]string{"bar": "baz"}, Type: v1.ParamTypeObject}}},
want: nil,
}, {
name: "multiple array params",
params: v1.ParamSpecs{
{Name: "foo", Default: &v1.ParamValue{ArrayVal: []string{"bar", "baz"}, Type: v1.ParamTypeArray}},
{Name: "abc", Default: &v1.ParamValue{ArrayVal: []string{"123", "456", "789"}, Type: v1.ParamTypeArray}},
{Name: "empty", Default: &v1.ParamValue{ArrayVal: []string{}, Type: v1.ParamTypeArray}},
},
want: map[string]int{"foo": 2, "abc": 3, "empty": 0},
}, {
name: "mixed param types",
params: v1.ParamSpecs{
{Name: "foo", Default: &v1.ParamValue{StringVal: "abc", Type: v1.ParamTypeString}},
{Name: "bar", Default: &v1.ParamValue{ArrayVal: []string{"def", "ghi"}, Type: v1.ParamTypeArray}},
{Name: "baz", Default: &v1.ParamValue{ObjectVal: map[string]string{"jkl": "mno"}, Type: v1.ParamTypeObject}},
},
want: map[string]int{"bar": 2},
}}
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
got := tc.params.ExtractDefaultParamArrayLengths()
if d := cmp.Diff(tc.want, got, cmpopts.EquateEmpty()); d != "" {
t.Errorf("wrong default param array lengths: %s", d)
}
})
}
}
15 changes: 0 additions & 15 deletions pkg/apis/pipeline/v1/pipeline_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -758,21 +758,6 @@ func validateResultsFromMatrixedPipelineTasksNotConsumed(tasks []PipelineTask, f
return errs
}

// ValidateParamArrayIndex validates if the param reference to an array param is out of bound.
// error is returned when the array indexing reference is out of bound of the array param
// e.g. if a param reference of $(params.array-param[2]) and the array param is of length 2.
// TODO(#6616): Move this functionality to the reconciler, as it is only used there
func (ps *PipelineSpec) ValidateParamArrayIndex(ctx context.Context, params Params) error {
// Collect all array params lengths
arrayParamsLengths := ps.Params.extractParamArrayLengths()
for k, v := range params.extractParamArrayLengths() {
arrayParamsLengths[k] = v
}
// extract all array indexing references, for example []{"$(params.array-params[1])"}
arrayIndexParamRefs := ps.GetIndexingReferencesToArrayParams().List()
return validateOutofBoundArrayParams(arrayIndexParamRefs, arrayParamsLengths)
}

// GetIndexingReferencesToArrayParams returns all strings referencing indices of PipelineRun array parameters
// from parameters, workspaces, and when expressions defined in the Pipeline's Tasks and Finally Tasks.
// For example, if a Task in the Pipeline has a parameter with a value "$(params.array-param-name[1])",
Expand Down
Loading