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 context-based validation of propagated params/workspaces #6684

Merged
merged 1 commit into from
May 24, 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
34 changes: 0 additions & 34 deletions pkg/apis/config/contexts.go

This file was deleted.

5 changes: 1 addition & 4 deletions pkg/apis/pipeline/v1/pipeline_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,6 @@ func TestPipelineTask_ValidateRegularTask_Success(t *testing.T) {
cfg.FeatureFlags.EnableAPIFields = config.BetaAPIFields
}
ctx = config.ToContext(ctx, cfg)
ctx = config.SkipValidationDueToPropagatedParametersAndWorkspaces(ctx, false)
err := tt.tasks.validateTask(ctx)
if err != nil {
t.Errorf("PipelineTask.validateTask() returned error for valid pipeline task: %v", err)
Expand Down Expand Up @@ -292,8 +291,7 @@ func TestPipelineTask_ValidateRegularTask_Failure(t *testing.T) {
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := config.SkipValidationDueToPropagatedParametersAndWorkspaces(context.Background(), false)
err := tt.task.validateTask(ctx)
err := tt.task.validateTask(context.Background())
if err == nil {
t.Error("PipelineTask.validateTask() did not return error for invalid pipeline task")
}
Expand Down Expand Up @@ -346,7 +344,6 @@ func TestPipelineTask_Validate_Failure(t *testing.T) {
if tt.wc != nil {
ctx = tt.wc(ctx)
}
ctx = config.SkipValidationDueToPropagatedParametersAndWorkspaces(ctx, false)
err := tt.p.Validate(ctx)
if err == nil {
t.Error("PipelineTask.Validate() did not return error for invalid pipeline task")
Expand Down
15 changes: 6 additions & 9 deletions pkg/apis/pipeline/v1/pipeline_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,12 @@ func (p *Pipeline) SupportedVerbs() []admissionregistrationv1.OperationType {
// that any references resources exist, that is done at run time.
func (p *Pipeline) Validate(ctx context.Context) *apis.FieldError {
errs := validate.ObjectMetadata(p.GetObjectMeta()).ViaField("metadata")
ctx = config.SkipValidationDueToPropagatedParametersAndWorkspaces(ctx, false)
return errs.Also(p.Spec.Validate(apis.WithinSpec(ctx)).ViaField("spec"))
errs = errs.Also(p.Spec.Validate(apis.WithinSpec(ctx)).ViaField("spec"))
// When a Pipeline is created directly, instead of declared inline in a PipelineRun,
// we do not support propagated parameters and workspaces.
// Validate that all params and workspaces it uses are declared.
errs = errs.Also(p.Spec.validatePipelineParameterUsage().ViaField("spec"))
return errs.Also(p.Spec.validatePipelineWorkspacesUsage().ViaField("spec"))
lbernick marked this conversation as resolved.
Show resolved Hide resolved
}

// Validate checks that taskNames in the Pipeline are valid and that the graph
Expand Down Expand Up @@ -77,13 +81,6 @@ func (ps *PipelineSpec) Validate(ctx context.Context) (errs *apis.FieldError) {
errs = errs.Also(validateMatrix(ctx, ps.Tasks).ViaField("tasks"))
errs = errs.Also(validateMatrix(ctx, ps.Finally).ViaField("finally"))
errs = errs.Also(validateResultsFromMatrixedPipelineTasksNotConsumed(ps.Tasks, ps.Finally))
// When propagating params and workspaces, params and workspaces used in the Pipeline spec may not be declared by the Pipeline.
// Only perform this validation after all declared params and workspaces have been propagated.
// TODO(#6647): Remove this flag and call this function in the reconciler instead
if config.ValidateParameterVariablesAndWorkspaces(ctx) {
errs = errs.Also(ps.validatePipelineParameterUsage())
errs = errs.Also(ps.validatePipelineWorkspacesUsage())
}
return errs
}

Expand Down
11 changes: 3 additions & 8 deletions pkg/apis/pipeline/v1/pipeline_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -614,8 +614,7 @@ func TestPipelineSpec_Validate_Failure(t *testing.T) {
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := config.SkipValidationDueToPropagatedParametersAndWorkspaces(context.Background(), false)
err := tt.ps.Validate(ctx)
err := tt.ps.Validate(context.Background())
if err == nil {
t.Errorf("PipelineSpec.Validate() did not return error for invalid pipelineSpec")
}
Expand All @@ -637,8 +636,7 @@ func TestPipelineSpec_Validate_Failure_CycleDAG(t *testing.T) {
Name: "baz", TaskRef: &TaskRef{Name: "baz-task"}, RunAfter: []string{"bar"},
}},
}
ctx := config.SkipValidationDueToPropagatedParametersAndWorkspaces(context.Background(), false)
err := ps.Validate(ctx)
err := ps.Validate(context.Background())
if err == nil {
t.Errorf("PipelineSpec.Validate() did not return error for invalid pipelineSpec: %s", name)
}
Expand Down Expand Up @@ -705,8 +703,7 @@ func TestValidatePipelineTasks_Failure(t *testing.T) {
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := config.SkipValidationDueToPropagatedParametersAndWorkspaces(context.Background(), false)
err := ValidatePipelineTasks(ctx, tt.tasks, tt.finalTasks)
err := ValidatePipelineTasks(context.Background(), tt.tasks, tt.finalTasks)
if err == nil {
t.Error("ValidatePipelineTasks() did not return error for invalid pipeline tasks")
}
Expand Down Expand Up @@ -1293,7 +1290,6 @@ func TestValidatePipelineParameterVariables_Success(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := config.EnableAlphaAPIFields(context.Background())
ctx = config.SkipValidationDueToPropagatedParametersAndWorkspaces(ctx, false)
err := ValidatePipelineParameterVariables(ctx, tt.tasks, tt.params)
if err != nil {
t.Errorf("Pipeline.ValidatePipelineParameterVariables() returned error for valid pipeline parameters: %v", err)
Expand Down Expand Up @@ -3039,7 +3035,6 @@ func TestMatrixIncompatibleAPIVersions(t *testing.T) {
ctx := config.ToContext(context.Background(), cfg)

ps.SetDefaults(ctx)
ctx = config.SkipValidationDueToPropagatedParametersAndWorkspaces(ctx, false)
err := ps.Validate(ctx)

if tt.requiredVersion != version && err == nil {
Expand Down
3 changes: 1 addition & 2 deletions pkg/apis/pipeline/v1/pipelinerun_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ func (ps *PipelineRunSpec) Validate(ctx context.Context) (errs *apis.FieldError)

// Validate PipelineSpec if it's present
if ps.PipelineSpec != nil {
ctx = config.SkipValidationDueToPropagatedParametersAndWorkspaces(ctx, true)
errs = errs.Also(ps.PipelineSpec.Validate(ctx).ViaField("pipelineSpec"))
}

Expand Down Expand Up @@ -180,7 +179,7 @@ func (ps *PipelineRunSpec) validateInlineParameters(ctx context.Context) (errs *
if pt.TaskSpec != nil && pt.TaskSpec.Steps != nil {
errs = errs.Also(ValidateParameterTypes(ctx, paramSpec))
errs = errs.Also(ValidateParameterVariables(ctx, pt.TaskSpec.Steps, paramSpec))
errs = errs.Also(validateUsageOfDeclaredParameters(ctx, pt.TaskSpec.Steps, paramSpec))
errs = errs.Also(ValidateUsageOfDeclaredParameters(ctx, pt.TaskSpec.Steps, paramSpec))
}
}
errs = errs.Also(ValidatePipelineParameterVariables(ctx, ps.PipelineSpec.Tasks, paramSpec))
Expand Down
17 changes: 6 additions & 11 deletions pkg/apis/pipeline/v1/task_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@ var objectVariableNameFormatRegex = regexp.MustCompile(objectVariableNameFormat)
// Validate implements apis.Validatable
func (t *Task) Validate(ctx context.Context) *apis.FieldError {
errs := validate.ObjectMetadata(t.GetObjectMeta()).ViaField("metadata")
ctx = config.SkipValidationDueToPropagatedParametersAndWorkspaces(ctx, false)
return errs.Also(t.Spec.Validate(apis.WithinSpec(ctx)).ViaField("spec"))
errs = errs.Also(t.Spec.Validate(apis.WithinSpec(ctx)).ViaField("spec"))
// When a Task is created directly, instead of declared inline in a TaskRun or PipelineRun,
// we do not support propagated parameters. Validate that all params it uses are declared.
return errs.Also(ValidateUsageOfDeclaredParameters(ctx, t.Spec.Steps, t.Spec.Params).ViaField("spec"))
lbernick marked this conversation as resolved.
Show resolved Hide resolved
}

// Validate implements apis.Validatable
Expand All @@ -72,13 +74,6 @@ func (ts *TaskSpec) Validate(ctx context.Context) (errs *apis.FieldError) {
errs = errs.Also(apis.ErrMissingField("steps"))
}

// When propagating parameters, parameters used in the Task spec may not be declared by the Task.
// Only perform this validation after all declared parameters have been propagated.
// TODO(#6647): Remove this flag and call this function in the reconciler instead
if config.ValidateParameterVariablesAndWorkspaces(ctx) {
errs = errs.Also(validateUsageOfDeclaredParameters(ctx, ts.Steps, ts.Params))
}

errs = errs.Also(ValidateVolumes(ts.Volumes).ViaField("volumes"))
errs = errs.Also(validateDeclaredWorkspaces(ts.Workspaces, ts.Steps, ts.StepTemplate).ViaField("workspaces"))
errs = errs.Also(validateWorkspaceUsages(ctx, ts))
Expand All @@ -102,8 +97,8 @@ func (ts *TaskSpec) Validate(ctx context.Context) (errs *apis.FieldError) {
return errs
}

// validateUsageOfDeclaredParameters validates that all parameters referenced in the Task are declared by the Task.
func validateUsageOfDeclaredParameters(ctx context.Context, steps []Step, params ParamSpecs) *apis.FieldError {
// ValidateUsageOfDeclaredParameters validates that all parameters referenced in the Task are declared by the Task.
func ValidateUsageOfDeclaredParameters(ctx context.Context, steps []Step, params ParamSpecs) *apis.FieldError {
var errs *apis.FieldError
_, _, objectParams := params.sortByType()
allParameterNames := sets.NewString(params.getNames()...)
Expand Down
Loading