Skip to content

Commit cdf077d

Browse files
authored
Merge pull request kubernetes#127565 from pohly/dra-structured-all-mode-fix
DRA: structured "all" mode fix
2 parents 1947bf5 + c1ec1dc commit cdf077d

File tree

2 files changed

+473
-27
lines changed

2 files changed

+473
-27
lines changed

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

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func NewAllocator(ctx context.Context,
6868
}, nil
6969
}
7070

71-
// ClaimsToAllocate returns the claims that the allocated was created for.
71+
// ClaimsToAllocate returns the claims that the allocator was created for.
7272
func (a *Allocator) ClaimsToAllocate() []*resourceapi.ResourceClaim {
7373
return a.claimsToAllocate
7474
}
@@ -169,9 +169,13 @@ func (a *Allocator) Allocate(ctx context.Context, node *v1.Node) (finalResult []
169169
return nil, fmt.Errorf("claim %s, request %s: could not retrieve device class %s: %w", klog.KObj(claim), request.Name, request.DeviceClassName, err)
170170
}
171171

172+
// Start collecting information about the request.
173+
// The class must be set and stored before calling isSelectable.
172174
requestData := requestData{
173175
class: class,
174176
}
177+
requestKey := requestIndices{claimIndex: claimIndex, requestIndex: requestIndex}
178+
alloc.requestData[requestKey] = requestData
175179

176180
switch request.AllocationMode {
177181
case resourceapi.DeviceAllocationModeExactCount:
@@ -190,7 +194,7 @@ func (a *Allocator) Allocate(ctx context.Context, node *v1.Node) (finalResult []
190194

191195
for _, slice := range pool.Slices {
192196
for deviceIndex := range slice.Spec.Devices {
193-
selectable, err := alloc.isSelectable(requestIndices{claimIndex: claimIndex, requestIndex: requestIndex}, slice, deviceIndex)
197+
selectable, err := alloc.isSelectable(requestKey, slice, deviceIndex)
194198
if err != nil {
195199
return nil, err
196200
}
@@ -205,7 +209,7 @@ func (a *Allocator) Allocate(ctx context.Context, node *v1.Node) (finalResult []
205209
default:
206210
return nil, fmt.Errorf("claim %s, request %s: unsupported count mode %s", klog.KObj(claim), request.Name, request.AllocationMode)
207211
}
208-
alloc.requestData[requestIndices{claimIndex: claimIndex, requestIndex: requestIndex}] = requestData
212+
alloc.requestData[requestKey] = requestData
209213
numDevices += requestData.numDevices
210214
}
211215
alloc.logger.V(6).Info("Checked claim", "claim", klog.KObj(claim), "numDevices", numDevices)
@@ -290,10 +294,13 @@ func (a *Allocator) Allocate(ctx context.Context, node *v1.Node) (finalResult []
290294
// All errors get created such that they can be returned by Allocate
291295
// without further wrapping.
292296
done, err := alloc.allocateOne(deviceIndices{})
297+
if errors.Is(err, errStop) {
298+
return nil, nil
299+
}
293300
if err != nil {
294301
return nil, err
295302
}
296-
if errors.Is(err, errStop) || !done {
303+
if !done {
297304
return nil, nil
298305
}
299306

@@ -347,7 +354,6 @@ type allocator struct {
347354
constraints [][]constraint // one list of constraints per claim
348355
requestData map[requestIndices]requestData // one entry per request
349356
allocated map[DeviceID]bool
350-
skippedUnknownDevice bool
351357
result []*resourceapi.AllocationResult
352358
}
353359

@@ -534,20 +540,23 @@ func (alloc *allocator) allocateOne(r deviceIndices) (bool, error) {
534540
// For "all" devices we already know which ones we need. We
535541
// just need to check whether we can use them.
536542
deviceWithID := requestData.allDevices[r.deviceIndex]
537-
_, _, err := alloc.allocateDevice(r, deviceWithID.device, deviceWithID.DeviceID, true)
543+
success, _, err := alloc.allocateDevice(r, deviceWithID.device, deviceWithID.DeviceID, true)
538544
if err != nil {
539545
return false, err
540546
}
547+
if !success {
548+
// The order in which we allocate "all" devices doesn't matter,
549+
// so we only try with the one which was up next. If we couldn't
550+
// get all of them, then there is no solution and we have to stop.
551+
return false, errStop
552+
}
541553
done, err := alloc.allocateOne(deviceIndices{claimIndex: r.claimIndex, requestIndex: r.requestIndex, deviceIndex: r.deviceIndex + 1})
542554
if err != nil {
543555
return false, err
544556
}
545-
546-
// The order in which we allocate "all" devices doesn't matter,
547-
// so we only try with the one which was up next. If we couldn't
548-
// get all of them, then there is no solution and we have to stop.
549557
if !done {
550-
return false, errStop
558+
// Backtrack.
559+
return false, nil
551560
}
552561
return done, nil
553562
}
@@ -610,9 +619,6 @@ func (alloc *allocator) isSelectable(r requestIndices, slice *resourceapi.Resour
610619
device := slice.Spec.Devices[deviceIndex].Basic
611620
if device == nil {
612621
// Must be some future, unknown device type. We cannot select it.
613-
// If we don't find anything else, then this will get reported
614-
// in the final result, so remember that we skipped some device.
615-
alloc.skippedUnknownDevice = true
616622
return false, nil
617623
}
618624

0 commit comments

Comments
 (0)