Plugins: Add synchronous CDN plugin loader (#99096)
* WIP * Run plugin validations and validation steps sequentially if feature is off * Remove dependency between sources.Service and pluginscdn.Service * lint * Parallelize validation only if class is CDN * re-generate feature toggles * remove waitgroup usage * PR review: Add loader concurrency limit setting * re-generate feature toggles * pr review feedback * fix const name * Skip module.js validation for cdn plugins * do not run validation steps in parallel * lint * reduce diff * re-generate feature toggles * lint * pr review feedback * remove leftover config.PluginManagementCfg from sources.Service
This commit is contained in:
@@ -233,6 +233,7 @@ Experimental features might be changed or removed without prior notice.
|
||||
| `elasticsearchImprovedParsing` | Enables less memory intensive Elasticsearch result parsing |
|
||||
| `datasourceConnectionsTab` | Shows defined connections for a data source in the plugins detail page |
|
||||
| `newLogsPanel` | Enables the new logs panel in Explore |
|
||||
| `pluginsCDNSyncLoader` | Load plugins from CDN synchronously |
|
||||
|
||||
## Development feature toggles
|
||||
|
||||
|
||||
@@ -257,4 +257,5 @@ export interface FeatureToggles {
|
||||
alertingAlertmanagerExtraDedupStageStopPipeline?: boolean;
|
||||
newLogsPanel?: boolean;
|
||||
grafanaconThemes?: boolean;
|
||||
pluginsCDNSyncLoader?: boolean;
|
||||
}
|
||||
|
||||
@@ -31,9 +31,10 @@ type PluginManagementCfg struct {
|
||||
|
||||
// Features contains the feature toggles used for the plugin management system.
|
||||
type Features struct {
|
||||
ExternalCorePluginsEnabled bool
|
||||
SkipHostEnvVarsEnabled bool
|
||||
SriChecksEnabled bool
|
||||
ExternalCorePluginsEnabled bool
|
||||
SkipHostEnvVarsEnabled bool
|
||||
SriChecksEnabled bool
|
||||
PluginsCDNSyncLoaderEnabled bool
|
||||
}
|
||||
|
||||
// NewPluginManagementCfg returns a new PluginManagementCfg.
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana/pkg/plugins"
|
||||
pluginsCfg "github.com/grafana/grafana/pkg/plugins/config"
|
||||
"github.com/grafana/grafana/pkg/plugins/log"
|
||||
"github.com/grafana/grafana/pkg/plugins/manager/pipeline/bootstrap"
|
||||
"github.com/grafana/grafana/pkg/plugins/manager/pipeline/discovery"
|
||||
@@ -17,7 +18,10 @@ import (
|
||||
"github.com/grafana/grafana/pkg/services/pluginsintegration/pluginerrs"
|
||||
)
|
||||
|
||||
const concurrencyLimit = 32
|
||||
|
||||
type Loader struct {
|
||||
cfg *pluginsCfg.PluginManagementCfg
|
||||
discovery discovery.Discoverer
|
||||
bootstrap bootstrap.Bootstrapper
|
||||
initializer initialization.Initializer
|
||||
@@ -27,9 +31,13 @@ type Loader struct {
|
||||
log log.Logger
|
||||
}
|
||||
|
||||
func New(discovery discovery.Discoverer, bootstrap bootstrap.Bootstrapper, validation validation.Validator,
|
||||
initializer initialization.Initializer, termination termination.Terminator, errorTracker pluginerrs.ErrorTracker) *Loader {
|
||||
func New(
|
||||
cfg *pluginsCfg.PluginManagementCfg,
|
||||
discovery discovery.Discoverer, bootstrap bootstrap.Bootstrapper, validation validation.Validator,
|
||||
initializer initialization.Initializer, termination termination.Terminator, errorTracker pluginerrs.ErrorTracker,
|
||||
) *Loader {
|
||||
return &Loader{
|
||||
cfg: cfg,
|
||||
discovery: discovery,
|
||||
bootstrap: bootstrap,
|
||||
validation: validation,
|
||||
@@ -55,11 +63,14 @@ func (l *Loader) recordError(ctx context.Context, p *plugins.Plugin, err error)
|
||||
func (l *Loader) Load(ctx context.Context, src plugins.PluginSource) ([]*plugins.Plugin, error) {
|
||||
end := l.instrumentLoad(ctx, src)
|
||||
|
||||
st := time.Now()
|
||||
discoveredPlugins, err := l.discovery.Discover(ctx, src)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
l.log.Debug("Discovered", "class", src.PluginClass(ctx), "duration", time.Since(st))
|
||||
|
||||
st = time.Now()
|
||||
bootstrappedPlugins := []*plugins.Plugin{}
|
||||
for _, foundBundle := range discoveredPlugins {
|
||||
bootstrappedPlugin, err := l.bootstrap.Bootstrap(ctx, src, foundBundle)
|
||||
@@ -72,17 +83,47 @@ func (l *Loader) Load(ctx context.Context, src plugins.PluginSource) ([]*plugins
|
||||
}
|
||||
bootstrappedPlugins = append(bootstrappedPlugins, bootstrappedPlugin...)
|
||||
}
|
||||
l.log.Debug("Bootstrapped", "class", src.PluginClass(ctx), "duration", time.Since(st))
|
||||
|
||||
st = time.Now()
|
||||
validatedPlugins := []*plugins.Plugin{}
|
||||
type validateResult struct {
|
||||
bootstrappedPlugin *plugins.Plugin
|
||||
err error
|
||||
}
|
||||
validateResults := make(chan validateResult, len(bootstrappedPlugins))
|
||||
|
||||
// If the PluginsCDNSyncLoaderEnabled feature is enabled, validate plugins in parallel.
|
||||
// Otherwise, validate plugins sequentially.
|
||||
var limitSize int
|
||||
if l.cfg.Features.PluginsCDNSyncLoaderEnabled && src.PluginClass(ctx) == plugins.ClassCDN {
|
||||
limitSize = min(len(bootstrappedPlugins), concurrencyLimit)
|
||||
} else {
|
||||
limitSize = 1
|
||||
}
|
||||
limit := make(chan struct{}, limitSize)
|
||||
for _, bootstrappedPlugin := range bootstrappedPlugins {
|
||||
err := l.validation.Validate(ctx, bootstrappedPlugin)
|
||||
if err != nil {
|
||||
l.recordError(ctx, bootstrappedPlugin, err)
|
||||
limit <- struct{}{}
|
||||
go func(p *plugins.Plugin) {
|
||||
err := l.validation.Validate(ctx, p)
|
||||
validateResults <- validateResult{
|
||||
bootstrappedPlugin: bootstrappedPlugin,
|
||||
err: err,
|
||||
}
|
||||
<-limit
|
||||
}(bootstrappedPlugin)
|
||||
}
|
||||
for i := 0; i < len(bootstrappedPlugins); i++ {
|
||||
r := <-validateResults
|
||||
if r.err != nil {
|
||||
l.recordError(ctx, r.bootstrappedPlugin, r.err)
|
||||
continue
|
||||
}
|
||||
validatedPlugins = append(validatedPlugins, bootstrappedPlugin)
|
||||
validatedPlugins = append(validatedPlugins, r.bootstrappedPlugin)
|
||||
}
|
||||
l.log.Debug("Validated", "class", src.PluginClass(ctx), "duration", time.Since(st), "total", len(validatedPlugins))
|
||||
|
||||
st = time.Now()
|
||||
initializedPlugins := []*plugins.Plugin{}
|
||||
for _, validatedPlugin := range validatedPlugins {
|
||||
initializedPlugin, err := l.initializer.Initialize(ctx, validatedPlugin)
|
||||
@@ -92,6 +133,7 @@ func (l *Loader) Load(ctx context.Context, src plugins.PluginSource) ([]*plugins
|
||||
}
|
||||
initializedPlugins = append(initializedPlugins, initializedPlugin)
|
||||
}
|
||||
l.log.Debug("Initialized", "class", src.PluginClass(ctx), "duration", time.Since(st))
|
||||
|
||||
// Clean errors from registry for initialized plugins
|
||||
for _, p := range initializedPlugins {
|
||||
|
||||
@@ -58,6 +58,7 @@ func TestLoader_Load(t *testing.T) {
|
||||
t.Errorf("could not construct absolute path of current dir")
|
||||
return
|
||||
}
|
||||
zeroCfg := &config.PluginManagementCfg{}
|
||||
tests := []struct {
|
||||
name string
|
||||
class plugins.Class
|
||||
@@ -420,7 +421,7 @@ func TestLoader_Load(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
et := pluginerrs.ProvideErrorTracker()
|
||||
|
||||
l := New(discovery.New(tt.cfg, discovery.Opts{}), bootstrap.New(tt.cfg, bootstrap.Opts{}),
|
||||
l := New(zeroCfg, discovery.New(tt.cfg, discovery.Opts{}), bootstrap.New(tt.cfg, bootstrap.Opts{}),
|
||||
validation.New(tt.cfg, validation.Opts{}), initialization.New(tt.cfg, initialization.Opts{}),
|
||||
terminationStage, et)
|
||||
|
||||
@@ -455,6 +456,7 @@ func TestLoader_Load(t *testing.T) {
|
||||
|
||||
var steps []string
|
||||
l := New(
|
||||
zeroCfg,
|
||||
&fakes.FakeDiscoverer{
|
||||
DiscoverFunc: func(ctx context.Context, s plugins.PluginSource) ([]*plugins.FoundBundle, error) {
|
||||
require.Equal(t, src, s)
|
||||
@@ -512,6 +514,7 @@ func TestLoader_Load(t *testing.T) {
|
||||
|
||||
var steps []string
|
||||
l := New(
|
||||
zeroCfg,
|
||||
&fakes.FakeDiscoverer{
|
||||
DiscoverFunc: func(ctx context.Context, s plugins.PluginSource) ([]*plugins.FoundBundle, error) {
|
||||
require.Equal(t, src, s)
|
||||
@@ -574,6 +577,7 @@ func TestLoader_Load(t *testing.T) {
|
||||
|
||||
var steps []string
|
||||
l := New(
|
||||
zeroCfg,
|
||||
&fakes.FakeDiscoverer{
|
||||
DiscoverFunc: func(ctx context.Context, s plugins.PluginSource) ([]*plugins.FoundBundle, error) {
|
||||
require.Equal(t, src, s)
|
||||
@@ -629,7 +633,9 @@ func TestLoader_Unload(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, tc := range tcs {
|
||||
l := New(&fakes.FakeDiscoverer{},
|
||||
l := New(
|
||||
&config.PluginManagementCfg{},
|
||||
&fakes.FakeDiscoverer{},
|
||||
&fakes.FakeBootstrapper{},
|
||||
&fakes.FakeValidator{},
|
||||
&fakes.FakeInitializer{},
|
||||
|
||||
@@ -57,6 +57,11 @@ func newModuleJSValidator() *ModuleJSValidator {
|
||||
}
|
||||
|
||||
func (v *ModuleJSValidator) Validate(_ context.Context, p *plugins.Plugin) error {
|
||||
// CDN plugins are ignored because the module.js is guaranteed to exist
|
||||
if p.Class == plugins.ClassCDN {
|
||||
return nil
|
||||
}
|
||||
|
||||
if !p.IsRenderer() && !p.IsCorePlugin() {
|
||||
f, err := p.FS.Open("module.js")
|
||||
if err != nil {
|
||||
|
||||
@@ -1794,6 +1794,12 @@ var (
|
||||
HideFromDocs: true,
|
||||
RequiresRestart: true,
|
||||
},
|
||||
{
|
||||
Name: "pluginsCDNSyncLoader",
|
||||
Description: "Load plugins from CDN synchronously",
|
||||
Stage: FeatureStageExperimental,
|
||||
Owner: grafanaPluginsPlatformSquad,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@@ -238,3 +238,4 @@ alertingAlertmanagerExtraDedupStage,experimental,@grafana/alerting-squad,false,t
|
||||
alertingAlertmanagerExtraDedupStageStopPipeline,experimental,@grafana/alerting-squad,false,true,false
|
||||
newLogsPanel,experimental,@grafana/observability-logs,false,false,true
|
||||
grafanaconThemes,experimental,@grafana/grafana-frontend-platform,false,true,false
|
||||
pluginsCDNSyncLoader,experimental,@grafana/plugins-platform-backend,false,false,false
|
||||
|
||||
|
@@ -962,4 +962,8 @@ const (
|
||||
// FlagGrafanaconThemes
|
||||
// Enables the temporary themes for GrafanaCon
|
||||
FlagGrafanaconThemes = "grafanaconThemes"
|
||||
|
||||
// FlagPluginsCDNSyncLoader
|
||||
// Load plugins from CDN synchronously
|
||||
FlagPluginsCDNSyncLoader = "pluginsCDNSyncLoader"
|
||||
)
|
||||
|
||||
@@ -3034,6 +3034,18 @@
|
||||
"frontend": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"metadata": {
|
||||
"name": "pluginsCDNSyncLoader",
|
||||
"resourceVersion": "1737026684018",
|
||||
"creationTimestamp": "2025-01-16T11:24:44Z"
|
||||
},
|
||||
"spec": {
|
||||
"description": "Load plugins from CDN synchronously",
|
||||
"stage": "experimental",
|
||||
"codeowner": "@grafana/plugins-platform-backend"
|
||||
}
|
||||
},
|
||||
{
|
||||
"metadata": {
|
||||
"name": "pluginsDetailsRightPanel",
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
|
||||
"github.com/grafana/grafana/pkg/plugins"
|
||||
"github.com/grafana/grafana/pkg/plugins/config"
|
||||
pluginsLoader "github.com/grafana/grafana/pkg/plugins/manager/loader"
|
||||
"github.com/grafana/grafana/pkg/plugins/manager/pipeline/bootstrap"
|
||||
"github.com/grafana/grafana/pkg/plugins/manager/pipeline/discovery"
|
||||
@@ -19,11 +20,13 @@ type Loader struct {
|
||||
loader *pluginsLoader.Loader
|
||||
}
|
||||
|
||||
func ProvideService(discovery discovery.Discoverer, bootstrap bootstrap.Bootstrapper, validation validation.Validator,
|
||||
func ProvideService(
|
||||
cfg *config.PluginManagementCfg,
|
||||
discovery discovery.Discoverer, bootstrap bootstrap.Bootstrapper, validation validation.Validator,
|
||||
initializer initialization.Initializer, termination termination.Terminator, errorTracker pluginerrs.ErrorTracker,
|
||||
) *Loader {
|
||||
return &Loader{
|
||||
loader: pluginsLoader.New(discovery, bootstrap, validation, initializer, termination, errorTracker),
|
||||
loader: pluginsLoader.New(cfg, discovery, bootstrap, validation, initializer, termination, errorTracker),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1575,7 +1575,7 @@ func newLoader(t *testing.T, cfg *config.PluginManagementCfg, reg registry.Servi
|
||||
terminate, err := pipeline.ProvideTerminationStage(cfg, reg, proc)
|
||||
require.NoError(t, err)
|
||||
|
||||
return ProvideService(pipeline.ProvideDiscoveryStage(cfg,
|
||||
return ProvideService(cfg, pipeline.ProvideDiscoveryStage(cfg,
|
||||
finder.NewLocalFinder(false), reg),
|
||||
pipeline.ProvideBootstrapStage(cfg, signature.DefaultCalculator(cfg), assets),
|
||||
pipeline.ProvideValidationStage(cfg, signature.NewValidator(signature.NewUnsignedAuthorizer(cfg)), angularInspector),
|
||||
@@ -1607,7 +1607,7 @@ func newLoaderWithOpts(t *testing.T, cfg *config.PluginManagementCfg, opts loade
|
||||
backendFactoryProvider = fakes.NewFakeBackendProcessProvider()
|
||||
}
|
||||
|
||||
return ProvideService(pipeline.ProvideDiscoveryStage(cfg,
|
||||
return ProvideService(cfg, pipeline.ProvideDiscoveryStage(cfg,
|
||||
finder.NewLocalFinder(false), reg),
|
||||
pipeline.ProvideBootstrapStage(cfg, signature.DefaultCalculator(cfg), assets),
|
||||
pipeline.ProvideValidationStage(cfg, signature.NewValidator(signature.NewUnsignedAuthorizer(cfg)), angularInspector),
|
||||
|
||||
@@ -30,9 +30,10 @@ func ProvidePluginManagementConfig(cfg *setting.Cfg, settingProvider setting.Pro
|
||||
cfg.PluginsCDNURLTemplate,
|
||||
cfg.AppURL,
|
||||
config.Features{
|
||||
ExternalCorePluginsEnabled: features.IsEnabledGlobally(featuremgmt.FlagExternalCorePlugins),
|
||||
SkipHostEnvVarsEnabled: features.IsEnabledGlobally(featuremgmt.FlagPluginsSkipHostEnvVars),
|
||||
SriChecksEnabled: features.IsEnabledGlobally(featuremgmt.FlagPluginsSriChecks),
|
||||
ExternalCorePluginsEnabled: features.IsEnabledGlobally(featuremgmt.FlagExternalCorePlugins),
|
||||
SkipHostEnvVarsEnabled: features.IsEnabledGlobally(featuremgmt.FlagPluginsSkipHostEnvVars),
|
||||
SriChecksEnabled: features.IsEnabledGlobally(featuremgmt.FlagPluginsSriChecks),
|
||||
PluginsCDNSyncLoaderEnabled: features.IsEnabledGlobally(featuremgmt.FlagPluginsCDNSyncLoader),
|
||||
},
|
||||
cfg.AngularSupportEnabled,
|
||||
cfg.GrafanaComAPIURL,
|
||||
|
||||
@@ -107,8 +107,6 @@ var WireSet = wire.NewSet(
|
||||
wire.Bind(new(repo.Service), new(*repo.Manager)),
|
||||
licensing.ProvideLicensing,
|
||||
wire.Bind(new(plugins.Licensing), new(*licensing.Service)),
|
||||
wire.Bind(new(sources.Registry), new(*sources.Service)),
|
||||
sources.ProvideService,
|
||||
pluginSettings.ProvideService,
|
||||
wire.Bind(new(pluginsettings.Service), new(*pluginSettings.Service)),
|
||||
filestore.ProvideService,
|
||||
@@ -146,6 +144,8 @@ var WireExtensionSet = wire.NewSet(
|
||||
wire.Bind(new(plugins.Client), new(*backend.MiddlewareHandler)),
|
||||
managedplugins.NewNoop,
|
||||
wire.Bind(new(managedplugins.Manager), new(*managedplugins.Noop)),
|
||||
sources.ProvideService,
|
||||
wire.Bind(new(sources.Registry), new(*sources.Service)),
|
||||
)
|
||||
|
||||
func ProvideClientWithMiddlewares(
|
||||
|
||||
@@ -141,5 +141,5 @@ func createLoader(cfg *config.PluginManagementCfg, pluginEnvProvider envvars.Pro
|
||||
|
||||
et := pluginerrs.ProvideErrorTracker()
|
||||
|
||||
return loader.New(d, b, v, i, t, et), nil
|
||||
return loader.New(cfg, d, b, v, i, t, et), nil
|
||||
}
|
||||
|
||||
@@ -110,5 +110,5 @@ func CreateTestLoader(t *testing.T, cfg *pluginsCfg.PluginManagementCfg, opts Lo
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
return loader.New(opts.Discoverer, opts.Bootstrapper, opts.Validator, opts.Initializer, opts.Terminator, pluginerrs.ProvideErrorTracker())
|
||||
return loader.New(cfg, opts.Discoverer, opts.Bootstrapper, opts.Validator, opts.Initializer, opts.Terminator, pluginerrs.ProvideErrorTracker())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user