Skip to content

Commit

Permalink
Cleanup: Move array indexing validation out of apis package
Browse files Browse the repository at this point in the history
We validate that indexing references to array parameters are in bounds,
based on the default values of the parameters and the parameter values
passed in from a PipelineRun/TaskRun. This validation happens in the reconciler,
since it requires comparing the Task/Pipeline spec with the parameters
defined in the TaskRun/PipelineRun.

Prior to this commit, validation code was in the apis package, even though it was called
in the reconciler. This is confusing, because this code cannot be used to validate a Task/Pipeline
spec in isolation; it can only be used in the context of TaskRuns and PipelineRuns.
This commit moves this validation logic to the reconciler and does not introduce any functional changes.
  • Loading branch information
lbernick authored and tekton-robot committed May 31, 2023
1 parent 8d1a903 commit 5d5fd5b
Show file tree
Hide file tree
Showing 18 changed files with 852 additions and 1,318 deletions.
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 {
// 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 {
// 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

0 comments on commit 5d5fd5b

Please sign in to comment.