@@ -23,182 +23,44 @@ import (
2323 resourceapi "k8s.io/api/resource/v1"
2424 "k8s.io/apimachinery/pkg/api/resource"
2525 "k8s.io/apimachinery/pkg/types"
26- "k8s.io/apimachinery/pkg/util/sets"
2726 "k8s.io/apimachinery/pkg/util/uuid"
28- draapi "k8s.io/dynamic-resource-allocation/api "
27+ "k8s.io/dynamic-resource-allocation/structured/schedulerapi "
2928)
3029
31- type DeviceID struct {
32- Driver , Pool , Device draapi.UniqueString
33- }
34-
35- func (d DeviceID ) String () string {
36- return d .Driver .String () + "/" + d .Pool .String () + "/" + d .Device .String ()
37- }
38-
30+ // Type aliases pointing to the schedulerapi package where the actual
31+ // definitions are maintained. This ensures that any changes to these types
32+ // require autoscaler approval.
33+ type DeviceID = schedulerapi.DeviceID
34+ type SharedDeviceID = schedulerapi.SharedDeviceID
35+ type AllocatedState = schedulerapi.AllocatedState
36+ type ConsumedCapacity = schedulerapi.ConsumedCapacity
37+ type ConsumedCapacityCollection = schedulerapi.ConsumedCapacityCollection
38+ type DeviceConsumedCapacity = schedulerapi.DeviceConsumedCapacity
39+
40+ // Wrapper functions that delegate to the schedulerapi package
3941func MakeDeviceID (driver , pool , device string ) DeviceID {
40- return DeviceID {
41- Driver : draapi .MakeUniqueString (driver ),
42- Pool : draapi .MakeUniqueString (pool ),
43- Device : draapi .MakeUniqueString (device ),
44- }
45- }
46-
47- type SharedDeviceID struct {
48- Driver , Pool , Device , ShareID draapi.UniqueString
42+ return schedulerapi .MakeDeviceID (driver , pool , device )
4943}
5044
51- // MakeSharedDeviceID creates a SharedDeviceID by extending MakeDeviceID with shareID.
5245func MakeSharedDeviceID (deviceID DeviceID , shareID * types.UID ) SharedDeviceID {
53- // This function avoids disruptive changes to MakeDeviceID
54- // while enabling ShareID as part of the device key.
55- var shareIDStr string
56- if shareID != nil {
57- shareIDStr = string (* shareID )
58- }
59- return SharedDeviceID {
60- Driver : deviceID .Driver ,
61- Pool : deviceID .Pool ,
62- Device : deviceID .Device ,
63- ShareID : draapi .MakeUniqueString (shareIDStr ),
64- }
65- }
66-
67- func (d SharedDeviceID ) String () string {
68- deviceIDStr := d .Driver .String () + "/" + d .Pool .String () + "/" + d .Device .String ()
69- if d .ShareID .String () != "" {
70- deviceIDStr += "/" + d .ShareID .String ()
71- }
72- return deviceIDStr
46+ return schedulerapi .MakeSharedDeviceID (deviceID , shareID )
7347}
7448
75- func GenerateShareID () * types.UID {
76- newUID := uuid .NewUUID ()
77- return & newUID
78- }
79-
80- // AllocatedState packs information of allocated devices which is gathered from allocated resource claims.
81- type AllocatedState struct {
82- AllocatedDevices sets.Set [DeviceID ]
83- AllocatedSharedDeviceIDs sets.Set [SharedDeviceID ]
84- AggregatedCapacity ConsumedCapacityCollection
85- }
86-
87- // ConsumedCapacity defines consumable capacity values
88- type ConsumedCapacity map [resourceapi.QualifiedName ]* resource.Quantity
89-
90- // ConsumedCapacityCollection collects consumable capacity values of each device
91- type ConsumedCapacityCollection map [DeviceID ]ConsumedCapacity
92-
93- // NewConsumedCapacity initiates a new map of consumable capacity values
9449func NewConsumedCapacity () ConsumedCapacity {
95- return make ( ConsumedCapacity )
50+ return schedulerapi . NewConsumedCapacity ( )
9651}
9752
98- // NewConsumedCapacity initiates a new map of device's consumable capacity values
9953func NewConsumedCapacityCollection () ConsumedCapacityCollection {
100- return make (ConsumedCapacityCollection )
101- }
102-
103- // Clone makes a copy of consumed capacity values
104- func (s ConsumedCapacity ) Clone () ConsumedCapacity {
105- clone := make (ConsumedCapacity )
106- for name , quantity := range s {
107- q := quantity .DeepCopy ()
108- clone [name ] = & q
109- }
110- return clone
54+ return schedulerapi .NewConsumedCapacityCollection ()
11155}
11256
113- // Add adds quantity to corresponding consumable capacity,
114- // and creates a new entry if no capacity created yet.
115- func (s ConsumedCapacity ) Add (addedCapacity ConsumedCapacity ) {
116- for name , quantity := range addedCapacity {
117- val := quantity .DeepCopy ()
118- if _ , found := s [name ]; found {
119- s [name ].Add (val )
120- } else {
121- s [name ] = & val
122- }
123- }
124- }
125-
126- // Sub subtracts quantity,
127- // and ignore if no capacity entry found.
128- func (s ConsumedCapacity ) Sub (subtractedCapacity ConsumedCapacity ) {
129- for name , quantity := range subtractedCapacity {
130- if _ , found := s [name ]; found {
131- s [name ].Sub (* quantity )
132- }
133- }
134- }
135-
136- // Empty return true if all quantity is zero.
137- func (s ConsumedCapacity ) Empty () bool {
138- for _ , quantity := range s {
139- if ! quantity .IsZero () {
140- return false
141- }
142- }
143- return true
144- }
145-
146- // Clone makes a copy of ConsumedCapacity of each capacity.
147- func (c ConsumedCapacityCollection ) Clone () ConsumedCapacityCollection {
148- clone := NewConsumedCapacityCollection ()
149- for deviceID , share := range c {
150- clone [deviceID ] = share .Clone ()
151- }
152- return clone
153- }
154-
155- // Insert adds a new allocated capacity to the collection.
156- func (c ConsumedCapacityCollection ) Insert (cap DeviceConsumedCapacity ) {
157- consumedCapacity := cap .ConsumedCapacity
158- if _ , found := c [cap .DeviceID ]; found {
159- c [cap .DeviceID ].Add (consumedCapacity )
160- } else {
161- c [cap .DeviceID ] = consumedCapacity .Clone ()
162- }
163- }
164-
165- // Remove removes an allocated capacity from the collection.
166- func (c ConsumedCapacityCollection ) Remove (cap DeviceConsumedCapacity ) {
167- if _ , found := c [cap .DeviceID ]; found {
168- c [cap .DeviceID ].Sub (cap .ConsumedCapacity )
169- if c [cap .DeviceID ].Empty () {
170- delete (c , cap .DeviceID )
171- }
172- }
173- }
174-
175- // DeviceConsumedCapacity contains consumed capacity result within device allocation.
176- type DeviceConsumedCapacity struct {
177- DeviceID
178- ConsumedCapacity
179- }
180-
181- // NewDeviceConsumedCapacity creates DeviceConsumedCapacity instance from device ID and its consumed capacity.
18257func NewDeviceConsumedCapacity (deviceID DeviceID , consumedCapacity map [resourceapi.QualifiedName ]resource.Quantity ) DeviceConsumedCapacity {
183- allocatedCapacity := NewConsumedCapacity ()
184- for name , quantity := range consumedCapacity {
185- allocatedCapacity [name ] = & quantity
186- }
187- return DeviceConsumedCapacity {
188- DeviceID : deviceID ,
189- ConsumedCapacity : allocatedCapacity ,
190- }
58+ return schedulerapi .NewDeviceConsumedCapacity (deviceID , consumedCapacity )
19159}
19260
193- // Clone makes a copy of DeviceConsumedCapacity.
194- func (a DeviceConsumedCapacity ) Clone () DeviceConsumedCapacity {
195- return DeviceConsumedCapacity {
196- DeviceID : a .DeviceID ,
197- ConsumedCapacity : a .ConsumedCapacity .Clone (),
198- }
199- }
200-
201- // String returns formatted device ID.
202- func (a DeviceConsumedCapacity ) String () string {
203- return a .DeviceID .String ()
61+ // GenerateShareID is a helper function that generates a new share ID.
62+ // This remains in the internal package as it's a utility function.
63+ func GenerateShareID () * types.UID {
64+ newUID := uuid .NewUUID ()
65+ return & newUID
20466}
0 commit comments