Skip to content

Commit ae60c55

Browse files
authored
Merge pull request kubernetes#134404 from nmn3m/dra-consumable-capacity-autoscaler-contract
Dedicated package for scheduler interaction with DRA structured types
2 parents 28c1e7a + dd8d0e6 commit ae60c55

File tree

5 files changed

+262
-175
lines changed

5 files changed

+262
-175
lines changed

pkg/scheduler/framework/autoscaler_contract/lister_contract_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
resourceapi "k8s.io/api/resource/v1"
2525
"k8s.io/apimachinery/pkg/types"
2626
"k8s.io/apimachinery/pkg/util/sets"
27-
"k8s.io/dynamic-resource-allocation/structured"
27+
"k8s.io/dynamic-resource-allocation/structured/schedulerapi"
2828
fwk "k8s.io/kube-scheduler/framework"
2929
)
3030

@@ -96,11 +96,11 @@ func (r *resourceClaimTrackerContract) Get(_, _ string) (*resourceapi.ResourceCl
9696
return nil, nil
9797
}
9898

99-
func (r *resourceClaimTrackerContract) ListAllAllocatedDevices() (sets.Set[structured.DeviceID], error) {
99+
func (r *resourceClaimTrackerContract) ListAllAllocatedDevices() (sets.Set[schedulerapi.DeviceID], error) {
100100
return nil, nil
101101
}
102102

103-
func (r *resourceClaimTrackerContract) GatherAllocatedState() (*structured.AllocatedState, error) {
103+
func (r *resourceClaimTrackerContract) GatherAllocatedState() (*schedulerapi.AllocatedState, error) {
104104
return nil, nil
105105
}
106106

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

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"k8s.io/dynamic-resource-allocation/structured/internal/experimental"
3232
"k8s.io/dynamic-resource-allocation/structured/internal/incubating"
3333
"k8s.io/dynamic-resource-allocation/structured/internal/stable"
34+
"k8s.io/dynamic-resource-allocation/structured/schedulerapi"
3435
)
3536

3637
// To keep the code in different packages simple, type aliases are used everywhere.
@@ -40,30 +41,32 @@ import (
4041

4142
type DeviceClassLister = internal.DeviceClassLister
4243
type Features = internal.Features
43-
type DeviceID = internal.DeviceID
44+
45+
// Type aliases to schedulerapi package for types that are part of the
46+
// scheduler and autoscaler contract. This ensures that changes to these
47+
// types require autoscaler approval.
48+
type DeviceID = schedulerapi.DeviceID
49+
type AllocatedState = schedulerapi.AllocatedState
50+
type SharedDeviceID = schedulerapi.SharedDeviceID
51+
type DeviceConsumedCapacity = schedulerapi.DeviceConsumedCapacity
52+
type ConsumedCapacityCollection = schedulerapi.ConsumedCapacityCollection
53+
type ConsumedCapacity = schedulerapi.ConsumedCapacity
4454

4555
func MakeDeviceID(driver, pool, device string) DeviceID {
46-
return internal.MakeDeviceID(driver, pool, device)
56+
return schedulerapi.MakeDeviceID(driver, pool, device)
4757
}
4858

49-
// types_experimental
50-
type AllocatedState = internal.AllocatedState
51-
type SharedDeviceID = internal.SharedDeviceID
52-
type DeviceConsumedCapacity = internal.DeviceConsumedCapacity
53-
type ConsumedCapacityCollection = internal.ConsumedCapacityCollection
54-
type ConsumedCapacity = internal.ConsumedCapacity
55-
5659
func MakeSharedDeviceID(deviceID DeviceID, shareID *types.UID) SharedDeviceID {
57-
return internal.MakeSharedDeviceID(deviceID, shareID)
60+
return schedulerapi.MakeSharedDeviceID(deviceID, shareID)
5861
}
5962

6063
func NewConsumedCapacityCollection() ConsumedCapacityCollection {
61-
return internal.NewConsumedCapacityCollection()
64+
return schedulerapi.NewConsumedCapacityCollection()
6265
}
6366

6467
func NewDeviceConsumedCapacity(deviceID DeviceID,
6568
consumedCapacity map[resourceapi.QualifiedName]resource.Quantity) DeviceConsumedCapacity {
66-
return internal.NewDeviceConsumedCapacity(deviceID, consumedCapacity)
69+
return schedulerapi.NewDeviceConsumedCapacity(deviceID, consumedCapacity)
6770
}
6871

6972
// Allocator calculates how to allocate a set of unallocated claims which use

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

Lines changed: 22 additions & 160 deletions
Original file line numberDiff line numberDiff line change
@@ -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
3941
func 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.
5245
func 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
9449
func NewConsumedCapacity() ConsumedCapacity {
95-
return make(ConsumedCapacity)
50+
return schedulerapi.NewConsumedCapacity()
9651
}
9752

98-
// NewConsumedCapacity initiates a new map of device's consumable capacity values
9953
func 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.
18257
func 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
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# See the OWNERS file documentation: https://git.k8s.io/community/contributors/guide/owners.md
2+
# Disable inheritance as code changes under this package should be known to sig-autoscaling team.
3+
options:
4+
no_parent_owners: true
5+
approvers:
6+
- sig-autoscaling-maintainers

0 commit comments

Comments
 (0)