Watch for unpaused Clusters in AWSCluster controller
Watch for unpaused Clusters in AWSCluster controller. This speeds up
cluster moves with clusterctl.
@@ -34,7 +34,11 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
+ "sigs.k8s.io/controller-runtime/pkg/event"
+ "sigs.k8s.io/controller-runtime/pkg/handler"
+ "sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
+ "sigs.k8s.io/controller-runtime/pkg/source"
)
// AWSClusterReconciler reconciles a AwsCluster object
@@ -190,9 +194,58 @@ func reconcileNormal(clusterScope *scope.ClusterScope) (reconcile.Result, error)
}
func (r *AWSClusterReconciler) SetupWithManager(mgr ctrl.Manager, options controller.Options) error {
- return ctrl.NewControllerManagedBy(mgr).
+ controller, err := ctrl.NewControllerManagedBy(mgr).
WithOptions(options).
For(&infrav1.AWSCluster{}).
WithEventFilter(pausePredicates).
- Complete(r)
+ Build(r)
+
+ if err != nil {
+ return errors.Wrap(err, "error creating controller")
+ }
+
+ return controller.Watch(
+ &source.Kind{Type: &clusterv1.Cluster{}},
+ &handler.EnqueueRequestsFromMapFunc{
+ ToRequests: handler.ToRequestsFunc(r.requeueAWSClusterForUnpausedCluster),
+ },
+ predicate.Funcs{
+ UpdateFunc: func(e event.UpdateEvent) bool {
+ oldCluster := e.ObjectOld.(*clusterv1.Cluster)
+ newCluster := e.ObjectNew.(*clusterv1.Cluster)
+ return oldCluster.Spec.Paused && !newCluster.Spec.Paused
+ },
+ CreateFunc: func(e event.CreateEvent) bool {
+ cluster := e.Object.(*clusterv1.Cluster)
+ return !cluster.Spec.Paused
+ },
+ DeleteFunc: func(e event.DeleteEvent) bool {
+ return false
+ },
+ },
+ )
+}
+
+func (r *AWSClusterReconciler) requeueAWSClusterForUnpausedCluster(o handler.MapObject) []ctrl.Request {
+ c, ok := o.Object.(*clusterv1.Cluster)
+ if !ok {
+ r.Log.Error(errors.Errorf("expected a Cluster but got a %T", o.Object), "failed to get AWSClusters for unpaused Cluster")
+ return nil
+ }
+
+ // Don't handle deleted clusters
+ if !c.ObjectMeta.DeletionTimestamp.IsZero() {
+ return nil
+ }
+
+ // Make sure the ref is set
+ if c.Spec.InfrastructureRef == nil {
+ return nil
+ }
+
+ return []ctrl.Request{
+ {
+ NamespacedName: client.ObjectKey{Namespace: c.Namespace, Name: c.Spec.InfrastructureRef.Name},
+ },
+ }
}
