Skip to content

Commit

Permalink
Modify describe to not list snapshots (#376)
Browse files Browse the repository at this point in the history
* Modify describe to not list snapshots

* Minor refactoring

* Fix params

* Modify func name
  • Loading branch information
DeepikaDixit authored and mergify[bot] committed Nov 1, 2019
1 parent 2e861c4 commit 5a12280
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 32 deletions.
1 change: 0 additions & 1 deletion pkg/function/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,6 @@ func (s *DataSuite) TestDescribeBackups(c *C) {
// Test DescribeBackups
bp2 := *newDescribeBackupsBlueprint()
out2 := runAction(c, bp2, "describeBackups", tp)
c.Assert(out2[DescribeBackupsSnapshotIDs], NotNil)
c.Assert(out2[DescribeBackupsFileCount].(string), Not(Equals), "")
c.Assert(out2[DescribeBackupsSize].(string), Not(Equals), "")
c.Assert(out2[DescribeBackupsPasswordIncorrect].(string), Not(Equals), "")
Expand Down
6 changes: 1 addition & 5 deletions pkg/function/describe_backups.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ const (
DescribeBackupsJobPrefix = "describe-backups-"
DescribeBackupsFileCount = "fileCount"
DescribeBackupsSize = "size"
DescribeBackupsSnapshotIDs = "snapshotIDs"
DescribeBackupsPasswordIncorrect = "passwordIncorrect"
DescribeBackupsRepoDoesNotExist = "repoUnavailable"
rawDataStatsMode = "raw-data"
Expand Down Expand Up @@ -86,13 +85,12 @@ func describeBackupsPodFunc(cli kubernetes.Interface, tp param.TemplateParams, n
return nil, err
}
defer CleanUpCredsFile(ctx, pw, pod.Namespace, pod.Name, pod.Spec.Containers[0].Name)
snapshotIDs, err := restic.GetSnapshotIDs(tp.Profile, cli, targetPath, encryptionKey, namespace, pod.Name, pod.Spec.Containers[0].Name)
err = restic.CheckIfRepoIsReachable(tp.Profile, targetPath, encryptionKey, cli, namespace, pod.Name, pod.Spec.Containers[0].Name)
switch {
case err == nil:
break
case strings.Contains(err.Error(), restic.PasswordIncorrect):
return map[string]interface{}{
DescribeBackupsSnapshotIDs: nil,
DescribeBackupsFileCount: nil,
DescribeBackupsSize: nil,
DescribeBackupsPasswordIncorrect: "true",
Expand All @@ -102,7 +100,6 @@ func describeBackupsPodFunc(cli kubernetes.Interface, tp param.TemplateParams, n

case strings.Contains(err.Error(), restic.RepoDoesNotExist):
return map[string]interface{}{
DescribeBackupsSnapshotIDs: nil,
DescribeBackupsFileCount: nil,
DescribeBackupsSize: nil,
DescribeBackupsPasswordIncorrect: "false",
Expand All @@ -129,7 +126,6 @@ func describeBackupsPodFunc(cli kubernetes.Interface, tp param.TemplateParams, n
return nil, errors.New("Failed to parse snapshot stats from logs")
}
return map[string]interface{}{
DescribeBackupsSnapshotIDs: snapshotIDs,
DescribeBackupsFileCount: fc,
DescribeBackupsSize: size,
DescribeBackupsPasswordIncorrect: "false",
Expand Down
47 changes: 21 additions & 26 deletions pkg/restic/restic.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,17 @@ func SnapshotsCommand(profile *param.Profile, repository, encryptionKey string)
return shCommand(command), nil
}

// LatestSnapshotsCommand returns restic snapshots command for last snapshots
func LatestSnapshotsCommand(profile *param.Profile, repository, encryptionKey string) ([]string, error) {
cmd, err := resticArgs(profile, repository, encryptionKey)
if err != nil {
return nil, err
}
cmd = append(cmd, "snapshots", "--last", "--json")
command := strings.Join(cmd, " ")
return shCommand(command), nil
}

// SnapshotsCommandByTag returns restic snapshots command
func SnapshotsCommandByTag(profile *param.Profile, repository, tag, encryptionKey string) ([]string, error) {
cmd, err := resticArgs(profile, repository, encryptionKey)
Expand Down Expand Up @@ -255,9 +266,7 @@ func resticAzureArgs(profile *param.Profile, repository string) []string {

// GetOrCreateRepository will check if the repository already exists and initialize one if not
func GetOrCreateRepository(cli kubernetes.Interface, namespace, pod, container, artifactPrefix, encryptionKey string, profile *param.Profile) error {
stdout, stderr, err := listSnapshots(profile, artifactPrefix, encryptionKey, cli, namespace, pod, container)
format.Log(pod, container, stdout)
format.Log(pod, container, stderr)
stdout, stderr, err := getLatestSnapshots(profile, artifactPrefix, encryptionKey, cli, namespace, pod, container)
if err == nil {
return nil
}
Expand All @@ -272,38 +281,24 @@ func GetOrCreateRepository(cli kubernetes.Interface, namespace, pod, container,
return errors.Wrapf(err, "Failed to create object store backup location")
}

// GetSnapshotIDs checks if repo is reachable with current encryptionKey, and get a list of snapshot IDs
func GetSnapshotIDs(profile *param.Profile, cli kubernetes.Interface, artifactPrefix, encryptionKey, namespace, pod, container string) ([]string, error) {
stdout, err := CheckIfRepoIsReachable(profile, artifactPrefix, encryptionKey, cli, namespace, pod, container)
if err != nil {
return nil, errors.Wrap(err, "Failed to connect to object store location")
}
// parse snapshots for list of IDs
snapshots, err := SnapshotIDsFromSnapshotCommand(stdout)
if err != nil {
return nil, errors.Wrap(err, "Failed to list snapshots")
}
return snapshots, nil
}

// CheckIfRepoIsReachable checks if repo can be reached by trying to list snapshots
func CheckIfRepoIsReachable(profile *param.Profile, artifactPrefix string, encryptionKey string, cli kubernetes.Interface, namespace string, pod string, container string) (string, error) {
stdout, stderr, err := listSnapshots(profile, artifactPrefix, encryptionKey, cli, namespace, pod, container)
func CheckIfRepoIsReachable(profile *param.Profile, artifactPrefix string, encryptionKey string, cli kubernetes.Interface, namespace string, pod string, container string) error {
_, stderr, err := getLatestSnapshots(profile, artifactPrefix, encryptionKey, cli, namespace, pod, container)
if IsPasswordIncorrect(stderr) { // If password didn't work
return "", errors.New(PasswordIncorrect)
return errors.New(PasswordIncorrect)
}
if DoesRepoExist(stderr) {
return "", errors.New(RepoDoesNotExist)
return errors.New(RepoDoesNotExist)
}
if err != nil {
return "", errors.Wrap(err, "Failed to list snapshots")
return errors.Wrap(err, "Failed to list snapshots")
}
return stdout, nil
return nil
}

func listSnapshots(profile *param.Profile, artifactPrefix string, encryptionKey string, cli kubernetes.Interface, namespace string, pod string, container string) (string, string, error) {
// Use the snapshots command to check if the repository exists
cmd, err := SnapshotsCommand(profile, artifactPrefix, encryptionKey)
func getLatestSnapshots(profile *param.Profile, artifactPrefix string, encryptionKey string, cli kubernetes.Interface, namespace string, pod string, container string) (string, string, error) {
// Use the latest snapshots command to check if the repository exists
cmd, err := LatestSnapshotsCommand(profile, artifactPrefix, encryptionKey)
if err != nil {
return "", "", errors.Wrap(err, "Failed to create snapshot command")
}
Expand Down

0 comments on commit 5a12280

Please sign in to comment.