mirror of
https://github.com/grafana/grafana.git
synced 2026-08-13 06:34:55 -05:00
Plugins: Create single point of entry for adding / removing plugins (#55463)
* split out plugin manager * remove whitespace * fix tests * split up tests * updating naming conventions * simplify manager * tidy * explorations * fix build * tidy * fix tests * add logger helper * pass the tests * tidying * fix tests * tidy and re-add test * store depends on loader * enrich tests * fix test * undo gomod changes
This commit is contained in:
@@ -2,10 +2,14 @@ package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
|
||||
"github.com/grafana/grafana/pkg/plugins"
|
||||
"github.com/grafana/grafana/pkg/plugins/config"
|
||||
"github.com/grafana/grafana/pkg/plugins/manager/loader"
|
||||
"github.com/grafana/grafana/pkg/plugins/manager/registry"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
)
|
||||
|
||||
var _ plugins.Store = (*Service)(nil)
|
||||
@@ -14,7 +18,17 @@ type Service struct {
|
||||
pluginRegistry registry.Service
|
||||
}
|
||||
|
||||
func ProvideService(pluginRegistry registry.Service) *Service {
|
||||
func ProvideService(gCfg *setting.Cfg, cfg *config.Cfg, pluginRegistry registry.Service,
|
||||
pluginLoader loader.Service) (*Service, error) {
|
||||
for _, ps := range pluginSources(gCfg, cfg) {
|
||||
if _, err := pluginLoader.Load(context.Background(), ps.Class, ps.Paths); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return New(pluginRegistry), nil
|
||||
}
|
||||
|
||||
func New(pluginRegistry registry.Service) *Service {
|
||||
return &Service{
|
||||
pluginRegistry: pluginRegistry,
|
||||
}
|
||||
@@ -49,6 +63,24 @@ func (s *Service) Plugins(ctx context.Context, pluginTypes ...plugins.Type) []pl
|
||||
return pluginsList
|
||||
}
|
||||
|
||||
func (s *Service) Renderer(ctx context.Context) *plugins.Plugin {
|
||||
for _, p := range s.availablePlugins(ctx) {
|
||||
if p.IsRenderer() {
|
||||
return p
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Service) SecretsManager(ctx context.Context) *plugins.Plugin {
|
||||
for _, p := range s.availablePlugins(ctx) {
|
||||
if p.IsSecretsManager() {
|
||||
return p
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// plugin finds a plugin with `pluginID` from the registry that is not decommissioned
|
||||
func (s *Service) plugin(ctx context.Context, pluginID string) (*plugins.Plugin, bool) {
|
||||
p, exists := s.pluginRegistry.Plugin(ctx, pluginID)
|
||||
@@ -87,3 +119,31 @@ func (s *Service) Routes() []*plugins.StaticRoute {
|
||||
}
|
||||
return staticRoutes
|
||||
}
|
||||
|
||||
func pluginSources(gCfg *setting.Cfg, cfg *config.Cfg) []plugins.PluginSource {
|
||||
return []plugins.PluginSource{
|
||||
{Class: plugins.Core, Paths: corePluginPaths(gCfg.StaticRootPath)},
|
||||
{Class: plugins.Bundled, Paths: []string{gCfg.BundledPluginsPath}},
|
||||
{Class: plugins.External, Paths: append([]string{cfg.PluginsPath}, pluginSettingPaths(cfg.PluginSettings)...)},
|
||||
}
|
||||
}
|
||||
|
||||
// corePluginPaths provides a list of the Core plugin paths which need to be scanned on init()
|
||||
func corePluginPaths(staticRootPath string) []string {
|
||||
datasourcePaths := filepath.Join(staticRootPath, "app/plugins/datasource")
|
||||
panelsPath := filepath.Join(staticRootPath, "app/plugins/panel")
|
||||
return []string{datasourcePaths, panelsPath}
|
||||
}
|
||||
|
||||
// pluginSettingPaths provides a plugin paths defined in cfg.PluginSettings which need to be scanned on init()
|
||||
func pluginSettingPaths(ps map[string]map[string]string) []string {
|
||||
var pluginSettingDirs []string
|
||||
for _, s := range ps {
|
||||
path, exists := s["path"]
|
||||
if !exists || path == "" {
|
||||
continue
|
||||
}
|
||||
pluginSettingDirs = append(pluginSettingDirs, path)
|
||||
}
|
||||
return pluginSettingDirs
|
||||
}
|
||||
|
||||
@@ -8,20 +8,48 @@ import (
|
||||
|
||||
"github.com/grafana/grafana/pkg/plugins"
|
||||
"github.com/grafana/grafana/pkg/plugins/backendplugin"
|
||||
"github.com/grafana/grafana/pkg/plugins/config"
|
||||
"github.com/grafana/grafana/pkg/plugins/manager/fakes"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
)
|
||||
|
||||
func TestStore_ProvideService(t *testing.T) {
|
||||
t.Run("Plugin sources are added in order", func(t *testing.T) {
|
||||
var addedPaths []string
|
||||
l := &fakes.FakeLoader{
|
||||
LoadFunc: func(ctx context.Context, class plugins.Class, paths []string) ([]*plugins.Plugin, error) {
|
||||
addedPaths = append(addedPaths, paths...)
|
||||
return nil, nil
|
||||
},
|
||||
}
|
||||
cfg := &setting.Cfg{
|
||||
BundledPluginsPath: "path1",
|
||||
}
|
||||
pCfg := &config.Cfg{
|
||||
PluginsPath: "path2",
|
||||
PluginSettings: setting.PluginSettings{
|
||||
"blah": map[string]string{
|
||||
"path": "path3",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
_, err := ProvideService(cfg, pCfg, fakes.NewFakePluginRegistry(), l)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, []string{"app/plugins/datasource", "app/plugins/panel", "path1", "path2", "path3"}, addedPaths)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStore_Plugin(t *testing.T) {
|
||||
t.Run("Plugin returns all non-decommissioned plugins", func(t *testing.T) {
|
||||
p1 := &plugins.Plugin{JSONData: plugins.JSONData{ID: "test-datasource"}}
|
||||
p1.RegisterClient(&DecommissionedPlugin{})
|
||||
p2 := &plugins.Plugin{JSONData: plugins.JSONData{ID: "test-panel"}}
|
||||
|
||||
ps := ProvideService(
|
||||
newFakePluginRegistry(map[string]*plugins.Plugin{
|
||||
p1.ID: p1,
|
||||
p2.ID: p2,
|
||||
}),
|
||||
)
|
||||
ps := New(newFakePluginRegistry(map[string]*plugins.Plugin{
|
||||
p1.ID: p1,
|
||||
p2.ID: p2,
|
||||
}))
|
||||
|
||||
p, exists := ps.Plugin(context.Background(), p1.ID)
|
||||
require.False(t, exists)
|
||||
@@ -42,15 +70,13 @@ func TestStore_Plugins(t *testing.T) {
|
||||
p5 := &plugins.Plugin{JSONData: plugins.JSONData{ID: "e-test-panel", Type: plugins.Panel}}
|
||||
p5.RegisterClient(&DecommissionedPlugin{})
|
||||
|
||||
ps := ProvideService(
|
||||
newFakePluginRegistry(map[string]*plugins.Plugin{
|
||||
p1.ID: p1,
|
||||
p2.ID: p2,
|
||||
p3.ID: p3,
|
||||
p4.ID: p4,
|
||||
p5.ID: p5,
|
||||
}),
|
||||
)
|
||||
ps := New(newFakePluginRegistry(map[string]*plugins.Plugin{
|
||||
p1.ID: p1,
|
||||
p2.ID: p2,
|
||||
p3.ID: p3,
|
||||
p4.ID: p4,
|
||||
p5.ID: p5,
|
||||
}))
|
||||
|
||||
pss := ps.Plugins(context.Background())
|
||||
require.Equal(t, pss, []plugins.PluginDTO{p1.ToDTO(), p2.ToDTO(), p3.ToDTO(), p4.ToDTO()})
|
||||
@@ -79,16 +105,14 @@ func TestStore_Routes(t *testing.T) {
|
||||
p6 := &plugins.Plugin{JSONData: plugins.JSONData{ID: "f-test-app", Type: plugins.App}}
|
||||
p6.RegisterClient(&DecommissionedPlugin{})
|
||||
|
||||
ps := ProvideService(
|
||||
newFakePluginRegistry(map[string]*plugins.Plugin{
|
||||
p1.ID: p1,
|
||||
p2.ID: p2,
|
||||
p3.ID: p3,
|
||||
p4.ID: p4,
|
||||
p5.ID: p5,
|
||||
p6.ID: p6,
|
||||
}),
|
||||
)
|
||||
ps := New(newFakePluginRegistry(map[string]*plugins.Plugin{
|
||||
p1.ID: p1,
|
||||
p2.ID: p2,
|
||||
p3.ID: p3,
|
||||
p4.ID: p4,
|
||||
p5.ID: p5,
|
||||
p6.ID: p6,
|
||||
}))
|
||||
|
||||
sr := func(p *plugins.Plugin) *plugins.StaticRoute {
|
||||
return &plugins.StaticRoute{PluginID: p.ID, Directory: p.PluginDir}
|
||||
@@ -99,13 +123,49 @@ func TestStore_Routes(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestStore_Renderer(t *testing.T) {
|
||||
t.Run("Renderer returns a single (non-decommissioned) renderer plugin", func(t *testing.T) {
|
||||
p1 := &plugins.Plugin{JSONData: plugins.JSONData{ID: "test-renderer", Type: plugins.Renderer}}
|
||||
p2 := &plugins.Plugin{JSONData: plugins.JSONData{ID: "test-panel", Type: plugins.Panel}}
|
||||
p3 := &plugins.Plugin{JSONData: plugins.JSONData{ID: "test-app", Type: plugins.App}}
|
||||
|
||||
ps := New(newFakePluginRegistry(map[string]*plugins.Plugin{
|
||||
p1.ID: p1,
|
||||
p2.ID: p2,
|
||||
p3.ID: p3,
|
||||
}))
|
||||
|
||||
r := ps.Renderer(context.Background())
|
||||
require.Equal(t, p1, r)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStore_SecretsManager(t *testing.T) {
|
||||
t.Run("Renderer returns a single (non-decommissioned) secrets manager plugin", func(t *testing.T) {
|
||||
p1 := &plugins.Plugin{JSONData: plugins.JSONData{ID: "test-renderer", Type: plugins.Renderer}}
|
||||
p2 := &plugins.Plugin{JSONData: plugins.JSONData{ID: "test-panel", Type: plugins.Panel}}
|
||||
p3 := &plugins.Plugin{JSONData: plugins.JSONData{ID: "test-secrets", Type: plugins.SecretsManager}}
|
||||
p4 := &plugins.Plugin{JSONData: plugins.JSONData{ID: "test-datasource", Type: plugins.DataSource}}
|
||||
|
||||
ps := New(newFakePluginRegistry(map[string]*plugins.Plugin{
|
||||
p1.ID: p1,
|
||||
p2.ID: p2,
|
||||
p3.ID: p3,
|
||||
p4.ID: p4,
|
||||
}))
|
||||
|
||||
r := ps.SecretsManager(context.Background())
|
||||
require.Equal(t, p3, r)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStore_availablePlugins(t *testing.T) {
|
||||
t.Run("Decommissioned plugins are excluded from availablePlugins", func(t *testing.T) {
|
||||
p1 := &plugins.Plugin{JSONData: plugins.JSONData{ID: "test-datasource"}}
|
||||
p1.RegisterClient(&DecommissionedPlugin{})
|
||||
p2 := &plugins.Plugin{JSONData: plugins.JSONData{ID: "test-app"}}
|
||||
|
||||
ps := ProvideService(
|
||||
ps := New(
|
||||
newFakePluginRegistry(map[string]*plugins.Plugin{
|
||||
p1.ID: p1,
|
||||
p2.ID: p2,
|
||||
|
||||
Reference in New Issue
Block a user