diff --git a/controller/linodecluster_controller.go b/controller/linodecluster_controller.go index b32f0f101..18813b743 100644 --- a/controller/linodecluster_controller.go +++ b/controller/linodecluster_controller.go @@ -221,14 +221,9 @@ func (r *LinodeClusterReconciler) SetupWithManager(mgr ctrl.Manager) error { return fmt.Errorf("failed to build controller: %w", err) } - err = controller.Watch( + return controller.Watch( source.Kind(mgr.GetCache(), &clusterv1.Cluster{}), handler.EnqueueRequestsFromMapFunc(kutil.ClusterToInfrastructureMapFunc(context.TODO(), infrav1alpha1.GroupVersion.WithKind("LinodeCluster"), mgr.GetClient(), &infrav1alpha1.LinodeCluster{})), predicates.ClusterUnpausedAndInfrastructureReady(mgr.GetLogger()), ) - if err != nil { - return fmt.Errorf("failed adding a watch for ready clusters: %w", err) - } - - return nil } diff --git a/controller/linodemachine_controller.go b/controller/linodemachine_controller.go index 7e5531c7e..0828245be 100644 --- a/controller/linodemachine_controller.go +++ b/controller/linodemachine_controller.go @@ -590,14 +590,14 @@ func (r *LinodeMachineReconciler) SetupWithManager(mgr ctrl.Manager) error { return fmt.Errorf("failed to build controller: %w", err) } - err = controller.Watch( - source.Kind(mgr.GetCache(), &clusterv1.Cluster{}), - handler.EnqueueRequestsFromMapFunc(r.requeueLinodeMachinesForUnpausedCluster(mgr.GetLogger())), - predicates.ClusterUnpausedAndInfrastructureReady(mgr.GetLogger()), - ) + linodeMachineMapper, err := kutil.ClusterToTypedObjectsMapper(r.Client, &infrav1alpha1.LinodeMachineList{}, mgr.GetScheme()) if err != nil { - return fmt.Errorf("failed adding a watch for ready clusters: %w", err) + return fmt.Errorf("failed to create mapper for LinodeMachines: %w", err) } - return nil + return controller.Watch( + source.Kind(mgr.GetCache(), &clusterv1.Cluster{}), + handler.EnqueueRequestsFromMapFunc(linodeMachineMapper), + predicates.ClusterUnpausedAndInfrastructureReady(mgr.GetLogger()), + ) } diff --git a/controller/linodemachine_controller_helpers.go b/controller/linodemachine_controller_helpers.go index ee51c770d..7e8482a4a 100644 --- a/controller/linodemachine_controller_helpers.go +++ b/controller/linodemachine_controller_helpers.go @@ -215,37 +215,6 @@ func (r *LinodeMachineReconciler) linodeClusterToLinodeMachines(logger logr.Logg } } -func (r *LinodeMachineReconciler) requeueLinodeMachinesForUnpausedCluster(logger logr.Logger) handler.MapFunc { - logger = logger.WithName("LinodeMachineReconciler").WithName("requeueLinodeMachinesForUnpausedCluster") - - return func(ctx context.Context, o client.Object) []ctrl.Request { - ctx, cancel := context.WithTimeout(ctx, reconciler.DefaultMappingTimeout) - defer cancel() - - cluster, ok := o.(*clusterv1.Cluster) - if !ok { - logger.Info("Failed to cast object to Cluster") - - return nil - } - - if !cluster.ObjectMeta.DeletionTimestamp.IsZero() { - logger.Info("Cluster has a deletion timestamp, skipping mapping") - - return nil - } - - requests, err := r.requestsForCluster(ctx, cluster.Namespace, cluster.Name) - if err != nil { - logger.Error(err, "Failed to create request for cluster") - - return nil - } - - return requests - } -} - func (r *LinodeMachineReconciler) requestsForCluster(ctx context.Context, namespace, name string) ([]ctrl.Request, error) { labels := map[string]string{clusterv1.ClusterNameLabel: name} diff --git a/controller/linodeobjectstoragebucket_controller.go b/controller/linodeobjectstoragebucket_controller.go index 6c916c04f..faff07e7e 100644 --- a/controller/linodeobjectstoragebucket_controller.go +++ b/controller/linodeobjectstoragebucket_controller.go @@ -30,12 +30,15 @@ import ( utilerrors "k8s.io/apimachinery/pkg/util/errors" "k8s.io/client-go/tools/record" clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" + kutil "sigs.k8s.io/cluster-api/util" "sigs.k8s.io/cluster-api/util/conditions" "sigs.k8s.io/cluster-api/util/predicates" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" + "sigs.k8s.io/controller-runtime/pkg/handler" "sigs.k8s.io/controller-runtime/pkg/predicate" + "sigs.k8s.io/controller-runtime/pkg/source" infrav1alpha1 "github.com/linode/cluster-api-provider-linode/api/v1alpha1" "github.com/linode/cluster-api-provider-linode/cloud/scope" @@ -253,12 +256,26 @@ func (r *LinodeObjectStorageBucketReconciler) reconcileDelete(ctx context.Contex // SetupWithManager sets up the controller with the Manager. func (r *LinodeObjectStorageBucketReconciler) SetupWithManager(mgr ctrl.Manager) error { - return ctrl.NewControllerManagedBy(mgr). + controller, err := ctrl.NewControllerManagedBy(mgr). For(&infrav1alpha1.LinodeObjectStorageBucket{}). Owns(&corev1.Secret{}). WithEventFilter(predicate.And( predicates.ResourceHasFilterLabel(mgr.GetLogger(), r.WatchFilterValue), predicate.GenerationChangedPredicate{}, )). - Complete(r) + Build(r) + if err != nil { + return fmt.Errorf("failed to build controller: %w", err) + } + + linodeObjectStorageBucketMapper, err := kutil.ClusterToTypedObjectsMapper(r.Client, &infrav1alpha1.LinodeObjectStorageBucketList{}, mgr.GetScheme()) + if err != nil { + return fmt.Errorf("failed to create mapper for LinodeObjectStorageBuckets: %w", err) + } + + return controller.Watch( + source.Kind(mgr.GetCache(), &clusterv1.Cluster{}), + handler.EnqueueRequestsFromMapFunc(linodeObjectStorageBucketMapper), + predicates.ClusterUnpausedAndInfrastructureReady(mgr.GetLogger()), + ) } diff --git a/controller/linodevpc_controller.go b/controller/linodevpc_controller.go index 407fbb9c4..78fdf36f4 100644 --- a/controller/linodevpc_controller.go +++ b/controller/linodevpc_controller.go @@ -30,14 +30,17 @@ import ( utilerrors "k8s.io/apimachinery/pkg/util/errors" "k8s.io/client-go/tools/record" clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" + kutil "sigs.k8s.io/cluster-api/util" "sigs.k8s.io/cluster-api/util/conditions" "sigs.k8s.io/cluster-api/util/predicates" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" "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" infrav1alpha1 "github.com/linode/cluster-api-provider-linode/api/v1alpha1" "github.com/linode/cluster-api-provider-linode/cloud/scope" @@ -264,7 +267,7 @@ func (r *LinodeVPCReconciler) reconcileDelete(ctx context.Context, logger logr.L // SetupWithManager sets up the controller with the Manager. func (r *LinodeVPCReconciler) SetupWithManager(mgr ctrl.Manager) error { - _, err := ctrl.NewControllerManagedBy(mgr). + controller, err := ctrl.NewControllerManagedBy(mgr). For(&infrav1alpha1.LinodeVPC{}). WithEventFilter( predicate.And( @@ -280,5 +283,14 @@ func (r *LinodeVPCReconciler) SetupWithManager(mgr ctrl.Manager) error { return fmt.Errorf("failed to build controller: %w", err) } - return nil + linodeVPCMapper, err := kutil.ClusterToTypedObjectsMapper(r.Client, &infrav1alpha1.LinodeVPCList{}, mgr.GetScheme()) + if err != nil { + return fmt.Errorf("failed to create mapper for LinodeVPCs: %w", err) + } + + return controller.Watch( + source.Kind(mgr.GetCache(), &clusterv1.Cluster{}), + handler.EnqueueRequestsFromMapFunc(linodeVPCMapper), + predicates.ClusterUnpausedAndInfrastructureReady(mgr.GetLogger()), + ) } diff --git a/templates/addons/etcd-backup-restore/linode-obj.yaml b/templates/addons/etcd-backup-restore/linode-obj.yaml index ddf56c9a7..dd62ec127 100644 --- a/templates/addons/etcd-backup-restore/linode-obj.yaml +++ b/templates/addons/etcd-backup-restore/linode-obj.yaml @@ -8,6 +8,7 @@ metadata: app.kubernetes.io/part-of: cluster-api-provider-linode app.kubernetes.io/managed-by: kustomize app.kubernetes.io/created-by: cluster-api-provider-linode + cluster.x-k8s.io/cluster-name: ${CLUSTER_NAME} name: ${CLUSTER_NAME}-etcd-backup spec: cluster: ${OBJ_BUCKET_REGION}