|
| 1 | +package multicluster |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + ctrl "sigs.k8s.io/controller-runtime" |
| 8 | + "sigs.k8s.io/controller-runtime/pkg/cluster" |
| 9 | + "sigs.k8s.io/controller-runtime/pkg/controller" |
| 10 | + "sigs.k8s.io/controller-runtime/pkg/predicate" |
| 11 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 12 | + mcbuilder "sigs.k8s.io/multicluster-runtime/pkg/builder" |
| 13 | + mcmanager "sigs.k8s.io/multicluster-runtime/pkg/manager" |
| 14 | + mcreconcile "sigs.k8s.io/multicluster-runtime/pkg/reconcile" |
| 15 | + |
| 16 | + "github.com/platform-mesh/golang-commons/controller/filter" |
| 17 | + "github.com/platform-mesh/golang-commons/controller/lifecycle" |
| 18 | + "github.com/platform-mesh/golang-commons/controller/lifecycle/conditions" |
| 19 | + "github.com/platform-mesh/golang-commons/controller/lifecycle/runtimeobject" |
| 20 | + "github.com/platform-mesh/golang-commons/controller/lifecycle/spread" |
| 21 | + "github.com/platform-mesh/golang-commons/controller/lifecycle/subroutine" |
| 22 | + "github.com/platform-mesh/golang-commons/logger" |
| 23 | +) |
| 24 | + |
| 25 | +type ClusterGetter interface { |
| 26 | + GetCluster(ctx context.Context, clusterName string) (cluster.Cluster, error) |
| 27 | +} |
| 28 | + |
| 29 | +type LifecycleManager struct { |
| 30 | + log *logger.Logger |
| 31 | + mgr ClusterGetter |
| 32 | + config lifecycle.Config |
| 33 | + subroutines []subroutine.Subroutine |
| 34 | + spreader *spread.Spreader |
| 35 | + conditionsManager *conditions.ConditionManager |
| 36 | + prepareContextFunc lifecycle.PrepareContextFunc |
| 37 | +} |
| 38 | + |
| 39 | +func NewLifecycleManager(log *logger.Logger, operatorName string, controllerName string, mgr ClusterGetter, subroutines []subroutine.Subroutine) *LifecycleManager { |
| 40 | + log = log.MustChildLoggerWithAttributes("operator", operatorName, "controller", controllerName) |
| 41 | + return &LifecycleManager{ |
| 42 | + log: log, |
| 43 | + mgr: mgr, |
| 44 | + subroutines: subroutines, |
| 45 | + config: lifecycle.Config{ |
| 46 | + OperatorName: operatorName, |
| 47 | + ControllerName: controllerName, |
| 48 | + }, |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func (l *LifecycleManager) Config() lifecycle.Config { |
| 53 | + return l.config |
| 54 | +} |
| 55 | +func (l *LifecycleManager) Log() *logger.Logger { |
| 56 | + return l.log |
| 57 | +} |
| 58 | +func (l *LifecycleManager) Subroutines() []subroutine.Subroutine { |
| 59 | + return l.subroutines |
| 60 | +} |
| 61 | +func (l *LifecycleManager) PrepareContextFunc() lifecycle.PrepareContextFunc { |
| 62 | + return l.prepareContextFunc |
| 63 | +} |
| 64 | +func (l *LifecycleManager) ConditionsManager() *conditions.ConditionManager { |
| 65 | + return l.conditionsManager |
| 66 | +} |
| 67 | +func (l *LifecycleManager) Spreader() *spread.Spreader { |
| 68 | + return l.spreader |
| 69 | +} |
| 70 | +func (l *LifecycleManager) Reconcile(ctx context.Context, req mcreconcile.Request, instance runtimeobject.RuntimeObject) (ctrl.Result, error) { |
| 71 | + cl, err := l.mgr.GetCluster(ctx, req.ClusterName) |
| 72 | + if err != nil { |
| 73 | + return reconcile.Result{}, fmt.Errorf("failed to get cluster: %w", err) |
| 74 | + } |
| 75 | + client := cl.GetClient() |
| 76 | + return lifecycle.Reconcile(ctx, req.NamespacedName, instance, client, l) |
| 77 | +} |
| 78 | +func (l *LifecycleManager) SetupWithManagerBuilder(mgr mcmanager.Manager, maxReconciles int, reconcilerName string, instance runtimeobject.RuntimeObject, debugLabelValue string, log *logger.Logger, eventPredicates ...predicate.Predicate) (*mcbuilder.Builder, error) { |
| 79 | + if err := lifecycle.ValidateInterfaces(instance, log, l); err != nil { |
| 80 | + return nil, err |
| 81 | + } |
| 82 | + |
| 83 | + if (l.ConditionsManager() != nil || l.Spreader() != nil) && l.Config().ReadOnly { |
| 84 | + return nil, fmt.Errorf("cannot use conditions or spread reconciles in read-only mode") |
| 85 | + } |
| 86 | + |
| 87 | + eventPredicates = append([]predicate.Predicate{filter.DebugResourcesBehaviourPredicate(debugLabelValue)}, eventPredicates...) |
| 88 | + opts := controller.TypedOptions[mcreconcile.Request]{ |
| 89 | + MaxConcurrentReconciles: maxReconciles, |
| 90 | + } |
| 91 | + return mcbuilder.ControllerManagedBy(mgr). |
| 92 | + Named(reconcilerName). |
| 93 | + For(instance). |
| 94 | + WithOptions(opts). |
| 95 | + WithEventFilter(predicate.And(eventPredicates...)), nil |
| 96 | +} |
| 97 | +func (l *LifecycleManager) SetupWithManager(mgr mcmanager.Manager, maxReconciles int, reconcilerName string, instance runtimeobject.RuntimeObject, debugLabelValue string, r mcreconcile.Reconciler, log *logger.Logger, eventPredicates ...predicate.Predicate) error { |
| 98 | + b, err := l.SetupWithManagerBuilder(mgr, maxReconciles, reconcilerName, instance, debugLabelValue, log, eventPredicates...) |
| 99 | + if err != nil { |
| 100 | + return err |
| 101 | + } |
| 102 | + |
| 103 | + return b.Complete(r) |
| 104 | +} |
| 105 | + |
| 106 | +// WithPrepareContextFunc allows to set a function that prepares the context before each reconciliation |
| 107 | +// This can be used to add additional information to the context that is needed by the subroutines |
| 108 | +// You need to return a new context and an OperatorError in case of an error |
| 109 | +func (l *LifecycleManager) WithPrepareContextFunc(prepareFunction lifecycle.PrepareContextFunc) *LifecycleManager { |
| 110 | + l.prepareContextFunc = prepareFunction |
| 111 | + return l |
| 112 | +} |
| 113 | + |
| 114 | +// WithReadOnly allows to set the controller to read-only mode |
| 115 | +// In read-only mode, the controller will not update the status of the instance |
| 116 | +func (l *LifecycleManager) WithReadOnly() *LifecycleManager { |
| 117 | + l.config.ReadOnly = true |
| 118 | + return l |
| 119 | +} |
| 120 | + |
| 121 | +// WithSpreadingReconciles sets the LifecycleManager to spread out the reconciles |
| 122 | +func (l *LifecycleManager) WithSpreadingReconciles() *LifecycleManager { |
| 123 | + l.spreader = spread.NewSpreader() |
| 124 | + return l |
| 125 | +} |
| 126 | + |
| 127 | +func (l *LifecycleManager) WithConditionManagement() *LifecycleManager { |
| 128 | + l.conditionsManager = conditions.NewConditionManager() |
| 129 | + return l |
| 130 | +} |
0 commit comments