diff --git a/changelogs/unreleased/5715-Lyndon-Li b/changelogs/unreleased/5715-Lyndon-Li new file mode 100644 index 000000000..05d9c8db1 --- /dev/null +++ b/changelogs/unreleased/5715-Lyndon-Li @@ -0,0 +1 @@ +Fix issue 5696, check if the repo is still openable before running the prune and forget operation, if not, try to reconnect the repo \ No newline at end of file diff --git a/pkg/repository/manager.go b/pkg/repository/manager.go index fae6877dc..c8cabdd81 100644 --- a/pkg/repository/manager.go +++ b/pkg/repository/manager.go @@ -172,6 +172,11 @@ func (m *manager) PruneRepo(repo *velerov1api.BackupRepository) error { if err != nil { return errors.WithStack(err) } + + if err := prd.BoostRepoConnect(context.Background(), param); err != nil { + return errors.WithStack(err) + } + return prd.PruneRepo(context.Background(), param) } @@ -207,6 +212,11 @@ func (m *manager) Forget(ctx context.Context, snapshot SnapshotIdentifier) error if err != nil { return errors.WithStack(err) } + + if err := prd.BoostRepoConnect(context.Background(), param); err != nil { + return errors.WithStack(err) + } + return prd.Forget(context.Background(), snapshot.SnapshotID, param) } diff --git a/pkg/repository/provider/provider.go b/pkg/repository/provider/provider.go index 6579386d6..4b76830f5 100644 --- a/pkg/repository/provider/provider.go +++ b/pkg/repository/provider/provider.go @@ -43,6 +43,11 @@ type Provider interface { // is already initialized, or do nothing if the repository is already connected PrepareRepo(ctx context.Context, param RepoParam) error + // BoostRepoConnect is used to re-ensure the local connection to the repo, + // so that the followed operations could succeed in some environment reset + // scenarios, for example, pod restart + BoostRepoConnect(ctx context.Context, param RepoParam) error + // PruneRepo does a full prune/maintenance of the repository PruneRepo(ctx context.Context, param RepoParam) error diff --git a/pkg/repository/provider/restic.go b/pkg/repository/provider/restic.go index 65038f0f2..a77c950be 100644 --- a/pkg/repository/provider/restic.go +++ b/pkg/repository/provider/restic.go @@ -62,6 +62,10 @@ func (r *resticRepositoryProvider) PrepareRepo(ctx context.Context, param RepoPa return nil } +func (r *resticRepositoryProvider) BoostRepoConnect(ctx context.Context, param RepoParam) error { + return nil +} + func (r *resticRepositoryProvider) PruneRepo(ctx context.Context, param RepoParam) error { return r.svc.PruneRepo(param.BackupLocation, param.BackupRepo) } diff --git a/pkg/repository/provider/unified_repo.go b/pkg/repository/provider/unified_repo.go index 2ba1c7c0d..9161e0621 100644 --- a/pkg/repository/provider/unified_repo.go +++ b/pkg/repository/provider/unified_repo.go @@ -200,6 +200,37 @@ func (urp *unifiedRepoProvider) PrepareRepo(ctx context.Context, param RepoParam return nil } +func (urp *unifiedRepoProvider) BoostRepoConnect(ctx context.Context, param RepoParam) error { + log := urp.log.WithFields(logrus.Fields{ + "BSL name": param.BackupLocation.Name, + "repo name": param.BackupRepo.Name, + "repo UID": param.BackupRepo.UID, + }) + + log.Debug("Start to boost repo connect") + + repoOption, err := udmrepo.NewRepoOptions( + udmrepo.WithPassword(urp, param), + udmrepo.WithConfigFile(urp.workPath, string(param.BackupRepo.UID)), + udmrepo.WithDescription(repoConnectDesc), + ) + + if err != nil { + return errors.Wrap(err, "error to get repo options") + } + + bkRepo, err := urp.repoService.Open(ctx, *repoOption) + if err == nil { + if c := bkRepo.Close(ctx); c != nil { + log.WithError(c).Error("Failed to close repo") + } + + return nil + } + + return urp.ConnectToRepo(ctx, param) +} + func (urp *unifiedRepoProvider) PruneRepo(ctx context.Context, param RepoParam) error { log := urp.log.WithFields(logrus.Fields{ "BSL name": param.BackupLocation.Name,