@@ -69,8 +69,6 @@ func (ps *PipelineSpec) Validate(ctx context.Context) (errs *apis.FieldError) {
69
69
errs = errs .Also (ps .ValidateBetaFeaturesEnabledForParamArrayIndexing (ctx ))
70
70
// Validate the pipeline's workspaces.
71
71
errs = errs .Also (validatePipelineWorkspacesDeclarations (ps .Workspaces ))
72
- errs = errs .Also (validatePipelineWorkspacesUsage (ctx , ps .Workspaces , ps .Tasks ).ViaField ("tasks" ))
73
- errs = errs .Also (validatePipelineWorkspacesUsage (ctx , ps .Workspaces , ps .Finally ).ViaField ("finally" ))
74
72
// Validate the pipeline's results
75
73
errs = errs .Also (validatePipelineResults (ps .Results , ps .Tasks , ps .Finally ))
76
74
errs = errs .Also (validateTasksAndFinallySection (ps ))
@@ -79,6 +77,13 @@ func (ps *PipelineSpec) Validate(ctx context.Context) (errs *apis.FieldError) {
79
77
errs = errs .Also (validateMatrix (ctx , ps .Tasks ).ViaField ("tasks" ))
80
78
errs = errs .Also (validateMatrix (ctx , ps .Finally ).ViaField ("finally" ))
81
79
errs = errs .Also (validateResultsFromMatrixedPipelineTasksNotConsumed (ps .Tasks , ps .Finally ))
80
+ // When propagating params and workspaces, params and workspaces used in the Pipeline spec may not be declared by the Pipeline.
81
+ // Only perform this validation after all declared params and workspaces have been propagated.
82
+ // TODO(#6647): Remove this flag and call this function in the reconciler instead
83
+ if config .ValidateParameterVariablesAndWorkspaces (ctx ) {
84
+ errs = errs .Also (ps .validatePipelineParameterUsage ())
85
+ errs = errs .Also (ps .validatePipelineWorkspacesUsage ())
86
+ }
82
87
return errs
83
88
}
84
89
@@ -297,12 +302,41 @@ func validatePipelineWorkspacesDeclarations(wss []PipelineWorkspaceDeclaration)
297
302
return errs
298
303
}
299
304
300
- // validatePipelineWorkspacesUsage validates that all the referenced workspaces (by pipeline tasks) are specified in
301
- // the pipeline
302
- func validatePipelineWorkspacesUsage (ctx context.Context , wss []PipelineWorkspaceDeclaration , pts []PipelineTask ) (errs * apis.FieldError ) {
303
- if ! config .ValidateParameterVariablesAndWorkspaces (ctx ) {
304
- return nil
305
+ // validatePipelineParameterUsage validates that parameters referenced in the Pipeline are declared by the Pipeline
306
+ func (ps * PipelineSpec ) validatePipelineParameterUsage () (errs * apis.FieldError ) {
307
+ errs = errs .Also (validatePipelineTaskParameterUsage (ps .Tasks , ps .Params ).ViaField ("tasks" ))
308
+ errs = errs .Also (validatePipelineTaskParameterUsage (ps .Finally , ps .Params ).ViaField ("finally" ))
309
+ return errs
310
+ }
311
+
312
+ // validatePipelineTaskParameterUsage validates that parameters referenced in the Pipeline Tasks are declared by the Pipeline
313
+ func validatePipelineTaskParameterUsage (tasks []PipelineTask , params ParamSpecs ) (errs * apis.FieldError ) {
314
+ allParamNames := sets .NewString (params .getNames ()... )
315
+ _ , arrayParams , objectParams := params .sortByType ()
316
+ arrayParamNames := sets .NewString (arrayParams .getNames ()... )
317
+ objectParameterNameKeys := map [string ][]string {}
318
+ for _ , p := range objectParams {
319
+ for k := range p .Properties {
320
+ objectParameterNameKeys [p .Name ] = append (objectParameterNameKeys [p .Name ], k )
321
+ }
305
322
}
323
+ errs = errs .Also (validatePipelineParametersVariables (tasks , "params" , allParamNames , arrayParamNames , objectParameterNameKeys ))
324
+ for i , task := range tasks {
325
+ errs = errs .Also (task .Params .validateDuplicateParameters ().ViaFieldIndex ("params" , i ))
326
+ }
327
+ return errs
328
+ }
329
+
330
+ // validatePipelineWorkspacesUsage validates that workspaces referenced in the Pipeline are declared by the Pipeline
331
+ func (ps * PipelineSpec ) validatePipelineWorkspacesUsage () (errs * apis.FieldError ) {
332
+ errs = errs .Also (validatePipelineTasksWorkspacesUsage (ps .Workspaces , ps .Tasks ).ViaField ("tasks" ))
333
+ errs = errs .Also (validatePipelineTasksWorkspacesUsage (ps .Workspaces , ps .Finally ).ViaField ("finally" ))
334
+ return errs
335
+ }
336
+
337
+ // validatePipelineTasksWorkspacesUsage validates that all the referenced workspaces (by pipeline tasks) are specified in
338
+ // the pipeline
339
+ func validatePipelineTasksWorkspacesUsage (wss []PipelineWorkspaceDeclaration , pts []PipelineTask ) (errs * apis.FieldError ) {
306
340
workspaceNames := sets .NewString ()
307
341
for _ , ws := range wss {
308
342
workspaceNames .Insert (ws .Name )
@@ -316,37 +350,16 @@ func validatePipelineWorkspacesUsage(ctx context.Context, wss []PipelineWorkspac
316
350
317
351
// ValidatePipelineParameterVariables validates parameters with those specified by each pipeline task,
318
352
// (1) it validates the type of parameter is either string or array (2) parameter default value matches
319
- // with the type of that param (3) ensures that the referenced param variable is defined is part of the param declarations
320
- func ValidatePipelineParameterVariables (ctx context.Context , tasks []PipelineTask , params []ParamSpec ) (errs * apis.FieldError ) {
321
- parameterNames := sets .NewString ()
322
- arrayParameterNames := sets .NewString ()
323
- objectParameterNameKeys := map [string ][]string {}
324
-
353
+ // with the type of that param
354
+ func ValidatePipelineParameterVariables (ctx context.Context , tasks []PipelineTask , params ParamSpecs ) (errs * apis.FieldError ) {
325
355
// validates all the types within a slice of ParamSpecs
326
356
errs = errs .Also (ValidateParameterTypes (ctx , params ).ViaField ("params" ))
327
-
328
- for _ , p := range params {
329
- if parameterNames .Has (p .Name ) {
330
- errs = errs .Also (apis .ErrGeneric ("parameter appears more than once" , "" ).ViaFieldKey ("params" , p .Name ))
331
- }
332
- // Add parameter name to parameterNames, and to arrayParameterNames if type is array.
333
- parameterNames .Insert (p .Name )
334
- if p .Type == ParamTypeArray {
335
- arrayParameterNames .Insert (p .Name )
336
- }
337
-
338
- if p .Type == ParamTypeObject {
339
- for k := range p .Properties {
340
- objectParameterNameKeys [p .Name ] = append (objectParameterNameKeys [p .Name ], k )
341
- }
342
- }
343
- }
344
- if config .ValidateParameterVariablesAndWorkspaces (ctx ) {
345
- errs = errs .Also (validatePipelineParametersVariables (tasks , "params" , parameterNames , arrayParameterNames , objectParameterNameKeys ))
357
+ errs = errs .Also (params .validateNoDuplicateNames ())
358
+ for i , task := range tasks {
359
+ errs = errs .Also (task .Params .validateDuplicateParameters ().ViaField ("params" ).ViaIndex (i ))
346
360
}
347
361
return errs
348
362
}
349
-
350
363
func validatePipelineParametersVariables (tasks []PipelineTask , prefix string , paramNames sets.String , arrayParamNames sets.String , objectParamNameKeys map [string ][]string ) (errs * apis.FieldError ) {
351
364
for idx , task := range tasks {
352
365
errs = errs .Also (validatePipelineParametersVariablesInTaskParameters (task .Params , prefix , paramNames , arrayParamNames , objectParamNameKeys ).ViaIndex (idx ))
0 commit comments