intro

If you don't know what go-plugin is, don't worry, here is a small introduction on the subject matter:

Back in the old days when Go didn't have the `plugin` package, HashiCorp was desperately looking for a way to use plugins.

In the old days, Lua plus Go wasn't really a thing yet, and to be honest, nobody wants to write Lua ( joking!).

And thus Mitchell had this brilliant idea of using RPC over the local network to serve a local interface as something that could easily be implemented with any other language that supported RPC. This sounds convoluted but has many benefits! For example, your code will never crash because of a plugin and the ability to use any language to implement a plugin. Not just Go.

It has been a battle-hardened solution for years now and is being actively used by Terraform, Vault, Consule, and especially Packer. All using go-plugin in order to provide a much needed flexibility. Writing a plugin is easy. Or so they say.

It can get complicated quickly, for example, if you are trying to use GRPC. You can lose sight of what exactly you'll need to implement, where and why; or utilizing various languages or using go-plugins in your own project and extending your CLI with pluggable components.

These are all nothing to sneeze at. Suddenly you'll find yourself with hundreds of lines of code pasted from various examples and yet nothing works. Or worse, it DOES work but you have no idea how. Then you find yourself needing to extend it with a new capability, or you find an elusive bug and can't trace its origins.

Fear not. I'll try to demystify things and draw a clear picture about how it works and how the pieces fit together. Let's start at the beginning.

Basic plugin

Let's start by writing a simple Go GRPC plugin. In fact, we can go through the basic example in the go-plugin’s repository which can be quite confusing when first starting out. We'll go step-by-step, and the switch to GRPC will be much easier!

Basic concepts

Server

In the case of plugins, the Server is the one serving the plugin's implementation. This means the server will have to provide the implementation to an interface.

Client

The Client calls the server in order to execute the desired behavior. The underlying logic will connect to the server running on localhost on a random higher port, call the wanted function’s implementation and wait for a response. Once the response is received provide that back to the calling Client.

Implementation

The main function

Logger

The plugins defined here use stdout in a special way. If you aren't writing a Go based plugin, you will have to do that yourself by outputting something like this:
  1. 1|1|tcp|127.0.0.1:1234|grpc
We'll come back to this later. Suffice to say the framework will pick this up and will connect to the plugin based on the output. In order to get some output back, we must define a special logger:
  1. // Create an hclog.Logger
  2. logger := hclog.New(&hclog.LoggerOptions{
  3.     Name:   "plugin",
  4.     Output: os.Stdout,
  5.     Level:  hclog.Debug,
  6. })

NewClient

  1. // We're a host! Start by launching the plugin process.
  2. client := plugin.NewClient(&plugin.ClientConfig{
  3.     HandshakeConfig: handshakeConfig,
  4.     Plugins:         pluginMap,
  5.     Cmd:             exec.Command("./plugin/greeter"),
  6.     Logger:          logger,
  7. })
  8. defer client.Kill()
What is happening here? Let's see one by one: HandshakeConfig: handshakeConfig: This part is the handshake configuration of the plugin. It has a nice comment as well.
  1. // handshakeConfigs are used to just do a basic handshake between
  2. // a plugin and host. If the handshake fails, a user friendly error is shown.
  3. // This prevents users from executing bad plugins or executing a plugin
  4. // directory. It is a UX feature, not a security feature.
  5. var handshakeConfig = plugin.HandshakeConfig{
  6.     ProtocolVersion:  1,
  7.     MagicCookieKey:   "BASIC_PLUGIN",
  8.     MagicCookieValue: "hello",
  9. }
The ProtocolVersion here is used in order to maintain compatibility with your current plugin versions. It's basically like an API version. If you increase this, you will have two options. Don't accept lower protocol versions nor switch to the version number and use a different client implementation for a lower version than for a higher version. This way you will maintain backwards compatibility. The MagicCookieKey and MagicCookieValue are used for a basic handshake which the comment is talking about. You have to set this ONCE for your application. Never change it again, for if you do, your plugins will no longer work. For uniqueness sake, I suggest using UUID. Cmd is one of the most important parts about a plugin. Basically how plugins work is that they boil down to a compiled binary which is executed and starts an RPC server. This is where you will have to define the binary which will be executed and does all this. Since this is all happening locally, (please keep in mind that Go-plugins only support localhost, and for a good reason), these binaries will most likely sit next to your application's binary or in a pre-configured global location. Something like: ~/.config/my-app/plugins. This is individual for each plugin of course. The plugins can be autoloaded via a discovery function given a path and a glob. And last but not least is the Plugins map. This map is used in order to identify a plugin called Dispense. This map is globally available and must stay consistent in order for all the plugins to work:
  1. // pluginMap is the map of plugins we can dispense.
  2. var pluginMap = map[string]plugin.Pluglin   "greeter": &example.GreeterPlugin{},
  3. }
You can see that the key is the name of the plugin and the value is the plugin. We then proceed to create an RPC client:
  1. // Connect via RPC
  2. rpcClient, err := client.Client()
  3. if err != nil {
  4.     log.Fatal(err)
  5. }
Nothing fancy about this one... Now comes the interesting part:
  1. // Request the plugin
  2. raw, err := rpcClient.Dispense("greeter")
  3. if err != nil {
  4.     log.Fatal(err)
  5. }
What's happening here? Dispense will look in the above created map and search for the plugin. If it cannot find it, it will throw an error at us. If it does find it, it will cast this plugin to an RPC or a GRPC type plugin. Then proceed to create an RPC or a GRPC client out of it. There is no call yet. This is just creating a client and parsing it to a respective representation. Now comes the magic:
  1. // We should have a Greeter now! This feels like a normal interface
  2. // implementation but is in fact over an RPC connection.
  3. greeter := raw.(example.Greeter)
  4. fmt.Println(greeter.Greet())
Here we are type asserting our raw GRPC client into our own plugin type. This is so we can call the respective function on the plugin! Once that's done we will have a {client,struct,implementation} that can be called like a simple function. The implementation right now comes from greeter_impl.go, but that will change once protoc makes an appearance. Behind the scenes, go-plugin will do a bunch of hat tricks with multiplexing TCP connections as well as a remote procedure call to our plugin. Our plugin then will run the function, generate some kind of output, and will then send that back for the waiting client. The client will then proceed to parse the message into a given response type and will then return it back to the client’s callee. This concludes main.go for now.

The Interface

Now let’s investigate the Interface. The interface is used to provide calling details. This interface will be what defines our plugins’ capabilities. How does our Greeter look like?
  1. // Greeter is the interface that we're exposing as a plugin.
  2. type Greeter interface {
  3.     Greet() string
  4. }
This is pretty simple. It defines a function which will return a string typed value. Now, we will need a couple of things for this to work. Firstly we need something which defines the RPC workings. go-plugin is working with `net/http` inside. It also uses something called Yamux for connection multiplexing, but we needn’t worry about this detail. Implementing the RPC details looks like this:
  1. // Here is an implementation that talks over RPC
  2. type GreeterRPC struct {
  3.     client *rpc.Client
  4. }
  5.  
  6. func (g *GreeterRPC) Greet() string {
  7.     var resp string
  8.     err := g.client.Call("Plugin.Greet", new(interface{}), &resp)
  9.     if err != nil {
  10.         // You usually want your interfaces to return errors. If they don't,
  11.         // there isn't much other choice here.
  12.         panic(err)
  13.     }
  14.  
  15.     return resp
  16. }
Here the GreeterRPC struct is an RPC specific implementation that will handle communication over RPC. This is Client in this setup. In case of gRPC, this would look something like this:
  1. // GRPCClient is an implementation of KV that talks over RPC.
  2. type GreeterGRPC struct{ client proto.GreeterClient }
  3.  
  4. func (m *GreeterGRPC) Greet() (string, error) {
  5.     s, err := m.client.Greet(context.Background(), &proto.Empty{})
  6.     return s, err
  7. }
What is happening here? What's Proto and what is GreeterClient? GRPC uses Google's protoc library to serialize and unserialize data. proto.GreeterClient is generated Go code by protoc. This code is a skeleton for which implementation detail will be replaced on run time. Well, the actual result will be used and not replaced as such. Back to our previous example. The RPC client calls a specific Plugin function called Greet for which the implementation will be provided by a Server that will be streamed back over the RPC protocol. The server is pretty easy to follow:
  1. // Here is the RPC server that GreeterRPC talks to, conforming to
  2. // the requirements of net/rpc
  3. type GreeterRPCServer struct {
  4.     // This is the real implementation
  5.     Impl Greeter
  6. }
Impl is the concrete implementation that will be called in the Server's implementation of the Greet plugin. Now we must define Greet on the RPCServer in order for it to be able to call the remote code. This looks like this:
  1. func (s *GreeterRPCServer) Greet(args interface{}, resp *string) error {
  2.     *resp = s.Impl.Greet()
  3.     return nil
  4. }
This is all still boilerplate for the RPC works. Now comes plugin. For this, the comment is actually quite good too:
  1. // This is the implementation of plugin.Plugin so we can serve/consume this
  2. //
  3. // This has two methods: Server must return an RPC server for this plugin
  4. // type. We construct a GreeterRPCServer for this.
  5. //
  6. // Client must return an implementation of our interface that communicates
  7. // over an RPC client. We return GreeterRPC for this.
  8. //
  9. // Ignore MuxBroker. That is used to create more multiplexed streams on our
  10. // plugin connection and is a more advanced use case.
  11. type GreeterPlugin struct {
  12.     // Impl Injection
  13.     Impl Greeter
  14. }
  15.  
  16. func (p *GreeterPlugin) Server(*plugin.MuxBroker) (interface{}, error) {
  17.     return &GreeterRPCServer{Impl: p.Impl}, nil
  18. }
  19.  
  20. func (GreeterPlugin) Client(b *plugin.MuxBroker, c *rpc.Client) (interface{}, error) {
  21.     return &GreeterRPC{client: c}, nil
  22. }
What does this mean? So, remember: GreeterRPCServer is the one calling the actual implementation while Client is receiving the result of that call. The GreeterPlugin has the Greeter interface embedded just like the RPCServer. We will use the GreeterPlugin as a struct in the plugin map. This is the plugin that we will actually use. This is all still common stuff. These things will need to be visible for both. The plugin's implementation will use the interface to see what it needs to implement. The Client will use it see what to call and what API is available. Like, Greet. How does the implementation look like?

The Implementation

In a completely separate package, but which still has access to the interface definition, this plugin could be something like this:
  1. // Here is a real implementation of Greeter
  2. type GreeterHello struct {
  3.     logger hclog.Logger
  4. }
  5.  
  6. func (g *GreeterHello) Greet() string {
  7.     g.logger.Debug("message from GreeterHello.Greet")
  8.     return "Hello!"
  9. }
We create a struct and then add the function to it which is defined by the plugin's interface. This interface, since it's required by both parties, could well sit in a common package outside of both programs. Something like a SDK. Both code could import it and use it as a common dependency. This way we have separated the interface from the plugin **and** the calling client. The main function could look something like this:
  1. logger := hclog.New(&hclog.LoggerOptions{
  2.     Level:      hclog.Trace,
  3.     Output:     os.Stderr,
  4.     JSONFormat: true,
  5. })
  6.  
  7. greeter := &GreeterHello{
  8.     logger: logger,
  9. }
  10. // pluginMap is the map of plugins we can dispense.
  11. var pluginMap = map[string]plugin.Plugin{
  12.     "greeter": &example.GreeterPlugin{Impl: greeter},
  13. }
  14.  
  15. logger.Debug("message from plugin", "foo", "bar")
  16.  
  17. plugin.Serve(&plugin.ServeConfig{
  18.     HandshakeConfig: handshakeConfig,
  19.     Plugins:         pluginMap,
  20. })
Notice two things that we need. One is the handshakeConfig. You can either define it here, with the same cookie details as you defined in the client code, or you can extract the handshake information into the SDK. This is up to you. Then the next interesting thing is the plugin.Serve method. This is where the magic happens. The plugins open up a RPC communication socket and over a hijacked stdout, broadcasts its availability to the calling Client in this format:
  1. CORE-PROTOCOL-VERSION | APP-PROTOCOL-VERSION | NETWORK-TYPE | NETWORK-ADDR | PROTOCOL
For Go plugins, you don't have to concern yourself with this. go-plugin takes care of all this for you. For non-Go versions, we must take this into account. And before calling serve, we need to output this information to stdout. For example, a Python plugin must deal with this himself. Like this:
  1. # Output information
  2. print("1|1|tcp|127.0.0.1:1234|grpc")
  3. sys.stdout.flush()
For GRPC plugins, it's also mandatory to implement a HealthChecker. How would all this look like with GRPC? It gets slightly more complicated but not too much. We need to use `protoc` to create a protocol description for our implementation, and then we will call that. Let's look at this now by converting the basic greeter example into GRPC.

GRPC Basic plugin

The example that's under GRPC is quite elaborate and perhaps you don't need the Python part. I will focus on the basic RPC example into a GRPC example. That should not be a problem.

The API

First and foremost, you will need to define an API to implement with protoc. For our basic example, the protoc file could look like this:
  1. syntax = "proto3";
  2. package proto;
  3.  
  4. message GreetResponse {
  5.     string message = 1;
  6. }
  7.  
  8. message Empty {}
  9.  
  10. service GreeterService {
  11.     rpc Greet(Empty) returns (GreetResponse);
  12. }
The syntax is quite simple and readable. What this defines is a message, which is a response, that will contain a message with the type string. The service defines a service which has a method called Greet. The service definition is basically an interface for which we will be providing the concrete implementation through the plugin. To read more about protoc, visit this page: Google Protocol Buffer.

Generate the code

Now, with the protoc definition in hand, we need to generate the stubs that the local client implementation can call. That client call will then, through the remote procedure call, call the right function on the server which will have the concrete implementation at the ready. Run it and return the result in the specified format. Because the stub needs to be available by both parties, (the client AND the server), this needs to live in a shared location. Why? Because the client is calling the stub and the server is implementing the stub. Both need it in order to know what to call/implement. To generate the code, run this command:
  1. protoc -I proto/ proto/greeter.proto --go_out=plugins=grpc:proto
I encourage you to read the generated code. Much will make little sense at first. It will have a bunch of structs and defined things that the GRPC package will use in order to server the function. The interesting bits and pieces are:
  1. func (m *GreetResponse) GetMessage() string {
  2.     if m != nil {
  3.         return m.Message
  4.     }
  5.     return ""
  6. }
Which will get use the message inside the response.
  1. type GreeterServiceClient interface {
  2.     Greet(ctx context.Context, in *Empty, opts ...grpc.CallOption) (*GreetResponse, error)
  3. }
This is our ServiceClient interface which defines the Greet function’s topology. And lastly, this guy:
  1. func RegisterGreeterServiceServer(s *grpc.Server, srv GreeterServiceServer) {
  2.     s.RegisterService(&_GreeterService_serviceDesc, srv)
  3. }
Which we will need in order to register our implementation for the server. We can ignore the rest. ## The interface Much like the RPC, we need to define an interface for the client and server to use. This must be in a shared place as both the server and the client need to know about it. You could put this into an SDK and your peers could just get the SDK and implement some function for define and done. The interface definition in the GRPC land could look something like this:
  1. // Greeter is the interface that we're exposing as a plugin.
  2. type Greeter interface {
  3.     Greet() string
  4. }
  5.  
  6. // This is the implementation of plugin.GRPCPlugin so we can serve/consume this.
  7. type GreeterGRPCPlugin struct {
  8.     // GRPCPlugin must still implement the Plugin interface
  9.     plugin.Plugin
  10.     // Concrete implementation, written in Go. This is only used for plugins
  11.     // that are written in Go.
  12.     Impl Greeter
  13. }
  14.  
  15. func (p *GreeterGRPCPlugin) GRPCServer(broker *plugin.GRPCBroker, s *grpc.Server) error {
  16.     proto.RegisterGreeterServer(s, &GRPCServer{Impl: p.Impl})
  17.     return nil
  18. }
  19.  
  20. func (p *GreeterGRPCPlugin) GRPCClient(ctx context.Context, broker *plugin.GRPCBroker, c *grpc.ClientConn) (interface{}, error) {
  21.     return &GRPCClient{client: proto.NewGreeterClient(c)}, nil
  22. }
With this we have the Plugin's implementation for hashicorp what needed to be done. The plugin will call the underlying implementation and serve/consume the plugin. We can now write the GRPC part of it. Please note that `proto` is a shared library too where the protocol stubs reside. That needs to be somewhere on the path or in a separate SDK of some sort, but it must be visible.

Writing the GRPC Client

Firstly we define the grpc client struct:
  1. // GRPCClient is an implementation of Greeter that talks over RPC.
  2. type GRPCClient struct{ client proto.GreeterClient }
Then we define how the client will call the remote function:
  1. func (m *GRPCClient) Greet() string {
  2.     ret, _ := m.client.Greet(context.Background(), &proto.Empty{})
  3.     return ret.Message
  4. }
This will take the client in the GRPCClient and will call the method on it. Once that's done we will return to the result Message property which will be Hello!. proto.Empty is an empty struct; we use this if there is no parameter for a defined method or no return value. We can't just leave it blank. protoc needs to be told explicitly that there is no parameter or return value.

Writing the GRPC Server

The server implementation will also be similar. We call Impl here which will have our concrete plugin implementation.
  1. // Here is the gRPC server that GRPCClient talks to.
  2. type GRPCServer struct {
  3.     // This is the real implementation
  4.     Impl Greeter
  5. }
  6.  
  7. func (m *GRPCServer) Greet(
  8.     ctx context.Context,
  9.     req *proto.Empty) *proto.GreeterResponse {
  10.     v := m.Impl.Greet()
  11.     return &proto.GreeterResponse{Message: v}
  12. }
And we will use the protoc defined message response. v will have the response from Greet which will be Hello! provided by the concrete plugin's implementation. We then transform that into a protoc type by setting the Message property on the GreeterResponse struct provided by the automatically generated protoc stub code. Easy, right?

Writing the plugin itself

The whole thing looks much like the RPC implementation with just a few small modifications and changes. This can sit completely outside of everything, or can even be provided by a third party implementor.
  1. // Here is a real implementation of KV that writes to a local file with
  2. // the key name and the contents are the value of the key.
  3. type Greeter struct{}
  4.  
  5. func (Greeter) Greet() error {
  6.     return "Hello!"
  7. }
  8.  
  9. func main() {
  10.     plugin.Serve(&plugin.ServeConfig{
  11.         HandshakeConfig: shared.Handshake,
  12.         Plugins: map[string]plugin.Plugin{
  13.             "greeter": &shared.GreeterGRPCPlugin{Impl: &Greeter{}},
  14.         },
  15.  
  16.         // A non-nil value here enables gRPC serving for this plugin...
  17.         GRPCServer: plugin.DefaultGRPCServer,
  18.     })
  19. }

Calling it all in the main

Once all that is done, the main function looks the same as RPC's main but with some small modifications.
  1. // We're a host. Start by launching the plugin process.
  2. client := plugin.NewClient(&plugin.ClientConfig{
  3.     HandshakeConfig: shared.Handshake,
  4.     Plugins:         shared.PluginMap,
  5.     Cmd:             exec.Command("./plugin/greeter"),
  6.     AllowedProtocols: []plugin.Protocol{plugin.ProtocolGRPC},
  7. })
The NewClient now defines AllowedProtocols to be ProtocolGRPC. The rest is the same as before calling Dispense and value hinting the plugin to the correct type then calling Greet().

Conclusion

This is it. We made it! Now our plugin works over GRPC with a defined API by protoc. The plugin's implementation can live where ever we want it to, but it needs some shared data. These are:
  • The generated code by `protoc`
  • The defined plugin interface
  • The GRPC Server and Client
These need to be visible by both the Client and the Server. The Server here is the plugin. If you are planning on making people be able to extend your application with go-plugin, you should make these available as a separate SDK. So people won't have to include your whole project just to implement an interface and use protoc. In fact, you could also extract the `protoc` definition into a separate repository so that your SDK can also pull it in. You will have three repositories:
  • Your application;
  • The SDK providing the interface and the GRPC Server and Client implementation;
  • The protoc definition file and generated skeleton ( for Go based plugins).
Other languages will have to generate their own protoc code, and includ it into the plugin; like the Python implementation example located here: Go-plugin Python Example. Also, read this documentation carefully: non-go go-plugin. This document will also clarify what 1|1|tcp|127.0.0.1:1234|grpc means and will dissipate the confusion around how plugins work. Lastly, if you would like to have an in-depth explanation about how go-plugin came to be, watch this video by Mitchell: go-plugin explanation video. I must warn you though- it's an hour long. But worth the watch! That's it. I hope this has helped to clear the confusion around how to use go-plugin.

Happy plugging! Gergely.