mirror of
https://github.com/grafana/grafana.git
synced 2025-09-25 16:03:46 +08:00
Plugins: Support Admission validation hooks (#87718)
This commit is contained in:
@ -18,6 +18,7 @@ type corePlugin struct {
|
||||
backend.CallResourceHandler
|
||||
backend.QueryDataHandler
|
||||
backend.StreamHandler
|
||||
backend.AdmissionHandler
|
||||
}
|
||||
|
||||
// New returns a new backendplugin.PluginFactoryFunc for creating a core (built-in) backendplugin.Plugin.
|
||||
@ -29,6 +30,7 @@ func New(opts backend.ServeOpts) backendplugin.PluginFactoryFunc {
|
||||
CheckHealthHandler: opts.CheckHealthHandler,
|
||||
CallResourceHandler: opts.CallResourceHandler,
|
||||
QueryDataHandler: opts.QueryDataHandler,
|
||||
AdmissionHandler: opts.AdmissionHandler,
|
||||
StreamHandler: opts.StreamHandler,
|
||||
}, nil
|
||||
}
|
||||
@ -124,3 +126,27 @@ func (cp *corePlugin) RunStream(ctx context.Context, req *backend.RunStreamReque
|
||||
}
|
||||
return plugins.ErrMethodNotImplemented
|
||||
}
|
||||
|
||||
func (cp *corePlugin) MutateAdmission(ctx context.Context, req *backend.AdmissionRequest) (*backend.MutationResponse, error) {
|
||||
if cp.AdmissionHandler != nil {
|
||||
ctx = backend.WithGrafanaConfig(ctx, req.PluginContext.GrafanaConfig)
|
||||
return cp.AdmissionHandler.MutateAdmission(ctx, req)
|
||||
}
|
||||
return nil, plugins.ErrMethodNotImplemented
|
||||
}
|
||||
|
||||
func (cp *corePlugin) ValidateAdmission(ctx context.Context, req *backend.AdmissionRequest) (*backend.ValidationResponse, error) {
|
||||
if cp.AdmissionHandler != nil {
|
||||
ctx = backend.WithGrafanaConfig(ctx, req.PluginContext.GrafanaConfig)
|
||||
return cp.AdmissionHandler.ValidateAdmission(ctx, req)
|
||||
}
|
||||
return nil, plugins.ErrMethodNotImplemented
|
||||
}
|
||||
|
||||
func (cp *corePlugin) ConvertObject(ctx context.Context, req *backend.ConversionRequest) (*backend.ConversionResponse, error) {
|
||||
if cp.AdmissionHandler != nil {
|
||||
ctx = backend.WithGrafanaConfig(ctx, req.PluginContext.GrafanaConfig)
|
||||
return cp.AdmissionHandler.ConvertObject(ctx, req)
|
||||
}
|
||||
return nil, plugins.ErrMethodNotImplemented
|
||||
}
|
||||
|
@ -10,6 +10,7 @@ import (
|
||||
sdktracing "github.com/grafana/grafana-plugin-sdk-go/backend/tracing"
|
||||
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/tracing"
|
||||
"github.com/grafana/grafana/pkg/plugins"
|
||||
"github.com/grafana/grafana/pkg/plugins/backendplugin"
|
||||
@ -145,6 +146,9 @@ func asBackendPlugin(svc any) backendplugin.PluginFactoryFunc {
|
||||
if healthHandler, ok := svc.(backend.CheckHealthHandler); ok {
|
||||
opts.CheckHealthHandler = healthHandler
|
||||
}
|
||||
if storageHandler, ok := svc.(backend.AdmissionHandler); ok {
|
||||
opts.AdmissionHandler = storageHandler
|
||||
}
|
||||
|
||||
if opts.QueryDataHandler != nil || opts.CallResourceHandler != nil ||
|
||||
opts.CheckHealthHandler != nil || opts.StreamHandler != nil {
|
||||
|
@ -33,6 +33,7 @@ var pluginSet = map[int]goplugin.PluginSet{
|
||||
"resource": &grpcplugin.ResourceGRPCPlugin{},
|
||||
"data": &grpcplugin.DataGRPCPlugin{},
|
||||
"stream": &grpcplugin.StreamGRPCPlugin{},
|
||||
"admission": &grpcplugin.AdmissionGRPCPlugin{},
|
||||
"renderer": &pluginextensionv2.RendererGRPCPlugin{},
|
||||
"secretsmanager": &secretsmanagerplugin.SecretsManagerGRPCPlugin{},
|
||||
},
|
||||
|
@ -24,6 +24,7 @@ type ClientV2 struct {
|
||||
grpcplugin.ResourceClient
|
||||
grpcplugin.DataClient
|
||||
grpcplugin.StreamClient
|
||||
grpcplugin.AdmissionClient
|
||||
pluginextensionv2.RendererPlugin
|
||||
secretsmanagerplugin.SecretsManagerPlugin
|
||||
}
|
||||
@ -44,6 +45,11 @@ func newClientV2(descriptor PluginDescriptor, logger log.Logger, rpcClient plugi
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rawAdmission, err := rpcClient.Dispense("admission")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rawStream, err := rpcClient.Dispense("stream")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -78,6 +84,12 @@ func newClientV2(descriptor PluginDescriptor, logger log.Logger, rpcClient plugi
|
||||
}
|
||||
}
|
||||
|
||||
if rawAdmission != nil {
|
||||
if admissionClient, ok := rawAdmission.(grpcplugin.AdmissionClient); ok {
|
||||
c.AdmissionClient = admissionClient
|
||||
}
|
||||
}
|
||||
|
||||
if rawStream != nil {
|
||||
if streamClient, ok := rawStream.(grpcplugin.StreamClient); ok {
|
||||
c.StreamClient = streamClient
|
||||
@ -257,3 +269,60 @@ func (c *ClientV2) RunStream(ctx context.Context, req *backend.RunStreamRequest,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *ClientV2) ValidateAdmission(ctx context.Context, req *backend.AdmissionRequest) (*backend.ValidationResponse, error) {
|
||||
if c.AdmissionClient == nil {
|
||||
return nil, plugins.ErrMethodNotImplemented
|
||||
}
|
||||
|
||||
protoReq := backend.ToProto().AdmissionRequest(req)
|
||||
protoResp, err := c.AdmissionClient.ValidateAdmission(ctx, protoReq)
|
||||
|
||||
if err != nil {
|
||||
if status.Code(err) == codes.Unimplemented {
|
||||
return nil, plugins.ErrMethodNotImplemented
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("%v: %w", "Failed to ValidateAdmission", err)
|
||||
}
|
||||
|
||||
return backend.FromProto().ValidationResponse(protoResp), nil
|
||||
}
|
||||
|
||||
func (c *ClientV2) MutateAdmission(ctx context.Context, req *backend.AdmissionRequest) (*backend.MutationResponse, error) {
|
||||
if c.AdmissionClient == nil {
|
||||
return nil, plugins.ErrMethodNotImplemented
|
||||
}
|
||||
|
||||
protoReq := backend.ToProto().AdmissionRequest(req)
|
||||
protoResp, err := c.AdmissionClient.MutateAdmission(ctx, protoReq)
|
||||
|
||||
if err != nil {
|
||||
if status.Code(err) == codes.Unimplemented {
|
||||
return nil, plugins.ErrMethodNotImplemented
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("%v: %w", "Failed to MutateAdmission", err)
|
||||
}
|
||||
|
||||
return backend.FromProto().MutationResponse(protoResp), nil
|
||||
}
|
||||
|
||||
func (c *ClientV2) ConvertObject(ctx context.Context, req *backend.ConversionRequest) (*backend.ConversionResponse, error) {
|
||||
if c.AdmissionClient == nil {
|
||||
return nil, plugins.ErrMethodNotImplemented
|
||||
}
|
||||
|
||||
protoReq := backend.ToProto().ConversionRequest(req)
|
||||
protoResp, err := c.AdmissionClient.ConvertObject(ctx, protoReq)
|
||||
|
||||
if err != nil {
|
||||
if status.Code(err) == codes.Unimplemented {
|
||||
return nil, plugins.ErrMethodNotImplemented
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("%v: %w", "Failed to ConvertObject", err)
|
||||
}
|
||||
|
||||
return backend.FromProto().ConversionResponse(protoResp), nil
|
||||
}
|
||||
|
@ -19,6 +19,7 @@ type pluginClient interface {
|
||||
backend.CheckHealthHandler
|
||||
backend.QueryDataHandler
|
||||
backend.CallResourceHandler
|
||||
backend.AdmissionHandler
|
||||
backend.StreamHandler
|
||||
}
|
||||
|
||||
@ -199,3 +200,27 @@ func (p *grpcPlugin) RunStream(ctx context.Context, req *backend.RunStreamReques
|
||||
}
|
||||
return pluginClient.RunStream(ctx, req, sender)
|
||||
}
|
||||
|
||||
func (p *grpcPlugin) ValidateAdmission(ctx context.Context, request *backend.AdmissionRequest) (*backend.ValidationResponse, error) {
|
||||
pluginClient, ok := p.getPluginClient()
|
||||
if !ok {
|
||||
return nil, plugins.ErrPluginUnavailable
|
||||
}
|
||||
return pluginClient.ValidateAdmission(ctx, request)
|
||||
}
|
||||
|
||||
func (p *grpcPlugin) MutateAdmission(ctx context.Context, request *backend.AdmissionRequest) (*backend.MutationResponse, error) {
|
||||
pluginClient, ok := p.getPluginClient()
|
||||
if !ok {
|
||||
return nil, plugins.ErrPluginUnavailable
|
||||
}
|
||||
return pluginClient.MutateAdmission(ctx, request)
|
||||
}
|
||||
|
||||
func (p *grpcPlugin) ConvertObject(ctx context.Context, request *backend.ConversionRequest) (*backend.ConversionResponse, error) {
|
||||
pluginClient, ok := p.getPluginClient()
|
||||
if !ok {
|
||||
return nil, plugins.ErrPluginUnavailable
|
||||
}
|
||||
return pluginClient.ConvertObject(ctx, request)
|
||||
}
|
||||
|
@ -23,6 +23,7 @@ type Plugin interface {
|
||||
backend.CheckHealthHandler
|
||||
backend.QueryDataHandler
|
||||
backend.CallResourceHandler
|
||||
backend.AdmissionHandler
|
||||
backend.StreamHandler
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user