2023-11-03 21:01:08 +08:00
|
|
|
package grpcplugin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
|
2024-10-11 08:30:56 +08:00
|
|
|
trace "go.opentelemetry.io/otel/trace"
|
2023-11-03 21:01:08 +08:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/genproto/pluginv2"
|
|
|
|
|
2024-04-18 00:47:01 +08:00
|
|
|
"github.com/grafana/grafana/pkg/plugins"
|
|
|
|
"github.com/grafana/grafana/pkg/plugins/backendplugin"
|
2023-11-03 21:01:08 +08:00
|
|
|
"github.com/grafana/grafana/pkg/plugins/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2024-09-24 20:55:02 +08:00
|
|
|
errClientNotAvailable = errors.New("plugin client not available")
|
2023-11-03 21:01:08 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ ProtoClient = (*protoClient)(nil)
|
|
|
|
|
2025-05-07 00:02:32 +08:00
|
|
|
type PluginV2 interface {
|
2023-11-03 21:01:08 +08:00
|
|
|
pluginv2.DataClient
|
|
|
|
pluginv2.ResourceClient
|
|
|
|
pluginv2.DiagnosticsClient
|
|
|
|
pluginv2.StreamClient
|
2024-07-09 21:03:46 +08:00
|
|
|
pluginv2.AdmissionControlClient
|
2024-08-16 04:02:21 +08:00
|
|
|
pluginv2.ResourceConversionClient
|
2025-05-07 00:02:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type ProtoClient interface {
|
|
|
|
PluginV2
|
2023-11-03 21:01:08 +08:00
|
|
|
|
2023-11-17 20:52:31 +08:00
|
|
|
PID(context.Context) (string, error)
|
2023-11-03 21:01:08 +08:00
|
|
|
PluginID() string
|
2023-11-15 23:53:30 +08:00
|
|
|
PluginVersion() string
|
2024-04-18 00:47:01 +08:00
|
|
|
Backend() backendplugin.Plugin
|
2023-11-03 21:01:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type protoClient struct {
|
2024-09-24 20:55:02 +08:00
|
|
|
plugin *grpcPlugin
|
|
|
|
pluginJSON plugins.JSONData
|
2023-11-03 21:01:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type ProtoClientOpts struct {
|
2024-04-18 00:47:01 +08:00
|
|
|
PluginJSON plugins.JSONData
|
2023-11-03 21:01:08 +08:00
|
|
|
ExecutablePath string
|
|
|
|
ExecutableArgs []string
|
|
|
|
Env []string
|
|
|
|
Logger log.Logger
|
2024-10-11 08:30:56 +08:00
|
|
|
Tracer trace.Tracer
|
2023-11-03 21:01:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewProtoClient(opts ProtoClientOpts) (ProtoClient, error) {
|
|
|
|
p := newGrpcPlugin(
|
|
|
|
PluginDescriptor{
|
2024-04-18 00:47:01 +08:00
|
|
|
pluginID: opts.PluginJSON.ID,
|
2023-11-03 21:01:08 +08:00
|
|
|
managed: true,
|
|
|
|
executablePath: opts.ExecutablePath,
|
|
|
|
executableArgs: opts.ExecutableArgs,
|
|
|
|
versionedPlugins: pluginSet,
|
|
|
|
},
|
|
|
|
opts.Logger,
|
2024-10-11 08:30:56 +08:00
|
|
|
opts.Tracer,
|
2023-11-03 21:01:08 +08:00
|
|
|
func() []string { return opts.Env },
|
|
|
|
)
|
|
|
|
|
2024-09-24 20:55:02 +08:00
|
|
|
return &protoClient{plugin: p, pluginJSON: opts.PluginJSON}, nil
|
2023-11-03 21:01:08 +08:00
|
|
|
}
|
|
|
|
|
2023-11-17 20:52:31 +08:00
|
|
|
func (r *protoClient) PID(ctx context.Context) (string, error) {
|
|
|
|
if _, exists := r.client(ctx); !exists {
|
2024-09-24 20:55:02 +08:00
|
|
|
return "", errClientNotAvailable
|
2023-11-13 21:41:53 +08:00
|
|
|
}
|
|
|
|
return r.plugin.client.ID(), nil
|
|
|
|
}
|
|
|
|
|
2023-11-03 21:01:08 +08:00
|
|
|
func (r *protoClient) PluginID() string {
|
|
|
|
return r.plugin.descriptor.pluginID
|
|
|
|
}
|
|
|
|
|
2023-11-15 23:53:30 +08:00
|
|
|
func (r *protoClient) PluginVersion() string {
|
2024-09-24 20:55:02 +08:00
|
|
|
return r.pluginJSON.Info.Version
|
2024-04-18 00:47:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *protoClient) Backend() backendplugin.Plugin {
|
|
|
|
return r.plugin
|
|
|
|
}
|
|
|
|
|
2023-11-03 21:01:08 +08:00
|
|
|
func (r *protoClient) Logger() log.Logger {
|
|
|
|
return r.plugin.logger
|
|
|
|
}
|
|
|
|
|
2024-09-24 00:26:02 +08:00
|
|
|
func (r *protoClient) client(ctx context.Context) (*ClientV2, bool) {
|
2024-09-24 20:55:02 +08:00
|
|
|
return r.plugin.getPluginClient(ctx)
|
2023-11-13 21:41:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *protoClient) QueryData(ctx context.Context, in *pluginv2.QueryDataRequest, opts ...grpc.CallOption) (*pluginv2.QueryDataResponse, error) {
|
2023-11-17 20:52:31 +08:00
|
|
|
c, exists := r.client(ctx)
|
2023-11-13 21:41:53 +08:00
|
|
|
if !exists {
|
2024-09-24 20:55:02 +08:00
|
|
|
return nil, errClientNotAvailable
|
2023-11-03 21:01:08 +08:00
|
|
|
}
|
2023-11-13 21:41:53 +08:00
|
|
|
return c.DataClient.QueryData(ctx, in, opts...)
|
2023-11-03 21:01:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *protoClient) CallResource(ctx context.Context, in *pluginv2.CallResourceRequest, opts ...grpc.CallOption) (pluginv2.Resource_CallResourceClient, error) {
|
2023-11-17 20:52:31 +08:00
|
|
|
c, exists := r.client(ctx)
|
2023-11-13 21:41:53 +08:00
|
|
|
if !exists {
|
2024-09-24 20:55:02 +08:00
|
|
|
return nil, errClientNotAvailable
|
2023-11-03 21:01:08 +08:00
|
|
|
}
|
2023-11-13 21:41:53 +08:00
|
|
|
return c.ResourceClient.CallResource(ctx, in, opts...)
|
2023-11-03 21:01:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *protoClient) CheckHealth(ctx context.Context, in *pluginv2.CheckHealthRequest, opts ...grpc.CallOption) (*pluginv2.CheckHealthResponse, error) {
|
2023-11-17 20:52:31 +08:00
|
|
|
c, exists := r.client(ctx)
|
2023-11-13 21:41:53 +08:00
|
|
|
if !exists {
|
2024-09-24 20:55:02 +08:00
|
|
|
return nil, errClientNotAvailable
|
2023-11-03 21:01:08 +08:00
|
|
|
}
|
2023-11-13 21:41:53 +08:00
|
|
|
return c.DiagnosticsClient.CheckHealth(ctx, in, opts...)
|
2023-11-03 21:01:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *protoClient) CollectMetrics(ctx context.Context, in *pluginv2.CollectMetricsRequest, opts ...grpc.CallOption) (*pluginv2.CollectMetricsResponse, error) {
|
2023-11-17 20:52:31 +08:00
|
|
|
c, exists := r.client(ctx)
|
2023-11-13 21:41:53 +08:00
|
|
|
if !exists {
|
2024-09-24 20:55:02 +08:00
|
|
|
return nil, errClientNotAvailable
|
2023-11-03 21:01:08 +08:00
|
|
|
}
|
2023-11-13 21:41:53 +08:00
|
|
|
return c.DiagnosticsClient.CollectMetrics(ctx, in, opts...)
|
2023-11-03 21:01:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *protoClient) SubscribeStream(ctx context.Context, in *pluginv2.SubscribeStreamRequest, opts ...grpc.CallOption) (*pluginv2.SubscribeStreamResponse, error) {
|
2023-11-17 20:52:31 +08:00
|
|
|
c, exists := r.client(ctx)
|
2023-11-13 21:41:53 +08:00
|
|
|
if !exists {
|
2024-09-24 20:55:02 +08:00
|
|
|
return nil, errClientNotAvailable
|
2023-11-03 21:01:08 +08:00
|
|
|
}
|
2023-11-13 21:41:53 +08:00
|
|
|
return c.StreamClient.SubscribeStream(ctx, in, opts...)
|
2023-11-03 21:01:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *protoClient) RunStream(ctx context.Context, in *pluginv2.RunStreamRequest, opts ...grpc.CallOption) (pluginv2.Stream_RunStreamClient, error) {
|
2023-11-17 20:52:31 +08:00
|
|
|
c, exists := r.client(ctx)
|
2023-11-13 21:41:53 +08:00
|
|
|
if !exists {
|
2024-09-24 20:55:02 +08:00
|
|
|
return nil, errClientNotAvailable
|
2023-11-03 21:01:08 +08:00
|
|
|
}
|
2023-11-13 21:41:53 +08:00
|
|
|
return c.StreamClient.RunStream(ctx, in, opts...)
|
2023-11-03 21:01:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *protoClient) PublishStream(ctx context.Context, in *pluginv2.PublishStreamRequest, opts ...grpc.CallOption) (*pluginv2.PublishStreamResponse, error) {
|
2023-11-17 20:52:31 +08:00
|
|
|
c, exists := r.client(ctx)
|
2023-11-13 21:41:53 +08:00
|
|
|
if !exists {
|
2024-09-24 20:55:02 +08:00
|
|
|
return nil, errClientNotAvailable
|
2023-11-03 21:01:08 +08:00
|
|
|
}
|
2023-11-13 21:41:53 +08:00
|
|
|
return c.StreamClient.PublishStream(ctx, in, opts...)
|
2023-11-03 21:01:08 +08:00
|
|
|
}
|
2024-07-09 21:03:46 +08:00
|
|
|
|
|
|
|
func (r *protoClient) ValidateAdmission(ctx context.Context, in *pluginv2.AdmissionRequest, opts ...grpc.CallOption) (*pluginv2.ValidationResponse, error) {
|
|
|
|
c, exists := r.client(ctx)
|
|
|
|
if !exists {
|
2024-09-24 20:55:02 +08:00
|
|
|
return nil, errClientNotAvailable
|
2024-07-09 21:03:46 +08:00
|
|
|
}
|
|
|
|
return c.AdmissionClient.ValidateAdmission(ctx, in, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *protoClient) MutateAdmission(ctx context.Context, in *pluginv2.AdmissionRequest, opts ...grpc.CallOption) (*pluginv2.MutationResponse, error) {
|
|
|
|
c, exists := r.client(ctx)
|
|
|
|
if !exists {
|
2024-09-24 20:55:02 +08:00
|
|
|
return nil, errClientNotAvailable
|
2024-07-09 21:03:46 +08:00
|
|
|
}
|
|
|
|
return c.AdmissionClient.MutateAdmission(ctx, in, opts...)
|
|
|
|
}
|
|
|
|
|
2024-08-16 04:02:21 +08:00
|
|
|
func (r *protoClient) ConvertObjects(ctx context.Context, in *pluginv2.ConversionRequest, opts ...grpc.CallOption) (*pluginv2.ConversionResponse, error) {
|
2024-07-09 21:03:46 +08:00
|
|
|
c, exists := r.client(ctx)
|
|
|
|
if !exists {
|
2024-09-24 20:55:02 +08:00
|
|
|
return nil, errClientNotAvailable
|
2024-07-09 21:03:46 +08:00
|
|
|
}
|
2024-08-16 04:02:21 +08:00
|
|
|
return c.ConversionClient.ConvertObjects(ctx, in, opts...)
|
2024-07-09 21:03:46 +08:00
|
|
|
}
|