@@ -21,9 +21,11 @@ import (
21
21
22
22
resourceapi "k8s.io/api/resource/v1"
23
23
"k8s.io/apimachinery/pkg/util/sets"
24
+ utilfeature "k8s.io/apiserver/pkg/util/feature"
24
25
"k8s.io/client-go/tools/cache"
25
26
"k8s.io/dynamic-resource-allocation/structured"
26
27
"k8s.io/klog/v2"
28
+ "k8s.io/kubernetes/pkg/features"
27
29
schedutil "k8s.io/kubernetes/pkg/scheduler/util"
28
30
"k8s.io/utils/ptr"
29
31
)
@@ -36,7 +38,11 @@ import (
36
38
// claims and are skipped without invoking the callback.
37
39
//
38
40
// 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 )) {
40
46
if claim .Status .Allocation == nil {
41
47
return
42
48
}
@@ -54,7 +60,24 @@ func foreachAllocatedDevice(claim *resourceapi.ResourceClaim, cb func(deviceID s
54
60
55
61
// None of the users of this helper need to abort iterating,
56
62
// 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 )
58
81
}
59
82
}
60
83
@@ -66,14 +89,20 @@ func foreachAllocatedDevice(claim *resourceapi.ResourceClaim, cb func(deviceID s
66
89
type allocatedDevices struct {
67
90
logger klog.Logger
68
91
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
71
97
}
72
98
73
99
func newAllocatedDevices (logger klog.Logger ) * allocatedDevices {
74
100
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 ),
77
106
}
78
107
}
79
108
@@ -84,6 +113,13 @@ func (a *allocatedDevices) Get() sets.Set[structured.DeviceID] {
84
113
return a .ids .Clone ()
85
114
}
86
115
116
+ func (a * allocatedDevices ) Capacities () structured.ConsumedCapacityCollection {
117
+ a .mutex .RLock ()
118
+ defer a .mutex .RUnlock ()
119
+
120
+ return a .capacities .Clone ()
121
+ }
122
+
87
123
func (a * allocatedDevices ) handlers () cache.ResourceEventHandler {
88
124
return cache.ResourceEventHandlerFuncs {
89
125
AddFunc : a .onAdd ,
@@ -142,16 +178,39 @@ func (a *allocatedDevices) addDevices(claim *resourceapi.ResourceClaim) {
142
178
// Locking of the mutex gets minimized by pre-computing what needs to be done
143
179
// without holding the lock.
144
180
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
+ )
149
202
150
203
a .mutex .Lock ()
151
204
defer a .mutex .Unlock ()
152
205
for _ , deviceID := range deviceIDs {
153
206
a .ids .Insert (deviceID )
154
207
}
208
+ for _ , shareID := range shareIDs {
209
+ a .shareIDs .Insert (shareID )
210
+ }
211
+ for _ , capacity := range deviceCapacities {
212
+ a .capacities .Insert (capacity )
213
+ }
155
214
}
156
215
157
216
func (a * allocatedDevices ) removeDevices (claim * resourceapi.ResourceClaim ) {
@@ -162,14 +221,35 @@ func (a *allocatedDevices) removeDevices(claim *resourceapi.ResourceClaim) {
162
221
// Locking of the mutex gets minimized by pre-computing what needs to be done
163
222
// without holding the lock.
164
223
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
+ })
170
244
a .mutex .Lock ()
171
245
defer a .mutex .Unlock ()
172
246
for _ , deviceID := range deviceIDs {
173
247
a .ids .Delete (deviceID )
174
248
}
249
+ for _ , shareID := range shareIDs {
250
+ a .shareIDs .Delete (shareID )
251
+ }
252
+ for _ , capacity := range deviceCapacities {
253
+ a .capacities .Remove (capacity )
254
+ }
175
255
}
0 commit comments