diff --git a/controllers/kamajicontrolplane_controller.go b/controllers/kamajicontrolplane_controller.go index 9205dbe..806746f 100644 --- a/controllers/kamajicontrolplane_controller.go +++ b/controllers/kamajicontrolplane_controller.go @@ -18,6 +18,8 @@ import ( "k8s.io/client-go/util/retry" "k8s.io/component-base/featuregate" capiv1beta1 "sigs.k8s.io/cluster-api/api/v1beta1" + "sigs.k8s.io/cluster-api/util/annotations" + "sigs.k8s.io/cluster-api/util/predicates" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/builder" "sigs.k8s.io/controller-runtime/pkg/client" @@ -74,11 +76,7 @@ func (r *KamajiControlPlaneReconciler) Reconcile(ctx context.Context, req ctrl.R return ctrl.Result{}, nil } - // Handling finalizer for external deployment: - // in case of ExternalClusterReference the remote TCP must be deleted. - if kcp.DeletionTimestamp != nil { - return r.handleDeletion(ctx, kcp) - } + // Retrieving the Cluster information cluster := capiv1beta1.Cluster{} cluster.SetName(kcp.GetOwnerReferences()[0].Name) @@ -95,6 +93,20 @@ func (r *KamajiControlPlaneReconciler) Reconcile(ctx context.Context, req ctrl.R return ctrl.Result{}, err //nolint:wrapcheck } + + // Return early if the object or Cluster is paused. + if annotations.IsPaused(&cluster, &kcp) { + log.Info("Reconciliation is paused for this object") + + return ctrl.Result{}, nil + } + + // Handling finalizer for external deployment: + // in case of ExternalClusterReference the remote TCP must be deleted. + if kcp.DeletionTimestamp != nil { + return r.handleDeletion(ctx, kcp) + } + // Extracting conditions, used to update the KamajiControlPlane ones upon the end of the reconciliation. conditions := kcp.Status.Conditions @@ -355,16 +367,16 @@ func (r *KamajiControlPlaneReconciler) updateKamajiControlPlaneStatus(ctx contex } // SetupWithManager sets up the controller with the Manager. -func (r *KamajiControlPlaneReconciler) SetupWithManager(mgr ctrl.Manager, channel chan event.GenericEvent) error { +func (r *KamajiControlPlaneReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, channel chan event.GenericEvent) error { r.client = mgr.GetClient() - ctrlBuilder := ctrl.NewControllerManagedBy(mgr). For(&kcpv1alpha1.KamajiControlPlane{}, builder.WithPredicates(predicate.NewPredicateFuncs(func(object client.Object) bool { return len(object.GetOwnerReferences()) > 0 }))). Owns(&corev1.Secret{}). WatchesRawSource(source.Channel(channel, &handler.EnqueueRequestForObject{})). - WithOptions(controller.Options{MaxConcurrentReconciles: r.MaxConcurrentReconciles}) + WithOptions(controller.Options{MaxConcurrentReconciles: r.MaxConcurrentReconciles}). + WithEventFilter(predicates.ResourceNotPaused(ctrl.LoggerFrom(ctx))) cs, csErr := kubernetes.NewForConfig(mgr.GetConfig()) if csErr != nil { diff --git a/go.mod b/go.mod index fc8f80e..f7578fc 100644 --- a/go.mod +++ b/go.mod @@ -14,6 +14,7 @@ require ( k8s.io/apimachinery v0.31.1 k8s.io/client-go v0.31.1 k8s.io/component-base v0.31.1 + k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 sigs.k8s.io/cluster-api v1.8.4 sigs.k8s.io/controller-runtime v0.19.0 ) @@ -72,7 +73,6 @@ require ( k8s.io/apiextensions-apiserver v0.31.1 // indirect k8s.io/klog/v2 v2.130.1 // indirect k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect - k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 // indirect sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect sigs.k8s.io/yaml v1.4.0 // indirect diff --git a/main.go b/main.go index 43273f7..05dc7d7 100644 --- a/main.go +++ b/main.go @@ -126,7 +126,7 @@ func main() { ExternalClusterReferenceStore: ecrStore, FeatureGates: featureGate, MaxConcurrentReconciles: maxConcurrentReconciles, - }).SetupWithManager(mgr, triggerChannel); err != nil { + }).SetupWithManager(ctx, mgr, triggerChannel); err != nil { setupLog.Error(err, "unable to create controller", "controller", "KamajiControlPlane") os.Exit(1) }