Skip to content

Commit

Permalink
fix(controller): use a copied object to update because of mutation (#…
Browse files Browse the repository at this point in the history
…2466)

Signed-off-by: Derek Wang <whynowy@gmail.com>
  • Loading branch information
whynowy committed Mar 3, 2023
1 parent 201b833 commit f28cde2
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 3 deletions.
3 changes: 2 additions & 1 deletion controllers/eventbus/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ func (r *reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
log.Errorw("reconcile error", zap.Error(reconcileErr))
}
if r.needsUpdate(eventBus, busCopy) {
if err := r.client.Update(ctx, busCopy); err != nil {
// Use a DeepCopy to update, because it will be mutated afterwards, with empty Status.
if err := r.client.Update(ctx, busCopy.DeepCopy()); err != nil {
return reconcile.Result{}, err
}
}
Expand Down
3 changes: 2 additions & 1 deletion controllers/eventsource/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ func (r *reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
log.Errorw("reconcile error", zap.Error(reconcileErr))
}
if r.needsUpdate(eventSource, esCopy) {
if err := r.client.Update(ctx, esCopy); err != nil {
// Use a DeepCopy to update, because it will be mutated afterwards, with empty Status.
if err := r.client.Update(ctx, esCopy.DeepCopy()); err != nil {
return reconcile.Result{}, err
}
}
Expand Down
3 changes: 2 additions & 1 deletion controllers/sensor/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ func (r *reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
log.Errorw("reconcile error", zap.Error(reconcileErr))
}
if r.needsUpdate(sensor, sensorCopy) {
if err := r.client.Update(ctx, sensorCopy); err != nil {
// Use a DeepCopy to update, because it will be mutated afterwards, with empty Status.
if err := r.client.Update(ctx, sensorCopy.DeepCopy()); err != nil {
return reconcile.Result{}, err
}
}
Expand Down

0 comments on commit f28cde2

Please sign in to comment.