Plugins: Refactor plugin config into separate env var and request scoped services (#83261)
* seperate services for env + req * merge with main * fix tests * undo changes to golden file * fix linter * remove unused fields * split out new config struct * provide config * undo go mod changes * more renaming * fix tests * undo bra.toml changes * update go.work.sum * undo changes * trigger * apply PR feedback
This commit is contained in:
@@ -42,7 +42,7 @@ type Opts struct {
|
||||
}
|
||||
|
||||
// New returns a new Bootstrap stage.
|
||||
func New(cfg *config.Cfg, opts Opts) *Bootstrap {
|
||||
func New(cfg *config.PluginManagementCfg, opts Opts) *Bootstrap {
|
||||
if opts.ConstructFunc == nil {
|
||||
opts.ConstructFunc = DefaultConstructFunc(signature.DefaultCalculator(cfg), assetpath.DefaultService(cfg))
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ func DefaultConstructFunc(signatureCalculator plugins.SignatureCalculator, asset
|
||||
}
|
||||
|
||||
// DefaultDecorateFuncs are the default DecorateFuncs used for the Decorate step of the Bootstrap stage.
|
||||
func DefaultDecorateFuncs(cfg *config.Cfg) []DecorateFunc {
|
||||
func DefaultDecorateFuncs(cfg *config.PluginManagementCfg) []DecorateFunc {
|
||||
return []DecorateFunc{
|
||||
AppDefaultNavURLDecorateFunc,
|
||||
TemplateDecorateFunc,
|
||||
@@ -161,7 +161,7 @@ func configureAppChildPlugin(parent *plugins.Plugin, child *plugins.Plugin) {
|
||||
// SkipHostEnvVarsDecorateFunc returns a DecorateFunc that configures the SkipHostEnvVars field of the plugin.
|
||||
// It will be set to true if the FlagPluginsSkipHostEnvVars feature flag is set, and the plugin is not present in the
|
||||
// ForwardHostEnvVars plugin ids list.
|
||||
func SkipHostEnvVarsDecorateFunc(cfg *config.Cfg) DecorateFunc {
|
||||
func SkipHostEnvVarsDecorateFunc(cfg *config.PluginManagementCfg) DecorateFunc {
|
||||
return func(_ context.Context, p *plugins.Plugin) (*plugins.Plugin, error) {
|
||||
p.SkipHostEnvVars = cfg.Features.IsEnabledGlobally(featuremgmt.FlagPluginsSkipHostEnvVars) &&
|
||||
!slices.Contains(cfg.ForwardHostEnvVars, p.ID)
|
||||
|
||||
@@ -144,7 +144,7 @@ func TestSkipEnvVarsDecorateFunc(t *testing.T) {
|
||||
const pluginID = "plugin-id"
|
||||
|
||||
t.Run("feature flag is not present", func(t *testing.T) {
|
||||
f := SkipHostEnvVarsDecorateFunc(&config.Cfg{Features: featuremgmt.WithFeatures()})
|
||||
f := SkipHostEnvVarsDecorateFunc(&config.PluginManagementCfg{Features: featuremgmt.WithFeatures()})
|
||||
p, err := f(context.Background(), &plugins.Plugin{JSONData: plugins.JSONData{ID: pluginID}})
|
||||
require.NoError(t, err)
|
||||
require.False(t, p.SkipHostEnvVars)
|
||||
@@ -152,7 +152,7 @@ func TestSkipEnvVarsDecorateFunc(t *testing.T) {
|
||||
|
||||
t.Run("feature flag is present", func(t *testing.T) {
|
||||
t.Run("no plugin settings should set SkipHostEnvVars to true", func(t *testing.T) {
|
||||
f := SkipHostEnvVarsDecorateFunc(&config.Cfg{
|
||||
f := SkipHostEnvVarsDecorateFunc(&config.PluginManagementCfg{
|
||||
Features: featuremgmt.WithFeatures(featuremgmt.FlagPluginsSkipHostEnvVars),
|
||||
})
|
||||
p, err := f(context.Background(), &plugins.Plugin{JSONData: plugins.JSONData{ID: pluginID}})
|
||||
@@ -188,7 +188,7 @@ func TestSkipEnvVarsDecorateFunc(t *testing.T) {
|
||||
},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
f := SkipHostEnvVarsDecorateFunc(&config.Cfg{
|
||||
f := SkipHostEnvVarsDecorateFunc(&config.PluginManagementCfg{
|
||||
Features: featuremgmt.WithFeatures(featuremgmt.FlagPluginsSkipHostEnvVars),
|
||||
ForwardHostEnvVars: tc.forwardHostEnvVars,
|
||||
})
|
||||
|
||||
@@ -40,7 +40,7 @@ type Opts struct {
|
||||
}
|
||||
|
||||
// New returns a new Discovery stage.
|
||||
func New(cfg *config.Cfg, opts Opts) *Discovery {
|
||||
func New(cfg *config.PluginManagementCfg, opts Opts) *Discovery {
|
||||
if opts.FindFunc == nil {
|
||||
opts.FindFunc = DefaultFindFunc(cfg)
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
|
||||
// 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.Cfg) FindFunc {
|
||||
func DefaultFindFunc(cfg *config.PluginManagementCfg) FindFunc {
|
||||
return finder.NewLocalFinder(cfg.DevMode, cfg.Features).Find
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ type Initializer interface {
|
||||
type InitializeFunc func(ctx context.Context, p *plugins.Plugin) (*plugins.Plugin, error)
|
||||
|
||||
type Initialize struct {
|
||||
cfg *config.Cfg
|
||||
cfg *config.PluginManagementCfg
|
||||
initializeSteps []InitializeFunc
|
||||
log log.Logger
|
||||
}
|
||||
@@ -27,7 +27,7 @@ type Opts struct {
|
||||
}
|
||||
|
||||
// New returns a new Initialization stage.
|
||||
func New(cfg *config.Cfg, opts Opts) *Initialize {
|
||||
func New(cfg *config.PluginManagementCfg, opts Opts) *Initialize {
|
||||
if opts.InitializeFuncs == nil {
|
||||
opts.InitializeFuncs = []InitializeFunc{}
|
||||
}
|
||||
|
||||
@@ -47,7 +47,7 @@ func (b *BackendClientInit) Initialize(ctx context.Context, p *plugins.Plugin) (
|
||||
}
|
||||
|
||||
// this will ensure that the env variables are calculated every time a plugin is started
|
||||
envFunc := func() []string { return b.envVarProvider.Get(ctx, p) }
|
||||
envFunc := func() []string { return b.envVarProvider.PluginEnvVars(ctx, p) }
|
||||
|
||||
if backendClient, err := backendFactory(p.ID, p.Logger(), envFunc); err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -121,12 +121,12 @@ func (f *fakeBackendProvider) BackendFactory(_ context.Context, _ *plugins.Plugi
|
||||
}
|
||||
|
||||
type fakeEnvVarsProvider struct {
|
||||
GetFunc func(ctx context.Context, p *plugins.Plugin) []string
|
||||
PluginEnvVarsFunc func(ctx context.Context, p *plugins.Plugin) []string
|
||||
}
|
||||
|
||||
func (f *fakeEnvVarsProvider) Get(ctx context.Context, p *plugins.Plugin) []string {
|
||||
if f.GetFunc != nil {
|
||||
return f.GetFunc(ctx, p)
|
||||
func (f *fakeEnvVarsProvider) PluginEnvVars(ctx context.Context, p *plugins.Plugin) []string {
|
||||
if f.PluginEnvVarsFunc != nil {
|
||||
return f.PluginEnvVars(ctx, p)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ type Terminator interface {
|
||||
type TerminateFunc func(ctx context.Context, p *plugins.Plugin) error
|
||||
|
||||
type Terminate struct {
|
||||
cfg *config.Cfg
|
||||
cfg *config.PluginManagementCfg
|
||||
terminateSteps []TerminateFunc
|
||||
log log.Logger
|
||||
}
|
||||
@@ -27,7 +27,7 @@ type Opts struct {
|
||||
}
|
||||
|
||||
// New returns a new Termination stage.
|
||||
func New(cfg *config.Cfg, opts Opts) (*Terminate, error) {
|
||||
func New(cfg *config.PluginManagementCfg, opts Opts) (*Terminate, error) {
|
||||
if opts.TerminateFuncs == nil {
|
||||
opts.TerminateFuncs = []TerminateFunc{}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ import (
|
||||
)
|
||||
|
||||
// DefaultValidateFuncs are the default ValidateFunc used for the Validate step of the Validation stage.
|
||||
func DefaultValidateFuncs(cfg *config.Cfg) []ValidateFunc {
|
||||
func DefaultValidateFuncs(cfg *config.PluginManagementCfg) []ValidateFunc {
|
||||
return []ValidateFunc{
|
||||
SignatureValidationStep(signature.NewValidator(signature.NewUnsignedAuthorizer(cfg))),
|
||||
ModuleJSValidationStep(),
|
||||
@@ -74,16 +74,16 @@ func (v *ModuleJSValidator) Validate(_ context.Context, p *plugins.Plugin) error
|
||||
}
|
||||
|
||||
type AngularDetector struct {
|
||||
cfg *config.Cfg
|
||||
cfg *config.PluginManagementCfg
|
||||
angularInspector angularinspector.Inspector
|
||||
log log.Logger
|
||||
}
|
||||
|
||||
func AngularDetectionStep(cfg *config.Cfg, angularInspector angularinspector.Inspector) ValidateFunc {
|
||||
func AngularDetectionStep(cfg *config.PluginManagementCfg, angularInspector angularinspector.Inspector) ValidateFunc {
|
||||
return newAngularDetector(cfg, angularInspector).Validate
|
||||
}
|
||||
|
||||
func newAngularDetector(cfg *config.Cfg, angularInspector angularinspector.Inspector) *AngularDetector {
|
||||
func newAngularDetector(cfg *config.PluginManagementCfg, angularInspector angularinspector.Inspector) *AngularDetector {
|
||||
return &AngularDetector{
|
||||
cfg: cfg,
|
||||
angularInspector: angularInspector,
|
||||
|
||||
@@ -17,7 +17,7 @@ type Validator interface {
|
||||
type ValidateFunc func(ctx context.Context, p *plugins.Plugin) error
|
||||
|
||||
type Validate struct {
|
||||
cfg *config.Cfg
|
||||
cfg *config.PluginManagementCfg
|
||||
validateSteps []ValidateFunc
|
||||
log log.Logger
|
||||
}
|
||||
@@ -27,7 +27,7 @@ type Opts struct {
|
||||
}
|
||||
|
||||
// New returns a new Validation stage.
|
||||
func New(cfg *config.Cfg, opts Opts) *Validate {
|
||||
func New(cfg *config.PluginManagementCfg, opts Opts) *Validate {
|
||||
if opts.ValidateFuncs == nil {
|
||||
opts.ValidateFuncs = DefaultValidateFuncs(cfg)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user