@@ -23,6 +23,7 @@ import (
23
23
24
24
v1 "k8s.io/api/core/v1"
25
25
"k8s.io/apimachinery/pkg/api/resource"
26
+ "k8s.io/apimachinery/pkg/types"
26
27
"k8s.io/apimachinery/pkg/util/sets"
27
28
pluginapi "k8s.io/kubelet/pkg/apis/deviceplugin/v1beta1"
28
29
"k8s.io/kubernetes/pkg/kubelet/cm/topologymanager"
@@ -52,13 +53,17 @@ func makeSocketMask(sockets ...int) bitmask.BitMask {
52
53
func TestGetTopologyHints (t * testing.T ) {
53
54
tcases := []struct {
54
55
description string
56
+ podUID string
57
+ containerName string
55
58
request map [string ]string
56
59
devices map [string ][]pluginapi.Device
57
- allocatedDevices map [string ][]string
60
+ allocatedDevices map [string ]map [ string ] map [ string ] []string
58
61
expectedHints map [string ][]topologymanager.TopologyHint
59
62
}{
60
63
{
61
- description : "Single Request, no alignment" ,
64
+ description : "Single Request, no alignment" ,
65
+ podUID : "fakePod" ,
66
+ containerName : "fakeContainer" ,
62
67
request : map [string ]string {
63
68
"testdevice" : "1" ,
64
69
},
@@ -73,7 +78,9 @@ func TestGetTopologyHints(t *testing.T) {
73
78
},
74
79
},
75
80
{
76
- description : "Single Request, only one with alignment" ,
81
+ description : "Single Request, only one with alignment" ,
82
+ podUID : "fakePod" ,
83
+ containerName : "fakeContainer" ,
77
84
request : map [string ]string {
78
85
"testdevice" : "1" ,
79
86
},
@@ -97,7 +104,9 @@ func TestGetTopologyHints(t *testing.T) {
97
104
},
98
105
},
99
106
{
100
- description : "Single Request, one device per socket" ,
107
+ description : "Single Request, one device per socket" ,
108
+ podUID : "fakePod" ,
109
+ containerName : "fakeContainer" ,
101
110
request : map [string ]string {
102
111
"testdevice" : "1" ,
103
112
},
@@ -125,7 +134,9 @@ func TestGetTopologyHints(t *testing.T) {
125
134
},
126
135
},
127
136
{
128
- description : "Request for 2, one device per socket" ,
137
+ description : "Request for 2, one device per socket" ,
138
+ podUID : "fakePod" ,
139
+ containerName : "fakeContainer" ,
129
140
request : map [string ]string {
130
141
"testdevice" : "2" ,
131
142
},
@@ -145,7 +156,9 @@ func TestGetTopologyHints(t *testing.T) {
145
156
},
146
157
},
147
158
{
148
- description : "Request for 2, 2 devices per socket" ,
159
+ description : "Request for 2, 2 devices per socket" ,
160
+ podUID : "fakePod" ,
161
+ containerName : "fakeContainer" ,
149
162
request : map [string ]string {
150
163
"testdevice" : "2" ,
151
164
},
@@ -175,7 +188,9 @@ func TestGetTopologyHints(t *testing.T) {
175
188
},
176
189
},
177
190
{
178
- description : "Request for 2, optimal on 1 NUMA node, forced cross-NUMA" ,
191
+ description : "Request for 2, optimal on 1 NUMA node, forced cross-NUMA" ,
192
+ podUID : "fakePod" ,
193
+ containerName : "fakeContainer" ,
179
194
request : map [string ]string {
180
195
"testdevice" : "2" ,
181
196
},
@@ -187,8 +202,12 @@ func TestGetTopologyHints(t *testing.T) {
187
202
makeNUMADevice ("Dev4" , 1 ),
188
203
},
189
204
},
190
- allocatedDevices : map [string ][]string {
191
- "testdevice" : {"Dev1" , "Dev2" },
205
+ allocatedDevices : map [string ]map [string ]map [string ][]string {
206
+ "fakePod" : {
207
+ "fakeOtherContainer" : {
208
+ "testdevice" : {"Dev1" , "Dev2" },
209
+ },
210
+ },
192
211
},
193
212
expectedHints : map [string ][]topologymanager.TopologyHint {
194
213
"testdevice" : {
@@ -200,7 +219,9 @@ func TestGetTopologyHints(t *testing.T) {
200
219
},
201
220
},
202
221
{
203
- description : "2 device types, mixed configuration" ,
222
+ description : "2 device types, mixed configuration" ,
223
+ podUID : "fakePod" ,
224
+ containerName : "fakeContainer" ,
204
225
request : map [string ]string {
205
226
"testdevice1" : "2" ,
206
227
"testdevice2" : "1" ,
@@ -243,6 +264,110 @@ func TestGetTopologyHints(t *testing.T) {
243
264
},
244
265
},
245
266
},
267
+ {
268
+ description : "Single device type, more requested than available" ,
269
+ podUID : "fakePod" ,
270
+ containerName : "fakeContainer" ,
271
+ request : map [string ]string {
272
+ "testdevice" : "6" ,
273
+ },
274
+ devices : map [string ][]pluginapi.Device {
275
+ "testdevice" : {
276
+ makeNUMADevice ("Dev1" , 0 ),
277
+ makeNUMADevice ("Dev2" , 0 ),
278
+ makeNUMADevice ("Dev3" , 1 ),
279
+ makeNUMADevice ("Dev4" , 1 ),
280
+ },
281
+ },
282
+ expectedHints : map [string ][]topologymanager.TopologyHint {
283
+ "testdevice" : {},
284
+ },
285
+ },
286
+ {
287
+ description : "Single device type, all already allocated to container" ,
288
+ podUID : "fakePod" ,
289
+ containerName : "fakeContainer" ,
290
+ request : map [string ]string {
291
+ "testdevice" : "2" ,
292
+ },
293
+ devices : map [string ][]pluginapi.Device {
294
+ "testdevice" : {
295
+ makeNUMADevice ("Dev1" , 0 ),
296
+ makeNUMADevice ("Dev2" , 0 ),
297
+ },
298
+ },
299
+ allocatedDevices : map [string ]map [string ]map [string ][]string {
300
+ "fakePod" : {
301
+ "fakeContainer" : {
302
+ "testdevice" : {"Dev1" , "Dev2" },
303
+ },
304
+ },
305
+ },
306
+ expectedHints : map [string ][]topologymanager.TopologyHint {
307
+ "testdevice" : {
308
+ {
309
+ NUMANodeAffinity : makeSocketMask (0 ),
310
+ Preferred : true ,
311
+ },
312
+ {
313
+ NUMANodeAffinity : makeSocketMask (0 , 1 ),
314
+ Preferred : false ,
315
+ },
316
+ },
317
+ },
318
+ },
319
+ {
320
+ description : "Single device type, less already allocated to container than requested" ,
321
+ podUID : "fakePod" ,
322
+ containerName : "fakeContainer" ,
323
+ request : map [string ]string {
324
+ "testdevice" : "4" ,
325
+ },
326
+ devices : map [string ][]pluginapi.Device {
327
+ "testdevice" : {
328
+ makeNUMADevice ("Dev1" , 0 ),
329
+ makeNUMADevice ("Dev2" , 0 ),
330
+ makeNUMADevice ("Dev3" , 1 ),
331
+ makeNUMADevice ("Dev4" , 1 ),
332
+ },
333
+ },
334
+ allocatedDevices : map [string ]map [string ]map [string ][]string {
335
+ "fakePod" : {
336
+ "fakeContainer" : {
337
+ "testdevice" : {"Dev1" , "Dev2" },
338
+ },
339
+ },
340
+ },
341
+ expectedHints : map [string ][]topologymanager.TopologyHint {
342
+ "testdevice" : {},
343
+ },
344
+ },
345
+ {
346
+ description : "Single device type, more already allocated to container than requested" ,
347
+ podUID : "fakePod" ,
348
+ containerName : "fakeContainer" ,
349
+ request : map [string ]string {
350
+ "testdevice" : "2" ,
351
+ },
352
+ devices : map [string ][]pluginapi.Device {
353
+ "testdevice" : {
354
+ makeNUMADevice ("Dev1" , 0 ),
355
+ makeNUMADevice ("Dev2" , 0 ),
356
+ makeNUMADevice ("Dev3" , 1 ),
357
+ makeNUMADevice ("Dev4" , 1 ),
358
+ },
359
+ },
360
+ allocatedDevices : map [string ]map [string ]map [string ][]string {
361
+ "fakePod" : {
362
+ "fakeContainer" : {
363
+ "testdevice" : {"Dev1" , "Dev2" , "Dev3" , "Dev4" },
364
+ },
365
+ },
366
+ },
367
+ expectedHints : map [string ][]topologymanager.TopologyHint {
368
+ "testdevice" : {},
369
+ },
370
+ },
246
371
}
247
372
248
373
for _ , tc := range tcases {
@@ -252,14 +377,16 @@ func TestGetTopologyHints(t *testing.T) {
252
377
}
253
378
254
379
pod := makePod (resourceList )
380
+ pod .UID = types .UID (tc .podUID )
381
+ pod .Spec .Containers [0 ].Name = tc .containerName
255
382
256
383
m := ManagerImpl {
257
384
allDevices : make (map [string ]map [string ]pluginapi.Device ),
258
385
healthyDevices : make (map [string ]sets.String ),
259
386
allocatedDevices : make (map [string ]sets.String ),
260
387
podDevices : make (podDevices ),
261
388
sourcesReady : & sourcesReadyStub {},
262
- activePods : func () []* v1.Pod { return []* v1.Pod {} },
389
+ activePods : func () []* v1.Pod { return []* v1.Pod {pod } },
263
390
numaNodes : []int {0 , 1 },
264
391
}
265
392
@@ -273,11 +400,16 @@ func TestGetTopologyHints(t *testing.T) {
273
400
}
274
401
}
275
402
276
- for r := range tc .allocatedDevices {
277
- m .allocatedDevices [r ] = sets .NewString ()
403
+ for p := range tc .allocatedDevices {
404
+ for c := range tc .allocatedDevices [p ] {
405
+ for r , devices := range tc.allocatedDevices [p ][c ] {
406
+ m .podDevices .insert (p , c , r , sets .NewString (devices ... ), nil )
278
407
279
- for _ , d := range tc .allocatedDevices [r ] {
280
- m .allocatedDevices [r ].Insert (d )
408
+ m .allocatedDevices [r ] = sets .NewString ()
409
+ for _ , d := range devices {
410
+ m .allocatedDevices [r ].Insert (d )
411
+ }
412
+ }
281
413
}
282
414
}
283
415
0 commit comments