@@ -21,9 +21,11 @@ import (
2121
2222 resourceapi "k8s.io/api/resource/v1"
2323 "k8s.io/apimachinery/pkg/util/sets"
24+ utilfeature "k8s.io/apiserver/pkg/util/feature"
2425 "k8s.io/client-go/tools/cache"
2526 "k8s.io/dynamic-resource-allocation/structured"
2627 "k8s.io/klog/v2"
28+ "k8s.io/kubernetes/pkg/features"
2729 schedutil "k8s.io/kubernetes/pkg/scheduler/util"
2830 "k8s.io/utils/ptr"
2931)
@@ -36,7 +38,11 @@ import (
3638// claims and are skipped without invoking the callback.
3739//
3840// foreachAllocatedDevice does nothing if the claim is not allocated.
39- func foreachAllocatedDevice (claim * resourceapi.ResourceClaim , cb func (deviceID structured.DeviceID )) {
41+ func foreachAllocatedDevice (claim * resourceapi.ResourceClaim ,
42+ dedicatedDeviceCallback func (deviceID structured.DeviceID ),
43+ enabledConsumableCapacity bool ,
44+ sharedDeviceCallback func (structured.SharedDeviceID ),
45+ consumedCapacityCallback func (structured.DeviceConsumedCapacity )) {
4046 if claim .Status .Allocation == nil {
4147 return
4248 }
@@ -54,7 +60,24 @@ func foreachAllocatedDevice(claim *resourceapi.ResourceClaim, cb func(deviceID s
5460
5561 // None of the users of this helper need to abort iterating,
5662 // therefore it's not supported as it only would add overhead.
57- cb (deviceID )
63+
64+ // Execute sharedDeviceCallback and consumedCapacityCallback correspondingly
65+ // if DRAConsumableCapacity feature is enabled
66+ if enabledConsumableCapacity {
67+ shared := result .ShareID != nil
68+ if shared {
69+ sharedDeviceID := structured .MakeSharedDeviceID (deviceID , result .ShareID )
70+ sharedDeviceCallback (sharedDeviceID )
71+ if result .ConsumedCapacity != nil {
72+ deviceConsumedCapacity := structured .NewDeviceConsumedCapacity (deviceID , result .ConsumedCapacity )
73+ consumedCapacityCallback (deviceConsumedCapacity )
74+ }
75+ continue
76+ }
77+ }
78+
79+ // Otherwise, execute dedicatedDeviceCallback
80+ dedicatedDeviceCallback (deviceID )
5881 }
5982}
6083
@@ -66,14 +89,20 @@ func foreachAllocatedDevice(claim *resourceapi.ResourceClaim, cb func(deviceID s
6689type allocatedDevices struct {
6790 logger klog.Logger
6891
69- mutex sync.RWMutex
70- ids sets.Set [structured.DeviceID ]
92+ mutex sync.RWMutex
93+ ids sets.Set [structured.DeviceID ]
94+ shareIDs sets.Set [structured.SharedDeviceID ]
95+ capacities structured.ConsumedCapacityCollection
96+ enabledConsumableCapacity bool
7197}
7298
7399func newAllocatedDevices (logger klog.Logger ) * allocatedDevices {
74100 return & allocatedDevices {
75- logger : logger ,
76- ids : sets .New [structured.DeviceID ](),
101+ logger : logger ,
102+ ids : sets .New [structured.DeviceID ](),
103+ shareIDs : sets .New [structured.SharedDeviceID ](),
104+ capacities : structured .NewConsumedCapacityCollection (),
105+ enabledConsumableCapacity : utilfeature .DefaultFeatureGate .Enabled (features .DRAConsumableCapacity ),
77106 }
78107}
79108
@@ -84,6 +113,13 @@ func (a *allocatedDevices) Get() sets.Set[structured.DeviceID] {
84113 return a .ids .Clone ()
85114}
86115
116+ func (a * allocatedDevices ) Capacities () structured.ConsumedCapacityCollection {
117+ a .mutex .RLock ()
118+ defer a .mutex .RUnlock ()
119+
120+ return a .capacities .Clone ()
121+ }
122+
87123func (a * allocatedDevices ) handlers () cache.ResourceEventHandler {
88124 return cache.ResourceEventHandlerFuncs {
89125 AddFunc : a .onAdd ,
@@ -142,16 +178,39 @@ func (a *allocatedDevices) addDevices(claim *resourceapi.ResourceClaim) {
142178 // Locking of the mutex gets minimized by pre-computing what needs to be done
143179 // without holding the lock.
144180 deviceIDs := make ([]structured.DeviceID , 0 , 20 )
145- foreachAllocatedDevice (claim , func (deviceID structured.DeviceID ) {
146- a .logger .V (6 ).Info ("Observed device allocation" , "device" , deviceID , "claim" , klog .KObj (claim ))
147- deviceIDs = append (deviceIDs , deviceID )
148- })
181+ var shareIDs []structured.SharedDeviceID
182+ var deviceCapacities []structured.DeviceConsumedCapacity
183+ if a .enabledConsumableCapacity {
184+ shareIDs = make ([]structured.SharedDeviceID , 0 , 20 )
185+ deviceCapacities = make ([]structured.DeviceConsumedCapacity , 0 , 20 )
186+ }
187+ foreachAllocatedDevice (claim ,
188+ func (deviceID structured.DeviceID ) {
189+ a .logger .V (6 ).Info ("Observed device allocation" , "device" , deviceID , "claim" , klog .KObj (claim ))
190+ deviceIDs = append (deviceIDs , deviceID )
191+ },
192+ a .enabledConsumableCapacity ,
193+ func (sharedDeviceID structured.SharedDeviceID ) {
194+ a .logger .V (6 ).Info ("Observed shared device allocation" , "shared device" , sharedDeviceID , "claim" , klog .KObj (claim ))
195+ shareIDs = append (shareIDs , sharedDeviceID )
196+ },
197+ func (capacity structured.DeviceConsumedCapacity ) {
198+ a .logger .V (6 ).Info ("Observed consumed capacity" , "device" , capacity .DeviceID , "consumed capacity" , capacity .ConsumedCapacity , "claim" , klog .KObj (claim ))
199+ deviceCapacities = append (deviceCapacities , capacity )
200+ },
201+ )
149202
150203 a .mutex .Lock ()
151204 defer a .mutex .Unlock ()
152205 for _ , deviceID := range deviceIDs {
153206 a .ids .Insert (deviceID )
154207 }
208+ for _ , shareID := range shareIDs {
209+ a .shareIDs .Insert (shareID )
210+ }
211+ for _ , capacity := range deviceCapacities {
212+ a .capacities .Insert (capacity )
213+ }
155214}
156215
157216func (a * allocatedDevices ) removeDevices (claim * resourceapi.ResourceClaim ) {
@@ -162,14 +221,35 @@ func (a *allocatedDevices) removeDevices(claim *resourceapi.ResourceClaim) {
162221 // Locking of the mutex gets minimized by pre-computing what needs to be done
163222 // without holding the lock.
164223 deviceIDs := make ([]structured.DeviceID , 0 , 20 )
165- foreachAllocatedDevice (claim , func (deviceID structured.DeviceID ) {
166- a .logger .V (6 ).Info ("Observed device deallocation" , "device" , deviceID , "claim" , klog .KObj (claim ))
167- deviceIDs = append (deviceIDs , deviceID )
168- })
169-
224+ var shareIDs []structured.SharedDeviceID
225+ var deviceCapacities []structured.DeviceConsumedCapacity
226+ if a .enabledConsumableCapacity {
227+ shareIDs = make ([]structured.SharedDeviceID , 0 , 20 )
228+ deviceCapacities = make ([]structured.DeviceConsumedCapacity , 0 , 20 )
229+ }
230+ foreachAllocatedDevice (claim ,
231+ func (deviceID structured.DeviceID ) {
232+ a .logger .V (6 ).Info ("Observed device deallocation" , "device" , deviceID , "claim" , klog .KObj (claim ))
233+ deviceIDs = append (deviceIDs , deviceID )
234+ },
235+ a .enabledConsumableCapacity ,
236+ func (sharedDeviceID structured.SharedDeviceID ) {
237+ a .logger .V (6 ).Info ("Observed shared device deallocation" , "shared device" , sharedDeviceID , "claim" , klog .KObj (claim ))
238+ shareIDs = append (shareIDs , sharedDeviceID )
239+ },
240+ func (capacity structured.DeviceConsumedCapacity ) {
241+ a .logger .V (6 ).Info ("Observed consumed capacity release" , "device id" , capacity .DeviceID , "consumed capacity" , capacity .ConsumedCapacity , "claim" , klog .KObj (claim ))
242+ deviceCapacities = append (deviceCapacities , capacity )
243+ })
170244 a .mutex .Lock ()
171245 defer a .mutex .Unlock ()
172246 for _ , deviceID := range deviceIDs {
173247 a .ids .Delete (deviceID )
174248 }
249+ for _ , shareID := range shareIDs {
250+ a .shareIDs .Delete (shareID )
251+ }
252+ for _ , capacity := range deviceCapacities {
253+ a .capacities .Remove (capacity )
254+ }
175255}
0 commit comments