Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use restic stats instead of check to check repo existence #1171

Merged
merged 1 commit into from
Feb 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pkg/controller/restic_repository_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,10 @@ func (c *resticRepositoryController) initializeRepo(req *v1.ResticRepository, lo
})
}

// ensureRepo first checks the repo, and returns if check passes. If it fails,
// attempts to init the repo, and returns the result.
// ensureRepo first tries to connect to the repo, and returns if it succeeds. If it fails,
// it attempts to init the repo, and returns the result.
func ensureRepo(repo *v1.ResticRepository, repoManager restic.RepositoryManager) error {
if repoManager.CheckRepo(repo) == nil {
if repoManager.ConnectToRepo(repo) == nil {
return nil
}

Expand Down
7 changes: 7 additions & 0 deletions pkg/restic/command_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ func InitCommand(repoIdentifier string) *Command {
}
}

func StatsCommand(repoIdentifier string) *Command {
return &Command{
Command: "stats",
RepoIdentifier: repoIdentifier,
}
}

func CheckCommand(repoIdentifier string) *Command {
return &Command{
Command: "check",
Expand Down
7 changes: 7 additions & 0 deletions pkg/restic/command_factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ func TestInitCommand(t *testing.T) {
assert.Equal(t, "repo-id", c.RepoIdentifier)
}

func TestStatsCommand(t *testing.T) {
c := StatsCommand("repo-id")

assert.Equal(t, "stats", c.Command)
assert.Equal(t, "repo-id", c.RepoIdentifier)
}

func TestCheckCommand(t *testing.T) {
c := CheckCommand("repo-id")

Expand Down
14 changes: 14 additions & 0 deletions pkg/restic/repository_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ type RepositoryManager interface {
// InitRepo initializes a repo with the specified name and identifier.
InitRepo(repo *velerov1api.ResticRepository) error

// ConnectToRepo runs the 'restic stats' command against the
// specified repo, and returns an error if it fails. This is
// intended to be used to ensure that the repo exists/can be
// authenticated to.
ConnectToRepo(repo *velerov1api.ResticRepository) error

// CheckRepo checks the specified repo for errors.
CheckRepo(repo *velerov1api.ResticRepository) error

Expand Down Expand Up @@ -170,6 +176,14 @@ func (rm *repositoryManager) InitRepo(repo *velerov1api.ResticRepository) error
return rm.exec(InitCommand(repo.Spec.ResticIdentifier), repo.Spec.BackupStorageLocation)
}

func (rm *repositoryManager) ConnectToRepo(repo *velerov1api.ResticRepository) error {
// restic stats requires a non-exclusive lock
rm.repoLocker.Lock(repo.Name)
defer rm.repoLocker.Unlock(repo.Name)

return rm.exec(StatsCommand(repo.Spec.ResticIdentifier), repo.Spec.BackupStorageLocation)
}

func (rm *repositoryManager) CheckRepo(repo *velerov1api.ResticRepository) error {
// restic check requires an exclusive lock
rm.repoLocker.LockExclusive(repo.Name)
Expand Down