@@ -172,9 +172,8 @@ func warningsForPodSpecAndMeta(fieldPath *field.Path, podSpec *api.PodSpec, meta
172
172
}
173
173
}
174
174
175
- overlappingPathInContainers := warningsForOverlappingVirtualPaths (podSpec .Volumes )
176
- if len (overlappingPathInContainers ) > 0 {
177
- warnings = append (warnings , overlappingPathInContainers ... )
175
+ if overlaps := warningsForOverlappingVirtualPaths (podSpec .Volumes ); len (overlaps ) > 0 {
176
+ warnings = append (warnings , overlaps ... )
178
177
}
179
178
180
179
// duplicate hostAliases (#91670, #58477)
@@ -379,71 +378,65 @@ func warningsForWeightedPodAffinityTerms(terms []api.WeightedPodAffinityTerm, fi
379
378
//
380
379
// In such cases we either get `is directory` or 'file exists' error message.
381
380
func warningsForOverlappingVirtualPaths (volumes []api.Volume ) []string {
382
- warnings := make ( []string , 0 )
381
+ var warnings []string
383
382
384
383
for _ , v := range volumes {
385
384
if v .ConfigMap != nil && v .ConfigMap .Items != nil {
386
- w := checkVolumeMappingForOverlap (extractPaths (v .ConfigMap .Items ), fmt .Sprintf ("volume %q (ConfigMap %q): overlapping path" , v .Name , v .ConfigMap .Name ))
387
- if len (w ) > 0 {
388
- warnings = append (warnings , w ... )
389
- }
385
+ warnings = append (warnings , checkVolumeMappingForOverlap (extractPaths (v .ConfigMap .Items ), fmt .Sprintf ("volume %q (ConfigMap %q): overlapping paths" , v .Name , v .ConfigMap .Name ))... )
390
386
}
391
387
392
388
if v .Secret != nil && v .Secret .Items != nil {
393
- w := checkVolumeMappingForOverlap (extractPaths (v .Secret .Items ), fmt .Sprintf ("volume %q (Secret %q): overlapping path" , v .Name , v .Secret .SecretName ))
394
- if len (w ) > 0 {
395
- warnings = append (warnings , w ... )
396
- }
389
+ warnings = append (warnings , checkVolumeMappingForOverlap (extractPaths (v .Secret .Items ), fmt .Sprintf ("volume %q (Secret %q): overlapping paths" , v .Name , v .Secret .SecretName ))... )
397
390
}
398
391
399
392
if v .DownwardAPI != nil && v .DownwardAPI .Items != nil {
400
- w := checkVolumeMappingForOverlap (extractPathsDownwardAPI (v .DownwardAPI .Items ), fmt .Sprintf ("volume %q (DownwardAPI): overlapping path" , v .Name ))
401
- if len (w ) > 0 {
402
- warnings = append (warnings , w ... )
403
- }
393
+ warnings = append (warnings , checkVolumeMappingForOverlap (extractPathsDownwardAPI (v .DownwardAPI .Items ), fmt .Sprintf ("volume %q (DownwardAPI): overlapping paths" , v .Name ))... )
404
394
}
405
395
406
396
if v .Projected != nil {
407
- var sourcePaths , allPaths []string
397
+ var sourcePaths []string
408
398
var errorMessage string
399
+ allPaths := sets .New [string ]()
409
400
410
401
for _ , source := range v .Projected .Sources {
402
+ if source == (api.VolumeProjection {}) {
403
+ warnings = append (warnings , fmt .Sprintf ("volume %q (Projected) has no sources provided" , v .Name ))
404
+ continue
405
+ }
411
406
switch {
412
407
case source .ConfigMap != nil && source .ConfigMap .Items != nil :
413
408
sourcePaths = extractPaths (source .ConfigMap .Items )
414
- errorMessage = fmt .Sprintf ("volume %q (Projected ConfigMap %q): overlapping path " , v .Name , source .ConfigMap .Name )
409
+ errorMessage = fmt .Sprintf ("volume %q (Projected ConfigMap %q): overlapping paths " , v .Name , source .ConfigMap .Name )
415
410
case source .Secret != nil && source .Secret .Items != nil :
416
411
sourcePaths = extractPaths (source .Secret .Items )
417
- errorMessage = fmt .Sprintf ("volume %q (Projected Secret %q): overlapping path " , v .Name , source .Secret .Name )
412
+ errorMessage = fmt .Sprintf ("volume %q (Projected Secret %q): overlapping paths " , v .Name , source .Secret .Name )
418
413
case source .DownwardAPI != nil && source .DownwardAPI .Items != nil :
419
414
sourcePaths = extractPathsDownwardAPI (source .DownwardAPI .Items )
420
- errorMessage = fmt .Sprintf ("volume %q (Projected DownwardAPI): overlapping path " , v .Name )
415
+ errorMessage = fmt .Sprintf ("volume %q (Projected DownwardAPI): overlapping paths " , v .Name )
421
416
case source .ServiceAccountToken != nil :
422
417
sourcePaths = []string {source .ServiceAccountToken .Path }
423
- errorMessage = fmt .Sprintf ("volume %q (Projected ServiceAccountToken): overlapping path " , v .Name )
418
+ errorMessage = fmt .Sprintf ("volume %q (Projected ServiceAccountToken): overlapping paths " , v .Name )
424
419
case source .ClusterTrustBundle != nil :
425
420
sourcePaths = []string {source .ClusterTrustBundle .Path }
426
421
if source .ClusterTrustBundle .Name != nil {
427
- errorMessage = fmt .Sprintf ("volume %q (Projected ClusterTrustBundle %q): overlapping path " , v .Name , * source .ClusterTrustBundle .Name )
422
+ errorMessage = fmt .Sprintf ("volume %q (Projected ClusterTrustBundle %q): overlapping paths " , v .Name , * source .ClusterTrustBundle .Name )
428
423
} else {
429
- errorMessage = fmt .Sprintf ("volume %q (Projected ClusterTrustBundle %q): overlapping path " , v .Name , * source .ClusterTrustBundle .SignerName )
424
+ errorMessage = fmt .Sprintf ("volume %q (Projected ClusterTrustBundle %q): overlapping paths " , v .Name , * source .ClusterTrustBundle .SignerName )
430
425
}
431
426
}
432
427
433
428
if len (sourcePaths ) == 0 {
434
429
continue
435
430
}
436
431
437
- warningsInSource := checkVolumeMappingForOverlap (sourcePaths , errorMessage )
438
- if len (warningsInSource ) > 0 {
439
- warnings = append (warnings , warningsInSource ... )
440
- }
432
+ warnings = append (warnings , checkVolumeMappingForOverlap (sourcePaths , errorMessage )... )
441
433
442
- allPaths = append (allPaths , sourcePaths ... )
443
- warningsInAllPaths := checkVolumeMappingForOverlap (allPaths , errorMessage )
444
- if len (warningsInAllPaths ) > 0 {
445
- warnings = append (warnings , warningsInAllPaths ... )
446
- }
434
+ // remove duplicates path and sort the new array, so we can get predetermined result
435
+ uniqueSourcePaths := sets .New [string ](sourcePaths ... ).UnsortedList ()
436
+ orderedSourcePaths := append (uniqueSourcePaths , allPaths .UnsortedList ()... )
437
+ sort .Strings (orderedSourcePaths )
438
+ warnings = append (warnings , checkVolumeMappingForOverlap (orderedSourcePaths , errorMessage )... )
439
+ allPaths .Insert (uniqueSourcePaths ... )
447
440
}
448
441
}
449
442
@@ -470,36 +463,36 @@ func extractPathsDownwardAPI(mapping []api.DownwardAPIVolumeFile) []string {
470
463
}
471
464
472
465
func checkVolumeMappingForOverlap (paths []string , warningMessage string ) []string {
466
+ var warnings []string
473
467
pathSeparator := string (os .PathSeparator )
474
- warnings := make ([]string , 0 )
475
468
uniquePaths := sets .New [string ]()
476
469
477
470
for _ , path := range paths {
478
471
normalizedPath := strings .TrimRight (path , pathSeparator )
479
- if collision := checkForOverlap (uniquePaths , normalizedPath ); collision != nil {
480
- warnings = append (warnings , fmt .Sprintf ("%s %q with %q" , warningMessage , normalizedPath , * collision ))
472
+ if collision := checkForOverlap (uniquePaths , normalizedPath ); collision != "" {
473
+ warnings = append (warnings , fmt .Sprintf ("%s: %q with %q" , warningMessage , normalizedPath , collision ))
481
474
}
482
475
uniquePaths .Insert (normalizedPath )
483
476
}
484
477
485
478
return warnings
486
479
}
487
480
488
- func checkForOverlap (paths sets.Set [string ], path string ) * string {
481
+ func checkForOverlap (paths sets.Set [string ], path string ) string {
489
482
pathSeparator := string (os .PathSeparator )
490
- p := paths .UnsortedList ()
491
- sort .Strings (p )
492
483
493
- for _ , item := range p {
484
+ for item := range paths {
494
485
switch {
486
+ case item == "" || path == "" :
487
+ return ""
495
488
case item == path :
496
- return & item
489
+ return item
497
490
case strings .HasPrefix (item + pathSeparator , path ):
498
- return & item
491
+ return item
499
492
case strings .HasPrefix (path + pathSeparator , item ):
500
- return & item
493
+ return item
501
494
}
502
495
}
503
496
504
- return nil
497
+ return ""
505
498
}
0 commit comments