From f28cde2edd923e2846b4e4e19109d38a0c0d80d9 Mon Sep 17 00:00:00 2001 From: Derek Wang Date: Thu, 2 Mar 2023 23:36:23 -0800 Subject: [PATCH] fix(controller): use a copied object to update because of mutation (#2466) Signed-off-by: Derek Wang --- controllers/eventbus/controller.go | 3 ++- controllers/eventsource/controller.go | 3 ++- controllers/sensor/controller.go | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/controllers/eventbus/controller.go b/controllers/eventbus/controller.go index caa1f5ffab..0a18f2060d 100644 --- a/controllers/eventbus/controller.go +++ b/controllers/eventbus/controller.go @@ -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 } } diff --git a/controllers/eventsource/controller.go b/controllers/eventsource/controller.go index 734a922204..c999e11099 100644 --- a/controllers/eventsource/controller.go +++ b/controllers/eventsource/controller.go @@ -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 } } diff --git a/controllers/sensor/controller.go b/controllers/sensor/controller.go index 07ccefca09..cb5421cd19 100644 --- a/controllers/sensor/controller.go +++ b/controllers/sensor/controller.go @@ -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 } }