Skip to content

Commit 5cd6d62

Browse files
authored
Merge pull request #2553 from k8s-infra-cherrypick-robot/cherry-pick-2532-to-release-1.4
[release-1.4] Watch Cluster for changes in AzureJSONMachineTemplate, AzureJSONMachine, and AzureJSONMachinePool controllers
2 parents 46f5aa4 + 86c7f8e commit 5cd6d62

File tree

3 files changed

+80
-6
lines changed

3 files changed

+80
-6
lines changed

controllers/azurejson_machine_controller.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@ import (
4141
"sigs.k8s.io/controller-runtime/pkg/client"
4242
"sigs.k8s.io/controller-runtime/pkg/controller"
4343
"sigs.k8s.io/controller-runtime/pkg/event"
44+
"sigs.k8s.io/controller-runtime/pkg/handler"
4445
"sigs.k8s.io/controller-runtime/pkg/predicate"
4546
"sigs.k8s.io/controller-runtime/pkg/reconcile"
47+
"sigs.k8s.io/controller-runtime/pkg/source"
4648
)
4749

4850
// AzureJSONMachineReconciler reconciles Azure json secrets for AzureMachine objects.
@@ -60,13 +62,35 @@ func (r *AzureJSONMachineReconciler) SetupWithManager(ctx context.Context, mgr c
6062
)
6163
defer done()
6264

63-
return ctrl.NewControllerManagedBy(mgr).
65+
azureMachineMapper, err := util.ClusterToObjectsMapper(r.Client, &infrav1.AzureMachineList{}, mgr.GetScheme())
66+
if err != nil {
67+
return errors.Wrap(err, "failed to create mapper for Cluster to AzureMachines")
68+
}
69+
70+
c, err := ctrl.NewControllerManagedBy(mgr).
6471
WithOptions(options).
6572
For(&infrav1.AzureMachine{}).
6673
WithEventFilter(filterUnclonedMachinesPredicate{log: log}).
6774
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(log, r.WatchFilterValue)).
6875
Owns(&corev1.Secret{}).
69-
Complete(r)
76+
Build(r)
77+
78+
if err != nil {
79+
return errors.Wrap(err, "failed to create controller")
80+
}
81+
82+
// Add a watch on Clusters to requeue when the infraRef is set. This is needed because the infraRef is not initially
83+
// set in Clusters created from a ClusterClass.
84+
if err := c.Watch(
85+
&source.Kind{Type: &clusterv1.Cluster{}},
86+
handler.EnqueueRequestsFromMapFunc(azureMachineMapper),
87+
predicates.ClusterUnpausedAndInfrastructureReady(log),
88+
predicates.ResourceNotPausedAndHasFilterLabel(log, r.WatchFilterValue),
89+
); err != nil {
90+
return errors.Wrap(err, "failed adding a watch for Clusters")
91+
}
92+
93+
return nil
7094
}
7195

7296
type filterUnclonedMachinesPredicate struct {

controllers/azurejson_machinepool_controller.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,15 @@ import (
3333
expv1 "sigs.k8s.io/cluster-api-provider-azure/exp/api/v1beta1"
3434
"sigs.k8s.io/cluster-api-provider-azure/util/reconciler"
3535
"sigs.k8s.io/cluster-api-provider-azure/util/tele"
36+
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
3637
"sigs.k8s.io/cluster-api/util"
3738
"sigs.k8s.io/cluster-api/util/predicates"
3839
ctrl "sigs.k8s.io/controller-runtime"
3940
"sigs.k8s.io/controller-runtime/pkg/client"
4041
"sigs.k8s.io/controller-runtime/pkg/controller"
42+
"sigs.k8s.io/controller-runtime/pkg/handler"
4143
"sigs.k8s.io/controller-runtime/pkg/reconcile"
44+
"sigs.k8s.io/controller-runtime/pkg/source"
4245
)
4346

4447
// AzureJSONMachinePoolReconciler reconciles Azure json secrets for AzureMachinePool objects.
@@ -56,12 +59,34 @@ func (r *AzureJSONMachinePoolReconciler) SetupWithManager(ctx context.Context, m
5659
)
5760
defer done()
5861

59-
return ctrl.NewControllerManagedBy(mgr).
62+
azureMachinePoolMapper, err := util.ClusterToObjectsMapper(r.Client, &expv1.AzureMachinePoolList{}, mgr.GetScheme())
63+
if err != nil {
64+
return errors.Wrap(err, "failed to create mapper for Cluster to AzureMachinePools")
65+
}
66+
67+
c, err := ctrl.NewControllerManagedBy(mgr).
6068
WithOptions(options).
6169
For(&expv1.AzureMachinePool{}).
6270
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(log, r.WatchFilterValue)).
6371
Owns(&corev1.Secret{}).
64-
Complete(r)
72+
Build(r)
73+
74+
if err != nil {
75+
return errors.Wrap(err, "failed to create controller")
76+
}
77+
78+
// Add a watch on Clusters to requeue when the infraRef is set. This is needed because the infraRef is not initially
79+
// set in Clusters created from a ClusterClass.
80+
if err := c.Watch(
81+
&source.Kind{Type: &clusterv1.Cluster{}},
82+
handler.EnqueueRequestsFromMapFunc(azureMachinePoolMapper),
83+
predicates.ClusterUnpausedAndInfrastructureReady(log),
84+
predicates.ResourceNotPausedAndHasFilterLabel(log, r.WatchFilterValue),
85+
); err != nil {
86+
return errors.Wrap(err, "failed adding a watch for Clusters")
87+
}
88+
89+
return nil
6590
}
6691

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

controllers/azurejson_machinetemplate_controller.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,16 @@ import (
3232
"sigs.k8s.io/cluster-api-provider-azure/azure/services/identities"
3333
"sigs.k8s.io/cluster-api-provider-azure/util/reconciler"
3434
"sigs.k8s.io/cluster-api-provider-azure/util/tele"
35+
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
3536
"sigs.k8s.io/cluster-api/util"
3637
"sigs.k8s.io/cluster-api/util/annotations"
3738
"sigs.k8s.io/cluster-api/util/predicates"
3839
ctrl "sigs.k8s.io/controller-runtime"
3940
"sigs.k8s.io/controller-runtime/pkg/client"
4041
"sigs.k8s.io/controller-runtime/pkg/controller"
42+
"sigs.k8s.io/controller-runtime/pkg/handler"
4143
"sigs.k8s.io/controller-runtime/pkg/reconcile"
44+
"sigs.k8s.io/controller-runtime/pkg/source"
4245
)
4346

4447
// AzureJSONTemplateReconciler reconciles Azure json secrets for AzureMachineTemplate objects.
@@ -56,12 +59,34 @@ func (r *AzureJSONTemplateReconciler) SetupWithManager(ctx context.Context, mgr
5659
)
5760
defer done()
5861

59-
return ctrl.NewControllerManagedBy(mgr).
62+
azureMachineTemplateMapper, err := util.ClusterToObjectsMapper(r.Client, &infrav1.AzureMachineTemplateList{}, mgr.GetScheme())
63+
if err != nil {
64+
return errors.Wrap(err, "failed to create mapper for Cluster to AzureMachineTemplates")
65+
}
66+
67+
c, err := ctrl.NewControllerManagedBy(mgr).
6068
WithOptions(options).
6169
For(&infrav1.AzureMachineTemplate{}).
6270
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(log, r.WatchFilterValue)).
6371
Owns(&corev1.Secret{}).
64-
Complete(r)
72+
Build(r)
73+
74+
if err != nil {
75+
return errors.Wrap(err, "failed to create controller")
76+
}
77+
78+
// Add a watch on Clusters to requeue when the infraRef is set. This is needed because the infraRef is not initially
79+
// set in Clusters created from a ClusterClass.
80+
if err := c.Watch(
81+
&source.Kind{Type: &clusterv1.Cluster{}},
82+
handler.EnqueueRequestsFromMapFunc(azureMachineTemplateMapper),
83+
predicates.ClusterUnpausedAndInfrastructureReady(log),
84+
predicates.ResourceNotPausedAndHasFilterLabel(log, r.WatchFilterValue),
85+
); err != nil {
86+
return errors.Wrap(err, "failed adding a watch for Clusters")
87+
}
88+
89+
return nil
6590
}
6691

6792
// Reconcile reconciles Azure json secrets for Azure machine templates.

0 commit comments

Comments
 (0)