From e8d6c59a1d3cdf37cd5f609f64eb03d04badd625 Mon Sep 17 00:00:00 2001 From: Ryotaro Banno Date: Thu, 5 Dec 2024 06:19:03 +0000 Subject: [PATCH] use Patch to update .Status.CreatedAt in CreateOrUpdateMantleBackup rpc In CreateOrUpdateMantleBackup rpc, we first need to create (or update) a MantleBackup and then update its status. This "update-after-create" process is likely to fail due to "the object has been modified" error, unless the cache for kubeapi refreshes quickly after the creation. This commit fixes this problem by using Patch instead of Update for the status. Signed-off-by: Ryotaro Banno --- internal/controller/replication.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/internal/controller/replication.go b/internal/controller/replication.go index eaee182c..57023d09 100644 --- a/internal/controller/replication.go +++ b/internal/controller/replication.go @@ -183,12 +183,11 @@ func (s *SecondaryServer) CreateOrUpdateMantleBackup( return nil, fmt.Errorf("CreateOrUpdate failed: %w", err) } - // Update the status here because ctrl.CreateOrUpdate doesn't change the status. - if err := updateStatus(ctx, s.client, &backup, func() error { - backup.Status.CreatedAt = backupReceived.Status.CreatedAt - return nil - }); err != nil { - return nil, fmt.Errorf("updateStatus failed: %w", err) + // Use Patch here because updateStatus is likely to fail due to "the object has been modified" error. + newBackup := backup.DeepCopy() + newBackup.Status.CreatedAt = backupReceived.Status.CreatedAt + if err := s.client.Status().Patch(ctx, newBackup, client.MergeFrom(&backup)); err != nil { + return nil, fmt.Errorf("status patch failed: %w", err) } return &proto.CreateOrUpdateMantleBackupResponse{}, nil