Skip to content

Commit d34a4cf

Browse files
authored
Merge pull request kubernetes-sigs#4983 from nojnhuh/watch
use Watches from controller builder instead of Watch method
2 parents 8282d80 + 257c0dc commit d34a4cf

10 files changed

+165
-233
lines changed

controllers/asosecret_controller.go

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ import (
3838
"sigs.k8s.io/cluster-api/util/annotations"
3939
"sigs.k8s.io/cluster-api/util/predicates"
4040
ctrl "sigs.k8s.io/controller-runtime"
41+
"sigs.k8s.io/controller-runtime/pkg/builder"
4142
"sigs.k8s.io/controller-runtime/pkg/client"
4243
"sigs.k8s.io/controller-runtime/pkg/controller"
4344
"sigs.k8s.io/controller-runtime/pkg/handler"
4445
"sigs.k8s.io/controller-runtime/pkg/reconcile"
45-
"sigs.k8s.io/controller-runtime/pkg/source"
4646
)
4747

4848
// ASOSecretReconciler reconciles ASO secrets associated with AzureCluster objects.
@@ -61,46 +61,36 @@ func (asos *ASOSecretReconciler) SetupWithManager(ctx context.Context, mgr ctrl.
6161
)
6262
defer done()
6363

64-
c, err := ctrl.NewControllerManagedBy(mgr).
64+
return ctrl.NewControllerManagedBy(mgr).
6565
WithOptions(options).
6666
For(&infrav1.AzureCluster{}).
6767
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(log, asos.WatchFilterValue)).
6868
WithEventFilter(predicates.ResourceIsNotExternallyManaged(log)).
6969
Named("ASOSecret").
7070
Owns(&corev1.Secret{}).
71-
Build(asos)
72-
if err != nil {
73-
return errors.Wrap(err, "error creating controller")
74-
}
75-
76-
// Add a watch on infrav1.AzureManagedControlPlane.
77-
if err = c.Watch(
78-
source.Kind(mgr.GetCache(), &infrav1.AzureManagedControlPlane{}),
79-
&handler.EnqueueRequestForObject{},
80-
predicates.ResourceNotPausedAndHasFilterLabel(log, asos.WatchFilterValue),
81-
); err != nil {
82-
return errors.Wrap(err, "failed adding a watch for ready AzureManagedControlPlanes")
83-
}
84-
85-
// Add a watch on ASO secrets owned by an AzureManagedControlPlane
86-
if err = c.Watch(
87-
source.Kind(mgr.GetCache(), &corev1.Secret{}),
88-
handler.EnqueueRequestForOwner(asos.Scheme(), asos.RESTMapper(), &infrav1.AzureManagedControlPlane{}, handler.OnlyControllerOwner()),
89-
); err != nil {
90-
return errors.Wrap(err, "failed adding a watch for secrets")
91-
}
92-
93-
// Add a watch on clusterv1.Cluster object for unpause notifications.
94-
if err = c.Watch(
95-
source.Kind(mgr.GetCache(), &clusterv1.Cluster{}),
96-
handler.EnqueueRequestsFromMapFunc(util.ClusterToInfrastructureMapFunc(ctx, infrav1.GroupVersion.WithKind(infrav1.AzureClusterKind), mgr.GetClient(), &infrav1.AzureCluster{})),
97-
predicates.ClusterUnpaused(log),
98-
predicates.ResourceNotPausedAndHasFilterLabel(log, asos.WatchFilterValue),
99-
); err != nil {
100-
return errors.Wrap(err, "failed adding a watch for ready clusters")
101-
}
102-
103-
return nil
71+
// Add a watch on ASO secrets owned by an AzureManagedControlPlane
72+
Watches(
73+
&corev1.Secret{},
74+
handler.EnqueueRequestForOwner(asos.Scheme(), asos.RESTMapper(), &infrav1.AzureManagedControlPlane{}, handler.OnlyControllerOwner()),
75+
).
76+
// Add a watch on infrav1.AzureManagedControlPlane.
77+
Watches(
78+
&infrav1.AzureManagedControlPlane{},
79+
&handler.EnqueueRequestForObject{},
80+
builder.WithPredicates(
81+
predicates.ResourceNotPausedAndHasFilterLabel(log, asos.WatchFilterValue),
82+
),
83+
).
84+
// Add a watch on clusterv1.Cluster object for unpause notifications.
85+
Watches(
86+
&clusterv1.Cluster{},
87+
handler.EnqueueRequestsFromMapFunc(util.ClusterToInfrastructureMapFunc(ctx, infrav1.GroupVersion.WithKind(infrav1.AzureClusterKind), mgr.GetClient(), &infrav1.AzureCluster{})),
88+
builder.WithPredicates(
89+
predicates.ClusterUnpaused(log),
90+
predicates.ResourceNotPausedAndHasFilterLabel(log, asos.WatchFilterValue),
91+
),
92+
).
93+
Complete(asos)
10494
}
10595

10696
// Reconcile reconciles the ASO secrets associated with AzureCluster objects.

controllers/azurecluster_controller.go

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ import (
3636
"sigs.k8s.io/cluster-api/util/conditions"
3737
"sigs.k8s.io/cluster-api/util/predicates"
3838
ctrl "sigs.k8s.io/controller-runtime"
39+
"sigs.k8s.io/controller-runtime/pkg/builder"
3940
"sigs.k8s.io/controller-runtime/pkg/client"
4041
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
4142
"sigs.k8s.io/controller-runtime/pkg/handler"
4243
"sigs.k8s.io/controller-runtime/pkg/reconcile"
43-
"sigs.k8s.io/controller-runtime/pkg/source"
4444
)
4545

4646
// AzureClusterReconciler reconciles an AzureCluster object.
@@ -81,27 +81,21 @@ func (acr *AzureClusterReconciler) SetupWithManager(ctx context.Context, mgr ctr
8181
r = coalescing.NewReconciler(acr, options.Cache, log)
8282
}
8383

84-
c, err := ctrl.NewControllerManagedBy(mgr).
84+
return ctrl.NewControllerManagedBy(mgr).
8585
WithOptions(options.Options).
8686
For(&infrav1.AzureCluster{}).
8787
WithEventFilter(predicates.ResourceHasFilterLabel(log, acr.WatchFilterValue)).
8888
WithEventFilter(predicates.ResourceIsNotExternallyManaged(log)).
89-
Build(r)
90-
if err != nil {
91-
return errors.Wrap(err, "error creating controller")
92-
}
93-
94-
// Add a watch on clusterv1.Cluster object for pause/unpause notifications.
95-
if err = c.Watch(
96-
source.Kind(mgr.GetCache(), &clusterv1.Cluster{}),
97-
handler.EnqueueRequestsFromMapFunc(util.ClusterToInfrastructureMapFunc(ctx, infrav1.GroupVersion.WithKind(infrav1.AzureClusterKind), mgr.GetClient(), &infrav1.AzureCluster{})),
98-
ClusterUpdatePauseChange(log),
99-
predicates.ResourceHasFilterLabel(log, acr.WatchFilterValue),
100-
); err != nil {
101-
return errors.Wrap(err, "failed adding a watch for ready clusters")
102-
}
103-
104-
return nil
89+
// Add a watch on clusterv1.Cluster object for pause/unpause notifications.
90+
Watches(
91+
&clusterv1.Cluster{},
92+
handler.EnqueueRequestsFromMapFunc(util.ClusterToInfrastructureMapFunc(ctx, infrav1.GroupVersion.WithKind(infrav1.AzureClusterKind), mgr.GetClient(), &infrav1.AzureCluster{})),
93+
builder.WithPredicates(
94+
ClusterUpdatePauseChange(log),
95+
predicates.ResourceHasFilterLabel(log, acr.WatchFilterValue),
96+
),
97+
).
98+
Complete(r)
10599
}
106100

107101
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=azureclusters,verbs=get;list;watch;create;update;patch;delete

controllers/azurejson_machine_controller.go

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ import (
3939
"sigs.k8s.io/cluster-api/util/annotations"
4040
"sigs.k8s.io/cluster-api/util/predicates"
4141
ctrl "sigs.k8s.io/controller-runtime"
42+
"sigs.k8s.io/controller-runtime/pkg/builder"
4243
"sigs.k8s.io/controller-runtime/pkg/client"
4344
"sigs.k8s.io/controller-runtime/pkg/controller"
4445
"sigs.k8s.io/controller-runtime/pkg/event"
4546
"sigs.k8s.io/controller-runtime/pkg/handler"
4647
"sigs.k8s.io/controller-runtime/pkg/predicate"
4748
"sigs.k8s.io/controller-runtime/pkg/reconcile"
48-
"sigs.k8s.io/controller-runtime/pkg/source"
4949
)
5050

5151
// AzureJSONMachineReconciler reconciles Azure json secrets for AzureMachine objects.
@@ -68,30 +68,23 @@ func (r *AzureJSONMachineReconciler) SetupWithManager(ctx context.Context, mgr c
6868
return errors.Wrap(err, "failed to create mapper for Cluster to AzureMachines")
6969
}
7070

71-
c, err := ctrl.NewControllerManagedBy(mgr).
71+
return ctrl.NewControllerManagedBy(mgr).
7272
WithOptions(options).
7373
For(&infrav1.AzureMachine{}).
7474
WithEventFilter(filterUnclonedMachinesPredicate{log: log}).
7575
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(log, r.WatchFilterValue)).
7676
Owns(&corev1.Secret{}).
77-
Build(r)
78-
79-
if err != nil {
80-
return errors.Wrap(err, "failed to create controller")
81-
}
82-
83-
// Add a watch on Clusters to requeue when the infraRef is set. This is needed because the infraRef is not initially
84-
// set in Clusters created from a ClusterClass.
85-
if err := c.Watch(
86-
source.Kind(mgr.GetCache(), &clusterv1.Cluster{}),
87-
handler.EnqueueRequestsFromMapFunc(azureMachineMapper),
88-
predicates.ClusterUnpausedAndInfrastructureReady(log),
89-
predicates.ResourceNotPausedAndHasFilterLabel(log, r.WatchFilterValue),
90-
); err != nil {
91-
return errors.Wrap(err, "failed adding a watch for Clusters")
92-
}
93-
94-
return nil
77+
// Add a watch on Clusters to requeue when the infraRef is set. This is needed because the infraRef is not initially
78+
// set in Clusters created from a ClusterClass.
79+
Watches(
80+
&clusterv1.Cluster{},
81+
handler.EnqueueRequestsFromMapFunc(azureMachineMapper),
82+
builder.WithPredicates(
83+
predicates.ClusterUnpausedAndInfrastructureReady(log),
84+
predicates.ResourceNotPausedAndHasFilterLabel(log, r.WatchFilterValue),
85+
),
86+
).
87+
Complete(r)
9588
}
9689

9790
type filterUnclonedMachinesPredicate struct {

controllers/azurejson_machinepool_controller.go

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ import (
3636
"sigs.k8s.io/cluster-api/util"
3737
"sigs.k8s.io/cluster-api/util/predicates"
3838
ctrl "sigs.k8s.io/controller-runtime"
39+
"sigs.k8s.io/controller-runtime/pkg/builder"
3940
"sigs.k8s.io/controller-runtime/pkg/client"
4041
"sigs.k8s.io/controller-runtime/pkg/controller"
4142
"sigs.k8s.io/controller-runtime/pkg/handler"
4243
"sigs.k8s.io/controller-runtime/pkg/reconcile"
43-
"sigs.k8s.io/controller-runtime/pkg/source"
4444
)
4545

4646
// AzureJSONMachinePoolReconciler reconciles Azure json secrets for AzureMachinePool objects.
@@ -63,29 +63,22 @@ func (r *AzureJSONMachinePoolReconciler) SetupWithManager(ctx context.Context, m
6363
return errors.Wrap(err, "failed to create mapper for Cluster to AzureMachinePools")
6464
}
6565

66-
c, err := ctrl.NewControllerManagedBy(mgr).
66+
return ctrl.NewControllerManagedBy(mgr).
6767
WithOptions(options).
6868
For(&infrav1exp.AzureMachinePool{}).
6969
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(log, r.WatchFilterValue)).
7070
Owns(&corev1.Secret{}).
71-
Build(r)
72-
73-
if err != nil {
74-
return errors.Wrap(err, "failed to create controller")
75-
}
76-
77-
// Add a watch on Clusters to requeue when the infraRef is set. This is needed because the infraRef is not initially
78-
// set in Clusters created from a ClusterClass.
79-
if err := c.Watch(
80-
source.Kind(mgr.GetCache(), &clusterv1.Cluster{}),
81-
handler.EnqueueRequestsFromMapFunc(azureMachinePoolMapper),
82-
predicates.ClusterUnpausedAndInfrastructureReady(log),
83-
predicates.ResourceNotPausedAndHasFilterLabel(log, r.WatchFilterValue),
84-
); err != nil {
85-
return errors.Wrap(err, "failed adding a watch for Clusters")
86-
}
87-
88-
return nil
71+
// Add a watch on Clusters to requeue when the infraRef is set. This is needed because the infraRef is not initially
72+
// set in Clusters created from a ClusterClass.
73+
Watches(
74+
&clusterv1.Cluster{},
75+
handler.EnqueueRequestsFromMapFunc(azureMachinePoolMapper),
76+
builder.WithPredicates(
77+
predicates.ClusterUnpausedAndInfrastructureReady(log),
78+
predicates.ResourceNotPausedAndHasFilterLabel(log, r.WatchFilterValue),
79+
),
80+
).
81+
Complete(r)
8982
}
9083

9184
// Reconcile reconciles the Azure json for AzureMachinePool objects.

controllers/azuremachine_controller.go

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ import (
3737
"sigs.k8s.io/cluster-api/util/conditions"
3838
"sigs.k8s.io/cluster-api/util/predicates"
3939
ctrl "sigs.k8s.io/controller-runtime"
40+
"sigs.k8s.io/controller-runtime/pkg/builder"
4041
"sigs.k8s.io/controller-runtime/pkg/client"
4142
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
4243
"sigs.k8s.io/controller-runtime/pkg/handler"
4344
"sigs.k8s.io/controller-runtime/pkg/reconcile"
44-
"sigs.k8s.io/controller-runtime/pkg/source"
4545
)
4646

4747
// AzureMachineReconciler reconciles an AzureMachine object.
@@ -88,7 +88,12 @@ func (amr *AzureMachineReconciler) SetupWithManager(ctx context.Context, mgr ctr
8888
return errors.Wrap(err, "failed to create AzureCluster to AzureMachines mapper")
8989
}
9090

91-
c, err := ctrl.NewControllerManagedBy(mgr).
91+
azureMachineMapper, err := util.ClusterToTypedObjectsMapper(amr.Client, &infrav1.AzureMachineList{}, mgr.GetScheme())
92+
if err != nil {
93+
return errors.Wrap(err, "failed to create mapper for Cluster to AzureMachines")
94+
}
95+
96+
return ctrl.NewControllerManagedBy(mgr).
9297
WithOptions(options.Options).
9398
For(&infrav1.AzureMachine{}).
9499
WithEventFilter(predicates.ResourceHasFilterLabel(log, amr.WatchFilterValue)).
@@ -102,27 +107,16 @@ func (amr *AzureMachineReconciler) SetupWithManager(ctx context.Context, mgr ctr
102107
&infrav1.AzureCluster{},
103108
handler.EnqueueRequestsFromMapFunc(azureClusterToAzureMachinesMapper),
104109
).
105-
Build(r)
106-
if err != nil {
107-
return errors.Wrap(err, "error creating controller")
108-
}
109-
110-
azureMachineMapper, err := util.ClusterToTypedObjectsMapper(amr.Client, &infrav1.AzureMachineList{}, mgr.GetScheme())
111-
if err != nil {
112-
return errors.Wrap(err, "failed to create mapper for Cluster to AzureMachines")
113-
}
114-
115-
// Add a watch on clusterv1.Cluster object for pause/unpause & ready notifications.
116-
if err := c.Watch(
117-
source.Kind(mgr.GetCache(), &clusterv1.Cluster{}),
118-
handler.EnqueueRequestsFromMapFunc(azureMachineMapper),
119-
ClusterPauseChangeAndInfrastructureReady(log),
120-
predicates.ResourceHasFilterLabel(log, amr.WatchFilterValue),
121-
); err != nil {
122-
return errors.Wrap(err, "failed adding a watch for ready clusters")
123-
}
124-
125-
return nil
110+
// Add a watch on clusterv1.Cluster object for pause/unpause & ready notifications.
111+
Watches(
112+
&clusterv1.Cluster{},
113+
handler.EnqueueRequestsFromMapFunc(azureMachineMapper),
114+
builder.WithPredicates(
115+
ClusterPauseChangeAndInfrastructureReady(log),
116+
predicates.ResourceHasFilterLabel(log, amr.WatchFilterValue),
117+
),
118+
).
119+
Complete(r)
126120
}
127121

128122
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=azuremachines,verbs=get;list;watch;create;update;patch;delete

controllers/azuremanagedcluster_controller.go

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ import (
3333
"sigs.k8s.io/cluster-api/util/patch"
3434
"sigs.k8s.io/cluster-api/util/predicates"
3535
ctrl "sigs.k8s.io/controller-runtime"
36+
"sigs.k8s.io/controller-runtime/pkg/builder"
3637
"sigs.k8s.io/controller-runtime/pkg/client"
3738
"sigs.k8s.io/controller-runtime/pkg/handler"
3839
"sigs.k8s.io/controller-runtime/pkg/reconcile"
39-
"sigs.k8s.io/controller-runtime/pkg/source"
4040
)
4141

4242
// AzureManagedClusterReconciler reconciles an AzureManagedCluster object.
@@ -67,7 +67,7 @@ func (amcr *AzureManagedClusterReconciler) SetupWithManager(ctx context.Context,
6767
return errors.Wrap(err, "failed to create AzureManagedControlPlane to AzureManagedClusters mapper")
6868
}
6969

70-
c, err := ctrl.NewControllerManagedBy(mgr).
70+
return ctrl.NewControllerManagedBy(mgr).
7171
WithOptions(options.Options).
7272
For(azManagedCluster).
7373
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(log, amcr.WatchFilterValue)).
@@ -76,21 +76,16 @@ func (amcr *AzureManagedClusterReconciler) SetupWithManager(ctx context.Context,
7676
&infrav1.AzureManagedControlPlane{},
7777
handler.EnqueueRequestsFromMapFunc(azureManagedControlPlaneMapper),
7878
).
79-
Build(r)
80-
if err != nil {
81-
return errors.Wrap(err, "error creating controller")
82-
}
83-
84-
// Add a watch on clusterv1.Cluster object for unpause notifications.
85-
if err = c.Watch(
86-
source.Kind(mgr.GetCache(), &clusterv1.Cluster{}),
87-
handler.EnqueueRequestsFromMapFunc(util.ClusterToInfrastructureMapFunc(ctx, infrav1.GroupVersion.WithKind(infrav1.AzureManagedClusterKind), mgr.GetClient(), &infrav1.AzureManagedCluster{})),
88-
predicates.ClusterUnpaused(log),
89-
predicates.ResourceNotPausedAndHasFilterLabel(log, amcr.WatchFilterValue),
90-
); err != nil {
91-
return errors.Wrap(err, "failed adding a watch for ready clusters")
92-
}
93-
return nil
79+
// Add a watch on clusterv1.Cluster object for unpause notifications.
80+
Watches(
81+
&clusterv1.Cluster{},
82+
handler.EnqueueRequestsFromMapFunc(util.ClusterToInfrastructureMapFunc(ctx, infrav1.GroupVersion.WithKind(infrav1.AzureManagedClusterKind), mgr.GetClient(), &infrav1.AzureManagedCluster{})),
83+
builder.WithPredicates(
84+
predicates.ClusterUnpaused(log),
85+
predicates.ResourceNotPausedAndHasFilterLabel(log, amcr.WatchFilterValue),
86+
),
87+
).
88+
Complete(r)
9489
}
9590

9691
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=azuremanagedclusters,verbs=get;list;watch;create;update;patch;delete

0 commit comments

Comments
 (0)