Skip to content

Commit 3a09aeb

Browse files
authored
Merge pull request kubernetes#131679 from mortent/automated-cherry-pick-of-#131662-upstream-release-1.33
Automated cherry pick of kubernetes#131662: DRA: Fix failure to allocate large number of devices
2 parents d3c7573 + b59deb4 commit 3a09aeb

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

staging/src/k8s.io/dynamic-resource-allocation/structured/allocator.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -745,9 +745,10 @@ func (alloc *allocator) allocateOne(r deviceIndices, allocateSubRequest bool) (b
745745
return alloc.allocateOne(deviceIndices{claimIndex: r.claimIndex, requestIndex: r.requestIndex + 1}, false)
746746
}
747747

748-
// Before trying to allocate devices, check if allocating the devices
749-
// in the current request will put us over the threshold.
750-
numDevicesAfterAlloc := len(alloc.result[r.claimIndex].devices) + requestData.numDevices
748+
// We can calculate this by adding the number of already allocated devices with the number
749+
// of devices in the current request, and then finally subtract the deviceIndex since we
750+
// don't want to double count any devices already allocated for the current request.
751+
numDevicesAfterAlloc := len(alloc.result[r.claimIndex].devices) + requestData.numDevices - r.deviceIndex
751752
if numDevicesAfterAlloc > resourceapi.AllocationResultsMaxSize {
752753
// Don't return an error here since we want to keep searching for
753754
// a solution that works.

staging/src/k8s.io/dynamic-resource-allocation/structured/allocator_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,14 @@ func deviceAllocationResult(request, driver, pool, device string, adminAccess bo
395395
return r
396396
}
397397

398+
func multipleDeviceAllocationResults(request, driver, pool string, count, startIndex int) []resourceapi.DeviceRequestAllocationResult {
399+
var results []resourceapi.DeviceRequestAllocationResult
400+
for i := startIndex; i < startIndex+count; i++ {
401+
results = append(results, deviceAllocationResult(request, driver, pool, fmt.Sprintf("device-%d", i), false))
402+
}
403+
return results
404+
}
405+
398406
// nodeLabelSelector creates a node selector with a label match for "key" in "values".
399407
func nodeLabelSelector(key string, values ...string) *v1.NodeSelector {
400408
requirements := []v1.NodeSelectorRequirement{{
@@ -3024,6 +3032,21 @@ func TestAllocator(t *testing.T) {
30243032
deviceAllocationResult(req0, driverA, pool1, device1, false),
30253033
)},
30263034
},
3035+
"max-number-devices": {
3036+
claimsToAllocate: objects(
3037+
claimWithRequests(
3038+
claim0, nil, request(req0, classA, resourceapi.AllocationResultsMaxSize),
3039+
),
3040+
),
3041+
classes: objects(class(classA, driverA)),
3042+
slices: objects(sliceWithMultipleDevices(slice1, node1, pool1, driverA, resourceapi.AllocationResultsMaxSize)),
3043+
node: node(node1, region1),
3044+
3045+
expectResults: []any{allocationResult(
3046+
localNodeSelector(node1),
3047+
multipleDeviceAllocationResults(req0, driverA, pool1, resourceapi.AllocationResultsMaxSize, 0)...,
3048+
)},
3049+
},
30273050
}
30283051

30293052
for name, tc := range testcases {

0 commit comments

Comments
 (0)