2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2018-08-13 17:29:29 +00:00
|
|
|
package device
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2018-08-17 22:19:44 +00:00
|
|
|
log "github.com/hashicorp/go-hclog"
|
2018-08-13 17:29:29 +00:00
|
|
|
plugin "github.com/hashicorp/go-plugin"
|
|
|
|
"github.com/hashicorp/nomad/plugins/base"
|
|
|
|
bproto "github.com/hashicorp/nomad/plugins/base/proto"
|
|
|
|
"github.com/hashicorp/nomad/plugins/device/proto"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
// PluginDevice is wraps a DevicePlugin and implements go-plugins GRPCPlugin
|
|
|
|
// interface to expose the interface over gRPC.
|
|
|
|
type PluginDevice struct {
|
|
|
|
plugin.NetRPCUnsupportedPlugin
|
|
|
|
Impl DevicePlugin
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PluginDevice) GRPCServer(broker *plugin.GRPCBroker, s *grpc.Server) error {
|
|
|
|
proto.RegisterDevicePluginServer(s, &devicePluginServer{
|
|
|
|
impl: p.Impl,
|
|
|
|
broker: broker,
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PluginDevice) GRPCClient(ctx context.Context, broker *plugin.GRPCBroker, c *grpc.ClientConn) (interface{}, error) {
|
|
|
|
return &devicePluginClient{
|
2018-09-28 17:09:01 +00:00
|
|
|
doneCtx: ctx,
|
|
|
|
client: proto.NewDevicePluginClient(c),
|
2018-08-13 17:29:29 +00:00
|
|
|
BasePluginClient: &base.BasePluginClient{
|
2018-11-13 01:09:27 +00:00
|
|
|
Client: bproto.NewBasePluginClient(c),
|
|
|
|
DoneCtx: ctx,
|
2018-08-13 17:29:29 +00:00
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
2018-08-17 22:19:44 +00:00
|
|
|
|
|
|
|
// Serve is used to serve a device plugin
|
|
|
|
func Serve(dev DevicePlugin, logger log.Logger) {
|
|
|
|
plugin.Serve(&plugin.ServeConfig{
|
|
|
|
HandshakeConfig: base.Handshake,
|
|
|
|
Plugins: map[string]plugin.Plugin{
|
|
|
|
base.PluginTypeBase: &base.PluginBase{Impl: dev},
|
|
|
|
base.PluginTypeDevice: &PluginDevice{Impl: dev},
|
|
|
|
},
|
|
|
|
GRPCServer: plugin.DefaultGRPCServer,
|
|
|
|
Logger: logger,
|
|
|
|
})
|
|
|
|
}
|