From c838d65ace88596e8598e54ed0533a1569682490 Mon Sep 17 00:00:00 2001 From: Pushpalanka Jayawardhana Date: Tue, 2 Jul 2024 14:14:00 +0200 Subject: [PATCH] Register bundleplugin listner only when plugin is initialized --- filters/openpolicyagent/openpolicyagent.go | 40 +++++++--------------- 1 file changed, 12 insertions(+), 28 deletions(-) diff --git a/filters/openpolicyagent/openpolicyagent.go b/filters/openpolicyagent/openpolicyagent.go index aed800e158..caf6645be7 100644 --- a/filters/openpolicyagent/openpolicyagent.go +++ b/filters/openpolicyagent/openpolicyagent.go @@ -478,7 +478,6 @@ func (registry *OpenPolicyAgentRegistry) new(store storage.Store, configBytes [] // policies, report status, etc. func (opa *OpenPolicyAgentInstance) Start(ctx context.Context, timeout time.Duration) error { discoveryPlugin := discovery.Lookup(opa.manager) - bundlePlugin := bundle.Lookup(opa.manager) done := make(chan struct{}) defer close(done) @@ -491,15 +490,19 @@ func (opa *OpenPolicyAgentInstance) Start(ctx context.Context, timeout time.Dura } }) - bundlePlugin.Register("startuplistener", func(status bundle.Status) { - if len(status.Errors) > 0 { - failed <- fmt.Errorf("bundle activation failed: %w", errors.Join(status.Errors...)) - } - }) - defer bundlePlugin.Unregister("startuplistener") - opa.manager.RegisterPluginStatusListener("startuplistener", func(status map[string]*plugins.Status) { - for _, pluginstatus := range status { + for pluginname, pluginstatus := range status { + if pluginname == "bundle" { //To make sure bundle plugin is present to register the listener + bundlePlugin := bundle.Lookup(opa.manager) + + bundlePlugin.Register("startuplistener", func(status bundle.Status) { + if len(status.Errors) > 0 { + failed <- fmt.Errorf("bundle activation failed: %w", errors.Join(status.Errors...)) + } + }) + defer bundlePlugin.Unregister("startuplistener") + } + if pluginstatus != nil && pluginstatus.State != plugins.StateOK { return } @@ -542,25 +545,6 @@ func (opa *OpenPolicyAgentInstance) Close(ctx context.Context) { }) } -func waitFunc(ctx context.Context, fun func() bool, interval time.Duration) error { - if fun() { - return nil - } - ticker := time.NewTicker(interval) - defer ticker.Stop() - - for { - select { - case <-ctx.Done(): - return fmt.Errorf("timed out while starting: %w", ctx.Err()) - case <-ticker.C: - if fun() { - return nil - } - } - } -} - func configLabelsInfo(opaConfig config.Config) func(*plugins.Manager) { info := ast.NewObject() labels := ast.NewObject()