Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pkg/deviceplugin/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ type PostAllocator interface {
PostAllocate(*pluginapi.AllocateResponse) error
}

// PreferredAllocator is an optional interface implemented by device plugins.
type PreferredAllocator interface {
// GetPreferredAllocation defines the list of devices preferred for allocating next.
GetPreferredAllocation(*pluginapi.PreferredAllocationRequest) (*pluginapi.PreferredAllocationResponse, error)
}

// ContainerPreStarter is an optional interface implemented by device plugins.
type ContainerPreStarter interface {
// PreStartContainer defines device initialization function before container is started.
Expand Down
17 changes: 13 additions & 4 deletions pkg/deviceplugin/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ import (
pluginapi "k8s.io/kubelet/pkg/apis/deviceplugin/v1beta1"
)

type postAllocateFunc func(*pluginapi.AllocateResponse) error
type preStartContainerFunc func(*pluginapi.PreStartContainerRequest) error
type getPreferredAllocationFunc func(*pluginapi.PreferredAllocationRequest) (*pluginapi.PreferredAllocationResponse, error)

// updateInfo contains info for added, updated and deleted devices.
type updateInfo struct {
Added DeviceTree
Expand Down Expand Up @@ -73,7 +77,7 @@ type Manager struct {
devicePlugin Scanner
namespace string
servers map[string]devicePluginServer
createServer func(string, func(*pluginapi.AllocateResponse) error, func(*pluginapi.PreStartContainerRequest) error) devicePluginServer
createServer func(string, postAllocateFunc, preStartContainerFunc, getPreferredAllocationFunc) devicePluginServer
}

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

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

m.servers[devType] = m.createServer(devType, postAllocate, preStartContainer)
if preferredAllocator, ok := m.devicePlugin.(PreferredAllocator); ok {
getPreferredAllocation = preferredAllocator.GetPreferredAllocation
}

m.servers[devType] = m.createServer(devType, postAllocate, preStartContainer, getPreferredAllocation)
go func(dt string) {
err := m.servers[dt].Serve(m.namespace)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/deviceplugin/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ func TestHandleUpdate(t *testing.T) {
mgr := Manager{
devicePlugin: &devicePluginStub{},
servers: tt.servers,
createServer: func(string, func(*pluginapi.AllocateResponse) error, func(*pluginapi.PreStartContainerRequest) error) devicePluginServer {
createServer: func(string, postAllocateFunc, preStartContainerFunc, getPreferredAllocationFunc) devicePluginServer {
return &serverStub{}
},
}
Expand All @@ -279,7 +279,7 @@ func TestHandleUpdate(t *testing.T) {

func TestRun(t *testing.T) {
mgr := NewManager("testnamespace", &devicePluginStub{})
mgr.createServer = func(string, func(*pluginapi.AllocateResponse) error, func(*pluginapi.PreStartContainerRequest) error) devicePluginServer {
mgr.createServer = func(string, postAllocateFunc, preStartContainerFunc, getPreferredAllocationFunc) devicePluginServer {
return &serverStub{}
}
mgr.Run()
Expand Down
38 changes: 22 additions & 16 deletions pkg/deviceplugin/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,27 +51,30 @@ type devicePluginServer interface {

// server implements devicePluginServer and pluginapi.PluginInterfaceServer interfaces.
type server struct {
devType string
grpcServer *grpc.Server
updatesCh chan map[string]DeviceInfo
devices map[string]DeviceInfo
postAllocate func(*pluginapi.AllocateResponse) error
preStartContainer func(*pluginapi.PreStartContainerRequest) error
state serverState
stateMutex sync.Mutex
devType string
grpcServer *grpc.Server
updatesCh chan map[string]DeviceInfo
devices map[string]DeviceInfo
postAllocate postAllocateFunc
preStartContainer preStartContainerFunc
getPreferredAllocation getPreferredAllocationFunc
state serverState
stateMutex sync.Mutex
}

// newServer creates a new server satisfying the devicePluginServer interface.
func newServer(devType string,
postAllocate func(*pluginapi.AllocateResponse) error,
preStartContainer func(*pluginapi.PreStartContainerRequest) error) devicePluginServer {
postAllocate postAllocateFunc,
preStartContainer preStartContainerFunc,
getPreferredAllocation getPreferredAllocationFunc) devicePluginServer {
return &server{
devType: devType,
updatesCh: make(chan map[string]DeviceInfo, 1), // TODO: is 1 needed?
devices: make(map[string]DeviceInfo),
postAllocate: postAllocate,
preStartContainer: preStartContainer,
state: uninitialized,
devType: devType,
updatesCh: make(chan map[string]DeviceInfo, 1), // TODO: is 1 needed?
devices: make(map[string]DeviceInfo),
postAllocate: postAllocate,
preStartContainer: preStartContainer,
getPreferredAllocation: getPreferredAllocation,
state: uninitialized,
}
}

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

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

Expand Down
2 changes: 1 addition & 1 deletion pkg/deviceplugin/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ func TestGetPreferredAllocation(t *testing.T) {
}

func TestNewServer(t *testing.T) {
_ = newServer("test", nil, nil)
_ = newServer("test", nil, nil, nil)
}

func TestUpdate(t *testing.T) {
Expand Down