Plugins: Make manager more easily composable (#44467)

* make more easily composable

* fix build
This commit is contained in:
Will Browne
2022-01-27 18:06:38 +01:00
committed by GitHub
parent d3b8fc53aa
commit b5dd4842d0
11 changed files with 84 additions and 86 deletions
+14 -17
View File
@@ -12,18 +12,28 @@ import (
"github.com/grafana/grafana/pkg/plugins/backendplugin/pluginextensionv2"
)
// PluginBackendProvider is a function type for initializing a Plugin backend.
type PluginBackendProvider func(_ context.Context, _ *plugins.Plugin) backendplugin.PluginFactoryFunc
type Service struct {
coreRegistry *coreplugin.Registry
providerChain []PluginBackendProvider
}
func ProvideService(coreRegistry *coreplugin.Registry) *Service {
func New(providers ...PluginBackendProvider) *Service {
if len(providers) == 0 {
return New(RendererProvider, DefaultProvider)
}
return &Service{
coreRegistry: coreRegistry,
providerChain: providers,
}
}
func ProvideService(coreRegistry *coreplugin.Registry) *Service {
return New(coreRegistry.BackendFactoryProvider(), RendererProvider, DefaultProvider)
}
func (s *Service) BackendFactory(ctx context.Context, p *plugins.Plugin) backendplugin.PluginFactoryFunc {
for _, provider := range []PluginBackendProvider{CorePluginProvider(ctx, s.coreRegistry), RendererProvider, DefaultProvider} {
for _, provider := range s.providerChain {
if factory := provider(ctx, p); factory != nil {
return factory
}
@@ -31,9 +41,6 @@ func (s *Service) BackendFactory(ctx context.Context, p *plugins.Plugin) backend
return nil
}
// PluginBackendProvider is a function type for initializing a Plugin backend.
type PluginBackendProvider func(_ context.Context, _ *plugins.Plugin) backendplugin.PluginFactoryFunc
var RendererProvider PluginBackendProvider = func(_ context.Context, p *plugins.Plugin) backendplugin.PluginFactoryFunc {
if !p.IsRenderer() {
return nil
@@ -52,13 +59,3 @@ var DefaultProvider PluginBackendProvider = func(_ context.Context, p *plugins.P
cmd := plugins.ComposePluginStartCommand(p.Executable)
return grpcplugin.NewBackendPlugin(p.ID, filepath.Join(p.PluginDir, cmd))
}
var CorePluginProvider = func(ctx context.Context, registry *coreplugin.Registry) PluginBackendProvider {
return func(_ context.Context, p *plugins.Plugin) backendplugin.PluginFactoryFunc {
if !p.IsCorePlugin() {
return nil
}
return registry.Get(p.ID)
}
}