Plugins: Move discovery logic to plugin sources (#106911)
* move finder behaviour to source * tidy * undo go.mod changes * fix comment * tidy unsafe local source
This commit is contained in:
@@ -13,62 +13,57 @@ type Discoverer interface {
|
||||
Discover(ctx context.Context, src plugins.PluginSource) ([]*plugins.FoundBundle, error)
|
||||
}
|
||||
|
||||
// FindFunc is the function used for the Find step of the Discovery stage.
|
||||
type FindFunc func(ctx context.Context, src plugins.PluginSource) ([]*plugins.FoundBundle, error)
|
||||
|
||||
// FindFilterFunc is the function used for the Filter step of the Discovery stage.
|
||||
type FindFilterFunc func(ctx context.Context, class plugins.Class, bundles []*plugins.FoundBundle) ([]*plugins.FoundBundle, error)
|
||||
// FilterFunc is the function used for the Filter step of the Discovery stage.
|
||||
type FilterFunc func(ctx context.Context, class plugins.Class, bundles []*plugins.FoundBundle) ([]*plugins.FoundBundle, error)
|
||||
|
||||
// Discovery implements the Discoverer interface.
|
||||
//
|
||||
// The Discovery stage is made up of the following steps (in order):
|
||||
// - Find: Find plugins (from disk, remote, etc.)
|
||||
// - Discover: Each source discovers its own plugins
|
||||
// - Filter: Filter the results based on some criteria.
|
||||
//
|
||||
// The Find step is implemented by the FindFunc type.
|
||||
//
|
||||
// The Filter step is implemented by the FindFilterFunc type.
|
||||
// The Filter step is implemented by the FilterFunc type.
|
||||
type Discovery struct {
|
||||
findStep FindFunc
|
||||
findFilterSteps []FindFilterFunc
|
||||
log log.Logger
|
||||
filterSteps []FilterFunc
|
||||
log log.Logger
|
||||
}
|
||||
|
||||
type Opts struct {
|
||||
FindFunc FindFunc
|
||||
FindFilterFuncs []FindFilterFunc
|
||||
FilterFuncs []FilterFunc
|
||||
}
|
||||
|
||||
// New returns a new Discovery stage.
|
||||
func New(cfg *config.PluginManagementCfg, opts Opts) *Discovery {
|
||||
if opts.FindFunc == nil {
|
||||
opts.FindFunc = DefaultFindFunc(cfg)
|
||||
}
|
||||
|
||||
if opts.FindFilterFuncs == nil {
|
||||
opts.FindFilterFuncs = []FindFilterFunc{} // no filters by default
|
||||
func New(_ *config.PluginManagementCfg, opts Opts) *Discovery {
|
||||
if opts.FilterFuncs == nil {
|
||||
opts.FilterFuncs = []FilterFunc{} // no filters by default
|
||||
}
|
||||
|
||||
return &Discovery{
|
||||
findStep: opts.FindFunc,
|
||||
findFilterSteps: opts.FindFilterFuncs,
|
||||
log: log.New("plugins.discovery"),
|
||||
filterSteps: opts.FilterFuncs,
|
||||
log: log.New("plugins.discovery"),
|
||||
}
|
||||
}
|
||||
|
||||
// Discover will execute the Find and Filter steps of the Discovery stage.
|
||||
// Discover will execute the Filter step of the Discovery stage.
|
||||
func (d *Discovery) Discover(ctx context.Context, src plugins.PluginSource) ([]*plugins.FoundBundle, error) {
|
||||
discoveredPlugins, err := d.findStep(ctx, src)
|
||||
// Use the source's own Discover method
|
||||
found, err := src.Discover(ctx)
|
||||
if err != nil {
|
||||
d.log.Warn("Discovery source failed", "class", src.PluginClass(ctx), "error", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, filter := range d.findFilterSteps {
|
||||
discoveredPlugins, err = filter(ctx, src.PluginClass(ctx), discoveredPlugins)
|
||||
d.log.Debug("Found plugins", "class", src.PluginClass(ctx), "count", len(found))
|
||||
|
||||
// Apply filtering steps
|
||||
result := found
|
||||
for _, filter := range d.filterSteps {
|
||||
result, err = filter(ctx, src.PluginClass(ctx), result)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return discoveredPlugins, nil
|
||||
d.log.Debug("Discovery complete", "class", src.PluginClass(ctx), "found", len(found), "filtered", len(result))
|
||||
return result, nil
|
||||
}
|
||||
|
||||
@@ -5,24 +5,16 @@ import (
|
||||
"slices"
|
||||
|
||||
"github.com/grafana/grafana/pkg/plugins"
|
||||
"github.com/grafana/grafana/pkg/plugins/config"
|
||||
"github.com/grafana/grafana/pkg/plugins/manager/loader/finder"
|
||||
)
|
||||
|
||||
// DefaultFindFunc is the default function used for the Find step of the Discovery stage. It will scan the local
|
||||
// filesystem for plugins.
|
||||
func DefaultFindFunc(cfg *config.PluginManagementCfg) FindFunc {
|
||||
return finder.NewLocalFinder(cfg.DevMode).Find
|
||||
}
|
||||
|
||||
// PermittedPluginTypesFilter is a filter step that will filter out any plugins that are not of a permitted type.
|
||||
type PermittedPluginTypesFilter struct {
|
||||
permittedTypes []plugins.Type
|
||||
}
|
||||
|
||||
// NewPermittedPluginTypesFilterStep returns a new FindFilterFunc for filtering out any plugins that are not of a
|
||||
// NewPermittedPluginTypesFilterStep returns a new FilterFunc for filtering out any plugins that are not of a
|
||||
// permitted type. This includes both the primary plugin and any child plugins.
|
||||
func NewPermittedPluginTypesFilterStep(permittedTypes []plugins.Type) FindFilterFunc {
|
||||
func NewPermittedPluginTypesFilterStep(permittedTypes []plugins.Type) FilterFunc {
|
||||
f := &PermittedPluginTypesFilter{
|
||||
permittedTypes: permittedTypes,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user