@@ -26,8 +26,10 @@ import (
2626 "k8s.io/apimachinery/pkg/api/resource"
2727 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2828
29+ apiext "github.com/koordinator-sh/koordinator/apis/extension"
2930 koordletutil "github.com/koordinator-sh/koordinator/pkg/koordlet/util"
3031 "github.com/koordinator-sh/koordinator/pkg/koordlet/util/system"
32+ "github.com/koordinator-sh/koordinator/pkg/util"
3133)
3234
3335func TestGenNodeLabels (t * testing.T ) {
@@ -131,8 +133,8 @@ func TestCommonCollectors(t *testing.T) {
131133 RecordCollectNodeCPUInfoStatus (testingErr )
132134 RecordCollectNodeCPUInfoStatus (nil )
133135 RecordBESuppressCores ("cfsQuota" , float64 (1000 ))
134- RecordBESuppressLSUsedCPU (float64 ( 1000 ) )
135- RecordNodeUsedCPU (float64 ( 2000 ) )
136+ RecordBESuppressLSUsedCPU (1.0 )
137+ RecordNodeUsedCPU (2.0 )
136138 RecordContainerScaledCFSBurstUS (testingPod .Namespace , testingPod .Name , testingContainer .ContainerID , testingContainer .Name , 1000000 )
137139 RecordContainerScaledCFSQuotaUS (testingPod .Namespace , testingPod .Name , testingContainer .ContainerID , testingContainer .Name , 1000000 )
138140 RecordPodEviction ("evictByCPU" )
@@ -144,3 +146,107 @@ func TestCommonCollectors(t *testing.T) {
144146 RecordPodPSI (testingPod , testingPSI )
145147 })
146148}
149+
150+ func TestResourceSummaryCollectors (t * testing.T ) {
151+ testingNode := & corev1.Node {
152+ ObjectMeta : metav1.ObjectMeta {
153+ Name : "test-node" ,
154+ Labels : map [string ]string {},
155+ },
156+ Status : corev1.NodeStatus {
157+ Allocatable : corev1.ResourceList {
158+ corev1 .ResourceCPU : resource .MustParse ("100" ),
159+ corev1 .ResourceMemory : resource .MustParse ("200Gi" ),
160+ apiext .BatchCPU : resource .MustParse ("50000" ),
161+ apiext .BatchMemory : resource .MustParse ("80Gi" ),
162+ },
163+ Capacity : corev1.ResourceList {
164+ corev1 .ResourceCPU : resource .MustParse ("100" ),
165+ corev1 .ResourceMemory : resource .MustParse ("200Gi" ),
166+ apiext .BatchCPU : resource .MustParse ("50000" ),
167+ apiext .BatchMemory : resource .MustParse ("80Gi" ),
168+ },
169+ },
170+ }
171+ testingPod := & corev1.Pod {
172+ ObjectMeta : metav1.ObjectMeta {
173+ Name : "test_pod" ,
174+ Namespace : "test_pod_namespace" ,
175+ UID : "test01" ,
176+ },
177+ Spec : corev1.PodSpec {
178+ Containers : []corev1.Container {
179+ {
180+ Name : "test_container" ,
181+ Resources : corev1.ResourceRequirements {
182+ Requests : corev1.ResourceList {
183+ corev1 .ResourceCPU : resource .MustParse ("1" ),
184+ corev1 .ResourceMemory : resource .MustParse ("2Gi" ),
185+ },
186+ Limits : corev1.ResourceList {
187+ corev1 .ResourceCPU : resource .MustParse ("2" ),
188+ corev1 .ResourceMemory : resource .MustParse ("4Gi" ),
189+ },
190+ },
191+ },
192+ },
193+ },
194+ Status : corev1.PodStatus {
195+ ContainerStatuses : []corev1.ContainerStatus {
196+ {
197+ Name : "test_container" ,
198+ ContainerID : "containerd://testxxx" ,
199+ },
200+ },
201+ },
202+ }
203+ testingBatchPod := & corev1.Pod {
204+ ObjectMeta : metav1.ObjectMeta {
205+ Name : "test_batch_pod" ,
206+ Namespace : "test_batch_pod_namespace" ,
207+ UID : "batch01" ,
208+ },
209+ Spec : corev1.PodSpec {
210+ Containers : []corev1.Container {
211+ {
212+ Name : "test_batch_container" ,
213+ Resources : corev1.ResourceRequirements {
214+ Requests : corev1.ResourceList {
215+ apiext .BatchCPU : resource .MustParse ("1000" ),
216+ apiext .BatchMemory : resource .MustParse ("2Gi" ),
217+ },
218+ Limits : corev1.ResourceList {
219+ apiext .BatchCPU : resource .MustParse ("1000" ),
220+ apiext .BatchMemory : resource .MustParse ("2Gi" ),
221+ },
222+ },
223+ },
224+ },
225+ },
226+ Status : corev1.PodStatus {
227+ ContainerStatuses : []corev1.ContainerStatus {
228+ {
229+ Name : "test_batch_container" ,
230+ ContainerID : "containerd://batchxxx" ,
231+ },
232+ },
233+ },
234+ }
235+
236+ t .Run ("test not panic" , func (t * testing.T ) {
237+ Register (testingNode )
238+ defer Register (nil )
239+
240+ RecordNodeResourceAllocatable (string (apiext .BatchCPU ), float64 (util .QuantityPtr (testingNode .Status .Allocatable [apiext .BatchCPU ]).Value ()))
241+ RecordNodeResourceAllocatable (string (apiext .BatchMemory ), float64 (util .QuantityPtr (testingNode .Status .Allocatable [apiext .BatchMemory ]).Value ()))
242+ RecordContainerResourceRequests (string (corev1 .ResourceCPU ), & testingPod .Status .ContainerStatuses [0 ], testingPod , float64 (testingPod .Spec .Containers [0 ].Resources .Requests .Cpu ().Value ()))
243+ RecordContainerResourceRequests (string (corev1 .ResourceMemory ), & testingPod .Status .ContainerStatuses [0 ], testingPod , float64 (testingPod .Spec .Containers [0 ].Resources .Requests .Memory ().Value ()))
244+ RecordContainerResourceRequests (string (apiext .BatchCPU ), & testingBatchPod .Status .ContainerStatuses [0 ], testingBatchPod , float64 (util .QuantityPtr (testingBatchPod .Spec .Containers [0 ].Resources .Requests [apiext .BatchCPU ]).Value ()))
245+ RecordContainerResourceRequests (string (apiext .BatchMemory ), & testingBatchPod .Status .ContainerStatuses [0 ], testingBatchPod , float64 (util .QuantityPtr (testingBatchPod .Spec .Containers [0 ].Resources .Requests [apiext .BatchMemory ]).Value ()))
246+ RecordContainerResourceLimits (string (apiext .BatchCPU ), & testingBatchPod .Status .ContainerStatuses [0 ], testingBatchPod , float64 (util .QuantityPtr (testingBatchPod .Spec .Containers [0 ].Resources .Limits [apiext .BatchCPU ]).Value ()))
247+ RecordContainerResourceLimits (string (apiext .BatchMemory ), & testingBatchPod .Status .ContainerStatuses [0 ], testingBatchPod , float64 (util .QuantityPtr (testingBatchPod .Spec .Containers [0 ].Resources .Limits [apiext .BatchMemory ]).Value ()))
248+
249+ ResetContainerResourceRequests ()
250+ ResetContainerResourceLimits ()
251+ })
252+ }
0 commit comments