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
+28 -8
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
}