@@ -3083,8 +3083,33 @@ func validateContainersOnlyForPod(containers []core.Container, fldPath *field.Pa
3083
3083
return allErrs
3084
3084
}
3085
3085
3086
+ // PodValidationOptions contains the different settings for pod validation
3087
+ type PodValidationOptions struct {
3088
+ // Allow pod spec to have more than one huge page resource (with different sizes)
3089
+ AllowMultipleHugePageResources bool
3090
+ }
3091
+
3092
+ // ValidatePodSingleHugePageResources checks if there are multiple huge
3093
+ // pages resources in the pod object.
3094
+ func ValidatePodSingleHugePageResources (pod * core.Pod , specPath * field.Path ) field.ErrorList {
3095
+ allErrs := field.ErrorList {}
3096
+ hugePageResources := sets .NewString ()
3097
+ for i := range pod .Spec .Containers {
3098
+ resourceSet := toContainerResourcesSet (& pod .Spec .Containers [i ])
3099
+ for resourceStr := range resourceSet {
3100
+ if v1helper .IsHugePageResourceName (v1 .ResourceName (resourceStr )) {
3101
+ hugePageResources .Insert (resourceStr )
3102
+ }
3103
+ }
3104
+ }
3105
+ if len (hugePageResources ) > 1 {
3106
+ allErrs = append (allErrs , field .Invalid (specPath , hugePageResources .List (), "must use a single hugepage size in a pod spec" ))
3107
+ }
3108
+ return allErrs
3109
+ }
3110
+
3086
3111
// ValidatePod tests if required fields in the pod are set.
3087
- func ValidatePod (pod * core.Pod ) field.ErrorList {
3112
+ func ValidatePod (pod * core.Pod , opts PodValidationOptions ) field.ErrorList {
3088
3113
fldPath := field .NewPath ("metadata" )
3089
3114
allErrs := ValidateObjectMeta (& pod .ObjectMeta , true , ValidatePodName , fldPath )
3090
3115
allErrs = append (allErrs , ValidatePodSpecificAnnotations (pod .ObjectMeta .Annotations , & pod .Spec , fldPath .Child ("annotations" ))... )
@@ -3111,17 +3136,8 @@ func ValidatePod(pod *core.Pod) field.ErrorList {
3111
3136
allErrs = append (allErrs , validateContainersOnlyForPod (pod .Spec .Containers , specPath .Child ("containers" ))... )
3112
3137
allErrs = append (allErrs , validateContainersOnlyForPod (pod .Spec .InitContainers , specPath .Child ("initContainers" ))... )
3113
3138
3114
- hugePageResources := sets .NewString ()
3115
- for i := range pod .Spec .Containers {
3116
- resourceSet := toContainerResourcesSet (& pod .Spec .Containers [i ])
3117
- for resourceStr := range resourceSet {
3118
- if v1helper .IsHugePageResourceName (v1 .ResourceName (resourceStr )) {
3119
- hugePageResources .Insert (resourceStr )
3120
- }
3121
- }
3122
- }
3123
- if len (hugePageResources ) > 1 {
3124
- allErrs = append (allErrs , field .Invalid (specPath , hugePageResources , "must use a single hugepage size in a pod spec" ))
3139
+ if ! opts .AllowMultipleHugePageResources {
3140
+ allErrs = append (allErrs , ValidatePodSingleHugePageResources (pod , specPath )... )
3125
3141
}
3126
3142
3127
3143
podIPsField := field .NewPath ("status" , "podIPs" )
@@ -3679,8 +3695,8 @@ func ValidateContainerUpdates(newContainers, oldContainers []core.Container, fld
3679
3695
}
3680
3696
3681
3697
// ValidatePodCreate validates a pod in the context of its initial create
3682
- func ValidatePodCreate (pod * core.Pod ) field.ErrorList {
3683
- allErrs := ValidatePod (pod )
3698
+ func ValidatePodCreate (pod * core.Pod , opts PodValidationOptions ) field.ErrorList {
3699
+ allErrs := ValidatePod (pod , opts )
3684
3700
3685
3701
fldPath := field .NewPath ("spec" )
3686
3702
// EphemeralContainers can only be set on update using the ephemeralcontainers subresource
@@ -3693,12 +3709,16 @@ func ValidatePodCreate(pod *core.Pod) field.ErrorList {
3693
3709
3694
3710
// ValidatePodUpdate tests to see if the update is legal for an end user to make. newPod is updated with fields
3695
3711
// that cannot be changed.
3696
- func ValidatePodUpdate (newPod , oldPod * core.Pod ) field.ErrorList {
3712
+ func ValidatePodUpdate (newPod , oldPod * core.Pod , opts PodValidationOptions ) field.ErrorList {
3697
3713
fldPath := field .NewPath ("metadata" )
3698
3714
allErrs := ValidateObjectMetaUpdate (& newPod .ObjectMeta , & oldPod .ObjectMeta , fldPath )
3699
3715
allErrs = append (allErrs , ValidatePodSpecificAnnotationUpdates (newPod , oldPod , fldPath .Child ("annotations" ))... )
3700
3716
specPath := field .NewPath ("spec" )
3701
3717
3718
+ if ! opts .AllowMultipleHugePageResources {
3719
+ allErrs = append (allErrs , ValidatePodSingleHugePageResources (newPod , specPath )... )
3720
+ }
3721
+
3702
3722
// validate updateable fields:
3703
3723
// 1. spec.containers[*].image
3704
3724
// 2. spec.initContainers[*].image
0 commit comments