Plugins: Refactor plugin repository API (#69063)

* support grafana wildcard version

* undo go.mod changes

* tidy

* flesh out tests

* refactor

* add tests

* tidy naming

* undo some changes

* split interfaces

* separation

* update new signature

* simplify

* update var namings

* unexport types

* introduce opts pattern

* reorder test

* fix compat checks

* middle ground

* unexport client

* move back

* fix tests

* inline logger

* make client usable

* use fake logger

* tidy errors

* remove unused types

* fix test

* review fixes

* rework compatibility

* adjust installer

* fix tests

* opts => cfg

* remove unused var

* fix var name
This commit is contained in:
Will Browne
2023-05-30 11:48:52 +02:00
committed by GitHub
parent e7e70dbac6
commit 12dc56ad0c
18 changed files with 724 additions and 358 deletions

View File

@@ -200,9 +200,9 @@ func (f *FakePluginRegistry) Remove(_ context.Context, id string) error {
}
type FakePluginRepo struct {
GetPluginArchiveFunc func(_ context.Context, pluginID, version string, _ repo.CompatOpts) (*repo.PluginArchive, error)
GetPluginArchiveByURLFunc func(_ context.Context, archiveURL string, _ repo.CompatOpts) (*repo.PluginArchive, error)
GetPluginDownloadOptionsFunc func(_ context.Context, pluginID, version string, _ repo.CompatOpts) (*repo.PluginDownloadOptions, error)
GetPluginArchiveFunc func(_ context.Context, pluginID, version string, _ repo.CompatOpts) (*repo.PluginArchive, error)
GetPluginArchiveByURLFunc func(_ context.Context, archiveURL string, _ repo.CompatOpts) (*repo.PluginArchive, error)
GetPluginArchiveInfoFunc func(_ context.Context, pluginID, version string, _ repo.CompatOpts) (*repo.PluginArchiveInfo, error)
}
// GetPluginArchive fetches the requested plugin archive.
@@ -223,12 +223,12 @@ func (r *FakePluginRepo) GetPluginArchiveByURL(ctx context.Context, archiveURL s
return &repo.PluginArchive{}, nil
}
// GetPluginDownloadOptions fetches information for downloading the requested plugin.
func (r *FakePluginRepo) GetPluginDownloadOptions(ctx context.Context, pluginID, version string, opts repo.CompatOpts) (*repo.PluginDownloadOptions, error) {
if r.GetPluginDownloadOptionsFunc != nil {
return r.GetPluginDownloadOptionsFunc(ctx, pluginID, version, opts)
// GetPluginArchiveInfo fetches information for downloading the requested plugin.
func (r *FakePluginRepo) GetPluginArchiveInfo(ctx context.Context, pluginID, version string, opts repo.CompatOpts) (*repo.PluginArchiveInfo, error) {
if r.GetPluginArchiveInfoFunc != nil {
return r.GetPluginArchiveInfoFunc(ctx, pluginID, version, opts)
}
return &repo.PluginDownloadOptions{}, nil
return &repo.PluginArchiveInfo{}, nil
}
type FakePluginStorage struct {

View File

@@ -2,6 +2,7 @@ package manager
import (
"context"
"errors"
"fmt"
"github.com/grafana/grafana/pkg/plugins"
@@ -26,7 +27,8 @@ type PluginInstaller struct {
func ProvideInstaller(cfg *config.Cfg, pluginRegistry registry.Service, pluginLoader loader.Service,
pluginRepo repo.Service) *PluginInstaller {
return New(pluginRegistry, pluginLoader, pluginRepo, storage.FileSystem(log.NewPrettyLogger("installer.fs"), cfg.PluginsPath))
return New(pluginRegistry, pluginLoader, pluginRepo,
storage.FileSystem(log.NewPrettyLogger("installer.fs"), cfg.PluginsPath))
}
func New(pluginRegistry registry.Service, pluginLoader loader.Service, pluginRepo repo.Service,
@@ -41,7 +43,10 @@ func New(pluginRegistry registry.Service, pluginLoader loader.Service, pluginRep
}
func (m *PluginInstaller) Add(ctx context.Context, pluginID, version string, opts plugins.CompatOpts) error {
compatOpts := repo.NewCompatOpts(opts.GrafanaVersion, opts.OS, opts.Arch)
compatOpts, err := repoCompatOpts(opts)
if err != nil {
return err
}
var pluginArchive *repo.PluginArchive
if plugin, exists := m.plugin(ctx, pluginID); exists {
@@ -56,19 +61,19 @@ func (m *PluginInstaller) Add(ctx context.Context, pluginID, version string, opt
}
// get plugin update information to confirm if target update is possible
dlOpts, err := m.pluginRepo.GetPluginDownloadOptions(ctx, pluginID, version, compatOpts)
pluginArchiveInfo, err := m.pluginRepo.GetPluginArchiveInfo(ctx, pluginID, version, compatOpts)
if err != nil {
return err
}
// if existing plugin version is the same as the target update version
if dlOpts.Version == plugin.Info.Version {
if pluginArchiveInfo.Version == plugin.Info.Version {
return plugins.DuplicateError{
PluginID: plugin.ID,
}
}
if dlOpts.PluginZipURL == "" && dlOpts.Version == "" {
if pluginArchiveInfo.URL == "" && pluginArchiveInfo.Version == "" {
return fmt.Errorf("could not determine update options for %s", pluginID)
}
@@ -78,13 +83,13 @@ func (m *PluginInstaller) Add(ctx context.Context, pluginID, version string, opt
return err
}
if dlOpts.PluginZipURL != "" {
pluginArchive, err = m.pluginRepo.GetPluginArchiveByURL(ctx, dlOpts.PluginZipURL, compatOpts)
if pluginArchiveInfo.URL != "" {
pluginArchive, err = m.pluginRepo.GetPluginArchiveByURL(ctx, pluginArchiveInfo.URL, compatOpts)
if err != nil {
return err
}
} else {
pluginArchive, err = m.pluginRepo.GetPluginArchive(ctx, pluginID, dlOpts.Version, compatOpts)
pluginArchive, err = m.pluginRepo.GetPluginArchive(ctx, pluginID, pluginArchiveInfo.Version, compatOpts)
if err != nil {
return err
}
@@ -153,3 +158,18 @@ func (m *PluginInstaller) plugin(ctx context.Context, pluginID string) (*plugins
return p, true
}
func repoCompatOpts(opts plugins.CompatOpts) (repo.CompatOpts, error) {
os := opts.OS()
arch := opts.Arch()
if len(os) == 0 || len(arch) == 0 {
return repo.CompatOpts{}, errors.New("invalid system compatibility options provided")
}
grafanaVersion := opts.GrafanaVersion()
if len(grafanaVersion) == 0 {
return repo.NewSystemCompatOpts(os, arch), nil
}
return repo.NewCompatOpts(grafanaVersion, os, arch), nil
}

View File

@@ -4,6 +4,7 @@ import (
"archive/zip"
"context"
"fmt"
"runtime"
"testing"
"github.com/stretchr/testify/require"
@@ -62,7 +63,7 @@ func TestPluginManager_Add_Remove(t *testing.T) {
}
inst := New(fakes.NewFakePluginRegistry(), loader, pluginRepo, fs)
err := inst.Add(context.Background(), pluginID, v1, plugins.CompatOpts{})
err := inst.Add(context.Background(), pluginID, v1, testCompatOpts())
require.NoError(t, err)
t.Run("Won't add if already exists", func(t *testing.T) {
@@ -72,7 +73,7 @@ func TestPluginManager_Add_Remove(t *testing.T) {
},
}
err = inst.Add(context.Background(), pluginID, v1, plugins.CompatOpts{})
err = inst.Add(context.Background(), pluginID, v1, testCompatOpts())
require.Equal(t, plugins.DuplicateError{
PluginID: pluginV1.ID,
}, err)
@@ -96,9 +97,9 @@ func TestPluginManager_Add_Remove(t *testing.T) {
require.Equal(t, []string{zipNameV2}, src.PluginURIs(ctx))
return []*plugins.Plugin{pluginV2}, nil
}
pluginRepo.GetPluginDownloadOptionsFunc = func(_ context.Context, pluginID, version string, _ repo.CompatOpts) (*repo.PluginDownloadOptions, error) {
return &repo.PluginDownloadOptions{
PluginZipURL: "https://grafanaplugins.com",
pluginRepo.GetPluginArchiveInfoFunc = func(_ context.Context, _, _ string, _ repo.CompatOpts) (*repo.PluginArchiveInfo, error) {
return &repo.PluginArchiveInfo{
URL: "https://grafanaplugins.com",
}, nil
}
pluginRepo.GetPluginArchiveByURLFunc = func(_ context.Context, pluginZipURL string, _ repo.CompatOpts) (*repo.PluginArchive, error) {
@@ -115,7 +116,7 @@ func TestPluginManager_Add_Remove(t *testing.T) {
}, nil
}
err = inst.Add(context.Background(), pluginID, v2, plugins.CompatOpts{})
err = inst.Add(context.Background(), pluginID, v2, testCompatOpts())
require.NoError(t, err)
})
@@ -168,10 +169,10 @@ func TestPluginManager_Add_Remove(t *testing.T) {
}
pm := New(reg, &fakes.FakeLoader{}, &fakes.FakePluginRepo{}, &fakes.FakePluginStorage{})
err := pm.Add(context.Background(), p.ID, "3.2.0", plugins.CompatOpts{})
err := pm.Add(context.Background(), p.ID, "3.2.0", testCompatOpts())
require.ErrorIs(t, err, plugins.ErrInstallCorePlugin)
err = pm.Add(context.Background(), testPluginID, "", plugins.CompatOpts{})
err = pm.Add(context.Background(), testPluginID, "", testCompatOpts())
require.Equal(t, plugins.ErrInstallCorePlugin, err)
t.Run(fmt.Sprintf("Can't uninstall %s plugin", tc.class), func(t *testing.T) {
@@ -206,3 +207,7 @@ func createPlugin(t *testing.T, pluginID string, class plugins.Class, managed, b
return p
}
func testCompatOpts() plugins.CompatOpts {
return plugins.NewCompatOpts("10.0.0", runtime.GOOS, runtime.GOARCH)
}