Plugins: Make Installer responsible for removing plugins from file system (#73323)

* installer is responsible for removing from file system

* take plugin as arg

* remove resolve step

* return plugin in test
This commit is contained in:
Will Browne
2023-08-16 15:44:20 +02:00
committed by GitHub
parent 243b757168
commit 3c50db328d
13 changed files with 64 additions and 106 deletions
+8 -7
View File
@@ -6,7 +6,6 @@ import (
"sync"
"github.com/grafana/grafana/pkg/plugins"
"github.com/grafana/grafana/pkg/plugins/backendplugin"
"github.com/grafana/grafana/pkg/plugins/manager/loader"
"github.com/grafana/grafana/pkg/plugins/manager/registry"
"github.com/grafana/grafana/pkg/plugins/manager/sources"
@@ -16,6 +15,7 @@ var _ plugins.Store = (*Service)(nil)
type Service struct {
pluginRegistry registry.Service
pluginLoader loader.Service
}
func ProvideService(pluginRegistry registry.Service, pluginSources sources.Registry,
@@ -26,7 +26,7 @@ func ProvideService(pluginRegistry registry.Service, pluginSources sources.Regis
return nil, err
}
}
return New(pluginRegistry), nil
return New(pluginRegistry, pluginLoader), nil
}
func (s *Service) Run(ctx context.Context) error {
@@ -35,9 +35,10 @@ func (s *Service) Run(ctx context.Context) error {
return ctx.Err()
}
func New(pluginRegistry registry.Service) *Service {
func New(pluginRegistry registry.Service, pluginLoader loader.Service) *Service {
return &Service{
pluginRegistry: pluginRegistry,
pluginLoader: pluginLoader,
}
}
@@ -131,16 +132,16 @@ func (s *Service) Routes(ctx context.Context) []*plugins.StaticRoute {
func (s *Service) shutdown(ctx context.Context) {
var wg sync.WaitGroup
for _, p := range s.pluginRegistry.Plugins(ctx) {
for _, plugin := range s.pluginRegistry.Plugins(ctx) {
wg.Add(1)
go func(p backendplugin.Plugin, ctx context.Context) {
go func(ctx context.Context, p *plugins.Plugin) {
defer wg.Done()
p.Logger().Debug("Stopping plugin")
if err := p.Stop(ctx); err != nil {
if _, err := s.pluginLoader.Unload(ctx, p); err != nil {
p.Logger().Error("Failed to stop plugin", "error", err)
}
p.Logger().Debug("Plugin stopped")
}(p, ctx)
}(ctx, plugin)
}
wg.Wait()
}
+14 -8
View File
@@ -61,7 +61,7 @@ func TestStore_Plugin(t *testing.T) {
p1.ID: p1,
p2.ID: p2,
},
})
}, &fakes.FakeLoader{})
p, exists := ps.Plugin(context.Background(), p1.ID)
require.False(t, exists)
@@ -90,7 +90,7 @@ func TestStore_Plugins(t *testing.T) {
p4.ID: p4,
p5.ID: p5,
},
})
}, &fakes.FakeLoader{})
pss := ps.Plugins(context.Background())
require.Equal(t, pss, []plugins.PluginDTO{p1.ToDTO(), p2.ToDTO(), p3.ToDTO(), p4.ToDTO()})
@@ -128,7 +128,7 @@ func TestStore_Routes(t *testing.T) {
p5.ID: p5,
p6.ID: p6,
},
})
}, &fakes.FakeLoader{})
sr := func(p *plugins.Plugin) *plugins.StaticRoute {
return &plugins.StaticRoute{PluginID: p.ID, Directory: p.FS.Base()}
@@ -151,7 +151,7 @@ func TestStore_Renderer(t *testing.T) {
p2.ID: p2,
p3.ID: p3,
},
})
}, &fakes.FakeLoader{})
r := ps.Renderer(context.Background())
require.Equal(t, p1, r)
@@ -172,7 +172,7 @@ func TestStore_SecretsManager(t *testing.T) {
p3.ID: p3,
p4.ID: p4,
},
})
}, &fakes.FakeLoader{})
r := ps.SecretsManager(context.Background())
require.Equal(t, p3, r)
@@ -185,10 +185,17 @@ func TestProcessManager_shutdown(t *testing.T) {
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
},
})
pCtx := context.Background()
@@ -205,8 +212,7 @@ func TestProcessManager_shutdown(t *testing.T) {
cancel()
wgRun.Wait()
require.ErrorIs(t, runErr, context.Canceled)
require.True(t, p.Exited())
require.Equal(t, 1, backend.StopCount)
require.True(t, unloaded)
})
}
@@ -221,7 +227,7 @@ func TestStore_availablePlugins(t *testing.T) {
p1.ID: p1,
p2.ID: p2,
},
})
}, &fakes.FakeLoader{})
aps := ps.availablePlugins(context.Background())
require.Len(t, aps, 1)