[WIP] Plugins: Refactoring backend initialization flow (#42247)
* refactoring store interface and init flow * fix import * fix linter * refactor resource calling * load with class * re-order args * fix tests * fix linter * remove old creator * add custom config struct * fix some tests * cleanup * fix linter * add connect failure error * remove unused err * convert test over
This commit is contained in:
@@ -1,104 +1,43 @@
|
||||
package initializer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/grafana/grafana-aws-sdk/pkg/awsds"
|
||||
|
||||
"github.com/gosimple/slug"
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/infra/metrics"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/plugins"
|
||||
"github.com/grafana/grafana/pkg/plugins/backendplugin"
|
||||
"github.com/grafana/grafana/pkg/plugins/backendplugin/grpcplugin"
|
||||
"github.com/grafana/grafana/pkg/plugins/backendplugin/pluginextensionv2"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/grafana/grafana/pkg/util"
|
||||
)
|
||||
|
||||
type Initializer struct {
|
||||
cfg *setting.Cfg
|
||||
license models.Licensing
|
||||
log log.Logger
|
||||
cfg *plugins.Cfg
|
||||
license models.Licensing
|
||||
backendProvider plugins.BackendFactoryProvider
|
||||
log log.Logger
|
||||
}
|
||||
|
||||
func New(cfg *setting.Cfg, license models.Licensing) Initializer {
|
||||
func New(cfg *plugins.Cfg, backendProvider plugins.BackendFactoryProvider, license models.Licensing) Initializer {
|
||||
return Initializer{
|
||||
cfg: cfg,
|
||||
license: license,
|
||||
log: log.New("plugin.initializer"),
|
||||
cfg: cfg,
|
||||
license: license,
|
||||
backendProvider: backendProvider,
|
||||
log: log.New("plugin.initializer"),
|
||||
}
|
||||
}
|
||||
|
||||
func (i *Initializer) Initialize(p *plugins.Plugin) error {
|
||||
if len(p.Dependencies.Plugins) == 0 {
|
||||
p.Dependencies.Plugins = []plugins.Dependency{}
|
||||
}
|
||||
|
||||
if p.Dependencies.GrafanaVersion == "" {
|
||||
p.Dependencies.GrafanaVersion = "*"
|
||||
}
|
||||
|
||||
for _, include := range p.Includes {
|
||||
if include.Role == "" {
|
||||
include.Role = models.ROLE_VIEWER
|
||||
}
|
||||
}
|
||||
|
||||
i.handleModuleDefaults(p)
|
||||
|
||||
p.Info.Logos.Small = pluginLogoURL(p.Type, p.Info.Logos.Small, p.BaseURL)
|
||||
p.Info.Logos.Large = pluginLogoURL(p.Type, p.Info.Logos.Large, p.BaseURL)
|
||||
|
||||
for i := 0; i < len(p.Info.Screenshots); i++ {
|
||||
p.Info.Screenshots[i].Path = evalRelativePluginURLPath(p.Info.Screenshots[i].Path, p.BaseURL, p.Type)
|
||||
}
|
||||
|
||||
if p.IsApp() {
|
||||
for _, child := range p.Children {
|
||||
i.setPathsBasedOnApp(p, child)
|
||||
}
|
||||
|
||||
// slugify pages
|
||||
for _, include := range p.Includes {
|
||||
if include.Slug == "" {
|
||||
include.Slug = slug.Make(include.Name)
|
||||
}
|
||||
if include.Type == "page" && include.DefaultNav {
|
||||
p.DefaultNavURL = i.cfg.AppSubURL + "/plugins/" + p.ID + "/page/" + include.Slug
|
||||
}
|
||||
if include.Type == "dashboard" && include.DefaultNav {
|
||||
p.DefaultNavURL = i.cfg.AppSubURL + "/dashboard/db/" + include.Slug
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pluginLog := i.log.New("pluginID", p.ID)
|
||||
p.SetLogger(pluginLog)
|
||||
|
||||
func (i *Initializer) Initialize(ctx context.Context, p *plugins.Plugin) error {
|
||||
if p.Backend {
|
||||
var backendFactory backendplugin.PluginFactoryFunc
|
||||
if p.IsRenderer() {
|
||||
cmd := plugins.ComposeRendererStartCommand()
|
||||
backendFactory = grpcplugin.NewRendererPlugin(p.ID, filepath.Join(p.PluginDir, cmd),
|
||||
func(pluginID string, renderer pluginextensionv2.RendererPlugin, logger log.Logger) error {
|
||||
p.Renderer = renderer
|
||||
return nil
|
||||
},
|
||||
)
|
||||
} else {
|
||||
cmd := plugins.ComposePluginStartCommand(p.Executable)
|
||||
backendFactory = grpcplugin.NewBackendPlugin(p.ID, filepath.Join(p.PluginDir, cmd))
|
||||
backendFactory := i.backendProvider.BackendFactory(ctx, p)
|
||||
if backendFactory == nil {
|
||||
return fmt.Errorf("could not find backend factory for plugin")
|
||||
}
|
||||
|
||||
if backendClient, err := backendFactory(p.ID, pluginLog, i.envVars(p)); err != nil {
|
||||
if backendClient, err := backendFactory(p.ID, p.Logger(), i.envVars(p)); err != nil {
|
||||
return err
|
||||
} else {
|
||||
p.RegisterClient(backendClient)
|
||||
@@ -109,88 +48,20 @@ func (i *Initializer) Initialize(p *plugins.Plugin) error {
|
||||
}
|
||||
|
||||
func (i *Initializer) InitializeWithFactory(p *plugins.Plugin, factory backendplugin.PluginFactoryFunc) error {
|
||||
err := i.Initialize(p)
|
||||
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
|
||||
}
|
||||
|
||||
if factory != nil {
|
||||
var err error
|
||||
|
||||
f, err := factory(p.ID, log.New("pluginID", p.ID), []string{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p.RegisterClient(f)
|
||||
} else {
|
||||
i.log.Warn("Could not initialize core plugin process", "pluginID", p.ID)
|
||||
return fmt.Errorf("could not initialize plugin %s", p.ID)
|
||||
}
|
||||
p.RegisterClient(f)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i *Initializer) handleModuleDefaults(p *plugins.Plugin) {
|
||||
if p.IsCorePlugin() {
|
||||
// Previously there was an assumption that the Core plugins directory
|
||||
// should be public/app/plugins/<plugin type>/<plugin id>
|
||||
// However this can be an issue if the Core plugins directory is renamed
|
||||
baseDir := filepath.Base(p.PluginDir)
|
||||
|
||||
// use path package for the following statements because these are not file paths
|
||||
p.Module = path.Join("app/plugins", string(p.Type), baseDir, "module")
|
||||
p.BaseURL = path.Join("public/app/plugins", string(p.Type), baseDir)
|
||||
return
|
||||
}
|
||||
|
||||
metrics.SetPluginBuildInformation(p.ID, string(p.Type), p.Info.Version, string(p.Signature))
|
||||
|
||||
p.Module = path.Join("plugins", p.ID, "module")
|
||||
p.BaseURL = path.Join("public/plugins", p.ID)
|
||||
}
|
||||
|
||||
func (i *Initializer) setPathsBasedOnApp(parent *plugins.Plugin, child *plugins.Plugin) {
|
||||
appSubPath := strings.ReplaceAll(strings.Replace(child.PluginDir, parent.PluginDir, "", 1), "\\", "/")
|
||||
child.IncludedInAppID = parent.ID
|
||||
child.BaseURL = parent.BaseURL
|
||||
|
||||
if parent.IsCorePlugin() {
|
||||
child.Module = util.JoinURLFragments("app/plugins/app/"+parent.ID, appSubPath) + "/module"
|
||||
} else {
|
||||
child.Module = util.JoinURLFragments("plugins/"+parent.ID, appSubPath) + "/module"
|
||||
}
|
||||
}
|
||||
|
||||
func pluginLogoURL(pluginType plugins.Type, path, baseURL string) string {
|
||||
if path == "" {
|
||||
return defaultLogoPath(pluginType)
|
||||
}
|
||||
|
||||
return evalRelativePluginURLPath(path, baseURL, pluginType)
|
||||
}
|
||||
|
||||
func defaultLogoPath(pluginType plugins.Type) string {
|
||||
return "public/img/icn-" + string(pluginType) + ".svg"
|
||||
}
|
||||
|
||||
func evalRelativePluginURLPath(pathStr, baseURL string, pluginType plugins.Type) string {
|
||||
if pathStr == "" {
|
||||
return ""
|
||||
}
|
||||
|
||||
u, _ := url.Parse(pathStr)
|
||||
if u.IsAbs() {
|
||||
return pathStr
|
||||
}
|
||||
|
||||
// is set as default or has already been prefixed with base path
|
||||
if pathStr == defaultLogoPath(pluginType) || strings.HasPrefix(pathStr, baseURL) {
|
||||
return pathStr
|
||||
}
|
||||
|
||||
return path.Join(baseURL, pathStr)
|
||||
}
|
||||
|
||||
func (i *Initializer) envVars(plugin *plugins.Plugin) []string {
|
||||
hostEnv := []string{
|
||||
fmt.Sprintf("GF_VERSION=%s", i.cfg.BuildVersion),
|
||||
@@ -260,7 +131,7 @@ func (ps pluginSettings) asEnvVar(prefix string, hostEnv []string) []string {
|
||||
return env
|
||||
}
|
||||
|
||||
func getPluginSettings(pluginID string, cfg *setting.Cfg) pluginSettings {
|
||||
func getPluginSettings(pluginID string, cfg *plugins.Cfg) pluginSettings {
|
||||
ps := pluginSettings{}
|
||||
for k, v := range cfg.PluginSettings[pluginID] {
|
||||
if k == "path" || strings.ToLower(k) == "id" {
|
||||
|
||||
@@ -1,17 +1,15 @@
|
||||
package initializer
|
||||
|
||||
import (
|
||||
"path"
|
||||
"context"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/plugins"
|
||||
"github.com/grafana/grafana/pkg/plugins/backendplugin"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
)
|
||||
|
||||
func TestInitializer_Initialize(t *testing.T) {
|
||||
@@ -36,21 +34,16 @@ func TestInitializer_Initialize(t *testing.T) {
|
||||
}
|
||||
|
||||
i := &Initializer{
|
||||
cfg: setting.NewCfg(),
|
||||
cfg: plugins.NewCfg(),
|
||||
log: &fakeLogger{},
|
||||
backendProvider: &fakeBackendProvider{
|
||||
plugin: p,
|
||||
},
|
||||
}
|
||||
|
||||
err := i.Initialize(p)
|
||||
err := i.Initialize(context.Background(), p)
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, "public/img/icn-datasource.svg", p.Info.Logos.Small)
|
||||
assert.Equal(t, "public/img/icn-datasource.svg", p.Info.Logos.Large)
|
||||
assert.Equal(t, "*", p.Dependencies.GrafanaVersion)
|
||||
assert.Len(t, p.Includes, 1)
|
||||
assert.Equal(t, models.ROLE_VIEWER, p.Includes[0].Role)
|
||||
assert.Equal(t, filepath.Join("app/plugins/datasource", filepath.Base(p.PluginDir), "module"), p.Module)
|
||||
assert.Equal(t, path.Join("public/app/plugins/datasource", filepath.Base(p.PluginDir)), p.BaseURL)
|
||||
assert.NotNil(t, p.Logger())
|
||||
c, exists := p.Client()
|
||||
assert.True(t, exists)
|
||||
assert.NotNil(t, c)
|
||||
@@ -71,101 +64,50 @@ func TestInitializer_Initialize(t *testing.T) {
|
||||
}
|
||||
|
||||
i := &Initializer{
|
||||
cfg: setting.NewCfg(),
|
||||
cfg: plugins.NewCfg(),
|
||||
log: fakeLogger{},
|
||||
backendProvider: &fakeBackendProvider{
|
||||
plugin: p,
|
||||
},
|
||||
}
|
||||
|
||||
err := i.Initialize(p)
|
||||
err := i.Initialize(context.Background(), p)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// TODO add default img to project
|
||||
assert.Equal(t, "public/img/icn-renderer.svg", p.Info.Logos.Small)
|
||||
assert.Equal(t, "public/img/icn-renderer.svg", p.Info.Logos.Large)
|
||||
assert.Equal(t, ">=8.x", p.Dependencies.GrafanaVersion)
|
||||
assert.Equal(t, "plugins/test/module", p.Module)
|
||||
assert.Equal(t, "public/plugins/test", p.BaseURL)
|
||||
assert.NotNil(t, p.Logger())
|
||||
c, exists := p.Client()
|
||||
assert.True(t, exists)
|
||||
assert.NotNil(t, c)
|
||||
})
|
||||
|
||||
t.Run("external app", func(t *testing.T) {
|
||||
t.Run("non backend plugin app", func(t *testing.T) {
|
||||
p := &plugins.Plugin{
|
||||
JSONData: plugins.JSONData{
|
||||
ID: "parent-plugin",
|
||||
Type: plugins.App,
|
||||
Includes: []*plugins.Includes{
|
||||
{
|
||||
Type: "page",
|
||||
DefaultNav: true,
|
||||
Slug: "myCustomSlug",
|
||||
},
|
||||
},
|
||||
},
|
||||
PluginDir: absCurPath,
|
||||
Class: plugins.External,
|
||||
Children: []*plugins.Plugin{
|
||||
{
|
||||
JSONData: plugins.JSONData{
|
||||
ID: "child-plugin",
|
||||
},
|
||||
PluginDir: absCurPath,
|
||||
},
|
||||
Backend: false,
|
||||
},
|
||||
}
|
||||
|
||||
i := &Initializer{
|
||||
cfg: &setting.Cfg{
|
||||
AppSubURL: "appSubURL",
|
||||
},
|
||||
cfg: &plugins.Cfg{},
|
||||
log: fakeLogger{},
|
||||
backendProvider: &fakeBackendProvider{
|
||||
plugin: p,
|
||||
},
|
||||
}
|
||||
|
||||
err := i.Initialize(p)
|
||||
err := i.Initialize(context.Background(), p)
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, "public/img/icn-app.svg", p.Info.Logos.Small)
|
||||
assert.Equal(t, "public/img/icn-app.svg", p.Info.Logos.Large)
|
||||
assert.Equal(t, "*", p.Dependencies.GrafanaVersion)
|
||||
assert.Len(t, p.Includes, 1)
|
||||
assert.Equal(t, models.ROLE_VIEWER, p.Includes[0].Role)
|
||||
assert.Equal(t, filepath.Join("plugins", p.ID, "module"), p.Module)
|
||||
assert.Equal(t, "public/plugins/parent-plugin", p.BaseURL)
|
||||
assert.NotNil(t, p.Logger())
|
||||
c, exists := p.Client()
|
||||
assert.False(t, exists)
|
||||
assert.Nil(t, c)
|
||||
|
||||
assert.Len(t, p.Children, 1)
|
||||
assert.Equal(t, p.ID, p.Children[0].IncludedInAppID)
|
||||
assert.Equal(t, "public/plugins/parent-plugin", p.Children[0].BaseURL)
|
||||
assert.Equal(t, "plugins/parent-plugin/module", p.Children[0].Module)
|
||||
assert.Equal(t, "appSubURL/plugins/parent-plugin/page/myCustomSlug", p.DefaultNavURL)
|
||||
})
|
||||
}
|
||||
|
||||
func TestInitializer_InitializeWithFactory(t *testing.T) {
|
||||
t.Run("happy path", func(t *testing.T) {
|
||||
p := &plugins.Plugin{
|
||||
JSONData: plugins.JSONData{
|
||||
ID: "test-plugin",
|
||||
Type: plugins.App,
|
||||
Includes: []*plugins.Includes{
|
||||
{
|
||||
Type: "page",
|
||||
DefaultNav: true,
|
||||
Slug: "myCustomSlug",
|
||||
},
|
||||
},
|
||||
},
|
||||
PluginDir: "test/folder",
|
||||
Class: plugins.External,
|
||||
}
|
||||
p := &plugins.Plugin{}
|
||||
i := &Initializer{
|
||||
cfg: &setting.Cfg{
|
||||
AppSubURL: "appSubURL",
|
||||
},
|
||||
cfg: &plugins.Cfg{},
|
||||
log: fakeLogger{},
|
||||
}
|
||||
|
||||
@@ -180,33 +122,19 @@ func TestInitializer_InitializeWithFactory(t *testing.T) {
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.True(t, factoryInvoked)
|
||||
assert.NotNil(t, p.Logger())
|
||||
client, exists := p.Client()
|
||||
assert.True(t, exists)
|
||||
assert.NotNil(t, client.(testPlugin))
|
||||
})
|
||||
|
||||
t.Run("invalid factory", func(t *testing.T) {
|
||||
p := &plugins.Plugin{
|
||||
JSONData: plugins.JSONData{
|
||||
ID: "test-plugin",
|
||||
Type: plugins.App,
|
||||
Includes: []*plugins.Includes{
|
||||
{
|
||||
Type: "page",
|
||||
DefaultNav: true,
|
||||
Slug: "myCustomSlug",
|
||||
},
|
||||
},
|
||||
},
|
||||
PluginDir: "test/folder",
|
||||
Class: plugins.External,
|
||||
}
|
||||
p := &plugins.Plugin{}
|
||||
i := &Initializer{
|
||||
cfg: &setting.Cfg{
|
||||
AppSubURL: "appSubURL",
|
||||
},
|
||||
cfg: &plugins.Cfg{},
|
||||
log: fakeLogger{},
|
||||
backendProvider: &fakeBackendProvider{
|
||||
plugin: p,
|
||||
},
|
||||
}
|
||||
|
||||
err := i.InitializeWithFactory(p, nil)
|
||||
@@ -232,7 +160,7 @@ func TestInitializer_envVars(t *testing.T) {
|
||||
}
|
||||
|
||||
i := &Initializer{
|
||||
cfg: &setting.Cfg{
|
||||
cfg: &plugins.Cfg{
|
||||
EnterpriseLicensePath: "/path/to/ent/license",
|
||||
PluginSettings: map[string]map[string]string{
|
||||
"test": {
|
||||
@@ -242,6 +170,9 @@ func TestInitializer_envVars(t *testing.T) {
|
||||
},
|
||||
license: licensing,
|
||||
log: fakeLogger{},
|
||||
backendProvider: &fakeBackendProvider{
|
||||
plugin: p,
|
||||
},
|
||||
}
|
||||
|
||||
envVars := i.envVars(p)
|
||||
@@ -254,33 +185,6 @@ func TestInitializer_envVars(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestInitializer_setPathsBasedOnApp(t *testing.T) {
|
||||
t.Run("When setting paths based on core plugin on Windows", func(t *testing.T) {
|
||||
i := &Initializer{
|
||||
cfg: setting.NewCfg(),
|
||||
log: fakeLogger{},
|
||||
}
|
||||
|
||||
child := &plugins.Plugin{
|
||||
PluginDir: "c:\\grafana\\public\\app\\plugins\\app\\testdata\\datasources\\datasource",
|
||||
}
|
||||
parent := &plugins.Plugin{
|
||||
JSONData: plugins.JSONData{
|
||||
ID: "testdata",
|
||||
},
|
||||
Class: plugins.Core,
|
||||
PluginDir: "c:\\grafana\\public\\app\\plugins\\app\\testdata",
|
||||
BaseURL: "public/app/plugins/app/testdata",
|
||||
}
|
||||
|
||||
i.setPathsBasedOnApp(parent, child)
|
||||
|
||||
assert.Equal(t, "app/plugins/app/testdata/datasources/datasource/module", child.Module)
|
||||
assert.Equal(t, "testdata", child.IncludedInAppID)
|
||||
assert.Equal(t, "public/app/plugins/app/testdata", child.BaseURL)
|
||||
})
|
||||
}
|
||||
|
||||
func TestInitializer_getAWSEnvironmentVariables(t *testing.T) {
|
||||
|
||||
}
|
||||
@@ -334,7 +238,7 @@ func (t *testLicensingService) ContentDeliveryPrefix() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (t *testLicensingService) LicenseURL(showAdminLicensingPage bool) string {
|
||||
func (t *testLicensingService) LicenseURL(_ bool) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
@@ -365,3 +269,15 @@ func (f fakeLogger) New(_ ...interface{}) log.MultiLoggers {
|
||||
func (f fakeLogger) Warn(_ string, _ ...interface{}) {
|
||||
|
||||
}
|
||||
|
||||
type fakeBackendProvider struct {
|
||||
plugins.BackendFactoryProvider
|
||||
|
||||
plugin *plugins.Plugin
|
||||
}
|
||||
|
||||
func (f *fakeBackendProvider) BackendFactory(_ context.Context, _ *plugins.Plugin) backendplugin.PluginFactoryFunc {
|
||||
return func(_ string, _ log.Logger, _ []string) (backendplugin.Plugin, error) {
|
||||
return f.plugin, nil
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user