mirror of
https://github.com/opentffoundation/opentf.git
synced 2026-03-01 08:04:04 -05:00
* Add support for plugin protocol v6 This PR turns on support for plugin protocol v6. A provider can advertise itself as supporting protocol version 6 and terraform will use the correct client. Todo: The "unmanaged" providers functionality does not support protocol version, so at the moment terraform will continue to assume that "unmanaged" providers are on protocol v5. This will require some upstream work on go-plugin (I believe). I would like to convert the builtin providers to use protocol v6 in a future PR; however it is not necessary until we remove protocol v6. * add e2e test for using both plugin protocol versions - copied grpcwrap and made a version that returns protocol v6 provider - copied the test provider, provider-simple, and made a version that's using protocol v6 with the above fun - added an e2etest
64 lines
1.9 KiB
Go
64 lines
1.9 KiB
Go
package plugin6
|
|
|
|
import (
|
|
"github.com/hashicorp/go-plugin"
|
|
proto "github.com/hashicorp/terraform/internal/tfplugin6"
|
|
)
|
|
|
|
const (
|
|
// The constants below are the names of the plugins that can be dispensed
|
|
// from the plugin server.
|
|
ProviderPluginName = "provider"
|
|
|
|
// DefaultProtocolVersion is the protocol version assumed for legacy clients
|
|
// that don't specify a particular version during their handshake. Since we
|
|
// explicitly set VersionedPlugins in Serve, this number does not need to
|
|
// change with the protocol version and can effectively stay 4 forever
|
|
// (unless we need the "biggest hammer" approach to break all provider
|
|
// compatibility).
|
|
DefaultProtocolVersion = 4
|
|
)
|
|
|
|
// Handshake is the HandshakeConfig used to configure clients and servers.
|
|
var Handshake = plugin.HandshakeConfig{
|
|
// The ProtocolVersion is the version that must match between TF core
|
|
// and TF plugins.
|
|
ProtocolVersion: DefaultProtocolVersion,
|
|
|
|
// The magic cookie values should NEVER be changed.
|
|
MagicCookieKey: "TF_PLUGIN_MAGIC_COOKIE",
|
|
MagicCookieValue: "d602bf8f470bc67ca7faa0386276bbdd4330efaf76d1a219cb4d6991ca9872b2",
|
|
}
|
|
|
|
type GRPCProviderFunc func() proto.ProviderServer
|
|
|
|
// ServeOpts are the configurations to serve a plugin.
|
|
type ServeOpts struct {
|
|
GRPCProviderFunc GRPCProviderFunc
|
|
}
|
|
|
|
// Serve serves a plugin. This function never returns and should be the final
|
|
// function called in the main function of the plugin.
|
|
func Serve(opts *ServeOpts) {
|
|
plugin.Serve(&plugin.ServeConfig{
|
|
HandshakeConfig: Handshake,
|
|
VersionedPlugins: pluginSet(opts),
|
|
GRPCServer: plugin.DefaultGRPCServer,
|
|
})
|
|
}
|
|
|
|
func pluginSet(opts *ServeOpts) map[int]plugin.PluginSet {
|
|
plugins := map[int]plugin.PluginSet{}
|
|
|
|
// add the new protocol versions if they're configured
|
|
if opts.GRPCProviderFunc != nil {
|
|
plugins[6] = plugin.PluginSet{}
|
|
if opts.GRPCProviderFunc != nil {
|
|
plugins[6]["provider"] = &GRPCProviderPlugin{
|
|
GRPCProvider: opts.GRPCProviderFunc,
|
|
}
|
|
}
|
|
}
|
|
return plugins
|
|
}
|