Skip to content

Commit

Permalink
Refactor Pipeline Parameters Validation
Browse files Browse the repository at this point in the history
Currently, pipeline parameters are only used in task parameters. we need
to use Pipeline Parameters in When Expressions as well. Trying to add the
new validation became ugly, so we're refactoring parameters validation so '
that we can easily add validation for parameters in when expressions.
  • Loading branch information
jerop authored and tekton-robot committed Sep 3, 2020
1 parent a677751 commit 9bc3fd8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 32 deletions.
40 changes: 40 additions & 0 deletions pkg/apis/pipeline/v1beta1/param_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ import (
"fmt"

resource "github.com/tektoncd/pipeline/pkg/apis/resource/v1alpha1"
"github.com/tektoncd/pipeline/pkg/substitution"
"k8s.io/apimachinery/pkg/util/sets"
"knative.dev/pkg/apis"
)

// ParamSpec defines arbitrary parameters needed beyond typed inputs (such as
Expand Down Expand Up @@ -142,3 +145,40 @@ func NewArrayOrString(value string, values ...string) ArrayOrString {
StringVal: value,
}
}

func validatePipelineParametersVariablesInTaskParameters(params []Param, prefix string, paramNames sets.String, arrayParamNames sets.String) *apis.FieldError {
for _, param := range params {
if param.Value.Type == ParamTypeString {
if err := validateStringVariableInTaskParameters(fmt.Sprintf("[%s]", param.Name), param.Value.StringVal, prefix, paramNames, arrayParamNames); err != nil {
return err
}
} else {
for _, arrayElement := range param.Value.ArrayVal {
if err := validateArrayVariableInTaskParameters(fmt.Sprintf("[%s]", param.Name), arrayElement, prefix, paramNames, arrayParamNames); err != nil {
return err
}
}
}
}
return nil
}

func validateStringVariableInTaskParameters(name, value, prefix string, stringVars sets.String, arrayVars sets.String) *apis.FieldError {
if err := substitution.ValidateVariable(name, value, prefix, "task parameter", "pipelinespec.params", stringVars); err != nil {
return err
}
if err := substitution.ValidateVariableProhibited(name, value, prefix, "task parameter", "pipelinespec.params", arrayVars); err != nil {
return err
}
return nil
}

func validateArrayVariableInTaskParameters(name, value, prefix string, stringVars sets.String, arrayVars sets.String) *apis.FieldError {
if err := substitution.ValidateVariable(name, value, prefix, "task parameter", "pipelinespec.params", stringVars); err != nil {
return err
}
if err := substitution.ValidateVariableIsolated(name, value, prefix, "task parameter", "pipelinespec.params", arrayVars); err != nil {
return err
}
return nil
}
36 changes: 4 additions & 32 deletions pkg/apis/pipeline/v1beta1/pipeline_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,46 +356,18 @@ func validatePipelineParameterVariables(tasks []PipelineTask, params []ParamSpec
}
}

return validatePipelineVariables(tasks, "params", parameterNames, arrayParameterNames)
return validatePipelineParametersVariables(tasks, "params", parameterNames, arrayParameterNames)
}

func validatePipelineVariables(tasks []PipelineTask, prefix string, paramNames sets.String, arrayParamNames sets.String) *apis.FieldError {
func validatePipelineParametersVariables(tasks []PipelineTask, prefix string, paramNames sets.String, arrayParamNames sets.String) *apis.FieldError {
for _, task := range tasks {
for _, param := range task.Params {
if param.Value.Type == ParamTypeString {
if err := validatePipelineVariable(fmt.Sprintf("param[%s]", param.Name), param.Value.StringVal, prefix, paramNames); err != nil {
return err
}
if err := validatePipelineNoArrayReferenced(fmt.Sprintf("param[%s]", param.Name), param.Value.StringVal, prefix, arrayParamNames); err != nil {
return err
}
} else {
for _, arrayElement := range param.Value.ArrayVal {
if err := validatePipelineVariable(fmt.Sprintf("param[%s]", param.Name), arrayElement, prefix, paramNames); err != nil {
return err
}
if err := validatePipelineArraysIsolated(fmt.Sprintf("param[%s]", param.Name), arrayElement, prefix, arrayParamNames); err != nil {
return err
}
}
}
if err := validatePipelineParametersVariablesInTaskParameters(task.Params, prefix, paramNames, arrayParamNames); err != nil {
return err
}
}
return nil
}

func validatePipelineVariable(name, value, prefix string, vars sets.String) *apis.FieldError {
return substitution.ValidateVariable(name, value, prefix, "task parameter", "pipelinespec.params", vars)
}

func validatePipelineNoArrayReferenced(name, value, prefix string, vars sets.String) *apis.FieldError {
return substitution.ValidateVariableProhibited(name, value, prefix, "task parameter", "pipelinespec.params", vars)
}

func validatePipelineArraysIsolated(name, value, prefix string, vars sets.String) *apis.FieldError {
return substitution.ValidateVariableIsolated(name, value, prefix, "task parameter", "pipelinespec.params", vars)
}

func validatePipelineContextVariables(tasks []PipelineTask) *apis.FieldError {
pipelineRunContextNames := sets.NewString().Insert(
"name",
Expand Down

0 comments on commit 9bc3fd8

Please sign in to comment.