Skip to content

Commit f8bdca3

Browse files
authored
Merge pull request #8743 from sbueringer/pr-remove-requeues
🌱 Remove unnecessary requeues
2 parents 664c9c9 + c1ef1d7 commit f8bdca3

File tree

8 files changed

+39
-57
lines changed

8 files changed

+39
-57
lines changed

controllers/external/tracker.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package external
1818

1919
import (
20+
"fmt"
2021
"sync"
2122

2223
"github.com/go-logr/logr"
@@ -56,15 +57,15 @@ func (o *ObjectTracker) Watch(log logr.Logger, obj runtime.Object, handler handl
5657
u := &unstructured.Unstructured{}
5758
u.SetGroupVersionKind(gvk)
5859

59-
log.Info("Adding watcher on external object", "groupVersionKind", gvk.String())
60+
log.Info(fmt.Sprintf("Adding watch on external object %q", gvk.String()))
6061
err := o.Controller.Watch(
6162
source.Kind(o.Cache, u),
6263
handler,
6364
append(p, predicates.ResourceNotPaused(log))...,
6465
)
6566
if err != nil {
6667
o.m.Delete(key)
67-
return errors.Wrapf(err, "failed to add watcher on external object %q", gvk.String())
68+
return errors.Wrapf(err, "failed to add watch on external object %q", gvk.String())
6869
}
6970
return nil
7071
}

exp/internal/controllers/machinepool_controller.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package controllers
1919
import (
2020
"context"
2121
"fmt"
22-
"sync"
2322

2423
"github.com/pkg/errors"
2524
corev1 "k8s.io/api/core/v1"
@@ -31,7 +30,6 @@ import (
3130
"k8s.io/klog/v2"
3231
ctrl "sigs.k8s.io/controller-runtime"
3332
"sigs.k8s.io/controller-runtime/pkg/builder"
34-
"sigs.k8s.io/controller-runtime/pkg/cache"
3533
"sigs.k8s.io/controller-runtime/pkg/client"
3634
"sigs.k8s.io/controller-runtime/pkg/controller"
3735
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
@@ -70,10 +68,9 @@ type MachinePoolReconciler struct {
7068
// WatchFilterValue is the label value used to filter events prior to reconciliation.
7169
WatchFilterValue string
7270

73-
controller controller.Controller
74-
recorder record.EventRecorder
75-
externalWatchers sync.Map
76-
cache cache.Cache
71+
controller controller.Controller
72+
recorder record.EventRecorder
73+
externalTracker external.ObjectTracker
7774
}
7875

7976
func (r *MachinePoolReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error {
@@ -104,7 +101,10 @@ func (r *MachinePoolReconciler) SetupWithManager(ctx context.Context, mgr ctrl.M
104101

105102
r.controller = c
106103
r.recorder = mgr.GetEventRecorderFor("machinepool-controller")
107-
r.cache = mgr.GetCache()
104+
r.externalTracker = external.ObjectTracker{
105+
Controller: c,
106+
Cache: mgr.GetCache(),
107+
}
108108
return nil
109109
}
110110

exp/internal/controllers/machinepool_controller_phases.go

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,16 @@ import (
2020
"context"
2121
"fmt"
2222
"reflect"
23-
"time"
2423

2524
"github.com/pkg/errors"
2625
corev1 "k8s.io/api/core/v1"
2726
apierrors "k8s.io/apimachinery/pkg/api/errors"
2827
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
28+
"k8s.io/klog/v2"
2929
"k8s.io/utils/pointer"
3030
ctrl "sigs.k8s.io/controller-runtime"
3131
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
3232
"sigs.k8s.io/controller-runtime/pkg/handler"
33-
"sigs.k8s.io/controller-runtime/pkg/source"
3433

3534
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
3635
"sigs.k8s.io/cluster-api/controllers/external"
@@ -43,10 +42,6 @@ import (
4342
"sigs.k8s.io/cluster-api/util/patch"
4443
)
4544

46-
var (
47-
externalReadyWait = 30 * time.Second
48-
)
49-
5045
func (r *MachinePoolReconciler) reconcilePhase(mp *expv1.MachinePool) {
5146
// Set the phase to "pending" if nil.
5247
if mp.Status.Phase == "" {
@@ -118,6 +113,11 @@ func (r *MachinePoolReconciler) reconcileExternal(ctx context.Context, cluster *
118113
return external.ReconcileOutput{}, err
119114
}
120115

116+
// Ensure we add a watch to the external object, if there isn't one already.
117+
if err := r.externalTracker.Watch(log, obj, handler.EnqueueRequestForOwner(r.Client.Scheme(), r.Client.RESTMapper(), &expv1.MachinePool{})); err != nil {
118+
return external.ReconcileOutput{}, err
119+
}
120+
121121
// if external ref is paused, return error.
122122
if annotations.IsPaused(cluster, obj) {
123123
log.V(3).Info("External object referenced is paused")
@@ -148,20 +148,6 @@ func (r *MachinePoolReconciler) reconcileExternal(ctx context.Context, cluster *
148148
return external.ReconcileOutput{}, err
149149
}
150150

151-
// Add watcher for external object, if there isn't one already.
152-
_, loaded := r.externalWatchers.LoadOrStore(obj.GroupVersionKind().String(), struct{}{})
153-
if !loaded && r.controller != nil {
154-
log.Info("Adding watcher on external object", "groupVersionKind", obj.GroupVersionKind())
155-
err := r.controller.Watch(
156-
source.Kind(r.cache, obj),
157-
handler.EnqueueRequestForOwner(r.Client.Scheme(), r.Client.RESTMapper(), &expv1.MachinePool{}),
158-
)
159-
if err != nil {
160-
r.externalWatchers.Delete(obj.GroupVersionKind().String())
161-
return external.ReconcileOutput{}, errors.Wrapf(err, "failed to add watcher on external object %q", obj.GroupVersionKind())
162-
}
163-
}
164-
165151
// Set failure reason and message, if any.
166152
failureReason, failureMessage, err := external.FailuresFrom(obj)
167153
if err != nil {
@@ -216,9 +202,9 @@ func (r *MachinePoolReconciler) reconcileBootstrap(ctx context.Context, cluster
216202
)
217203

218204
if !ready {
219-
log.V(2).Info("Bootstrap provider is not ready, requeuing")
205+
log.Info("Waiting for bootstrap provider to generate data secret and report status.ready", bootstrapConfig.GetKind(), klog.KObj(bootstrapConfig))
220206
m.Status.BootstrapReady = ready
221-
return ctrl.Result{RequeueAfter: externalReadyWait}, nil
207+
return ctrl.Result{}, nil
222208
}
223209

224210
// Get and set the name of the secret containing the bootstrap data.
@@ -289,8 +275,8 @@ func (r *MachinePoolReconciler) reconcileInfrastructure(ctx context.Context, clu
289275
)
290276

291277
if !mp.Status.InfrastructureReady {
292-
log.Info("Infrastructure provider is not ready, requeuing")
293-
return ctrl.Result{RequeueAfter: externalReadyWait}, nil
278+
log.Info("Infrastructure provider is not yet ready", infraConfig.GetKind(), klog.KObj(infraConfig))
279+
return ctrl.Result{}, nil
294280
}
295281

296282
var providerIDList []string
@@ -308,8 +294,8 @@ func (r *MachinePoolReconciler) reconcileInfrastructure(ctx context.Context, clu
308294
}
309295

310296
if len(providerIDList) == 0 && mp.Status.Replicas != 0 {
311-
log.Info("Retrieved empty Spec.ProviderIDList from infrastructure provider but Status.Replicas is not zero.", "replicas", mp.Status.Replicas)
312-
return ctrl.Result{RequeueAfter: externalReadyWait}, nil
297+
log.Info("Retrieved empty spec.providerIDList from infrastructure provider but status.replicas is not zero.", "replicas", mp.Status.Replicas)
298+
return ctrl.Result{}, nil
313299
}
314300

315301
if !reflect.DeepEqual(mp.Spec.ProviderIDList, providerIDList) {

exp/internal/controllers/machinepool_controller_phases_test.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,6 @@ const (
4343
wrongNamespace = "wrong-namespace"
4444
)
4545

46-
func init() {
47-
externalReadyWait = 1 * time.Second
48-
}
49-
5046
func TestReconcileMachinePoolPhases(t *testing.T) {
5147
deletionTimestamp := metav1.Now()
5248

@@ -569,7 +565,7 @@ func TestReconcileMachinePoolBootstrap(t *testing.T) {
569565
"status": map[string]interface{}{},
570566
},
571567
expectError: false,
572-
expectResult: ctrl.Result{RequeueAfter: externalReadyWait},
568+
expectResult: ctrl.Result{},
573569
expected: func(g *WithT, m *expv1.MachinePool) {
574570
g.Expect(m.Status.BootstrapReady).To(BeFalse())
575571
},
@@ -727,7 +723,7 @@ func TestReconcileMachinePoolBootstrap(t *testing.T) {
727723
},
728724
},
729725
expectError: false,
730-
expectResult: ctrl.Result{RequeueAfter: externalReadyWait},
726+
expectResult: ctrl.Result{},
731727
expected: func(g *WithT, m *expv1.MachinePool) {
732728
g.Expect(m.Status.BootstrapReady).To(BeFalse())
733729
},

internal/controllers/cluster/cluster_controller_phases.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ func (r *Reconciler) reconcileExternal(ctx context.Context, cluster *clusterv1.C
9292
return external.ReconcileOutput{}, err
9393
}
9494

95+
// Ensure we add a watcher to the external object.
96+
if err := r.externalTracker.Watch(log, obj, handler.EnqueueRequestForOwner(r.Client.Scheme(), r.Client.RESTMapper(), &clusterv1.Cluster{})); err != nil {
97+
return external.ReconcileOutput{}, err
98+
}
99+
95100
// if external ref is paused, return error.
96101
if annotations.IsPaused(cluster, obj) {
97102
log.V(3).Info("External object referenced is paused")
@@ -122,11 +127,6 @@ func (r *Reconciler) reconcileExternal(ctx context.Context, cluster *clusterv1.C
122127
return external.ReconcileOutput{}, err
123128
}
124129

125-
// Ensure we add a watcher to the external object.
126-
if err := r.externalTracker.Watch(log, obj, handler.EnqueueRequestForOwner(r.Client.Scheme(), r.Client.RESTMapper(), &clusterv1.Cluster{})); err != nil {
127-
return external.ReconcileOutput{}, err
128-
}
129-
130130
// Set failure reason and message, if any.
131131
failureReason, failureMessage, err := external.FailuresFrom(obj)
132132
if err != nil {

internal/controllers/machine/machine_controller.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, opt
128128
}
129129

130130
r.controller = c
131-
132131
r.recorder = mgr.GetEventRecorderFor("machine-controller")
133132
r.externalTracker = external.ObjectTracker{
134133
Controller: c,

internal/controllers/machine/machine_controller_phases.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,17 @@ func (r *Reconciler) reconcileExternal(ctx context.Context, cluster *clusterv1.C
9898
obj, err := external.Get(ctx, r.Client, ref, m.Namespace)
9999
if err != nil {
100100
if apierrors.IsNotFound(errors.Cause(err)) {
101-
log.Info("could not find external ref, requeuing", ref.Kind, klog.KRef(m.Namespace, ref.Name))
101+
log.Info("could not find external ref, requeuing", ref.Kind, klog.KRef(ref.Namespace, ref.Name))
102102
return external.ReconcileOutput{RequeueAfter: externalReadyWait}, nil
103103
}
104104
return external.ReconcileOutput{}, err
105105
}
106106

107+
// Ensure we add a watch to the external object, if there isn't one already.
108+
if err := r.externalTracker.Watch(log, obj, handler.EnqueueRequestForOwner(r.Client.Scheme(), r.Client.RESTMapper(), &clusterv1.Machine{})); err != nil {
109+
return external.ReconcileOutput{}, err
110+
}
111+
107112
// if external ref is paused, return error.
108113
if annotations.IsPaused(cluster, obj) {
109114
log.V(3).Info("External object referenced is paused")
@@ -141,11 +146,6 @@ func (r *Reconciler) reconcileExternal(ctx context.Context, cluster *clusterv1.C
141146
return external.ReconcileOutput{}, err
142147
}
143148

144-
// Ensure we add a watcher to the external object.
145-
if err := r.externalTracker.Watch(log, obj, handler.EnqueueRequestForOwner(r.Client.Scheme(), r.Client.RESTMapper(), &clusterv1.Machine{})); err != nil {
146-
return external.ReconcileOutput{}, err
147-
}
148-
149149
// Set failure reason and message, if any.
150150
failureReason, failureMessage, err := external.FailuresFrom(obj)
151151
if err != nil {
@@ -217,7 +217,7 @@ func (r *Reconciler) reconcileBootstrap(ctx context.Context, cluster *clusterv1.
217217
// If the bootstrap provider is not ready, requeue.
218218
if !ready {
219219
log.Info("Waiting for bootstrap provider to generate data secret and report status.ready", bootstrapConfig.GetKind(), klog.KObj(bootstrapConfig))
220-
return ctrl.Result{RequeueAfter: externalReadyWait}, nil
220+
return ctrl.Result{}, nil
221221
}
222222

223223
// Get and set the name of the secret containing the bootstrap data.
@@ -284,7 +284,7 @@ func (r *Reconciler) reconcileInfrastructure(ctx context.Context, cluster *clust
284284
// If the infrastructure provider is not ready, return early.
285285
if !ready {
286286
log.Info("Waiting for infrastructure provider to create machine infrastructure and report status.ready", infraConfig.GetKind(), klog.KObj(infraConfig))
287-
return ctrl.Result{RequeueAfter: externalReadyWait}, nil
287+
return ctrl.Result{}, nil
288288
}
289289

290290
// Get Spec.ProviderID from the infrastructure provider.

internal/controllers/machine/machine_controller_phases_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ func TestReconcileBootstrap(t *testing.T) {
715715
"spec": map[string]interface{}{},
716716
"status": map[string]interface{}{},
717717
},
718-
expectResult: ctrl.Result{RequeueAfter: externalReadyWait},
718+
expectResult: ctrl.Result{},
719719
expectError: false,
720720
expected: func(g *WithT, m *clusterv1.Machine) {
721721
g.Expect(m.Status.BootstrapReady).To(BeFalse())
@@ -836,7 +836,7 @@ func TestReconcileBootstrap(t *testing.T) {
836836
BootstrapReady: true,
837837
},
838838
},
839-
expectResult: ctrl.Result{RequeueAfter: externalReadyWait},
839+
expectResult: ctrl.Result{},
840840
expectError: false,
841841
expected: func(g *WithT, m *clusterv1.Machine) {
842842
g.Expect(m.GetOwnerReferences()).NotTo(ContainRefOfGroupKind("cluster.x-k8s.io", "MachineSet"))

0 commit comments

Comments
 (0)