@@ -478,7 +478,6 @@ func (registry *OpenPolicyAgentRegistry) new(store storage.Store, configBytes []
478
478
// policies, report status, etc.
479
479
func (opa * OpenPolicyAgentInstance ) Start (ctx context.Context , timeout time.Duration ) error {
480
480
discoveryPlugin := discovery .Lookup (opa .manager )
481
- bundlePlugin := bundle .Lookup (opa .manager )
482
481
483
482
done := make (chan struct {})
484
483
defer close (done )
@@ -491,15 +490,19 @@ func (opa *OpenPolicyAgentInstance) Start(ctx context.Context, timeout time.Dura
491
490
}
492
491
})
493
492
494
- bundlePlugin .Register ("startuplistener" , func (status bundle.Status ) {
495
- if len (status .Errors ) > 0 {
496
- failed <- fmt .Errorf ("bundle activation failed: %w" , errors .Join (status .Errors ... ))
497
- }
498
- })
499
- defer bundlePlugin .Unregister ("startuplistener" )
500
-
501
493
opa .manager .RegisterPluginStatusListener ("startuplistener" , func (status map [string ]* plugins.Status ) {
502
- for _ , pluginstatus := range status {
494
+ for pluginname , pluginstatus := range status {
495
+ if pluginname == "bundle" { //To make sure bundle plugin is present to register the listener
496
+ bundlePlugin := bundle .Lookup (opa .manager )
497
+
498
+ bundlePlugin .Register ("startuplistener" , func (status bundle.Status ) {
499
+ if len (status .Errors ) > 0 {
500
+ failed <- fmt .Errorf ("bundle activation failed: %w" , errors .Join (status .Errors ... ))
501
+ }
502
+ })
503
+ defer bundlePlugin .Unregister ("startuplistener" )
504
+ }
505
+
503
506
if pluginstatus != nil && pluginstatus .State != plugins .StateOK {
504
507
return
505
508
}
@@ -521,17 +524,21 @@ func (opa *OpenPolicyAgentInstance) Start(ctx context.Context, timeout time.Dura
521
524
522
525
return err
523
526
case <- ctx .Done ():
527
+ timeoutErr := ctx .Err ()
524
528
for pluginName , status := range opa .manager .PluginStatus () {
525
529
if status != nil && status .State != plugins .StateOK {
526
530
opa .Logger ().WithFields (map [string ]interface {}{
527
531
"plugin_name" : pluginName ,
528
532
"plugin_state" : status .State ,
529
533
"error_message" : status .Message ,
530
- }).Error ("Open policy agent plugin did not start in time" )
534
+ }).Error ("Open policy agent plugin: %v did not start in time" , pluginName )
531
535
}
532
536
}
533
537
opa .Close (ctx )
534
- return fmt .Errorf ("one or more open policy agent plugins failed to start in %v with error: %w" , timeout , err )
538
+ if timeoutErr != nil {
539
+ return fmt .Errorf ("one or more open policy agent plugins failed to start in %v with error: %w" , timeout , timeoutErr )
540
+ }
541
+ return fmt .Errorf ("one or more open policy agent plugins failed to start in %v" , timeout )
535
542
}
536
543
}
537
544
@@ -542,25 +549,6 @@ func (opa *OpenPolicyAgentInstance) Close(ctx context.Context) {
542
549
})
543
550
}
544
551
545
- func waitFunc (ctx context.Context , fun func () bool , interval time.Duration ) error {
546
- if fun () {
547
- return nil
548
- }
549
- ticker := time .NewTicker (interval )
550
- defer ticker .Stop ()
551
-
552
- for {
553
- select {
554
- case <- ctx .Done ():
555
- return fmt .Errorf ("timed out while starting: %w" , ctx .Err ())
556
- case <- ticker .C :
557
- if fun () {
558
- return nil
559
- }
560
- }
561
- }
562
- }
563
-
564
552
func configLabelsInfo (opaConfig config.Config ) func (* plugins.Manager ) {
565
553
info := ast .NewObject ()
566
554
labels := ast .NewObject ()
0 commit comments