@@ -26,6 +26,8 @@ import (
2626 "k8s.io/client-go/tools/record"
2727 infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1alpha3"
2828 "sigs.k8s.io/cluster-api-provider-azure/cloud/scope"
29+ "sigs.k8s.io/cluster-api-provider-azure/util/reconciler"
30+
2931 clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
3032 "sigs.k8s.io/cluster-api/util"
3133 ctrl "sigs.k8s.io/controller-runtime"
@@ -38,8 +40,9 @@ import (
3840// AzureClusterReconciler reconciles a AzureCluster object
3941type AzureClusterReconciler struct {
4042 client.Client
41- Log logr.Logger
42- Recorder record.EventRecorder
43+ Log logr.Logger
44+ Recorder record.EventRecorder
45+ ReconcileTimeout time.Duration
4346}
4447
4548func (r * AzureClusterReconciler ) SetupWithManager (mgr ctrl.Manager , options controller.Options ) error {
@@ -55,7 +58,8 @@ func (r *AzureClusterReconciler) SetupWithManager(mgr ctrl.Manager, options cont
5558// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=azuremachinetemplates;azuremachinetemplates/status,verbs=get;list;watch
5659
5760func (r * AzureClusterReconciler ) Reconcile (req ctrl.Request ) (_ ctrl.Result , reterr error ) {
58- ctx := context .TODO ()
61+ ctx , cancel := context .WithTimeout (context .Background (), reconciler .DefaultedLoopTimeout (r .ReconcileTimeout ))
62+ defer cancel ()
5963 log := r .Log .WithValues ("namespace" , req .Namespace , "AzureCluster" , req .Name )
6064
6165 // Fetch the AzureCluster instance
@@ -93,33 +97,32 @@ func (r *AzureClusterReconciler) Reconcile(req ctrl.Request) (_ ctrl.Result, ret
9397
9498 // Always close the scope when exiting this function so we can persist any AzureMachine changes.
9599 defer func () {
96- if err := clusterScope .Close (); err != nil && reterr == nil {
100+ if err := clusterScope .Close (ctx ); err != nil && reterr == nil {
97101 reterr = err
98102 }
99103 }()
100104
101105 // Handle deleted clusters
102106 if ! azureCluster .DeletionTimestamp .IsZero () {
103- return r .reconcileDelete (clusterScope )
107+ return r .reconcileDelete (ctx , clusterScope )
104108 }
105109
106110 // Handle non-deleted clusters
107- return r .reconcileNormal (clusterScope )
111+ return r .reconcileNormal (ctx , clusterScope )
108112}
109113
110- func (r * AzureClusterReconciler ) reconcileNormal (clusterScope * scope.ClusterScope ) (reconcile.Result , error ) {
114+ func (r * AzureClusterReconciler ) reconcileNormal (ctx context. Context , clusterScope * scope.ClusterScope ) (reconcile.Result , error ) {
111115 clusterScope .Info ("Reconciling AzureCluster" )
112-
113116 azureCluster := clusterScope .AzureCluster
114117
115118 // If the AzureCluster doesn't have our finalizer, add it.
116119 controllerutil .AddFinalizer (azureCluster , infrav1 .ClusterFinalizer )
117120 // Register the finalizer immediately to avoid orphaning Azure resources on delete
118- if err := clusterScope .PatchObject (); err != nil {
121+ if err := clusterScope .PatchObject (ctx ); err != nil {
119122 return reconcile.Result {}, err
120123 }
121124
122- err := newAzureClusterReconciler (clusterScope ).Reconcile ()
125+ err := newAzureClusterReconciler (clusterScope ).Reconcile (ctx )
123126 if err != nil {
124127 return reconcile.Result {}, errors .Wrap (err , "failed to reconcile cluster services" )
125128 }
@@ -141,12 +144,12 @@ func (r *AzureClusterReconciler) reconcileNormal(clusterScope *scope.ClusterScop
141144 return reconcile.Result {}, nil
142145}
143146
144- func (r * AzureClusterReconciler ) reconcileDelete (clusterScope * scope.ClusterScope ) (reconcile.Result , error ) {
147+ func (r * AzureClusterReconciler ) reconcileDelete (ctx context. Context , clusterScope * scope.ClusterScope ) (reconcile.Result , error ) {
145148 clusterScope .Info ("Reconciling AzureCluster delete" )
146149
147150 azureCluster := clusterScope .AzureCluster
148151
149- if err := newAzureClusterReconciler (clusterScope ).Delete (); err != nil {
152+ if err := newAzureClusterReconciler (clusterScope ).Delete (ctx ); err != nil {
150153 return reconcile.Result {}, errors .Wrapf (err , "error deleting AzureCluster %s/%s" , azureCluster .Namespace , azureCluster .Name )
151154 }
152155
0 commit comments