@@ -158,6 +158,12 @@ func validLimitRangeNoDefaults() corev1.LimitRange {
158158 return externalLimitRange
159159}
160160
161+ func validPodWithPodLevelResources (name string , numContainers int , containerResources api.ResourceRequirements , podResources api.ResourceRequirements ) api.Pod {
162+ pod := validPod (name , numContainers , containerResources )
163+ pod .Spec .Resources = & podResources
164+ return pod
165+ }
166+
161167func validPod (name string , numContainers int , resources api.ResourceRequirements ) api.Pod {
162168 pod := api.Pod {
163169 ObjectMeta : metav1.ObjectMeta {Name : name , Namespace : "test" },
@@ -280,8 +286,9 @@ func TestMergePodResourceRequirements(t *testing.T) {
280286
281287func TestPodLimitFunc (t * testing.T ) {
282288 type testCase struct {
283- pod api.Pod
284- limitRange corev1.LimitRange
289+ pod api.Pod
290+ limitRange corev1.LimitRange
291+ podLevelResourcesEnabled bool
285292 }
286293
287294 successCases := []testCase {
@@ -453,17 +460,42 @@ func TestPodLimitFunc(t *testing.T) {
453460 pod : validPod ("pod-max-local-ephemeral-storage-ratio" , 3 , getResourceRequirements (getLocalStorageResourceList ("300Mi" ), getLocalStorageResourceList ("450Mi" ))),
454461 limitRange : createLimitRange (api .LimitTypePod , api.ResourceList {}, getLocalStorageResourceList ("2Gi" ), api.ResourceList {}, api.ResourceList {}, getLocalStorageResourceList ("1.5" )),
455462 },
463+ {
464+ pod : validPodWithPodLevelResources ("pod-level-resources-with-min-max" , 3 , getResourceRequirements (getComputeResourceList ("100m" , "60Mi" ), getComputeResourceList ("200m" , "100Mi" )),
465+ getResourceRequirements (getComputeResourceList ("200m" , "180Mi" ), getComputeResourceList ("400m" , "200Mi" )),
466+ ),
467+ limitRange : createLimitRange (api .LimitTypePod , api.ResourceList {}, getComputeResourceList ("400m" , "200Mi" ), api.ResourceList {}, api.ResourceList {}, api.ResourceList {}),
468+ podLevelResourcesEnabled : true ,
469+ },
470+ {
471+ pod : validPodWithPodLevelResources ("pod-level-requests-with-min" , 3 , getResourceRequirements (getComputeResourceList ("50m" , "60Mi" ), getComputeResourceList ("" , "" )),
472+ getResourceRequirements (getComputeResourceList ("160m" , "200Mi" ), getComputeResourceList ("" , "" )),
473+ ),
474+ limitRange : createLimitRange (api .LimitTypePod , getComputeResourceList ("160m" , "200Mi" ), getComputeResourceList ("" , "" ), api.ResourceList {}, api.ResourceList {}, api.ResourceList {}),
475+ podLevelResourcesEnabled : true ,
476+ },
477+ {
478+ pod : validPodWithPodLevelResources ("pod-level-limits-with-max" , 3 , getResourceRequirements (getComputeResourceList ("" , "" ), getComputeResourceList ("50m" , "60Mi" )),
479+ getResourceRequirements (getComputeResourceList ("" , "" ), getComputeResourceList ("160m" , "200Mi" )),
480+ ),
481+ limitRange : createLimitRange (api .LimitTypePod , getComputeResourceList ("" , "" ), getComputeResourceList ("160m" , "200Mi" ), api.ResourceList {}, api.ResourceList {}, api.ResourceList {}),
482+ podLevelResourcesEnabled : true ,
483+ },
456484 }
457485 for i := range successCases {
458486 test := successCases [i ]
459- err := PodMutateLimitFunc (& test .limitRange , & test .pod )
460- if err != nil {
461- t .Errorf ("Unexpected error for pod: %s, %v" , test .pod .Name , err )
462- }
463- err = PodValidateLimitFunc (& test .limitRange , & test .pod )
464- if err != nil {
465- t .Errorf ("Unexpected error for pod: %s, %v" , test .pod .Name , err )
466- }
487+ t .Run (test .pod .Name , func (t * testing.T ) {
488+ featuregatetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .PodLevelResources , test .podLevelResourcesEnabled )
489+ err := PodMutateLimitFunc (& test .limitRange , & test .pod )
490+ if err != nil {
491+ t .Errorf ("Unexpected error for pod: %s, %v" , test .pod .Name , err )
492+ }
493+
494+ err = PodValidateLimitFunc (& test .limitRange , & test .pod )
495+ if err != nil {
496+ t .Errorf ("Unexpected error for pod: %s, %v" , test .pod .Name , err )
497+ }
498+ })
467499 }
468500
469501 errorCases := []testCase {
@@ -641,18 +673,41 @@ func TestPodLimitFunc(t *testing.T) {
641673 pod : withRestartableInitContainer (getComputeResourceList ("1500m" , "" ), api.ResourceList {},
642674 validPod ("ctr-max-cpu-limit-restartable-init-container" , 1 , getResourceRequirements (getComputeResourceList ("1000m" , "" ), getComputeResourceList ("1500m" , "" )))),
643675 limitRange : createLimitRange (api .LimitTypePod , api.ResourceList {}, getComputeResourceList ("2" , "" ), api.ResourceList {}, api.ResourceList {}, api.ResourceList {}),
676+ }, {
677+ pod : validPodWithPodLevelResources ("pod-level-resources-exceeding-max" , 3 , getResourceRequirements (getComputeResourceList ("100m" , "60Mi" ), getComputeResourceList ("200m" , "100Mi" )),
678+ getResourceRequirements (getComputeResourceList ("200m" , "180Mi" ), getComputeResourceList ("500m" , "280Mi" )),
679+ ),
680+ limitRange : createLimitRange (api .LimitTypePod , api.ResourceList {}, getComputeResourceList ("400m" , "200Mi" ), api.ResourceList {}, api.ResourceList {}, api.ResourceList {}),
681+ podLevelResourcesEnabled : true ,
682+ },
683+ {
684+ pod : validPodWithPodLevelResources ("pod-level-requests-less-than-min" , 3 , getResourceRequirements (getComputeResourceList ("50m" , "60Mi" ), getComputeResourceList ("" , "" )),
685+ getResourceRequirements (getComputeResourceList ("100m" , "200Mi" ), getComputeResourceList ("" , "" )),
686+ ),
687+ limitRange : createLimitRange (api .LimitTypePod , getComputeResourceList ("160m" , "200Mi" ), getComputeResourceList ("" , "" ), api.ResourceList {}, api.ResourceList {}, api.ResourceList {}),
688+ podLevelResourcesEnabled : true ,
689+ },
690+ {
691+ pod : validPodWithPodLevelResources ("pod-level-limits-exceeding-max" , 3 , getResourceRequirements (getComputeResourceList ("" , "" ), getComputeResourceList ("50m" , "60Mi" )),
692+ getResourceRequirements (getComputeResourceList ("" , "" ), getComputeResourceList ("160m" , "300Mi" )),
693+ ),
694+ limitRange : createLimitRange (api .LimitTypePod , getComputeResourceList ("" , "" ), getComputeResourceList ("160m" , "200Mi" ), api.ResourceList {}, api.ResourceList {}, api.ResourceList {}),
695+ podLevelResourcesEnabled : true ,
644696 },
645697 }
646698 for i := range errorCases {
647699 test := errorCases [i ]
648- err := PodMutateLimitFunc (& test .limitRange , & test .pod )
649- if err != nil {
650- t .Errorf ("Unexpected error for pod: %s, %v" , test .pod .Name , err )
651- }
652- err = PodValidateLimitFunc (& test .limitRange , & test .pod )
653- if err == nil {
654- t .Errorf ("Expected error for pod: %s" , test .pod .Name )
655- }
700+ t .Run (test .pod .Name , func (t * testing.T ) {
701+ featuregatetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .PodLevelResources , test .podLevelResourcesEnabled )
702+ err := PodMutateLimitFunc (& test .limitRange , & test .pod )
703+ if err != nil {
704+ t .Errorf ("Unexpected error for pod: %s, %v" , test .pod .Name , err )
705+ }
706+ err = PodValidateLimitFunc (& test .limitRange , & test .pod )
707+ if err == nil {
708+ t .Errorf ("Expected error for pod: %s" , test .pod .Name )
709+ }
710+ })
656711 }
657712}
658713
0 commit comments