Skip to content

Commit

Permalink
Merge pull request #5293 from cleverhu/optimize-restore
Browse files Browse the repository at this point in the history
optimize code for restore exists resources
  • Loading branch information
sseago authored Sep 8, 2022
2 parents 8888f87 + 7de6f2a commit be0a1cf
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 17 deletions.
1 change: 1 addition & 0 deletions changelogs/unreleased/5293-cleverhu
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Optimize code for restore exists resources.
27 changes: 10 additions & 17 deletions pkg/restore/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -1252,29 +1252,22 @@ func (ctx *restoreContext) restoreItem(obj *unstructured.Unstructured, groupReso

// check if we want to treat the error as a warning, in some cases the creation call might not get executed due to object API validations
// and Velero might not get the already exists error type but in reality the object already exists
objectExists := false
var fromCluster *unstructured.Unstructured

if restoreErr != nil && !isAlreadyExistsError {
if restoreErr != nil {
// check for the existence of the object in cluster, if no error then it implies that object exists
// and if err then we want to fallthrough and do another get call later
// and if err then we want to judge whether there is an existing error in the previous creation.
// if so, we will return the 'get' error.
// otherwise, we will return the original creation error.
fromCluster, err = resourceClient.Get(name, metav1.GetOptions{})
if err == nil {
objectExists = true
if err != nil && isAlreadyExistsError {
ctx.log.Errorf("Error retrieving in-cluster version of %s: %v", kube.NamespaceAndName(obj), err)
errs.Add(namespace, err)
return warnings, errs
}
}

if isAlreadyExistsError || objectExists {
// do a get call if we did not run this previously i.e.
// we've only run this for errors other than isAlreadyExistError
if fromCluster == nil {
fromCluster, err = resourceClient.Get(name, metav1.GetOptions{})
if err != nil {
ctx.log.Errorf("Error retrieving cluster version of %s: %v", kube.NamespaceAndName(obj), err)
errs.Add(namespace, err)
return warnings, errs
}
}
if fromCluster != nil {
// Remove insubstantial metadata.
fromCluster, err = resetMetadataAndStatus(fromCluster)
if err != nil {
Expand Down Expand Up @@ -1462,7 +1455,7 @@ func isAlreadyExistsError(ctx *restoreContext, obj *unstructured.Unstructured, e
}
}

// the "already allocated" error may caused by other services, check whether the expected service exists or not
// the "already allocated" error may be caused by other services, check whether the expected service exists or not
if _, err = client.Get(obj.GetName(), metav1.GetOptions{}); err != nil {
if apierrors.IsNotFound(err) {
ctx.log.Debugf("Service %s not found", kube.NamespaceAndName(obj))
Expand Down

0 comments on commit be0a1cf

Please sign in to comment.