Skip to content

Commit

Permalink
Register bundleplugin listner only when plugin is initialized
Browse files Browse the repository at this point in the history
  • Loading branch information
Pushpalanka committed Jul 2, 2024
1 parent 526b67e commit c838d65
Showing 1 changed file with 12 additions and 28 deletions.
40 changes: 12 additions & 28 deletions filters/openpolicyagent/openpolicyagent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit c838d65

Please sign in to comment.