Skip to content

Commit 231df24

Browse files
authored
Merge pull request #535 from uniemimu/preferred
api: add PreferredAllocator interface
2 parents c2e323b + 4c92093 commit 231df24

File tree

5 files changed

+44
-23
lines changed

5 files changed

+44
-23
lines changed

pkg/deviceplugin/api.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ type PostAllocator interface {
9696
PostAllocate(*pluginapi.AllocateResponse) error
9797
}
9898

99+
// PreferredAllocator is an optional interface implemented by device plugins.
100+
type PreferredAllocator interface {
101+
// GetPreferredAllocation defines the list of devices preferred for allocating next.
102+
GetPreferredAllocation(*pluginapi.PreferredAllocationRequest) (*pluginapi.PreferredAllocationResponse, error)
103+
}
104+
99105
// ContainerPreStarter is an optional interface implemented by device plugins.
100106
type ContainerPreStarter interface {
101107
// PreStartContainer defines device initialization function before container is started.

pkg/deviceplugin/manager.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ import (
2222
pluginapi "k8s.io/kubelet/pkg/apis/deviceplugin/v1beta1"
2323
)
2424

25+
type postAllocateFunc func(*pluginapi.AllocateResponse) error
26+
type preStartContainerFunc func(*pluginapi.PreStartContainerRequest) error
27+
type getPreferredAllocationFunc func(*pluginapi.PreferredAllocationRequest) (*pluginapi.PreferredAllocationResponse, error)
28+
2529
// updateInfo contains info for added, updated and deleted devices.
2630
type updateInfo struct {
2731
Added DeviceTree
@@ -73,7 +77,7 @@ type Manager struct {
7377
devicePlugin Scanner
7478
namespace string
7579
servers map[string]devicePluginServer
76-
createServer func(string, func(*pluginapi.AllocateResponse) error, func(*pluginapi.PreStartContainerRequest) error) devicePluginServer
80+
createServer func(string, postAllocateFunc, preStartContainerFunc, getPreferredAllocationFunc) devicePluginServer
7781
}
7882

7983
// NewManager creates a new instance of Manager.
@@ -107,8 +111,9 @@ func (m *Manager) Run() {
107111
func (m *Manager) handleUpdate(update updateInfo) {
108112
klog.V(4).Info("Received dev updates:", update)
109113
for devType, devices := range update.Added {
110-
var postAllocate func(*pluginapi.AllocateResponse) error
111-
var preStartContainer func(*pluginapi.PreStartContainerRequest) error
114+
var postAllocate postAllocateFunc
115+
var preStartContainer preStartContainerFunc
116+
var getPreferredAllocation getPreferredAllocationFunc
112117

113118
if postAllocator, ok := m.devicePlugin.(PostAllocator); ok {
114119
postAllocate = postAllocator.PostAllocate
@@ -118,7 +123,11 @@ func (m *Manager) handleUpdate(update updateInfo) {
118123
preStartContainer = containerPreStarter.PreStartContainer
119124
}
120125

121-
m.servers[devType] = m.createServer(devType, postAllocate, preStartContainer)
126+
if preferredAllocator, ok := m.devicePlugin.(PreferredAllocator); ok {
127+
getPreferredAllocation = preferredAllocator.GetPreferredAllocation
128+
}
129+
130+
m.servers[devType] = m.createServer(devType, postAllocate, preStartContainer, getPreferredAllocation)
122131
go func(dt string) {
123132
err := m.servers[dt].Serve(m.namespace)
124133
if err != nil {

pkg/deviceplugin/manager_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ func TestHandleUpdate(t *testing.T) {
265265
mgr := Manager{
266266
devicePlugin: &devicePluginStub{},
267267
servers: tt.servers,
268-
createServer: func(string, func(*pluginapi.AllocateResponse) error, func(*pluginapi.PreStartContainerRequest) error) devicePluginServer {
268+
createServer: func(string, postAllocateFunc, preStartContainerFunc, getPreferredAllocationFunc) devicePluginServer {
269269
return &serverStub{}
270270
},
271271
}
@@ -279,7 +279,7 @@ func TestHandleUpdate(t *testing.T) {
279279

280280
func TestRun(t *testing.T) {
281281
mgr := NewManager("testnamespace", &devicePluginStub{})
282-
mgr.createServer = func(string, func(*pluginapi.AllocateResponse) error, func(*pluginapi.PreStartContainerRequest) error) devicePluginServer {
282+
mgr.createServer = func(string, postAllocateFunc, preStartContainerFunc, getPreferredAllocationFunc) devicePluginServer {
283283
return &serverStub{}
284284
}
285285
mgr.Run()

pkg/deviceplugin/server.go

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,27 +51,30 @@ type devicePluginServer interface {
5151

5252
// server implements devicePluginServer and pluginapi.PluginInterfaceServer interfaces.
5353
type server struct {
54-
devType string
55-
grpcServer *grpc.Server
56-
updatesCh chan map[string]DeviceInfo
57-
devices map[string]DeviceInfo
58-
postAllocate func(*pluginapi.AllocateResponse) error
59-
preStartContainer func(*pluginapi.PreStartContainerRequest) error
60-
state serverState
61-
stateMutex sync.Mutex
54+
devType string
55+
grpcServer *grpc.Server
56+
updatesCh chan map[string]DeviceInfo
57+
devices map[string]DeviceInfo
58+
postAllocate postAllocateFunc
59+
preStartContainer preStartContainerFunc
60+
getPreferredAllocation getPreferredAllocationFunc
61+
state serverState
62+
stateMutex sync.Mutex
6263
}
6364

6465
// newServer creates a new server satisfying the devicePluginServer interface.
6566
func newServer(devType string,
66-
postAllocate func(*pluginapi.AllocateResponse) error,
67-
preStartContainer func(*pluginapi.PreStartContainerRequest) error) devicePluginServer {
67+
postAllocate postAllocateFunc,
68+
preStartContainer preStartContainerFunc,
69+
getPreferredAllocation getPreferredAllocationFunc) devicePluginServer {
6870
return &server{
69-
devType: devType,
70-
updatesCh: make(chan map[string]DeviceInfo, 1), // TODO: is 1 needed?
71-
devices: make(map[string]DeviceInfo),
72-
postAllocate: postAllocate,
73-
preStartContainer: preStartContainer,
74-
state: uninitialized,
71+
devType: devType,
72+
updatesCh: make(chan map[string]DeviceInfo, 1), // TODO: is 1 needed?
73+
devices: make(map[string]DeviceInfo),
74+
postAllocate: postAllocate,
75+
preStartContainer: preStartContainer,
76+
getPreferredAllocation: getPreferredAllocation,
77+
state: uninitialized,
7578
}
7679
}
7780

@@ -162,6 +165,9 @@ func (srv *server) PreStartContainer(ctx context.Context, rqt *pluginapi.PreStar
162165
}
163166

164167
func (srv *server) GetPreferredAllocation(ctx context.Context, rqt *pluginapi.PreferredAllocationRequest) (*pluginapi.PreferredAllocationResponse, error) {
168+
if srv.getPreferredAllocation != nil {
169+
return srv.getPreferredAllocation(rqt)
170+
}
165171
return nil, errors.New("GetPreferredAllocation should not be called as this device plugin doesn't implement it")
166172
}
167173

pkg/deviceplugin/server_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ func TestGetPreferredAllocation(t *testing.T) {
530530
}
531531

532532
func TestNewServer(t *testing.T) {
533-
_ = newServer("test", nil, nil)
533+
_ = newServer("test", nil, nil, nil)
534534
}
535535

536536
func TestUpdate(t *testing.T) {

0 commit comments

Comments
 (0)