Skip to content

Commit

Permalink
use Patch to update .Status.CreatedAt in CreateOrUpdateMantleBackup rpc
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
ushitora-anqou committed Dec 6, 2024
1 parent dd61d19 commit e8d6c59
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions internal/controller/replication.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e8d6c59

Please sign in to comment.