github.com/cilium/cilium@v1.16.2/pkg/bgpv1/manager/reconcilerv2/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/manager/instance"
    14  	v2api "github.com/cilium/cilium/pkg/k8s/apis/cilium.io/v2"
    15  	"github.com/cilium/cilium/pkg/k8s/apis/cilium.io/v2alpha1"
    16  )
    17  
    18  type ReconcileParams struct {
    19  	BGPInstance   *instance.BGPInstance
    20  	DesiredConfig *v2alpha1.CiliumBGPNodeInstance
    21  	CiliumNode    *v2api.CiliumNode
    22  }
    23  
    24  type ConfigReconciler interface {
    25  	// Name returns the name of a reconciler.
    26  	Name() string
    27  	// Priority is used to determine the order in which reconcilers are called. Reconcilers are called from lowest to
    28  	// highest.
    29  	Priority() int
    30  	// Init is called upon virtual router instance creation. Reconcilers can initialize any instance-specific
    31  	// resources here, and clean them up upon Cleanup call.
    32  	Init(i *instance.BGPInstance) error
    33  	// Cleanup is called upon virtual router instance deletion. When called, reconcilers are supposed
    34  	// to clean up all instance-specific resources saved outside the instance Metadata.
    35  	Cleanup(i *instance.BGPInstance)
    36  	// Reconcile performs the reconciliation actions for given BGPInstance.
    37  	Reconcile(ctx context.Context, params ReconcileParams) error
    38  }
    39  
    40  var ConfigReconcilers = cell.ProvidePrivate(
    41  	NewPreflightReconciler,
    42  	NewNeighborReconciler,
    43  	NewPodCIDRReconciler,
    44  	NewPodIPPoolReconciler,
    45  	NewServiceReconciler,
    46  )
    47  
    48  // GetActiveReconcilers returns a list of reconcilers in order of priority that should be used to reconcile the BGP config.
    49  func GetActiveReconcilers(log logrus.FieldLogger, reconcilers []ConfigReconciler) []ConfigReconciler {
    50  	recMap := make(map[string]ConfigReconciler)
    51  	for _, r := range reconcilers {
    52  		if r == nil {
    53  			continue // reconciler not initialized
    54  		}
    55  		if existing, exists := recMap[r.Name()]; exists {
    56  			if existing.Priority() == r.Priority() {
    57  				log.Warnf("Skipping duplicate reconciler %s with the same priority (%d)", existing.Name(), existing.Priority())
    58  				continue
    59  			}
    60  			if existing.Priority() < r.Priority() {
    61  				log.Debugf("Skipping reconciler %s (priority %d) as it has lower priority than the existing one (%d)",
    62  					r.Name(), r.Priority(), existing.Priority())
    63  				continue
    64  			}
    65  			log.Debugf("Overriding existing reconciler %s (priority %d) with higher priority one (%d)",
    66  				existing.Name(), existing.Priority(), r.Priority())
    67  		}
    68  		recMap[r.Name()] = r
    69  	}
    70  
    71  	var activeReconcilers []ConfigReconciler
    72  	for _, r := range recMap {
    73  		log.Debugf("Adding BGP reconciler: %v (priority %d)", r.Name(), r.Priority())
    74  		activeReconcilers = append(activeReconcilers, r)
    75  	}
    76  	sort.Slice(activeReconcilers, func(i, j int) bool {
    77  		return activeReconcilers[i].Priority() < activeReconcilers[j].Priority()
    78  	})
    79  
    80  	return activeReconcilers
    81  }