@@ -21,88 +21,46 @@ import (
2121 . "github.com/onsi/ginkgo/v2"
2222 . "github.com/onsi/gomega"
2323 corev1 "k8s.io/api/core/v1"
24- apiResource "k8s.io/apimachinery/pkg/api/resource"
25- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2624)
2725
2826var _ = Describe ("Pod" , func () {
29- var pod * corev1. Pod
30- var annot * bpod.BoostPodAnnotation
31- var containerOne , containerTwo string
32- var reqQuantity , limitQuantity apiResource. Quantity
33- var err error
27+ var (
28+ annot * bpod.BoostPodAnnotation
29+ pod * corev1. Pod
30+ err error
31+ )
3432
3533 BeforeEach (func () {
36- containerOne = "container-one"
37- containerTwo = "container-one"
38- reqQuantity , err = apiResource .ParseQuantity ("1" )
39- Expect (err ).ShouldNot (HaveOccurred ())
40- limitQuantity , err = apiResource .ParseQuantity ("2" )
41- Expect (err ).ShouldNot (HaveOccurred ())
4234 annot = & bpod.BoostPodAnnotation {
4335 BoostTimestamp : time .Now (),
4436 InitCPURequests : map [string ]string {
45- containerOne : "500m" ,
46- containerTwo : "500m" ,
37+ containerOneName : "500m" ,
38+ containerTwoName : "500m" ,
4739 },
4840 InitCPULimits : map [string ]string {
49- containerOne : "1" ,
50- containerTwo : "1" ,
41+ containerOneName : "1" ,
42+ containerTwoName : "1" ,
5143 },
5244 }
53- pod = & corev1.Pod {
54- ObjectMeta : metav1.ObjectMeta {
55- Name : "test-pod" ,
56- Labels : map [string ]string {
57- bpod .BoostLabelKey : "boost-001" ,
58- },
59- Annotations : map [string ]string {
60- bpod .BoostAnnotationKey : annot .ToJSON (),
61- },
62- },
63- Spec : corev1.PodSpec {
64- Containers : []corev1.Container {
65- {
66- Name : containerOne ,
67- Resources : corev1.ResourceRequirements {
68- Requests : corev1.ResourceList {
69- corev1 .ResourceCPU : reqQuantity ,
70- },
71- Limits : corev1.ResourceList {
72- corev1 .ResourceCPU : limitQuantity ,
73- },
74- },
75- },
76- {
77- Name : containerTwo ,
78- Resources : corev1.ResourceRequirements {
79- Requests : corev1.ResourceList {
80- corev1 .ResourceCPU : reqQuantity ,
81- },
82- Limits : corev1.ResourceList {
83- corev1 .ResourceCPU : limitQuantity ,
84- },
85- },
86- },
87- },
88- },
45+ pod = podTemplate .DeepCopy ()
46+ pod .Annotations = map [string ]string {
47+ bpod .BoostAnnotationKey : annot .ToJSON (),
8948 }
9049 })
9150
9251 Describe ("Reverts the POD container resources to original values" , func () {
52+ JustBeforeEach (func () {
53+ err = bpod .RevertResourceBoost (pod )
54+ })
9355 When ("POD is missing startup-cpu-boost annotation" , func () {
9456 BeforeEach (func () {
9557 delete (pod .ObjectMeta .Annotations , bpod .BoostAnnotationKey )
96- err = bpod .RevertResourceBoost (pod )
9758 })
9859 It ("errors" , func () {
9960 Expect (err ).Should (HaveOccurred ())
10061 })
10162 })
10263 When ("POD has valid startup-cpu-boost annotation" , func () {
103- BeforeEach (func () {
104- err = bpod .RevertResourceBoost (pod )
105- })
10664 It ("does not error" , func () {
10765 Expect (err ).ShouldNot (HaveOccurred ())
10866 })
@@ -115,14 +73,41 @@ var _ = Describe("Pod", func() {
11573 It ("reverts CPU requests to initial values" , func () {
11674 cpuReqOne := pod .Spec .Containers [0 ].Resources .Requests [corev1 .ResourceCPU ]
11775 cpuReqTwo := pod .Spec .Containers [1 ].Resources .Requests [corev1 .ResourceCPU ]
118- Expect (cpuReqOne .String ()).Should (Equal (annot .InitCPURequests [containerOne ]))
119- Expect (cpuReqTwo .String ()).Should (Equal (annot .InitCPURequests [containerTwo ]))
76+ Expect (cpuReqOne .String ()).Should (Equal (annot .InitCPURequests [containerOneName ]))
77+ Expect (cpuReqTwo .String ()).Should (Equal (annot .InitCPURequests [containerTwoName ]))
12078 })
12179 It ("reverts CPU limits to initial values" , func () {
12280 cpuReqOne := pod .Spec .Containers [0 ].Resources .Limits [corev1 .ResourceCPU ]
12381 cpuReqTwo := pod .Spec .Containers [1 ].Resources .Limits [corev1 .ResourceCPU ]
124- Expect (cpuReqOne .String ()).Should (Equal (annot .InitCPULimits [containerOne ]))
125- Expect (cpuReqTwo .String ()).Should (Equal (annot .InitCPULimits [containerTwo ]))
82+ Expect (cpuReqOne .String ()).Should (Equal (annot .InitCPULimits [containerOneName ]))
83+ Expect (cpuReqTwo .String ()).Should (Equal (annot .InitCPULimits [containerTwoName ]))
84+ })
85+ When ("Limits were removed during boost" , func () {
86+ BeforeEach (func () {
87+ pod .Spec .Containers [0 ].Resources .Limits = nil
88+ pod .Spec .Containers [1 ].Resources .Limits = nil
89+ })
90+ It ("does not error" , func () {
91+ Expect (err ).ShouldNot (HaveOccurred ())
92+ })
93+ It ("removes startup-cpu-boost label" , func () {
94+ Expect (pod .Labels ).NotTo (HaveKey (bpod .BoostLabelKey ))
95+ })
96+ It ("removes startup-cpu-boost annotation" , func () {
97+ Expect (pod .Annotations ).NotTo (HaveKey (bpod .BoostAnnotationKey ))
98+ })
99+ It ("reverts CPU requests to initial values" , func () {
100+ cpuReqOne := pod .Spec .Containers [0 ].Resources .Requests [corev1 .ResourceCPU ]
101+ cpuReqTwo := pod .Spec .Containers [1 ].Resources .Requests [corev1 .ResourceCPU ]
102+ Expect (cpuReqOne .String ()).Should (Equal (annot .InitCPURequests [containerOneName ]))
103+ Expect (cpuReqTwo .String ()).Should (Equal (annot .InitCPURequests [containerTwoName ]))
104+ })
105+ It ("reverts CPU limits to initial values" , func () {
106+ cpuReqOne := pod .Spec .Containers [0 ].Resources .Limits [corev1 .ResourceCPU ]
107+ cpuReqTwo := pod .Spec .Containers [1 ].Resources .Limits [corev1 .ResourceCPU ]
108+ Expect (cpuReqOne .String ()).Should (Equal (annot .InitCPULimits [containerOneName ]))
109+ Expect (cpuReqTwo .String ()).Should (Equal (annot .InitCPULimits [containerTwoName ]))
110+ })
126111 })
127112 })
128113 })
0 commit comments