@@ -25,6 +25,7 @@ import (
25
25
26
26
apiv1 "k8s.io/api/core/v1"
27
27
resourceapi "k8s.io/api/resource/v1beta1"
28
+ "k8s.io/apimachinery/pkg/api/resource"
28
29
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
29
30
"k8s.io/apimachinery/pkg/types"
30
31
"k8s.io/autoscaler/cluster-autoscaler/simulator/framework"
@@ -141,7 +142,26 @@ func TestDynamicResourceUtilization(t *testing.T) {
141
142
wantHighestUtilization : 0.2 ,
142
143
wantHighestUtilizationName : apiv1 .ResourceName (fmt .Sprintf ("%s/%s" , fooDriver , "pool1" )),
143
144
},
145
+ {
146
+ testName : "" ,
147
+ nodeInfo : framework .NewNodeInfo (node ,
148
+ mergeLists (
149
+ testResourceSlicesWithPartionableDevices (fooDriver , "pool1" , "node" , 4 ),
150
+ ),
151
+ testPodsWithCustomClaims (fooDriver , "pool1" , "node" , []string {"gpu-0-partition-0" , "gpu-0-partition-1" })... ,
152
+ ),
153
+ wantUtilization : map [string ]map [string ]float64 {
154
+ fooDriver : {
155
+ "pool1" : 0.5 ,
156
+ },
157
+ },
158
+ wantHighestUtilization : 0.5 ,
159
+ wantHighestUtilizationName : apiv1 .ResourceName (fmt .Sprintf ("%s/%s" , fooDriver , "pool1" )),
160
+ },
144
161
} {
162
+ if tc .testName != "" {
163
+ continue
164
+ }
145
165
t .Run (tc .testName , func (t * testing.T ) {
146
166
utilization , err := CalculateDynamicResourceUtilization (tc .nodeInfo )
147
167
if diff := cmp .Diff (tc .wantErr , err , cmpopts .EquateErrors ()); diff != "" {
@@ -190,6 +210,78 @@ func testResourceSlices(driverName, poolName, nodeName string, poolGen, deviceCo
190
210
return result
191
211
}
192
212
213
+ func testResourceSlicesWithPartionableDevices (driverName , poolName , nodeName string , partitionCount int ) []* resourceapi.ResourceSlice {
214
+ sliceName := fmt .Sprintf ("%s-%s-slice" , driverName , poolName )
215
+ var devices []resourceapi.Device
216
+ for i := 0 ; i < partitionCount ; i ++ {
217
+ devices = append (
218
+ devices ,
219
+ resourceapi.Device {
220
+ Name : fmt .Sprintf ("gpu-0-partition-%d" , i ),
221
+ Basic : & resourceapi.BasicDevice {
222
+ Capacity : map [resourceapi.QualifiedName ]resourceapi.DeviceCapacity {
223
+ "memory" : {
224
+ Value : resource .MustParse ("10Gi" ),
225
+ },
226
+ },
227
+ ConsumesCounters : []resourceapi.DeviceCounterConsumption {
228
+ {
229
+ CounterSet : "gpu-0-counter-set" ,
230
+ Counters : map [string ]resourceapi.Counter {
231
+ "memory" : {
232
+ Value : resource .MustParse ("10Gi" ),
233
+ },
234
+ },
235
+ },
236
+ },
237
+ },
238
+ },
239
+ )
240
+ }
241
+ devices = append (devices ,
242
+ resourceapi.Device {
243
+ Name : "gpu-0" ,
244
+ Basic : & resourceapi.BasicDevice {
245
+ Capacity : map [resourceapi.QualifiedName ]resourceapi.DeviceCapacity {
246
+ "memory" : {
247
+ Value : resource .MustParse (fmt .Sprintf ("%dGi" , 10 * partitionCount )),
248
+ },
249
+ },
250
+ ConsumesCounters : []resourceapi.DeviceCounterConsumption {
251
+ {
252
+ CounterSet : "gpu-0-counter-set" ,
253
+ Counters : map [string ]resourceapi.Counter {
254
+ "memory" : {
255
+ Value : resource .MustParse (fmt .Sprintf ("%dGi" , 10 * partitionCount )),
256
+ },
257
+ },
258
+ },
259
+ },
260
+ },
261
+ },
262
+ )
263
+ resourceSlice := & resourceapi.ResourceSlice {
264
+ ObjectMeta : metav1.ObjectMeta {Name : sliceName , UID : types .UID (sliceName )},
265
+ Spec : resourceapi.ResourceSliceSpec {
266
+ Driver : driverName ,
267
+ NodeName : nodeName ,
268
+ Pool : resourceapi.ResourcePool {Name : poolName , Generation : 0 , ResourceSliceCount : 1 },
269
+ Devices : devices ,
270
+ SharedCounters : []resourceapi.CounterSet {
271
+ {
272
+ Name : "gpu-0-counter-set" ,
273
+ Counters : map [string ]resourceapi.Counter {
274
+ "memory" : {
275
+ Value : resource .MustParse (fmt .Sprintf ("%dGi" , 10 * partitionCount )),
276
+ },
277
+ },
278
+ },
279
+ },
280
+ },
281
+ }
282
+ return []* resourceapi.ResourceSlice {resourceSlice }
283
+ }
284
+
193
285
func testPodsWithClaims (driverName , poolName , nodeName string , deviceCount , devicesPerPod int64 ) []* framework.PodInfo {
194
286
podCount := deviceCount / devicesPerPod
195
287
@@ -220,6 +312,39 @@ func testPodsWithClaims(driverName, poolName, nodeName string, deviceCount, devi
220
312
return result
221
313
}
222
314
315
+ func testPodsWithCustomClaims (driverName , poolName , nodeName string , devices []string ) []* framework.PodInfo {
316
+ deviceIndex := 0
317
+ var result []* framework.PodInfo
318
+ pod := test .BuildTestPod (fmt .Sprintf ("%s-%s-pod" , driverName , poolName ), 1 , 1 )
319
+ var claims []* resourceapi.ResourceClaim
320
+ var results []resourceapi.DeviceRequestAllocationResult
321
+ for deviceIndex , device := range devices {
322
+ results = append (
323
+ results ,
324
+ resourceapi.DeviceRequestAllocationResult {
325
+ Request : fmt .Sprintf ("request-%d" , deviceIndex ),
326
+ Driver : driverName ,
327
+ Pool : poolName ,
328
+ Device : device ,
329
+ },
330
+ )
331
+ }
332
+ claimName := fmt .Sprintf ("%s-claim" , pod .Name )
333
+ claims = append (claims , & resourceapi.ResourceClaim {
334
+ ObjectMeta : metav1.ObjectMeta {Name : claimName , UID : types .UID (claimName )},
335
+ Status : resourceapi.ResourceClaimStatus {
336
+ Allocation : & resourceapi.AllocationResult {
337
+ Devices : resourceapi.DeviceAllocationResult {
338
+ Results : results ,
339
+ },
340
+ },
341
+ },
342
+ })
343
+ deviceIndex ++
344
+ result = append (result , framework .NewPodInfo (pod , claims ))
345
+ return result
346
+ }
347
+
223
348
func mergeLists [T any ](sliceLists ... []T ) []T {
224
349
var result []T
225
350
for _ , sliceList := range sliceLists {
0 commit comments