Skip to content

Commit

Permalink
controllers: objects need to be cloned before update (#6006)
Browse files Browse the repository at this point in the history
controller-runtime Get() sets the pointer to an object
in the cache.

Status().Update(obj) modifies the object :\

this was leading to really weird race conditions
that were nagging at me.

Signed-off-by: Nick Santos <[email protected]>

Signed-off-by: Nick Santos <[email protected]>
  • Loading branch information
nicks authored Jan 6, 2023
1 parent f4ca7c2 commit bfe8403
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
9 changes: 5 additions & 4 deletions internal/controllers/core/cluster/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,10 @@ func (r *Reconciler) maybeUpdateStatus(ctx context.Context, obj *v1alpha1.Cluste
return nil
}

oldStatus := obj.Status
obj.Status = newStatus
err := r.ctrlClient.Status().Update(ctx, obj)
update := obj.DeepCopy()
oldStatus := update.Status
update.Status = newStatus
err := r.ctrlClient.Status().Update(ctx, update)
if err != nil {
return fmt.Errorf("updating cluster %s status: %v", obj.Name, err)
}
Expand All @@ -285,7 +286,7 @@ func (r *Reconciler) maybeUpdateStatus(ctx context.Context, obj *v1alpha1.Cluste
logger.Get(ctx).Errorf("Cluster status error: %v", newStatus.Error)
}

r.reportConnectionEvent(ctx, obj)
r.reportConnectionEvent(ctx, update)

return nil
}
Expand Down
1 change: 1 addition & 0 deletions internal/controllers/core/togglebutton/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, request reconcile.Request) (

origError := tb.Status.Error
// clear the error - if its conditions still apply, it will get re-set
tb = tb.DeepCopy()
tb.Status.Error = ""

err = r.processClick(ctx, tb)
Expand Down

0 comments on commit bfe8403

Please sign in to comment.