Skip to content

Commit

Permalink
Adding cluster unpause reconcile for LinodeObjectStorageBucket and Li…
Browse files Browse the repository at this point in the history
…nodeVPC (#245)



---------

Co-authored-by: Khaja Omer <[email protected]>
  • Loading branch information
komer3 and komer3 authored Apr 9, 2024
1 parent cf11b44 commit 77375a7
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 48 deletions.
7 changes: 1 addition & 6 deletions controller/linodecluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
14 changes: 7 additions & 7 deletions controller/linodemachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
)
}
31 changes: 0 additions & 31 deletions controller/linodemachine_controller_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down
21 changes: 19 additions & 2 deletions controller/linodeobjectstoragebucket_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()),
)
}
16 changes: 14 additions & 2 deletions controller/linodevpc_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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(
Expand All @@ -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()),
)
}
1 change: 1 addition & 0 deletions templates/addons/etcd-backup-restore/linode-obj.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down

0 comments on commit 77375a7

Please sign in to comment.