Skip to content

Commit 31460fb

Browse files
authored
Merge pull request #306 from hrak/predicates
Replace predicate functions with CAPI util functions
2 parents beb35e2 + e268a87 commit 31460fb

File tree

4 files changed

+18
-26
lines changed

4 files changed

+18
-26
lines changed

controllers/cloudstackcluster_controller.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
csCtrlrUtils "sigs.k8s.io/cluster-api-provider-cloudstack/controllers/utils"
3535
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
3636
"sigs.k8s.io/cluster-api/util"
37+
"sigs.k8s.io/cluster-api/util/predicates"
3738
)
3839

3940
// RBAC permissions used in all reconcilers. Events and Secrets.
@@ -156,6 +157,8 @@ func (r *CloudStackClusterReconciliationRunner) ReconcileDelete() (ctrl.Result,
156157

157158
// Called in main, this registers the cluster reconciler to the CAPI controller manager.
158159
func (reconciler *CloudStackClusterReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, opts controller.Options) error {
160+
log := ctrl.LoggerFrom(ctx)
161+
159162
controller, err := ctrl.NewControllerManagedBy(mgr).
160163
WithOptions(opts).
161164
For(&infrav1.CloudStackCluster{}).
@@ -186,18 +189,14 @@ func (reconciler *CloudStackClusterReconciler) SetupWithManager(ctx context.Cont
186189
}
187190

188191
// Add a watch on CAPI Cluster objects for unpause and ready events.
189-
err = controller.Watch(
192+
if err = controller.Watch(
190193
&source.Kind{Type: &clusterv1.Cluster{}},
191194
handler.EnqueueRequestsFromMapFunc(
192195
util.ClusterToInfrastructureMapFunc(ctx, infrav1.GroupVersion.WithKind("CloudStackCluster"), mgr.GetClient(), &infrav1.CloudStackCluster{})),
193-
predicate.Funcs{
194-
UpdateFunc: func(e event.UpdateEvent) bool {
195-
oldCluster := e.ObjectOld.(*clusterv1.Cluster)
196-
newCluster := e.ObjectNew.(*clusterv1.Cluster)
197-
return oldCluster.Spec.Paused && !newCluster.Spec.Paused
198-
},
199-
DeleteFunc: func(e event.DeleteEvent) bool { return false },
200-
CreateFunc: func(e event.CreateEvent) bool { return false },
201-
})
202-
return errors.Wrap(err, "building CloudStackCluster controller")
196+
predicates.ClusterUnpaused(log),
197+
); err != nil {
198+
return errors.Wrap(err, "building CloudStackCluster controller")
199+
}
200+
201+
return nil
203202
}

controllers/cloudstackmachine_controller.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3131
"k8s.io/apimachinery/pkg/types"
3232
"sigs.k8s.io/cluster-api/util"
33+
"sigs.k8s.io/cluster-api/util/predicates"
3334
ctrl "sigs.k8s.io/controller-runtime"
3435
"sigs.k8s.io/controller-runtime/pkg/client"
3536
"sigs.k8s.io/controller-runtime/pkg/controller"
@@ -346,7 +347,9 @@ func (r *CloudStackMachineReconciliationRunner) ReconcileDelete() (retRes ctrl.R
346347
}
347348

348349
// SetupWithManager registers the machine reconciler to the CAPI controller manager.
349-
func (reconciler *CloudStackMachineReconciler) SetupWithManager(mgr ctrl.Manager, opts controller.Options) error {
350+
func (reconciler *CloudStackMachineReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, opts controller.Options) error {
351+
log := ctrl.LoggerFrom(ctx)
352+
350353
controller, err := ctrl.NewControllerManagedBy(mgr).
351354
WithOptions(opts).
352355
For(&infrav1.CloudStackMachine{}).
@@ -415,16 +418,6 @@ func (reconciler *CloudStackMachineReconciler) SetupWithManager(mgr ctrl.Manager
415418
return controller.Watch(
416419
&source.Kind{Type: &clusterv1.Cluster{}},
417420
handler.EnqueueRequestsFromMapFunc(csMachineMapper),
418-
predicate.Funcs{
419-
UpdateFunc: func(e event.UpdateEvent) bool {
420-
oldCluster := e.ObjectOld.(*clusterv1.Cluster)
421-
newCluster := e.ObjectNew.(*clusterv1.Cluster)
422-
return oldCluster.Spec.Paused && !newCluster.Spec.Paused
423-
},
424-
CreateFunc: func(e event.CreateEvent) bool {
425-
_, ok := e.Object.GetAnnotations()[clusterv1.PausedAnnotation]
426-
return ok
427-
},
428-
},
421+
predicates.ClusterUnpausedAndInfrastructureReady(log),
429422
)
430423
}

controllers/cloudstackmachine_controller_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ var _ = Describe("CloudStackMachineReconciler", func() {
4444
dummies.CSCluster.Spec.FailureDomains = dummies.CSCluster.Spec.FailureDomains[:1]
4545
dummies.CSCluster.Spec.FailureDomains[0].Name = dummies.CSFailureDomain1.Spec.Name
4646

47-
SetupTestEnvironment() // Must happen before setting up managers/reconcilers.
48-
Ω(MachineReconciler.SetupWithManager(k8sManager, controller.Options{})).Should(Succeed()) // Register the CloudStack MachineReconciler.
47+
SetupTestEnvironment() // Must happen before setting up managers/reconcilers.
48+
Ω(MachineReconciler.SetupWithManager(ctx, k8sManager, controller.Options{})).Should(Succeed()) // Register the CloudStack MachineReconciler.
4949

5050
// Point CAPI machine Bootstrap secret ref to dummy bootstrap secret.
5151
dummies.CAPIMachine.Spec.Bootstrap.DataSecretName = &dummies.BootstrapSecret.Name

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ func setupReconcilers(ctx context.Context, base utils.ReconcilerBase, opts manag
214214
setupLog.Error(err, "unable to create controller", "controller", "CloudStackCluster")
215215
os.Exit(1)
216216
}
217-
if err := (&controllers.CloudStackMachineReconciler{ReconcilerBase: base}).SetupWithManager(mgr, controller.Options{MaxConcurrentReconciles: opts.CloudStackMachineConcurrency}); err != nil {
217+
if err := (&controllers.CloudStackMachineReconciler{ReconcilerBase: base}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: opts.CloudStackMachineConcurrency}); err != nil {
218218
setupLog.Error(err, "unable to create controller", "controller", "CloudStackMachine")
219219
os.Exit(1)
220220
}

0 commit comments

Comments
 (0)