Skip to content

Commit

Permalink
Handle update conflict when restoring the status
Browse files Browse the repository at this point in the history
Handle update conflict when restoring the status

Fixes #8184

Signed-off-by: Wenkai Yin(尹文开) <[email protected]>
  • Loading branch information
ywk253100 committed Jan 21, 2025
1 parent 223e1fc commit f0efe2a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
1 change: 1 addition & 0 deletions changelogs/unreleased/8630-ywk253100
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Handle update conflict when restoring the status
24 changes: 19 additions & 5 deletions pkg/restore/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import (
"k8s.io/client-go/dynamic/dynamicinformer"
corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/retry"
crclient "sigs.k8s.io/controller-runtime/pkg/client"

"github.com/vmware-tanzu/velero/internal/credentials"
Expand Down Expand Up @@ -1669,13 +1670,26 @@ func (ctx *restoreContext) restoreItem(obj *unstructured.Unstructured, groupReso
errs.Add(namespace, err)
return warnings, errs, itemExists
}
obj.SetResourceVersion(createdObj.GetResourceVersion())
updated, err := resourceClient.UpdateStatus(obj, metav1.UpdateOptions{})
if err != nil {

resourceVersion := createdObj.GetResourceVersion()
if err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
obj.SetResourceVersion(resourceVersion)
updated, err := resourceClient.UpdateStatus(obj, metav1.UpdateOptions{})
if err != nil {
if apierrors.IsConflict(err) {
res, err := resourceClient.Get(name, metav1.GetOptions{})
if err == nil {
resourceVersion = res.GetResourceVersion()
}

Check warning on line 1683 in pkg/restore/restore.go

View check run for this annotation

Codecov / codecov/patch

pkg/restore/restore.go#L1674-L1683

Added lines #L1674 - L1683 were not covered by tests
}
return err

Check warning on line 1685 in pkg/restore/restore.go

View check run for this annotation

Codecov / codecov/patch

pkg/restore/restore.go#L1685

Added line #L1685 was not covered by tests
}

createdObj = updated
return nil
}); err != nil {

Check warning on line 1690 in pkg/restore/restore.go

View check run for this annotation

Codecov / codecov/patch

pkg/restore/restore.go#L1688-L1690

Added lines #L1688 - L1690 were not covered by tests
ctx.log.Infof("status field update failed %s: %v", kube.NamespaceAndName(obj), err)
warnings.Add(namespace, err)
} else {
createdObj = updated
}
}

Expand Down

0 comments on commit f0efe2a

Please sign in to comment.