github.com/ferranbt/nomad@v0.9.3-0.20190607002617-85c449b7667c/plugins/device/plugin.go (about)

     1  package device
     2  
     3  import (
     4  	"context"
     5  
     6  	log "github.com/hashicorp/go-hclog"
     7  	plugin "github.com/hashicorp/go-plugin"
     8  	"github.com/hashicorp/nomad/plugins/base"
     9  	bproto "github.com/hashicorp/nomad/plugins/base/proto"
    10  	"github.com/hashicorp/nomad/plugins/device/proto"
    11  	"google.golang.org/grpc"
    12  )
    13  
    14  // PluginDevice is wraps a DevicePlugin and implements go-plugins GRPCPlugin
    15  // interface to expose the interface over gRPC.
    16  type PluginDevice struct {
    17  	plugin.NetRPCUnsupportedPlugin
    18  	Impl DevicePlugin
    19  }
    20  
    21  func (p *PluginDevice) GRPCServer(broker *plugin.GRPCBroker, s *grpc.Server) error {
    22  	proto.RegisterDevicePluginServer(s, &devicePluginServer{
    23  		impl:   p.Impl,
    24  		broker: broker,
    25  	})
    26  	return nil
    27  }
    28  
    29  func (p *PluginDevice) GRPCClient(ctx context.Context, broker *plugin.GRPCBroker, c *grpc.ClientConn) (interface{}, error) {
    30  	return &devicePluginClient{
    31  		doneCtx: ctx,
    32  		client:  proto.NewDevicePluginClient(c),
    33  		BasePluginClient: &base.BasePluginClient{
    34  			Client:  bproto.NewBasePluginClient(c),
    35  			DoneCtx: ctx,
    36  		},
    37  	}, nil
    38  }
    39  
    40  // Serve is used to serve a device plugin
    41  func Serve(dev DevicePlugin, logger log.Logger) {
    42  	plugin.Serve(&plugin.ServeConfig{
    43  		HandshakeConfig: base.Handshake,
    44  		Plugins: map[string]plugin.Plugin{
    45  			base.PluginTypeBase:   &base.PluginBase{Impl: dev},
    46  			base.PluginTypeDevice: &PluginDevice{Impl: dev},
    47  		},
    48  		GRPCServer: plugin.DefaultGRPCServer,
    49  		Logger:     logger,
    50  	})
    51  }