2020-06-11 09:14:05 -05:00
|
|
|
package grpcplugin
|
2019-10-29 11:22:31 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os/exec"
|
|
|
|
|
2020-04-21 09:16:41 -05:00
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend/grpcplugin"
|
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2020-06-11 09:14:05 -05:00
|
|
|
"github.com/grafana/grafana/pkg/plugins/backendplugin"
|
2020-04-21 09:16:41 -05:00
|
|
|
"github.com/grafana/grafana/pkg/plugins/backendplugin/pluginextensionv2"
|
2020-01-31 04:15:50 -06:00
|
|
|
goplugin "github.com/hashicorp/go-plugin"
|
2019-10-29 11:22:31 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// Handshake is the HandshakeConfig used to configure clients and servers.
|
2020-01-31 04:15:50 -06:00
|
|
|
var handshake = goplugin.HandshakeConfig{
|
2019-10-29 11:22:31 -05:00
|
|
|
// The ProtocolVersion is the version that must match between Grafana core
|
|
|
|
// and Grafana plugins. This should be bumped whenever a (breaking) change
|
|
|
|
// happens in one or the other that makes it so that they can't safely communicate.
|
2021-05-19 01:57:48 -05:00
|
|
|
ProtocolVersion: grpcplugin.ProtocolVersion,
|
2019-10-29 11:22:31 -05:00
|
|
|
|
|
|
|
// The magic cookie values should NEVER be changed.
|
2020-11-05 08:37:11 -06:00
|
|
|
MagicCookieKey: grpcplugin.MagicCookieKey,
|
|
|
|
MagicCookieValue: grpcplugin.MagicCookieValue,
|
2019-10-29 11:22:31 -05:00
|
|
|
}
|
|
|
|
|
2020-12-15 02:32:06 -06:00
|
|
|
func newClientConfig(executablePath string, env []string, logger log.Logger,
|
|
|
|
versionedPlugins map[int]goplugin.PluginSet) *goplugin.ClientConfig {
|
|
|
|
// We can ignore gosec G201 here, since the dynamic part of executablePath comes from the plugin definition
|
|
|
|
// nolint:gosec
|
2020-04-14 11:04:27 -05:00
|
|
|
cmd := exec.Command(executablePath)
|
|
|
|
cmd.Env = env
|
|
|
|
|
2020-01-31 04:15:50 -06:00
|
|
|
return &goplugin.ClientConfig{
|
2020-04-14 11:04:27 -05:00
|
|
|
Cmd: cmd,
|
2019-10-29 11:22:31 -05:00
|
|
|
HandshakeConfig: handshake,
|
|
|
|
VersionedPlugins: versionedPlugins,
|
|
|
|
Logger: logWrapper{Logger: logger},
|
2020-01-31 04:15:50 -06:00
|
|
|
AllowedProtocols: []goplugin.Protocol{goplugin.ProtocolGRPC},
|
2019-10-29 11:22:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-03 07:16:58 -05:00
|
|
|
// StartRendererFunc callback function called when a renderer plugin is started.
|
|
|
|
type StartRendererFunc func(pluginID string, renderer pluginextensionv2.RendererPlugin, logger log.Logger) error
|
2020-01-13 10:13:17 -06:00
|
|
|
|
2020-05-12 08:48:24 -05:00
|
|
|
// PluginDescriptor is a descriptor used for registering backend plugins.
|
2020-01-08 10:43:28 -06:00
|
|
|
type PluginDescriptor struct {
|
|
|
|
pluginID string
|
|
|
|
executablePath string
|
|
|
|
managed bool
|
2020-01-31 04:15:50 -06:00
|
|
|
versionedPlugins map[int]goplugin.PluginSet
|
2021-06-03 07:16:58 -05:00
|
|
|
startRendererFn StartRendererFunc
|
2019-10-29 11:22:31 -05:00
|
|
|
}
|
|
|
|
|
2020-04-21 09:16:41 -05:00
|
|
|
// getV2PluginSet returns list of plugins supported on v2.
|
|
|
|
func getV2PluginSet() goplugin.PluginSet {
|
|
|
|
return goplugin.PluginSet{
|
2020-11-05 08:37:11 -06:00
|
|
|
"diagnostics": &grpcplugin.DiagnosticsGRPCPlugin{},
|
|
|
|
"resource": &grpcplugin.ResourceGRPCPlugin{},
|
|
|
|
"data": &grpcplugin.DataGRPCPlugin{},
|
2021-03-23 12:24:08 -05:00
|
|
|
"stream": &grpcplugin.StreamGRPCPlugin{},
|
2020-04-21 09:16:41 -05:00
|
|
|
"renderer": &pluginextensionv2.RendererGRPCPlugin{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 09:14:05 -05:00
|
|
|
// NewBackendPlugin creates a new backend plugin factory used for registering a backend plugin.
|
2021-06-03 07:16:58 -05:00
|
|
|
func NewBackendPlugin(pluginID, executablePath string) backendplugin.PluginFactoryFunc {
|
2020-12-15 02:32:06 -06:00
|
|
|
return newPlugin(PluginDescriptor{
|
2020-01-08 10:43:28 -06:00
|
|
|
pluginID: pluginID,
|
|
|
|
executablePath: executablePath,
|
|
|
|
managed: true,
|
2020-01-31 04:15:50 -06:00
|
|
|
versionedPlugins: map[int]goplugin.PluginSet{
|
2020-11-05 08:37:11 -06:00
|
|
|
grpcplugin.ProtocolVersion: getV2PluginSet(),
|
2019-10-29 11:22:31 -05:00
|
|
|
},
|
2020-06-11 09:14:05 -05:00
|
|
|
})
|
2019-10-29 11:22:31 -05:00
|
|
|
}
|
|
|
|
|
2020-06-11 09:14:05 -05:00
|
|
|
// NewRendererPlugin creates a new renderer plugin factory used for registering a backend renderer plugin.
|
2021-06-03 07:16:58 -05:00
|
|
|
func NewRendererPlugin(pluginID, executablePath string, startFn StartRendererFunc) backendplugin.PluginFactoryFunc {
|
2020-12-15 02:32:06 -06:00
|
|
|
return newPlugin(PluginDescriptor{
|
2020-01-08 10:43:28 -06:00
|
|
|
pluginID: pluginID,
|
|
|
|
executablePath: executablePath,
|
|
|
|
managed: false,
|
2020-01-31 04:15:50 -06:00
|
|
|
versionedPlugins: map[int]goplugin.PluginSet{
|
2020-11-05 08:37:11 -06:00
|
|
|
grpcplugin.ProtocolVersion: getV2PluginSet(),
|
2019-10-29 11:22:31 -05:00
|
|
|
},
|
2021-06-03 07:16:58 -05:00
|
|
|
startRendererFn: startFn,
|
2020-06-11 09:14:05 -05:00
|
|
|
})
|
2020-01-31 04:15:50 -06:00
|
|
|
}
|