@@ -366,6 +366,7 @@ func missingKeysofObjectResults(tr *v1beta1.TaskRun, specResults []v1beta1.TaskR
366
366
return findMissingKeys (neededKeys , providedKeys )
367
367
}
368
368
369
+ // validateParamArrayIndex validate if the array indexing param reference target is existent
369
370
func validateParamArrayIndex (ctx context.Context , params []v1beta1.Param , spec * v1beta1.TaskSpec ) error {
370
371
cfg := config .FromContextOrDefaults (ctx )
371
372
if cfg .FeatureFlags .EnableAPIFields != config .AlphaAPIFields {
@@ -377,7 +378,25 @@ func validateParamArrayIndex(ctx context.Context, params []v1beta1.Param, spec *
377
378
defaults = append (defaults , spec .Params ... )
378
379
}
379
380
// Collect all array params
380
- arrayParams := make (map [string ]int )
381
+ arrayParams := ExtractParamArrayLengths (defaults , params )
382
+
383
+ // extract all array indexing references
384
+ arrayIndexingParams := []string {}
385
+ extractStepsParamArrayIndexing (spec .Steps , arrayParams , & arrayIndexingParams )
386
+ extractStepsTemplateParamArrayIndexing (spec .StepTemplate , arrayParams , & arrayIndexingParams )
387
+ extractVolumesParamArrayIndexing (spec .Volumes , arrayParams , & arrayIndexingParams )
388
+ for _ , v := range spec .Workspaces {
389
+ ExtractArrayIndexingParamReference (v .MountPath , arrayParams , & arrayIndexingParams )
390
+ }
391
+ extractSidecarsParamArrayIndexing (spec .Sidecars , arrayParams , & arrayIndexingParams )
392
+
393
+ return ValidateOutofBoundArrayParams (arrayIndexingParams , arrayParams )
394
+ }
395
+
396
+ // ExtractParamArrayLengths extract and return the lengths of all array params
397
+ func ExtractParamArrayLengths (defaults []v1beta1.ParamSpec , params []v1beta1.Param ) map [string ]int {
398
+ // Collect all array params
399
+ arrayParamsLengths := make (map [string ]int )
381
400
382
401
patterns := []string {
383
402
"$(params.%s)" ,
@@ -391,173 +410,171 @@ func validateParamArrayIndex(ctx context.Context, params []v1beta1.Param, spec *
391
410
if p .Default .Type == v1beta1 .ParamTypeArray {
392
411
for _ , pattern := range patterns {
393
412
for i := 0 ; i < len (p .Default .ArrayVal ); i ++ {
394
- arrayParams [fmt .Sprintf (pattern , p .Name )] = len (p .Default .ArrayVal )
413
+ arrayParamsLengths [fmt .Sprintf (pattern , p .Name )] = len (p .Default .ArrayVal )
395
414
}
396
415
}
397
416
}
398
417
}
399
418
}
400
419
401
- // Collect array params lengths from pipeline
420
+ // Collect array params lengths from params
402
421
for _ , p := range params {
403
422
if p .Value .Type == v1beta1 .ParamTypeArray {
404
423
for _ , pattern := range patterns {
405
424
for i := 0 ; i < len (p .Value .ArrayVal ); i ++ {
406
- arrayParams [fmt .Sprintf (pattern , p .Name )] = len (p .Value .ArrayVal )
425
+ arrayParamsLengths [fmt .Sprintf (pattern , p .Name )] = len (p .Value .ArrayVal )
407
426
}
408
427
}
409
428
}
410
429
}
430
+ return arrayParamsLengths
431
+ }
411
432
433
+ // ValidateOutofBoundArrayParams validates if the array indexing params are out of bound
434
+ func ValidateOutofBoundArrayParams (arrayIndexingParams []string , arrayParams map [string ]int ) error {
412
435
outofBoundParams := sets.String {}
413
-
414
- // Validate array param in steps fields.
415
- validateStepsParamArrayIndexing (spec .Steps , arrayParams , & outofBoundParams )
416
-
417
- // Validate array param in StepTemplate fields.
418
- validateStepsTemplateParamArrayIndexing (spec .StepTemplate , arrayParams , & outofBoundParams )
419
-
420
- // Validate array param in build's volumes
421
- validateVolumesParamArrayIndexing (spec .Volumes , arrayParams , & outofBoundParams )
422
-
423
- for _ , v := range spec .Workspaces {
424
- extractParamIndex (v .MountPath , arrayParams , & outofBoundParams )
436
+ for _ , val := range arrayIndexingParams {
437
+ indexString := substitution .ExtractIndexString (val )
438
+ idx , _ := substitution .ExtractIndex (indexString )
439
+ v := substitution .TrimArrayIndex (val )
440
+ if paramLength , ok := arrayParams [v ]; ok {
441
+ if idx >= paramLength {
442
+ outofBoundParams .Insert (val )
443
+ }
444
+ }
425
445
}
426
-
427
- validateSidecarsParamArrayIndexing (spec .Sidecars , arrayParams , & outofBoundParams )
428
-
429
446
if outofBoundParams .Len () > 0 {
430
447
return fmt .Errorf ("non-existent param references:%v" , outofBoundParams .List ())
431
448
}
432
-
433
449
return nil
434
450
}
435
451
436
- func extractParamIndex (paramReference string , arrayParams map [string ]int , outofBoundParams * sets. String ) {
452
+ func ExtractArrayIndexingParamReference (paramReference string , arrayParams map [string ]int , arrayIndexingParams * [] string ) {
437
453
list := substitution .ExtractParamsExpressions (paramReference )
438
454
for _ , val := range list {
439
- indexString := substitution .ExtractIndexString (paramReference )
440
- idx , _ := substitution .ExtractIndex (indexString )
441
- v := substitution .TrimArrayIndex (val )
442
- if paramLength , ok := arrayParams [v ]; ok {
443
- if idx >= paramLength {
444
- outofBoundParams .Insert (val )
445
- }
455
+ indexString := substitution .ExtractIndexString (val )
456
+ if indexString != "" {
457
+ * arrayIndexingParams = append (* arrayIndexingParams , val )
446
458
}
447
459
}
448
460
}
449
461
450
- func validateStepsParamArrayIndexing (steps []v1beta1.Step , arrayParams map [string ]int , outofBoundParams * sets.String ) {
462
+ // extractStepsParamArrayIndexing get all array indexing references from steps
463
+ func extractStepsParamArrayIndexing (steps []v1beta1.Step , arrayParams map [string ]int , arrayIndexingParams * []string ) {
451
464
for _ , step := range steps {
452
- extractParamIndex (step .Script , arrayParams , outofBoundParams )
465
+ ExtractArrayIndexingParamReference (step .Script , arrayParams , arrayIndexingParams )
453
466
container := step .ToK8sContainer ()
454
- validateContainerParamArrayIndexing (container , arrayParams , outofBoundParams )
467
+ extractContainerParamArrayIndexing (container , arrayParams , arrayIndexingParams )
455
468
}
456
469
}
457
470
458
- func validateStepsTemplateParamArrayIndexing (stepTemplate * v1beta1.StepTemplate , arrayParams map [string ]int , outofBoundParams * sets.String ) {
471
+ // extractStepsTemplateParamArrayIndexing get all array indexing references from StepsTemplate
472
+ func extractStepsTemplateParamArrayIndexing (stepTemplate * v1beta1.StepTemplate , arrayParams map [string ]int , arrayIndexingParams * []string ) {
459
473
if stepTemplate == nil {
460
474
return
461
475
}
462
476
container := stepTemplate .ToK8sContainer ()
463
- validateContainerParamArrayIndexing (container , arrayParams , outofBoundParams )
477
+ extractContainerParamArrayIndexing (container , arrayParams , arrayIndexingParams )
464
478
}
465
479
466
- func validateSidecarsParamArrayIndexing (sidecars []v1beta1.Sidecar , arrayParams map [string ]int , outofBoundParams * sets.String ) {
480
+ // extractSidecarsParamArrayIndexing get all array indexing references from sidecars
481
+ func extractSidecarsParamArrayIndexing (sidecars []v1beta1.Sidecar , arrayParams map [string ]int , arrayIndexingParams * []string ) {
467
482
for _ , s := range sidecars {
468
- extractParamIndex (s .Script , arrayParams , outofBoundParams )
483
+ ExtractArrayIndexingParamReference (s .Script , arrayParams , arrayIndexingParams )
469
484
container := s .ToK8sContainer ()
470
- validateContainerParamArrayIndexing (container , arrayParams , outofBoundParams )
485
+ extractContainerParamArrayIndexing (container , arrayParams , arrayIndexingParams )
471
486
}
472
487
}
473
488
474
- func validateVolumesParamArrayIndexing (volumes []corev1.Volume , arrayParams map [string ]int , outofBoundParams * sets.String ) {
489
+ // extractVolumesParamArrayIndexing get all array indexing references from volumes
490
+ func extractVolumesParamArrayIndexing (volumes []corev1.Volume , arrayParams map [string ]int , arrayIndexingParams * []string ) {
475
491
for i , v := range volumes {
476
- extractParamIndex (v .Name , arrayParams , outofBoundParams )
492
+ ExtractArrayIndexingParamReference (v .Name , arrayParams , arrayIndexingParams )
477
493
if v .VolumeSource .ConfigMap != nil {
478
- extractParamIndex (v .ConfigMap .Name , arrayParams , outofBoundParams )
494
+ ExtractArrayIndexingParamReference (v .ConfigMap .Name , arrayParams , arrayIndexingParams )
479
495
for _ , item := range v .ConfigMap .Items {
480
- extractParamIndex (item .Key , arrayParams , outofBoundParams )
481
- extractParamIndex (item .Path , arrayParams , outofBoundParams )
496
+ ExtractArrayIndexingParamReference (item .Key , arrayParams , arrayIndexingParams )
497
+ ExtractArrayIndexingParamReference (item .Path , arrayParams , arrayIndexingParams )
482
498
}
483
499
}
484
500
if v .VolumeSource .Secret != nil {
485
- extractParamIndex (v .Secret .SecretName , arrayParams , outofBoundParams )
501
+ ExtractArrayIndexingParamReference (v .Secret .SecretName , arrayParams , arrayIndexingParams )
486
502
for _ , item := range v .Secret .Items {
487
- extractParamIndex (item .Key , arrayParams , outofBoundParams )
488
- extractParamIndex (item .Path , arrayParams , outofBoundParams )
503
+ ExtractArrayIndexingParamReference (item .Key , arrayParams , arrayIndexingParams )
504
+ ExtractArrayIndexingParamReference (item .Path , arrayParams , arrayIndexingParams )
489
505
}
490
506
}
491
507
if v .PersistentVolumeClaim != nil {
492
- extractParamIndex (v .PersistentVolumeClaim .ClaimName , arrayParams , outofBoundParams )
508
+ ExtractArrayIndexingParamReference (v .PersistentVolumeClaim .ClaimName , arrayParams , arrayIndexingParams )
493
509
}
494
510
if v .Projected != nil {
495
511
for _ , s := range volumes [i ].Projected .Sources {
496
512
if s .ConfigMap != nil {
497
- extractParamIndex (s .ConfigMap .Name , arrayParams , outofBoundParams )
513
+ ExtractArrayIndexingParamReference (s .ConfigMap .Name , arrayParams , arrayIndexingParams )
498
514
}
499
515
if s .Secret != nil {
500
- extractParamIndex (s .Secret .Name , arrayParams , outofBoundParams )
516
+ ExtractArrayIndexingParamReference (s .Secret .Name , arrayParams , arrayIndexingParams )
501
517
}
502
518
if s .ServiceAccountToken != nil {
503
- extractParamIndex (s .ServiceAccountToken .Audience , arrayParams , outofBoundParams )
519
+ ExtractArrayIndexingParamReference (s .ServiceAccountToken .Audience , arrayParams , arrayIndexingParams )
504
520
}
505
521
}
506
522
}
507
523
if v .CSI != nil {
508
524
if v .CSI .NodePublishSecretRef != nil {
509
- extractParamIndex (v .CSI .NodePublishSecretRef .Name , arrayParams , outofBoundParams )
525
+ ExtractArrayIndexingParamReference (v .CSI .NodePublishSecretRef .Name , arrayParams , arrayIndexingParams )
510
526
}
511
527
if v .CSI .VolumeAttributes != nil {
512
528
for _ , value := range v .CSI .VolumeAttributes {
513
- extractParamIndex (value , arrayParams , outofBoundParams )
529
+ ExtractArrayIndexingParamReference (value , arrayParams , arrayIndexingParams )
514
530
}
515
531
}
516
532
}
517
533
}
518
534
}
519
535
520
- func validateContainerParamArrayIndexing (c * corev1.Container , arrayParams map [string ]int , outofBoundParams * sets.String ) {
521
- extractParamIndex (c .Name , arrayParams , outofBoundParams )
522
- extractParamIndex (c .Image , arrayParams , outofBoundParams )
523
- extractParamIndex (string (c .ImagePullPolicy ), arrayParams , outofBoundParams )
536
+ // extractContainerParamArrayIndexing get all array indexing references from container
537
+ func extractContainerParamArrayIndexing (c * corev1.Container , arrayParams map [string ]int , arrayIndexingParams * []string ) {
538
+ ExtractArrayIndexingParamReference (c .Name , arrayParams , arrayIndexingParams )
539
+ ExtractArrayIndexingParamReference (c .Image , arrayParams , arrayIndexingParams )
540
+ ExtractArrayIndexingParamReference (string (c .ImagePullPolicy ), arrayParams , arrayIndexingParams )
524
541
525
542
for _ , a := range c .Args {
526
- extractParamIndex (a , arrayParams , outofBoundParams )
543
+ ExtractArrayIndexingParamReference (a , arrayParams , arrayIndexingParams )
527
544
}
528
545
529
546
for ie , e := range c .Env {
530
- extractParamIndex (e .Value , arrayParams , outofBoundParams )
547
+ ExtractArrayIndexingParamReference (e .Value , arrayParams , arrayIndexingParams )
531
548
if c .Env [ie ].ValueFrom != nil {
532
549
if e .ValueFrom .SecretKeyRef != nil {
533
- extractParamIndex (e .ValueFrom .SecretKeyRef .LocalObjectReference .Name , arrayParams , outofBoundParams )
534
- extractParamIndex (e .ValueFrom .SecretKeyRef .Key , arrayParams , outofBoundParams )
550
+ ExtractArrayIndexingParamReference (e .ValueFrom .SecretKeyRef .LocalObjectReference .Name , arrayParams , arrayIndexingParams )
551
+ ExtractArrayIndexingParamReference (e .ValueFrom .SecretKeyRef .Key , arrayParams , arrayIndexingParams )
535
552
}
536
553
if e .ValueFrom .ConfigMapKeyRef != nil {
537
- extractParamIndex (e .ValueFrom .ConfigMapKeyRef .LocalObjectReference .Name , arrayParams , outofBoundParams )
538
- extractParamIndex (e .ValueFrom .ConfigMapKeyRef .Key , arrayParams , outofBoundParams )
554
+ ExtractArrayIndexingParamReference (e .ValueFrom .ConfigMapKeyRef .LocalObjectReference .Name , arrayParams , arrayIndexingParams )
555
+ ExtractArrayIndexingParamReference (e .ValueFrom .ConfigMapKeyRef .Key , arrayParams , arrayIndexingParams )
539
556
}
540
557
}
541
558
}
542
559
543
560
for _ , e := range c .EnvFrom {
544
- extractParamIndex (e .Prefix , arrayParams , outofBoundParams )
561
+ ExtractArrayIndexingParamReference (e .Prefix , arrayParams , arrayIndexingParams )
545
562
if e .ConfigMapRef != nil {
546
- extractParamIndex (e .ConfigMapRef .LocalObjectReference .Name , arrayParams , outofBoundParams )
563
+ ExtractArrayIndexingParamReference (e .ConfigMapRef .LocalObjectReference .Name , arrayParams , arrayIndexingParams )
547
564
}
548
565
if e .SecretRef != nil {
549
- extractParamIndex (e .SecretRef .LocalObjectReference .Name , arrayParams , outofBoundParams )
566
+ ExtractArrayIndexingParamReference (e .SecretRef .LocalObjectReference .Name , arrayParams , arrayIndexingParams )
550
567
}
551
568
}
552
569
553
- extractParamIndex (c .WorkingDir , arrayParams , outofBoundParams )
570
+ ExtractArrayIndexingParamReference (c .WorkingDir , arrayParams , arrayIndexingParams )
554
571
for _ , cc := range c .Command {
555
- extractParamIndex (cc , arrayParams , outofBoundParams )
572
+ ExtractArrayIndexingParamReference (cc , arrayParams , arrayIndexingParams )
556
573
}
557
574
558
575
for _ , v := range c .VolumeMounts {
559
- extractParamIndex (v .Name , arrayParams , outofBoundParams )
560
- extractParamIndex (v .MountPath , arrayParams , outofBoundParams )
561
- extractParamIndex (v .SubPath , arrayParams , outofBoundParams )
576
+ ExtractArrayIndexingParamReference (v .Name , arrayParams , arrayIndexingParams )
577
+ ExtractArrayIndexingParamReference (v .MountPath , arrayParams , arrayIndexingParams )
578
+ ExtractArrayIndexingParamReference (v .SubPath , arrayParams , arrayIndexingParams )
562
579
}
563
580
}
0 commit comments