@@ -19,10 +19,12 @@ package podresources
19
19
import (
20
20
"context"
21
21
"fmt"
22
+ "sort"
22
23
"testing"
23
24
24
25
"github.com/google/go-cmp/cmp"
25
26
"github.com/google/go-cmp/cmp/cmpopts"
27
+ "github.com/stretchr/testify/mock"
26
28
27
29
v1 "k8s.io/api/core/v1"
28
30
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -221,6 +223,7 @@ func TestListPodResourcesV1(t *testing.T) {
221
223
mockDynamicResourcesProvider := podresourcetest .NewMockDynamicResourcesProvider (t )
222
224
223
225
mockPodsProvider .EXPECT ().GetPods ().Return (tc .pods ).Maybe ()
226
+ mockPodsProvider .EXPECT ().GetActivePods ().Return (tc .pods ).Maybe ()
224
227
mockDevicesProvider .EXPECT ().GetDevices (string (podUID ), containerName ).Return (tc .devices ).Maybe ()
225
228
mockCPUsProvider .EXPECT ().GetCPUs (string (podUID ), containerName ).Return (tc .cpus ).Maybe ()
226
229
mockMemoryProvider .EXPECT ().GetMemory (string (podUID ), containerName ).Return (tc .memory ).Maybe ()
@@ -249,6 +252,159 @@ func TestListPodResourcesV1(t *testing.T) {
249
252
}
250
253
}
251
254
255
+ func makePod (idx int ) * v1.Pod {
256
+ podNamespace := "pod-namespace"
257
+ podName := fmt .Sprintf ("pod-name-%d" , idx )
258
+ podUID := types .UID (fmt .Sprintf ("pod-uid-%d" , idx ))
259
+ containerName := fmt .Sprintf ("container-name-%d" , idx )
260
+ containers := []v1.Container {
261
+ {
262
+ Name : containerName ,
263
+ },
264
+ }
265
+ return & v1.Pod {
266
+ ObjectMeta : metav1.ObjectMeta {
267
+ Name : podName ,
268
+ Namespace : podNamespace ,
269
+ UID : podUID ,
270
+ },
271
+ Spec : v1.PodSpec {
272
+ Containers : containers ,
273
+ },
274
+ }
275
+ }
276
+
277
+ func collectNamespacedNamesFromPods (pods []* v1.Pod ) []string {
278
+ ret := make ([]string , 0 , len (pods ))
279
+ for _ , pod := range pods {
280
+ ret = append (ret , pod .Namespace + "/" + pod .Name )
281
+ }
282
+ sort .Strings (ret )
283
+ return ret
284
+ }
285
+
286
+ func collectNamespacedNamesFromPodResources (prs []* podresourcesapi.PodResources ) []string {
287
+ ret := make ([]string , 0 , len (prs ))
288
+ for _ , pr := range prs {
289
+ ret = append (ret , pr .Namespace + "/" + pr .Name )
290
+ }
291
+ sort .Strings (ret )
292
+ return ret
293
+ }
294
+
295
+ func TestListPodResourcesUsesOnlyActivePodsV1 (t * testing.T ) {
296
+ numaID := int64 (1 )
297
+
298
+ // we abuse the fact that we don't care about the assignments,
299
+ // so we reuse the same for all pods which is actually wrong.
300
+ devs := []* podresourcesapi.ContainerDevices {
301
+ {
302
+ ResourceName : "resource" ,
303
+ DeviceIds : []string {"dev0" },
304
+ Topology : & podresourcesapi.TopologyInfo {Nodes : []* podresourcesapi.NUMANode {{ID : numaID }}},
305
+ },
306
+ }
307
+
308
+ cpus := []int64 {1 , 9 }
309
+
310
+ mems := []* podresourcesapi.ContainerMemory {
311
+ {
312
+ MemoryType : "memory" ,
313
+ Size_ : 1073741824 ,
314
+ Topology : & podresourcesapi.TopologyInfo {Nodes : []* podresourcesapi.NUMANode {{ID : numaID }}},
315
+ },
316
+ {
317
+ MemoryType : "hugepages-1Gi" ,
318
+ Size_ : 1073741824 ,
319
+ Topology : & podresourcesapi.TopologyInfo {Nodes : []* podresourcesapi.NUMANode {{ID : numaID }}},
320
+ },
321
+ }
322
+
323
+ for _ , tc := range []struct {
324
+ desc string
325
+ pods []* v1.Pod
326
+ activePods []* v1.Pod
327
+ }{
328
+ {
329
+ desc : "no pods" ,
330
+ pods : []* v1.Pod {},
331
+ activePods : []* v1.Pod {},
332
+ },
333
+ {
334
+ desc : "no differences" ,
335
+ pods : []* v1.Pod {
336
+ makePod (1 ),
337
+ makePod (2 ),
338
+ makePod (3 ),
339
+ makePod (4 ),
340
+ makePod (5 ),
341
+ },
342
+ activePods : []* v1.Pod {
343
+ makePod (1 ),
344
+ makePod (2 ),
345
+ makePod (3 ),
346
+ makePod (4 ),
347
+ makePod (5 ),
348
+ },
349
+ },
350
+ {
351
+ desc : "some terminated pods" ,
352
+ pods : []* v1.Pod {
353
+ makePod (1 ),
354
+ makePod (2 ),
355
+ makePod (3 ),
356
+ makePod (4 ),
357
+ makePod (5 ),
358
+ makePod (6 ),
359
+ makePod (7 ),
360
+ },
361
+ activePods : []* v1.Pod {
362
+ makePod (1 ),
363
+ makePod (3 ),
364
+ makePod (4 ),
365
+ makePod (5 ),
366
+ makePod (6 ),
367
+ },
368
+ },
369
+ } {
370
+ t .Run (tc .desc , func (t * testing.T ) {
371
+ mockDevicesProvider := podresourcetest .NewMockDevicesProvider (t )
372
+ mockPodsProvider := podresourcetest .NewMockPodsProvider (t )
373
+ mockCPUsProvider := podresourcetest .NewMockCPUsProvider (t )
374
+ mockMemoryProvider := podresourcetest .NewMockMemoryProvider (t )
375
+ mockDynamicResourcesProvider := podresourcetest .NewMockDynamicResourcesProvider (t )
376
+
377
+ mockPodsProvider .EXPECT ().GetPods ().Return (tc .pods ).Maybe ()
378
+ mockPodsProvider .EXPECT ().GetActivePods ().Return (tc .activePods ).Maybe ()
379
+ mockDevicesProvider .EXPECT ().GetDevices (mock .Anything , mock .Anything ).Return (devs ).Maybe ()
380
+ mockCPUsProvider .EXPECT ().GetCPUs (mock .Anything , mock .Anything ).Return (cpus ).Maybe ()
381
+ mockMemoryProvider .EXPECT ().GetMemory (mock .Anything , mock .Anything ).Return (mems ).Maybe ()
382
+ mockDevicesProvider .EXPECT ().UpdateAllocatedDevices ().Return ().Maybe ()
383
+ mockCPUsProvider .EXPECT ().GetAllocatableCPUs ().Return ([]int64 {}).Maybe ()
384
+ mockDevicesProvider .EXPECT ().GetAllocatableDevices ().Return ([]* podresourcesapi.ContainerDevices {}).Maybe ()
385
+ mockMemoryProvider .EXPECT ().GetAllocatableMemory ().Return ([]* podresourcesapi.ContainerMemory {}).Maybe ()
386
+
387
+ providers := PodResourcesProviders {
388
+ Pods : mockPodsProvider ,
389
+ Devices : mockDevicesProvider ,
390
+ Cpus : mockCPUsProvider ,
391
+ Memory : mockMemoryProvider ,
392
+ DynamicResources : mockDynamicResourcesProvider ,
393
+ }
394
+ server := NewV1PodResourcesServer (providers )
395
+ resp , err := server .List (context .TODO (), & podresourcesapi.ListPodResourcesRequest {})
396
+ if err != nil {
397
+ t .Errorf ("want err = %v, got %q" , nil , err )
398
+ }
399
+ expectedNames := collectNamespacedNamesFromPods (tc .activePods )
400
+ gotNames := collectNamespacedNamesFromPodResources (resp .GetPodResources ())
401
+ if diff := cmp .Diff (expectedNames , gotNames , cmpopts .EquateEmpty ()); diff != "" {
402
+ t .Fatal (diff )
403
+ }
404
+ })
405
+ }
406
+ }
407
+
252
408
func TestListPodResourcesWithInitContainersV1 (t * testing.T ) {
253
409
featuregatetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , pkgfeatures .KubeletPodResourcesDynamicResources , true )
254
410
@@ -530,6 +686,7 @@ func TestListPodResourcesWithInitContainersV1(t *testing.T) {
530
686
mockDynamicResourcesProvider := podresourcetest .NewMockDynamicResourcesProvider (t )
531
687
532
688
mockPodsProvider .EXPECT ().GetPods ().Return (tc .pods ).Maybe ()
689
+ mockPodsProvider .EXPECT ().GetActivePods ().Return (tc .pods ).Maybe ()
533
690
tc .mockFunc (tc .pods , mockDevicesProvider , mockCPUsProvider , mockMemoryProvider , mockDynamicResourcesProvider )
534
691
535
692
providers := PodResourcesProviders {
0 commit comments