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 committed May 19, 2023
1 parent 2eadca5 commit 77aaf21
Show file tree
Hide file tree
Showing 16 changed files with 714 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
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 @@ -700,21 +700,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)
}

// ValidateBetaFeaturesEnabledForParamArrayIndexing validates that "enable-api-fields" is set to "alpha" or "beta" if the pipeline spec
// contains indexing references to array params.
// This can be removed when array param indexing is moved to "stable".
Expand Down
Loading

0 comments on commit 77aaf21

Please sign in to comment.