@@ -41,11 +41,12 @@ import (
4141var _ = Describe ("Pod CPU Boost Webhook" , func () {
4242 Describe ("Handles admission requests" , func () {
4343 var (
44- mockCtrl * gomock.Controller
45- manager * mock.MockManager
46- managerCall * gomock.Call
47- pod * corev1.Pod
48- response webhook.AdmissionResponse
44+ mockCtrl * gomock.Controller
45+ manager * mock.MockManager
46+ managerCall * gomock.Call
47+ pod * corev1.Pod
48+ response webhook.AdmissionResponse
49+ removeLimits bool
4950 )
5051 BeforeEach (func () {
5152 pod = podTemplate .DeepCopy ()
@@ -72,7 +73,7 @@ var _ = Describe("Pod CPU Boost Webhook", func() {
7273 },
7374 },
7475 }
75- hook := bwebhook .NewPodCPUBoostWebHook (manager , scheme .Scheme )
76+ hook := bwebhook .NewPodCPUBoostWebHook (manager , scheme .Scheme , removeLimits )
7677 response = hook .Handle (context .TODO (), admissionReq )
7778 })
7879 When ("there is no matching Startup CPU Boost" , func () {
@@ -130,6 +131,7 @@ var _ = Describe("Pod CPU Boost Webhook", func() {
130131 resPolicyCallOne = boost .EXPECT ().ResourcePolicy (gomock .Eq (containerOneName )).Return (resPolicy , true )
131132 resPolicyCallTwo = boost .EXPECT ().ResourcePolicy (gomock .Eq (containerTwoName )).Return (nil , false )
132133 managerCall .Return (boost , true )
134+ removeLimits = true
133135 })
134136 It ("retrieves resource policy for containers" , func () {
135137 resPolicyCallOne .Times (1 )
@@ -162,10 +164,28 @@ var _ = Describe("Pod CPU Boost Webhook", func() {
162164 patch := containerResourcePatch (pod , resPolicy , "requests" , 0 )
163165 Expect (response .Patches ).To (ContainElement (patch ))
164166 })
165- It ("returns admission with container-one limits patch" , func () {
166- patch := containerResourcePatch ( pod , resPolicy , "limits" , 0 )
167+ It ("returns admission with container-one remove limits patch" , func () {
168+ patch := containerRemoveRequirementPatch ( "limits" , 0 )
167169 Expect (response .Patches ).To (ContainElement (patch ))
168170 })
171+ When ("container has memory limits set" , func () {
172+ BeforeEach (func () {
173+ pod .Spec .Containers [0 ].Resources .Limits [corev1 .ResourceMemory ] = apiResource .MustParse ("100Mi" )
174+ })
175+ It ("returns admission with container-one remove CPU limits patch" , func () {
176+ patch := containerRemoveCPURequirementPatch ("limits" , 0 )
177+ Expect (response .Patches ).To (ContainElement (patch ))
178+ })
179+ })
180+ When ("removeLimits is not set" , func () {
181+ BeforeEach (func () {
182+ removeLimits = false
183+ })
184+ It ("returns admission with container-one limits patch" , func () {
185+ patch := containerResourcePatch (pod , resPolicy , "limits" , 0 )
186+ Expect (response .Patches ).To (ContainElement (patch ))
187+ })
188+ })
169189 When ("container has no request and no limits set" , func () {
170190 BeforeEach (func () {
171191 pod .Spec .Containers [0 ].Resources .Requests = nil
@@ -294,3 +314,17 @@ func containerResourcePatch(pod *corev1.Pod, policy resource.ContainerPolicy, re
294314 Value : newQuantity .String (),
295315 }
296316}
317+
318+ func containerRemoveCPURequirementPatch (requirement string , containerIdx int ) jsonpatch.Operation {
319+ return jsonpatch.Operation {
320+ Operation : "remove" ,
321+ Path : fmt .Sprintf ("/spec/containers/%d/resources/%s/cpu" , containerIdx , requirement ),
322+ }
323+ }
324+
325+ func containerRemoveRequirementPatch (requirement string , containerIdx int ) jsonpatch.Operation {
326+ return jsonpatch.Operation {
327+ Operation : "remove" ,
328+ Path : fmt .Sprintf ("/spec/containers/%d/resources/%s" , containerIdx , requirement ),
329+ }
330+ }
0 commit comments