Plugins: Move store init to dskit service (#111823)

This commit is contained in:
Todd Treece
2025-10-02 19:53:31 -04:00
committed by GitHub
parent 5798181fb0
commit 2d232aa10d
16 changed files with 394 additions and 222 deletions
@@ -41,8 +41,10 @@ func TestGet(t *testing.T) {
cfg := setting.NewCfg()
ds := &fakeDatasources.FakeDataSourceService{}
db := &dbtest.FakeDB{ExpectedError: pluginsettings.ErrPluginSettingNotFound}
store, err := pluginstore.NewPluginStoreForTest(preg, &pluginFakes.FakeLoader{}, &pluginFakes.FakeSourceRegistry{})
require.NoError(t, err)
pcp := plugincontext.ProvideService(cfg, localcache.ProvideService(),
pluginstore.New(preg, &pluginFakes.FakeLoader{}), &fakeDatasources.FakeCacheService{},
store, &fakeDatasources.FakeCacheService{},
ds, pluginSettings.ProvideService(db, secretstest.NewFakeSecretsService()), pluginconfig.NewFakePluginRequestConfigProvider(),
)
identity := &user.SignedInUser{OrgID: int64(1), Login: "admin"}
@@ -8,6 +8,7 @@ import (
"sync"
"time"
"github.com/grafana/dskit/services"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/plugins"
"github.com/grafana/grafana/pkg/plugins/repo"
@@ -18,6 +19,8 @@ import (
"github.com/prometheus/client_golang/prometheus"
)
const ServiceName = "plugin.backgroundinstaller"
var (
installRequestCounter = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: "plugins",
@@ -36,6 +39,7 @@ var (
)
type Service struct {
services.NamedService
cfg *setting.Cfg
log log.Logger
pluginInstaller plugins.Installer
@@ -43,6 +47,7 @@ type Service struct {
pluginRepo repo.Service
features featuremgmt.FeatureToggles
updateChecker pluginchecker.PluginUpdateChecker
installComplete chan struct{} // closed when all plugins are installed (used for testing)
}
func ProvideService(
@@ -60,21 +65,18 @@ func ProvideService(
})
s := &Service{
log: log.New("plugin.backgroundinstaller"),
log: log.New(ServiceName),
cfg: cfg,
pluginInstaller: pluginInstaller,
pluginStore: pluginStore,
pluginRepo: pluginRepo,
features: features,
updateChecker: updateChecker,
installComplete: make(chan struct{}),
}
if len(cfg.PreinstallPluginsSync) > 0 {
// Block initialization process until plugins are installed
err := s.installPluginsWithTimeout(cfg.PreinstallPluginsSync)
if err != nil {
return nil, err
}
}
s.NamedService = services.NewBasicService(s.starting, s.running, nil).WithName(ServiceName)
return s, nil
}
@@ -83,24 +85,6 @@ func (s *Service) IsDisabled() bool {
return len(s.cfg.PreinstallPluginsAsync) == 0
}
func (s *Service) installPluginsWithTimeout(pluginsToInstall []setting.InstallPlugin) error {
// Installation process does not timeout by default nor reuses the context
// passed to the request so we need to handle the timeout here.
// We could make this timeout configurable in the future.
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
done := make(chan struct{ err error })
go func() {
done <- struct{ err error }{err: s.installPlugins(ctx, pluginsToInstall, true)}
}()
select {
case <-ctx.Done():
return fmt.Errorf("failed to install plugins: %w", ctx.Err())
case d := <-done:
return d.err
}
}
func (s *Service) shouldUpdate(ctx context.Context, pluginID, currentVersion string, pluginURL string) bool {
// If the plugin is installed from a URL, we cannot check for updates as we do not have the version information
// from the repository. Therefore, we assume that the plugin should be updated if the URL is provided.
@@ -166,11 +150,34 @@ func (s *Service) installPlugins(ctx context.Context, pluginsToInstall []setting
return nil
}
func (s *Service) Run(ctx context.Context) error {
err := s.installPlugins(ctx, s.cfg.PreinstallPluginsAsync, false)
if err != nil {
// Unexpected error, asynchronous installation should not return errors
s.log.Error("Failed to install plugins", "error", err)
func (s *Service) starting(ctx context.Context) error {
if len(s.cfg.PreinstallPluginsSync) > 0 {
s.log.Info("Installing plugins", "plugins", s.cfg.PreinstallPluginsSync)
if err := s.installPlugins(ctx, s.cfg.PreinstallPluginsSync, true); err != nil {
s.log.Error("Failed to install plugins", "error", err)
return err
}
}
s.log.Info("Plugins installed", "plugins", s.cfg.PreinstallPluginsSync)
return nil
}
func (s *Service) running(ctx context.Context) error {
if len(s.cfg.PreinstallPluginsAsync) > 0 {
s.log.Info("Installing plugins", "plugins", s.cfg.PreinstallPluginsAsync)
if err := s.installPlugins(ctx, s.cfg.PreinstallPluginsAsync, false); err != nil {
s.log.Error("Failed to install plugins", "error", err)
return err
}
}
close(s.installComplete)
<-ctx.Done()
return nil
}
func (s *Service) Run(ctx context.Context) error {
if err := s.StartAsync(ctx); err != nil {
return err
}
return s.AwaitTerminated(ctx)
}
@@ -26,7 +26,7 @@ func TestService_IsDisabled(t *testing.T) {
&setting.Cfg{
PreinstallPluginsAsync: []setting.InstallPlugin{{ID: "myplugin"}},
},
pluginstore.New(registry.NewInMemory(), &fakes.FakeLoader{}),
pluginstore.New(registry.NewInMemory(), &fakes.FakeLoader{}, &fakes.FakeSourceRegistry{}),
&fakes.FakePluginInstaller{},
prometheus.NewRegistry(),
&fakes.FakePluginRepo{},
@@ -160,12 +160,14 @@ func TestService_Run(t *testing.T) {
}
installed := 0
installedFromURL := 0
store, err := pluginstore.NewPluginStoreForTest(preg, &fakes.FakeLoader{}, &fakes.FakeSourceRegistry{})
require.NoError(t, err)
s, err := ProvideService(
&setting.Cfg{
PreinstallPluginsAsync: tt.pluginsToInstall,
PreinstallPluginsSync: tt.pluginsToInstallSync,
},
pluginstore.New(preg, &fakes.FakeLoader{}),
store,
&fakes.FakePluginInstaller{
AddFunc: func(ctx context.Context, pluginID string, version string, opts plugins.AddOpts) error {
for _, plugin := range tt.pluginsToFail {
@@ -203,13 +205,26 @@ func TestService_Run(t *testing.T) {
&pluginchecker.FakePluginPreinstall{},
),
)
require.NoError(t, err)
t.Cleanup(func() {
s.StopAsync()
err := s.AwaitTerminated(context.Background())
if tt.shouldThrowError {
require.ErrorContains(t, err, "Failed to install plugin")
return
}
require.NoError(t, err)
})
err = s.StartAsync(context.Background())
require.NoError(t, err)
err = s.AwaitRunning(context.Background())
if tt.shouldThrowError {
require.ErrorContains(t, err, "Failed to install plugin")
return
}
require.NoError(t, err)
err = s.Run(context.Background())
require.NoError(t, err)
if tt.shouldInstall {
expectedInstalled := 0
@@ -232,6 +247,7 @@ func TestService_Run(t *testing.T) {
expectedInstalled++
}
}
<-s.installComplete
require.Equal(t, expectedInstalled, installed)
require.Equal(t, expectedInstalledFromURL, installedFromURL)
}
@@ -3,18 +3,21 @@ package pluginstore
import (
"context"
"sort"
"sync"
"time"
"github.com/grafana/dskit/services"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/plugins"
"github.com/grafana/grafana/pkg/plugins/manager/loader"
"github.com/grafana/grafana/pkg/plugins/manager/registry"
"github.com/grafana/grafana/pkg/plugins/manager/sources"
"golang.org/x/sync/errgroup"
)
var _ Store = (*Service)(nil)
const ServiceName = "plugins.store"
// Store is the publicly accessible storage for plugins.
type Store interface {
// Plugin finds a plugin by its ID.
@@ -25,47 +28,81 @@ type Store interface {
}
type Service struct {
services.NamedService
pluginRegistry registry.Service
pluginLoader loader.Service
pluginSources sources.Registry
}
func ProvideService(pluginRegistry registry.Service, pluginSources sources.Registry,
pluginLoader loader.Service) (*Service, error) {
ctx := context.Background()
pluginLoader loader.Service) *Service {
return New(pluginRegistry, pluginLoader, pluginSources)
}
func (s *Service) Run(ctx context.Context) error {
if err := s.StartAsync(ctx); err != nil {
return err
}
stopCtx := context.Background()
return s.AwaitTerminated(stopCtx)
}
func NewPluginStoreForTest(pluginRegistry registry.Service, pluginLoader loader.Service, pluginSources sources.Registry) (*Service, error) {
s := New(pluginRegistry, pluginLoader, pluginSources)
if err := s.StartAsync(context.Background()); err != nil {
return nil, err
}
if err := s.AwaitRunning(context.Background()); err != nil {
return nil, err
}
return s, nil
}
func New(pluginRegistry registry.Service, pluginLoader loader.Service, pluginSources sources.Registry) *Service {
s := &Service{
pluginRegistry: pluginRegistry,
pluginLoader: pluginLoader,
pluginSources: pluginSources,
}
s.NamedService = services.NewBasicService(s.starting, s.running, s.stopping).WithName(ServiceName)
return s
}
func (s *Service) starting(ctx context.Context) error {
start := time.Now()
totalPlugins := 0
logger := log.New("plugin.store")
logger := log.New(ServiceName)
logger.Info("Loading plugins...")
for _, ps := range pluginSources.List(ctx) {
loadedPlugins, err := pluginLoader.Load(ctx, ps)
for _, ps := range s.pluginSources.List(ctx) {
loadedPlugins, err := s.pluginLoader.Load(ctx, ps)
if err != nil {
logger.Error("Loading plugin source failed", "source", ps.PluginClass(ctx), "error", err)
return nil, err
return err
}
totalPlugins += len(loadedPlugins)
}
logger.Info("Plugins loaded", "count", totalPlugins, "duration", time.Since(start))
return New(pluginRegistry, pluginLoader), nil
return nil
}
func (s *Service) Run(ctx context.Context) error {
func (s *Service) running(ctx context.Context) error {
<-ctx.Done()
s.shutdown(ctx)
return ctx.Err()
return nil
}
func New(pluginRegistry registry.Service, pluginLoader loader.Service) *Service {
return &Service{
pluginRegistry: pluginRegistry,
pluginLoader: pluginLoader,
}
func (s *Service) stopping(failureReason error) error {
return s.shutdown(context.Background())
}
func (s *Service) Plugin(ctx context.Context, pluginID string) (Plugin, bool) {
if err := s.AwaitRunning(ctx); err != nil {
log.New(ServiceName).FromContext(ctx).Error("Failed to get plugin", "error", err)
return Plugin{}, false
}
p, exists := s.plugin(ctx, pluginID)
if !exists {
return Plugin{}, false
@@ -75,6 +112,10 @@ func (s *Service) Plugin(ctx context.Context, pluginID string) (Plugin, bool) {
}
func (s *Service) Plugins(ctx context.Context, pluginTypes ...plugins.Type) []Plugin {
if err := s.AwaitRunning(ctx); err != nil {
log.New(ServiceName).FromContext(ctx).Error("Failed to get plugins", "error", err)
return []Plugin{}
}
// if no types passed, assume all
if len(pluginTypes) == 0 {
pluginTypes = plugins.PluginTypes
@@ -125,6 +166,10 @@ func (s *Service) availablePlugins(ctx context.Context) []*plugins.Plugin {
}
func (s *Service) Routes(ctx context.Context) []*plugins.StaticRoute {
if err := s.AwaitRunning(ctx); err != nil {
log.New(ServiceName).FromContext(ctx).Error("Failed to get routes", "error", err)
return []*plugins.StaticRoute{}
}
staticRoutes := make([]*plugins.StaticRoute, 0)
for _, p := range s.availablePlugins(ctx) {
@@ -135,18 +180,20 @@ func (s *Service) Routes(ctx context.Context) []*plugins.StaticRoute {
return staticRoutes
}
func (s *Service) shutdown(ctx context.Context) {
var wg sync.WaitGroup
for _, plugin := range s.pluginRegistry.Plugins(ctx) {
wg.Add(1)
go func(ctx context.Context, p *plugins.Plugin) {
defer wg.Done()
p.Logger().Debug("Stopping plugin")
if _, err := s.pluginLoader.Unload(ctx, p); err != nil {
p.Logger().Error("Failed to stop plugin", "error", err)
func (s *Service) shutdown(ctx context.Context) error {
var errgroup errgroup.Group
plugins := s.pluginRegistry.Plugins(ctx)
for _, p := range plugins {
plugin := p // capture loop variable
errgroup.Go(func() error {
plugin.Logger().Debug("Stopping plugin")
if _, err := s.pluginLoader.Unload(ctx, plugin); err != nil {
plugin.Logger().Error("Failed to stop plugin", "error", err)
return err
}
p.Logger().Debug("Plugin stopped")
}(ctx, plugin)
plugin.Logger().Debug("Plugin stopped")
return nil
})
}
wg.Wait()
return errgroup.Wait()
}
@@ -2,7 +2,7 @@ package pluginstore
import (
"context"
"sync"
"errors"
"testing"
"github.com/stretchr/testify/require"
@@ -43,7 +43,11 @@ func TestStore_ProvideService(t *testing.T) {
}
}}
_, err := ProvideService(fakes.NewFakePluginRegistry(), srcs, l)
service := ProvideService(fakes.NewFakePluginRegistry(), srcs, l)
ctx := context.Background()
err := service.StartAsync(ctx)
require.NoError(t, err)
err = service.AwaitRunning(ctx)
require.NoError(t, err)
require.Equal(t, []plugins.Class{"1", "2", "3"}, loadedSrcs)
})
@@ -55,12 +59,13 @@ func TestStore_Plugin(t *testing.T) {
p1.RegisterClient(&DecommissionedPlugin{})
p2 := &plugins.Plugin{JSONData: plugins.JSONData{ID: "test-panel"}}
ps := New(&fakes.FakePluginRegistry{
ps, err := NewPluginStoreForTest(&fakes.FakePluginRegistry{
Store: map[string]*plugins.Plugin{
p1.ID: p1,
p2.ID: p2,
},
}, &fakes.FakeLoader{})
}, &fakes.FakeLoader{}, &fakes.FakeSourceRegistry{})
require.NoError(t, err)
p, exists := ps.Plugin(context.Background(), p1.ID)
require.False(t, exists)
@@ -81,7 +86,7 @@ func TestStore_Plugins(t *testing.T) {
p5 := &plugins.Plugin{JSONData: plugins.JSONData{ID: "e-test-panel", Type: plugins.TypePanel}}
p5.RegisterClient(&DecommissionedPlugin{})
ps := New(&fakes.FakePluginRegistry{
ps, err := NewPluginStoreForTest(&fakes.FakePluginRegistry{
Store: map[string]*plugins.Plugin{
p1.ID: p1,
p2.ID: p2,
@@ -89,7 +94,8 @@ func TestStore_Plugins(t *testing.T) {
p4.ID: p4,
p5.ID: p5,
},
}, &fakes.FakeLoader{})
}, &fakes.FakeLoader{}, &fakes.FakeSourceRegistry{})
require.NoError(t, err)
ToGrafanaDTO(p1)
pss := ps.Plugins(context.Background())
@@ -124,7 +130,7 @@ func TestStore_Routes(t *testing.T) {
p6 := &plugins.Plugin{JSONData: plugins.JSONData{ID: "f-test-app", Type: plugins.TypeApp}}
p6.RegisterClient(&DecommissionedPlugin{})
ps := New(&fakes.FakePluginRegistry{
ps, err := NewPluginStoreForTest(&fakes.FakePluginRegistry{
Store: map[string]*plugins.Plugin{
p1.ID: p1,
p2.ID: p2,
@@ -132,7 +138,8 @@ func TestStore_Routes(t *testing.T) {
p5.ID: p5,
p6.ID: p6,
},
}, &fakes.FakeLoader{})
}, &fakes.FakeLoader{}, &fakes.FakeSourceRegistry{})
require.NoError(t, err)
sr := func(p *plugins.Plugin) *plugins.StaticRoute {
return &plugins.StaticRoute{PluginID: p.ID, Directory: p.FS.Base()}
@@ -144,39 +151,62 @@ func TestStore_Routes(t *testing.T) {
}
func TestProcessManager_shutdown(t *testing.T) {
p := &plugins.Plugin{JSONData: plugins.JSONData{ID: "test-datasource", Type: plugins.TypeDataSource}} // Backend: true
backend := &fakes.FakeBackendPlugin{}
p.RegisterClient(backend)
p.SetLogger(log.NewTestLogger())
t.Run("When context is cancelled the plugin is stopped", func(t *testing.T) {
p := &plugins.Plugin{JSONData: plugins.JSONData{ID: "test-datasource", Type: plugins.TypeDataSource}} // Backend: true
backend := &fakes.FakeBackendPlugin{}
p.RegisterClient(backend)
p.SetLogger(log.NewTestLogger())
unloaded := false
ps := New(&fakes.FakePluginRegistry{
Store: map[string]*plugins.Plugin{
p.ID: p,
},
}, &fakes.FakeLoader{
UnloadFunc: func(_ context.Context, plugin *plugins.Plugin) (*plugins.Plugin, error) {
require.Equal(t, p, plugin)
unloaded = true
return nil, nil
},
unloaded := false
ps := New(&fakes.FakePluginRegistry{
Store: map[string]*plugins.Plugin{
p.ID: p,
},
}, &fakes.FakeLoader{
UnloadFunc: func(_ context.Context, plugin *plugins.Plugin) (*plugins.Plugin, error) {
require.Equal(t, p, plugin)
unloaded = true
return nil, nil
},
}, &fakes.FakeSourceRegistry{})
ctx, cancel := context.WithCancel(context.Background())
err := ps.StartAsync(ctx)
require.NoError(t, err)
err = ps.AwaitRunning(ctx)
require.NoError(t, err)
// Cancel context to trigger shutdown
cancel()
// Wait for service to be fully terminated
err = ps.AwaitTerminated(context.Background())
require.NoError(t, err)
require.True(t, unloaded)
})
pCtx := context.Background()
cCtx, cancel := context.WithCancel(pCtx)
var wgRun sync.WaitGroup
wgRun.Add(1)
var runErr error
go func() {
runErr = ps.Run(cCtx)
wgRun.Done()
}()
t.Run("When shutdown fails, stopping method returns error", func(t *testing.T) {
p := &plugins.Plugin{JSONData: plugins.JSONData{ID: "test-datasource", Type: plugins.TypeDataSource}}
backend := &fakes.FakeBackendPlugin{}
p.RegisterClient(backend)
p.SetLogger(log.NewTestLogger())
t.Run("When context is cancelled the plugin is stopped", func(t *testing.T) {
cancel()
wgRun.Wait()
require.ErrorIs(t, runErr, context.Canceled)
require.True(t, unloaded)
expectedErr := errors.New("unload failed")
ps, err := NewPluginStoreForTest(&fakes.FakePluginRegistry{
Store: map[string]*plugins.Plugin{
p.ID: p,
},
}, &fakes.FakeLoader{
UnloadFunc: func(_ context.Context, plugin *plugins.Plugin) (*plugins.Plugin, error) {
return nil, expectedErr
},
}, &fakes.FakeSourceRegistry{})
require.NoError(t, err)
err = ps.stopping(nil)
require.Error(t, err)
require.ErrorIs(t, err, expectedErr)
})
}
@@ -186,12 +216,13 @@ func TestStore_availablePlugins(t *testing.T) {
p1.RegisterClient(&DecommissionedPlugin{})
p2 := &plugins.Plugin{JSONData: plugins.JSONData{ID: "test-app"}}
ps := New(&fakes.FakePluginRegistry{
ps, err := NewPluginStoreForTest(&fakes.FakePluginRegistry{
Store: map[string]*plugins.Plugin{
p1.ID: p1,
p2.ID: p2,
},
}, &fakes.FakeLoader{})
}, &fakes.FakeLoader{}, &fakes.FakeSourceRegistry{})
require.NoError(t, err)
aps := ps.availablePlugins(context.Background())
require.Len(t, aps, 1)
@@ -67,7 +67,7 @@ func CreateIntegrationTestCtx(t *testing.T, cfg *setting.Cfg, coreRegistry *core
Terminator: term,
})
ps, err := pluginstore.ProvideService(reg, sources.ProvideService(cfg, pCfg), l)
ps, err := pluginstore.NewPluginStoreForTest(reg, l, sources.ProvideService(cfg, pCfg))
require.NoError(t, err)
return &IntegrationTestCtx{