Skip to content

Commit 4036110

Browse files
committed
adjust call sites
1 parent 01a8397 commit 4036110

File tree

8 files changed

+64
-17
lines changed

8 files changed

+64
-17
lines changed

controllers/remote/cluster_cache_tracker.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -437,29 +437,29 @@ func (t *ClusterCacheTracker) Watch(ctx context.Context, input WatchInput) error
437437
return errors.New("input.Name is required")
438438
}
439439

440-
a, err := t.getClusterAccessor(ctx, input.Cluster, t.indexes...)
440+
accessor, err := t.getClusterAccessor(ctx, input.Cluster, t.indexes...)
441441
if err != nil {
442-
return err
442+
return errors.Wrapf(err, "failed to add %s watch on cluster %s", input.Kind, klog.KRef(input.Cluster.Namespace, input.Cluster.Name))
443443
}
444444

445445
// We have to lock the cluster, so that the watch is not created multiple times in parallel.
446446
ok := t.clusterLock.TryLock(input.Cluster)
447447
if !ok {
448-
return errors.Wrapf(ErrClusterLocked, "failed to add watch: error getting lock for cluster")
448+
return errors.Wrapf(ErrClusterLocked, "failed to add %s watch on cluster %s: failed to get lock for cluster", input.Kind, klog.KRef(input.Cluster.Namespace, input.Cluster.Name))
449449
}
450450
defer t.clusterLock.Unlock(input.Cluster)
451451

452-
if a.watches.Has(input.Name) {
452+
if accessor.watches.Has(input.Name) {
453453
t.log.V(6).Info("Watch already exists", "Cluster", klog.KRef(input.Cluster.Namespace, input.Cluster.Name), "name", input.Name)
454454
return nil
455455
}
456456

457457
// Need to create the watch
458-
if err := input.Watcher.Watch(source.NewKindWithCache(input.Kind, a.cache), input.EventHandler, input.Predicates...); err != nil {
459-
return errors.Wrap(err, "error creating watch")
458+
if err := input.Watcher.Watch(source.NewKindWithCache(input.Kind, accessor.cache), input.EventHandler, input.Predicates...); err != nil {
459+
return errors.Wrapf(err, "failed to add %s watch on cluster %s: failed to create watch", input.Kind, klog.KRef(input.Cluster.Namespace, input.Cluster.Name))
460460
}
461461

462-
a.watches.Insert(input.Name)
462+
accessor.watches.Insert(input.Name)
463463

464464
return nil
465465
}

controlplane/kubeadm/internal/controllers/controller.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,11 +204,23 @@ func (r *KubeadmControlPlaneReconciler) Reconcile(ctx context.Context, req ctrl.
204204

205205
if !kcp.ObjectMeta.DeletionTimestamp.IsZero() {
206206
// Handle deletion reconciliation loop.
207-
return r.reconcileDelete(ctx, cluster, kcp)
207+
res, err = r.reconcileDelete(ctx, cluster, kcp)
208+
// Requeue if the reconcile failed because the ClusterCacheTracker was locked for
209+
// the current cluster because of concurrent access.
210+
if errors.Is(err, remote.ErrClusterLocked) {
211+
return ctrl.Result{Requeue: true}, nil
212+
}
213+
return res, err
208214
}
209215

210216
// Handle normal reconciliation loop.
211-
return r.reconcile(ctx, cluster, kcp)
217+
res, err = r.reconcile(ctx, cluster, kcp)
218+
// Requeue if the reconcile failed because the ClusterCacheTracker was locked for
219+
// the current cluster because of concurrent access.
220+
if errors.Is(err, remote.ErrClusterLocked) {
221+
return ctrl.Result{Requeue: true}, nil
222+
}
223+
return res, err
212224
}
213225

214226
func patchKubeadmControlPlane(ctx context.Context, patchHelper *patch.Helper, kcp *controlplanev1.KubeadmControlPlane) error {

exp/addons/internal/controllers/clusterresourceset_controller.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,11 @@ func (r *ClusterResourceSetReconciler) Reconcile(ctx context.Context, req ctrl.R
152152

153153
for _, cluster := range clusters {
154154
if err := r.ApplyClusterResourceSet(ctx, cluster, clusterResourceSet); err != nil {
155+
// Requeue if the reconcile failed because the ClusterCacheTracker was locked for
156+
// the current cluster because of concurrent access.
157+
if errors.Is(err, remote.ErrClusterLocked) {
158+
return ctrl.Result{Requeue: true}, nil
159+
}
155160
return ctrl.Result{}, err
156161
}
157162
}

internal/controllers/machine/machine_controller.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,23 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Re
207207

208208
// Handle deletion reconciliation loop.
209209
if !m.ObjectMeta.DeletionTimestamp.IsZero() {
210-
return r.reconcileDelete(ctx, cluster, m)
210+
res, err := r.reconcileDelete(ctx, cluster, m)
211+
// Requeue if the reconcile failed because the ClusterCacheTracker was locked for
212+
// the current cluster because of concurrent access.
213+
if errors.Is(err, remote.ErrClusterLocked) {
214+
return ctrl.Result{Requeue: true}, nil
215+
}
216+
return res, err
211217
}
212218

213219
// Handle normal reconciliation loop.
214-
return r.reconcile(ctx, cluster, m)
220+
res, err := r.reconcile(ctx, cluster, m)
221+
// Requeue if the reconcile failed because the ClusterCacheTracker was locked for
222+
// the current cluster because of concurrent access.
223+
if errors.Is(err, remote.ErrClusterLocked) {
224+
return ctrl.Result{Requeue: true}, nil
225+
}
226+
return res, err
215227
}
216228

217229
func patchMachine(ctx context.Context, patchHelper *patch.Helper, machine *clusterv1.Machine, options ...patch.Option) error {
@@ -253,10 +265,7 @@ func patchMachine(ctx context.Context, patchHelper *patch.Helper, machine *clust
253265
}
254266

255267
func (r *Reconciler) reconcile(ctx context.Context, cluster *clusterv1.Cluster, m *clusterv1.Machine) (ctrl.Result, error) {
256-
log := ctrl.LoggerFrom(ctx)
257-
258268
if err := r.watchClusterNodes(ctx, cluster); err != nil {
259-
log.Error(err, "error watching nodes on target cluster")
260269
return ctrl.Result{}, err
261270
}
262271

internal/controllers/machinehealthcheck/machinehealthcheck_controller.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,11 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Re
174174

175175
result, err := r.reconcile(ctx, log, cluster, m)
176176
if err != nil {
177+
// Requeue if the reconcile failed because the ClusterCacheTracker was locked for
178+
// the current cluster because of concurrent access.
179+
if errors.Is(err, remote.ErrClusterLocked) {
180+
return ctrl.Result{Requeue: true}, nil
181+
}
177182
log.Error(err, "Failed to reconcile MachineHealthCheck")
178183
r.recorder.Eventf(m, corev1.EventTypeWarning, "ReconcileError", "%v", err)
179184

@@ -201,7 +206,6 @@ func (r *Reconciler) reconcile(ctx context.Context, logger logr.Logger, cluster
201206
}
202207

203208
if err := r.watchClusterNodes(ctx, cluster); err != nil {
204-
logger.Error(err, "error watching nodes on target cluster")
205209
return ctrl.Result{}, err
206210
}
207211

internal/controllers/machineset/machineset_controller.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Re
172172

173173
result, err := r.reconcile(ctx, cluster, machineSet)
174174
if err != nil {
175+
// Requeue if the reconcile failed because the ClusterCacheTracker was locked for
176+
// the current cluster because of concurrent access.
177+
if errors.Is(err, remote.ErrClusterLocked) {
178+
return ctrl.Result{Requeue: true}, nil
179+
}
175180
log.Error(err, "Failed to reconcile MachineSet")
176181
r.recorder.Eventf(machineSet, corev1.EventTypeWarning, "ReconcileError", "%v", err)
177182
}

test/infrastructure/docker/exp/internal/controllers/dockermachinepool_controller.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,13 @@ func (r *DockerMachinePoolReconciler) Reconcile(ctx context.Context, req ctrl.Re
128128
}
129129

130130
// Handle non-deleted machines
131-
return r.reconcileNormal(ctx, cluster, machinePool, dockerMachinePool)
131+
res, err = r.reconcileNormal(ctx, cluster, machinePool, dockerMachinePool)
132+
// Requeue if the reconcile failed because the ClusterCacheTracker was locked for
133+
// the current cluster because of concurrent access.
134+
if errors.Is(err, remote.ErrClusterLocked) {
135+
return ctrl.Result{Requeue: true}, nil
136+
}
137+
return res, err
132138
}
133139

134140
// SetupWithManager will add watches for this controller.

test/infrastructure/docker/internal/controllers/dockermachine_controller.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,13 @@ func (r *DockerMachineReconciler) Reconcile(ctx context.Context, req ctrl.Reques
167167
}
168168

169169
// Handle non-deleted machines
170-
return r.reconcileNormal(ctx, cluster, machine, dockerMachine, externalMachine, externalLoadBalancer)
170+
res, err := r.reconcileNormal(ctx, cluster, machine, dockerMachine, externalMachine, externalLoadBalancer)
171+
// Requeue if the reconcile failed because the ClusterCacheTracker was locked for
172+
// the current cluster because of concurrent access.
173+
if errors.Is(err, remote.ErrClusterLocked) {
174+
return ctrl.Result{Requeue: true}, nil
175+
}
176+
return res, err
171177
}
172178

173179
func patchDockerMachine(ctx context.Context, patchHelper *patch.Helper, dockerMachine *infrav1.DockerMachine) error {

0 commit comments

Comments
 (0)