github.com/cilium/cilium@v1.16.2/pkg/bgpv1/manager/reconcilerv2/state_reconcilers.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package reconcilerv2
     5  
     6  import (
     7  	"context"
     8  	"sort"
     9  
    10  	"github.com/cilium/hive/cell"
    11  	"github.com/sirupsen/logrus"
    12  
    13  	"github.com/cilium/cilium/pkg/bgpv1/agent/mode"
    14  	"github.com/cilium/cilium/pkg/bgpv1/manager/instance"
    15  )
    16  
    17  type StateReconcileParams struct {
    18  	// ConfigMode is the current configuration mode of BGP control plane
    19  	// This is required by some reconcilers to determine if they need to run or not.
    20  	ConfigMode *mode.ConfigMode
    21  
    22  	// UpdatedInstance is the BGP instance that is being updated.
    23  	UpdatedInstance *instance.BGPInstance
    24  
    25  	// DeletedInstance is the BGP instance that is already deleted.
    26  	DeletedInstance string
    27  }
    28  
    29  type StateReconciler interface {
    30  	Name() string
    31  	Priority() int
    32  	Reconcile(ctx context.Context, params StateReconcileParams) error
    33  }
    34  
    35  var StateReconcilers = cell.ProvidePrivate(
    36  	NewStatusReconciler,
    37  )
    38  
    39  func GetActiveStateReconcilers(log logrus.FieldLogger, reconcilers []StateReconciler) []StateReconciler {
    40  	recMap := make(map[string]StateReconciler)
    41  	for _, r := range reconcilers {
    42  		if r == nil {
    43  			continue // reconciler not initialized
    44  		}
    45  		if existing, exists := recMap[r.Name()]; exists {
    46  			if existing.Priority() == r.Priority() {
    47  				log.Warnf("Skipping duplicate reconciler %s with the same priority (%d)", existing.Name(), existing.Priority())
    48  				continue
    49  			}
    50  			if existing.Priority() < r.Priority() {
    51  				log.Debugf("Skipping reconciler %s (priority %d) as it has lower priority than the existing one (%d)",
    52  					r.Name(), r.Priority(), existing.Priority())
    53  				continue
    54  			}
    55  			log.Debugf("Overriding existing reconciler %s (priority %d) with higher priority one (%d)",
    56  				existing.Name(), existing.Priority(), r.Priority())
    57  		}
    58  		recMap[r.Name()] = r
    59  	}
    60  
    61  	var activeReconcilers []StateReconciler
    62  	for _, r := range recMap {
    63  		log.Debugf("Adding BGP state reconciler: %v (priority %d)", r.Name(), r.Priority())
    64  		activeReconcilers = append(activeReconcilers, r)
    65  	}
    66  	sort.Slice(activeReconcilers, func(i, j int) bool {
    67  		return activeReconcilers[i].Priority() < activeReconcilers[j].Priority()
    68  	})
    69  
    70  	return activeReconcilers
    71  }