Skip to content

Commit

Permalink
use restic stats instead of check to check repo existence
Browse files Browse the repository at this point in the history
Signed-off-by: Steve Kriss <[email protected]>
  • Loading branch information
skriss committed Feb 6, 2019
1 parent 3054a38 commit 723cda2
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
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

0 comments on commit 723cda2

Please sign in to comment.