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

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package reconciler
     5  
     6  import (
     7  	"context"
     8  	"sort"
     9  
    10  	"github.com/cilium/hive/cell"
    11  
    12  	"github.com/cilium/cilium/pkg/bgpv1/manager/instance"
    13  	v2api "github.com/cilium/cilium/pkg/k8s/apis/cilium.io/v2"
    14  	v2alpha1api "github.com/cilium/cilium/pkg/k8s/apis/cilium.io/v2alpha1"
    15  	"github.com/cilium/cilium/pkg/logging"
    16  	"github.com/cilium/cilium/pkg/logging/logfields"
    17  )
    18  
    19  type ReconcileParams struct {
    20  	CurrentServer *instance.ServerWithConfig
    21  	DesiredConfig *v2alpha1api.CiliumBGPVirtualRouter
    22  	CiliumNode    *v2api.CiliumNode
    23  }
    24  
    25  // ConfigReconciler is a interface for reconciling a particular aspect
    26  // of an old and new *v2alpha1api.CiliumBGPVirtualRouter
    27  type ConfigReconciler interface {
    28  	// Name returns the name of a reconciler.
    29  	Name() string
    30  	// Priority is used to determine the order in which reconcilers are called. Reconcilers are called from lowest to
    31  	// highest.
    32  	Priority() int
    33  	// Init is called upon virtual router instance creation. Reconcilers can initialize any instance-specific
    34  	// resources here, and clean them up upon Cleanup call.
    35  	Init(sc *instance.ServerWithConfig) error
    36  	// Cleanup is called upon virtual router instance deletion. When called, reconcilers are supposed
    37  	// to clean up all instance-specific resources saved outside the ReconcilerMetadata.
    38  	Cleanup(sc *instance.ServerWithConfig)
    39  	// Reconcile If the `Config` field in `params.sc` is nil the reconciler should unconditionally
    40  	// perform the reconciliation actions, as no previous configuration is present.
    41  	Reconcile(ctx context.Context, params ReconcileParams) error
    42  }
    43  
    44  // ConfigReconcilers contains all reconcilers used by the route manager to manage the BGP config.
    45  var ConfigReconcilers = cell.Provide(
    46  	NewPreflightReconciler,
    47  	NewNeighborReconciler,
    48  	NewExportPodCIDRReconciler,
    49  	NewPodIPPoolReconciler,
    50  	NewServiceReconciler,
    51  	NewRoutePolicyReconciler,
    52  )
    53  
    54  // log is the logger used by the reconcilers
    55  var log = logging.DefaultLogger.WithField(logfields.LogSubsys, "bgp-control-plane")
    56  
    57  func GetActiveReconcilers(reconcilers []ConfigReconciler) []ConfigReconciler {
    58  	recMap := make(map[string]ConfigReconciler)
    59  	for _, r := range reconcilers {
    60  		if r == nil {
    61  			continue // reconciler not initialized
    62  		}
    63  		if existing, exists := recMap[r.Name()]; exists {
    64  			if existing.Priority() == r.Priority() {
    65  				log.Warnf("Skipping duplicate reconciler %s with the same priority (%d)", existing.Name(), existing.Priority())
    66  				continue
    67  			}
    68  			if existing.Priority() < r.Priority() {
    69  				log.Debugf("Skipping reconciler %s (priority %d) as it has lower priority than the existing one (%d)",
    70  					r.Name(), r.Priority(), existing.Priority())
    71  				continue
    72  			}
    73  			log.Debugf("Overriding existing reconciler %s (priority %d) with higher priority one (%d)",
    74  				existing.Name(), existing.Priority(), r.Priority())
    75  		}
    76  		recMap[r.Name()] = r
    77  	}
    78  
    79  	var activeReconcilers []ConfigReconciler
    80  	for _, r := range recMap {
    81  		log.Debugf("Adding BGP reconciler: %v (priority %d)", r.Name(), r.Priority())
    82  		activeReconcilers = append(activeReconcilers, r)
    83  	}
    84  	sort.Slice(activeReconcilers, func(i, j int) bool {
    85  		return activeReconcilers[i].Priority() < activeReconcilers[j].Priority()
    86  	})
    87  
    88  	return activeReconcilers
    89  }