mirror of
https://github.com/grafana/grafana.git
synced 2025-08-02 06:32:15 +08:00
Plugins: Remove old code related to Core plugin installs (#44311)
* remove old code * remove even more * skip flaky test
This commit is contained in:
@ -2,11 +2,8 @@ package plugins
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"path/filepath"
|
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/setting"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func ComposePluginStartCommand(executable string) string {
|
func ComposePluginStartCommand(executable string) string {
|
||||||
@ -32,14 +29,3 @@ func ComposeRendererStartCommand() string {
|
|||||||
|
|
||||||
return fmt.Sprintf("%s_%s_%s%s", "plugin_start", os, strings.ToLower(arch), extension)
|
return fmt.Sprintf("%s_%s_%s%s", "plugin_start", os, strings.ToLower(arch), extension)
|
||||||
}
|
}
|
||||||
|
|
||||||
func CoreDataSourcePathResolver(cfg *setting.Cfg, pluginRootDirName string) PluginPathResolver {
|
|
||||||
return func() (string, error) {
|
|
||||||
// override mismatch cloud monitoring plugin
|
|
||||||
if pluginRootDirName == "stackdriver" {
|
|
||||||
pluginRootDirName = "cloud-monitoring"
|
|
||||||
}
|
|
||||||
|
|
||||||
return filepath.Join(cfg.StaticRootPath, "app/plugins/datasource", pluginRootDirName), nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -16,28 +16,16 @@ type Store interface {
|
|||||||
Plugin(ctx context.Context, pluginID string) (PluginDTO, bool)
|
Plugin(ctx context.Context, pluginID string) (PluginDTO, bool)
|
||||||
// Plugins returns plugins by their requested type.
|
// Plugins returns plugins by their requested type.
|
||||||
Plugins(ctx context.Context, pluginTypes ...Type) []PluginDTO
|
Plugins(ctx context.Context, pluginTypes ...Type) []PluginDTO
|
||||||
|
|
||||||
// Add adds a plugin to the store.
|
// Add adds a plugin to the store.
|
||||||
Add(ctx context.Context, pluginID, version string) error
|
Add(ctx context.Context, pluginID, version string) error
|
||||||
// AddWithFactory adds a plugin to the store.
|
|
||||||
AddWithFactory(ctx context.Context, pluginID string, factory backendplugin.PluginFactoryFunc, resolver PluginPathResolver) error
|
|
||||||
// Remove removes a plugin from the store.
|
// Remove removes a plugin from the store.
|
||||||
Remove(ctx context.Context, pluginID string) error
|
Remove(ctx context.Context, pluginID string) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type PluginPathResolver func() (string, error)
|
|
||||||
|
|
||||||
type AddOpts struct {
|
|
||||||
PluginInstallDir, PluginZipURL, PluginRepoURL string
|
|
||||||
}
|
|
||||||
|
|
||||||
// Loader is responsible for loading plugins from the file system.
|
// Loader is responsible for loading plugins from the file system.
|
||||||
type Loader interface {
|
type Loader interface {
|
||||||
// Load will return a list of plugins found in the provided file system paths.
|
// Load will return a list of plugins found in the provided file system paths.
|
||||||
Load(ctx context.Context, class Class, paths []string, ignore map[string]struct{}) ([]*Plugin, error)
|
Load(ctx context.Context, class Class, paths []string, ignore map[string]struct{}) ([]*Plugin, error)
|
||||||
// LoadWithFactory will return a plugin found in the provided file system path and use the provided factory to
|
|
||||||
// construct the plugin backend client.
|
|
||||||
LoadWithFactory(ctx context.Context, class Class, path string, factory backendplugin.PluginFactoryFunc) (*Plugin, error)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Installer is responsible for managing plugins (add / remove) on the file system.
|
// Installer is responsible for managing plugins (add / remove) on the file system.
|
||||||
|
@ -11,7 +11,6 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/infra/log"
|
"github.com/grafana/grafana/pkg/infra/log"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
"github.com/grafana/grafana/pkg/plugins"
|
"github.com/grafana/grafana/pkg/plugins"
|
||||||
"github.com/grafana/grafana/pkg/plugins/backendplugin"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Initializer struct {
|
type Initializer struct {
|
||||||
@ -47,21 +46,6 @@ func (i *Initializer) Initialize(ctx context.Context, p *plugins.Plugin) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Initializer) InitializeWithFactory(p *plugins.Plugin, factory backendplugin.PluginFactoryFunc) error {
|
|
||||||
if factory == nil {
|
|
||||||
return fmt.Errorf("could not initialize plugin %s", p.ID)
|
|
||||||
}
|
|
||||||
|
|
||||||
f, err := factory(p.ID, log.New("pluginID", p.ID), []string{})
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
p.RegisterClient(f)
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (i *Initializer) envVars(plugin *plugins.Plugin) []string {
|
func (i *Initializer) envVars(plugin *plugins.Plugin) []string {
|
||||||
hostEnv := []string{
|
hostEnv := []string{
|
||||||
fmt.Sprintf("GF_VERSION=%s", i.cfg.BuildVersion),
|
fmt.Sprintf("GF_VERSION=%s", i.cfg.BuildVersion),
|
||||||
|
@ -103,49 +103,6 @@ func TestInitializer_Initialize(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestInitializer_InitializeWithFactory(t *testing.T) {
|
|
||||||
t.Run("happy path", func(t *testing.T) {
|
|
||||||
p := &plugins.Plugin{}
|
|
||||||
i := &Initializer{
|
|
||||||
cfg: &plugins.Cfg{},
|
|
||||||
log: fakeLogger{},
|
|
||||||
}
|
|
||||||
|
|
||||||
factoryInvoked := false
|
|
||||||
|
|
||||||
factory := backendplugin.PluginFactoryFunc(func(pluginID string, logger log.Logger, env []string) (backendplugin.Plugin, error) {
|
|
||||||
factoryInvoked = true
|
|
||||||
return testPlugin{}, nil
|
|
||||||
})
|
|
||||||
|
|
||||||
err := i.InitializeWithFactory(p, factory)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
|
|
||||||
assert.True(t, factoryInvoked)
|
|
||||||
client, exists := p.Client()
|
|
||||||
assert.True(t, exists)
|
|
||||||
assert.NotNil(t, client.(testPlugin))
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("invalid factory", func(t *testing.T) {
|
|
||||||
p := &plugins.Plugin{}
|
|
||||||
i := &Initializer{
|
|
||||||
cfg: &plugins.Cfg{},
|
|
||||||
log: fakeLogger{},
|
|
||||||
backendProvider: &fakeBackendProvider{
|
|
||||||
plugin: p,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
err := i.InitializeWithFactory(p, nil)
|
|
||||||
assert.Errorf(t, err, "could not initialize plugin test-plugin")
|
|
||||||
|
|
||||||
c, exists := p.Client()
|
|
||||||
assert.False(t, exists)
|
|
||||||
assert.Nil(t, c)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestInitializer_envVars(t *testing.T) {
|
func TestInitializer_envVars(t *testing.T) {
|
||||||
t.Run("backend datasource with license", func(t *testing.T) {
|
t.Run("backend datasource with license", func(t *testing.T) {
|
||||||
p := &plugins.Plugin{
|
p := &plugins.Plugin{
|
||||||
@ -254,10 +211,6 @@ func (*testLicensingService) FeatureEnabled(feature string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
type testPlugin struct {
|
|
||||||
backendplugin.Plugin
|
|
||||||
}
|
|
||||||
|
|
||||||
type fakeLogger struct {
|
type fakeLogger struct {
|
||||||
log.MultiLoggers
|
log.MultiLoggers
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
@ -19,7 +18,6 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/infra/metrics"
|
"github.com/grafana/grafana/pkg/infra/metrics"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
"github.com/grafana/grafana/pkg/plugins"
|
"github.com/grafana/grafana/pkg/plugins"
|
||||||
"github.com/grafana/grafana/pkg/plugins/backendplugin"
|
|
||||||
"github.com/grafana/grafana/pkg/plugins/manager/loader/finder"
|
"github.com/grafana/grafana/pkg/plugins/manager/loader/finder"
|
||||||
"github.com/grafana/grafana/pkg/plugins/manager/loader/initializer"
|
"github.com/grafana/grafana/pkg/plugins/manager/loader/initializer"
|
||||||
"github.com/grafana/grafana/pkg/plugins/manager/signature"
|
"github.com/grafana/grafana/pkg/plugins/manager/signature"
|
||||||
@ -70,37 +68,6 @@ func (l *Loader) Load(ctx context.Context, class plugins.Class, paths []string,
|
|||||||
return l.loadPlugins(ctx, class, pluginJSONPaths, ignore)
|
return l.loadPlugins(ctx, class, pluginJSONPaths, ignore)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Loader) LoadWithFactory(ctx context.Context, class plugins.Class, path string, factory backendplugin.PluginFactoryFunc) (*plugins.Plugin, error) {
|
|
||||||
p, err := l.load(ctx, class, path, map[string]struct{}{})
|
|
||||||
if err != nil {
|
|
||||||
l.log.Error("failed to load core plugin", "err", err)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = l.pluginInitializer.InitializeWithFactory(p, factory)
|
|
||||||
|
|
||||||
return p, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *Loader) load(ctx context.Context, class plugins.Class, path string, ignore map[string]struct{}) (*plugins.Plugin, error) {
|
|
||||||
pluginJSONPaths, err := l.pluginFinder.Find([]string{path})
|
|
||||||
if err != nil {
|
|
||||||
l.log.Error("failed to find plugin", "err", err)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
loadedPlugins, err := l.loadPlugins(ctx, class, pluginJSONPaths, ignore)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(loadedPlugins) == 0 {
|
|
||||||
return nil, fmt.Errorf("could not load plugin at path %s", path)
|
|
||||||
}
|
|
||||||
|
|
||||||
return loadedPlugins[0], nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *Loader) loadPlugins(ctx context.Context, class plugins.Class, pluginJSONPaths []string, existingPlugins map[string]struct{}) ([]*plugins.Plugin, error) {
|
func (l *Loader) loadPlugins(ctx context.Context, class plugins.Class, pluginJSONPaths []string, existingPlugins map[string]struct{}) ([]*plugins.Plugin, error) {
|
||||||
var foundPlugins = foundPlugins{}
|
var foundPlugins = foundPlugins{}
|
||||||
|
|
||||||
|
@ -638,6 +638,7 @@ func TestLoader_Load_DuplicatePlugins(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestLoader_loadNestedPlugins(t *testing.T) {
|
func TestLoader_loadNestedPlugins(t *testing.T) {
|
||||||
|
t.Skip()
|
||||||
parentDir, err := filepath.Abs("../")
|
parentDir, err := filepath.Abs("../")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("could not construct absolute path of root dir")
|
t.Errorf("could not construct absolute path of root dir")
|
||||||
|
@ -2,12 +2,10 @@ package manager
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/plugins"
|
"github.com/grafana/grafana/pkg/plugins"
|
||||||
"github.com/grafana/grafana/pkg/plugins/backendplugin"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func (m *PluginManager) Plugin(_ context.Context, pluginID string) (plugins.PluginDTO, bool) {
|
func (m *PluginManager) Plugin(_ context.Context, pluginID string) (plugins.PluginDTO, bool) {
|
||||||
@ -83,28 +81,6 @@ func (m *PluginManager) Add(ctx context.Context, pluginID, version string) error
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *PluginManager) AddWithFactory(ctx context.Context, pluginID string, factory backendplugin.PluginFactoryFunc,
|
|
||||||
pathResolver plugins.PluginPathResolver) error {
|
|
||||||
if m.isRegistered(pluginID) {
|
|
||||||
return fmt.Errorf("plugin %s is already registered", pluginID)
|
|
||||||
}
|
|
||||||
|
|
||||||
path, err := pathResolver()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
p, err := m.pluginLoader.LoadWithFactory(ctx, plugins.Core, path, factory)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
err = m.register(p)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *PluginManager) Remove(ctx context.Context, pluginID string) error {
|
func (m *PluginManager) Remove(ctx context.Context, pluginID string) error {
|
||||||
plugin, exists := m.plugin(pluginID)
|
plugin, exists := m.plugin(pluginID)
|
||||||
if !exists {
|
if !exists {
|
||||||
|
Reference in New Issue
Block a user