refactor to make valid dirs part of an object store layout

Signed-off-by: Steve Kriss <steve@heptio.com>
This commit is contained in:
Steve Kriss
2018-09-25 13:10:03 -06:00
parent 8bc7e4f6aa
commit 42b54586cd
3 changed files with 47 additions and 36 deletions

View File

@@ -57,7 +57,7 @@ const DownloadURLTTL = 10 * time.Minute
type objectBackupStore struct {
objectStore cloudprovider.ObjectStore
bucket string
layout *objectStoreLayout
layout *ObjectStoreLayout
logger logrus.FieldLogger
}
@@ -103,7 +103,7 @@ func NewObjectBackupStore(location *arkv1api.BackupStorageLocation, objectStoreG
return &objectBackupStore{
objectStore: objectStore,
bucket: location.Spec.ObjectStorage.Bucket,
layout: newObjectStoreLayout(location.Spec.ObjectStorage.Prefix),
layout: NewObjectStoreLayout(location.Spec.ObjectStorage.Prefix),
logger: log,
}, nil
}
@@ -117,7 +117,7 @@ func (s *objectBackupStore) IsValid() error {
var invalid []string
for _, dir := range dirs {
subdir := strings.TrimSuffix(strings.TrimPrefix(dir, s.layout.rootPrefix), "/")
if !validRootDirs.Has(subdir) {
if !s.layout.isValidSubdir(subdir) {
invalid = append(invalid, subdir)
}
}
@@ -134,7 +134,7 @@ func (s *objectBackupStore) IsValid() error {
}
func (s *objectBackupStore) ListBackups() ([]*arkv1api.Backup, error) {
prefixes, err := s.objectStore.ListCommonPrefixes(s.bucket, s.layout.backupsDir, "/")
prefixes, err := s.objectStore.ListCommonPrefixes(s.bucket, s.layout.subdirs["backups"], "/")
if err != nil {
return nil, err
}
@@ -149,7 +149,7 @@ func (s *objectBackupStore) ListBackups() ([]*arkv1api.Backup, error) {
// ListCommonPrefixes method return the *full* prefix, inclusive
// of s.backupsPrefix, and include the delimiter ("/") as a suffix. Trim
// each of those off to get the backup name.
backupName := strings.TrimSuffix(strings.TrimPrefix(prefix, s.layout.backupsDir), "/")
backupName := strings.TrimSuffix(strings.TrimPrefix(prefix, s.layout.subdirs["backups"]), "/")
backup, err := s.GetBackupMetadata(backupName)
if err != nil {

View File

@@ -20,57 +20,68 @@ import (
"fmt"
"path"
"strings"
"k8s.io/apimachinery/pkg/util/sets"
)
var validRootDirs = sets.NewString("backups", "restores")
type objectStoreLayout struct {
rootPrefix string
backupsDir string
restoresDir string
// ObjectStoreLayout defines how Ark's persisted files map to
// keys in an object storage bucket.
type ObjectStoreLayout struct {
rootPrefix string
subdirs map[string]string
}
func newObjectStoreLayout(prefix string) *objectStoreLayout {
func NewObjectStoreLayout(prefix string) *ObjectStoreLayout {
if prefix != "" && !strings.HasSuffix(prefix, "/") {
prefix = prefix + "/"
}
backupsDir := path.Join(prefix, "backups") + "/"
restoresDir := path.Join(prefix, "restores") + "/"
subdirs := map[string]string{
"backups": path.Join(prefix, "backups") + "/",
"restores": path.Join(prefix, "restores") + "/",
"restic": path.Join(prefix, "restic") + "/",
}
return &objectStoreLayout{
rootPrefix: prefix,
backupsDir: backupsDir,
restoresDir: restoresDir,
return &ObjectStoreLayout{
rootPrefix: prefix,
subdirs: subdirs,
}
}
func (l *objectStoreLayout) getBackupDir(backup string) string {
return path.Join(l.backupsDir, backup) + "/"
// GetResticDir returns the full prefix representing the restic
// directory within an object storage bucket containing a backup
// store.
func (l *ObjectStoreLayout) GetResticDir() string {
return l.subdirs["restic"]
}
func (l *objectStoreLayout) getRestoreDir(restore string) string {
return path.Join(l.restoresDir, restore) + "/"
func (l *ObjectStoreLayout) isValidSubdir(name string) bool {
_, ok := l.subdirs[name]
return ok
}
func (l *objectStoreLayout) getBackupMetadataKey(backup string) string {
return path.Join(l.backupsDir, backup, "ark-backup.json")
func (l *ObjectStoreLayout) getBackupDir(backup string) string {
return path.Join(l.subdirs["backups"], backup) + "/"
}
func (l *objectStoreLayout) getBackupContentsKey(backup string) string {
return path.Join(l.backupsDir, backup, fmt.Sprintf("%s.tar.gz", backup))
func (l *ObjectStoreLayout) getRestoreDir(restore string) string {
return path.Join(l.subdirs["restores"], restore) + "/"
}
func (l *objectStoreLayout) getBackupLogKey(backup string) string {
return path.Join(l.backupsDir, backup, fmt.Sprintf("%s-logs.gz", backup))
func (l *ObjectStoreLayout) getBackupMetadataKey(backup string) string {
return path.Join(l.subdirs["backups"], backup, "ark-backup.json")
}
func (l *objectStoreLayout) getRestoreLogKey(restore string) string {
return path.Join(l.restoresDir, restore, fmt.Sprintf("restore-%s-logs.gz", restore))
func (l *ObjectStoreLayout) getBackupContentsKey(backup string) string {
return path.Join(l.subdirs["backups"], backup, fmt.Sprintf("%s.tar.gz", backup))
}
func (l *objectStoreLayout) getRestoreResultsKey(restore string) string {
return path.Join(l.restoresDir, restore, fmt.Sprintf("restore-%s-results.gz", restore))
func (l *ObjectStoreLayout) getBackupLogKey(backup string) string {
return path.Join(l.subdirs["backups"], backup, fmt.Sprintf("%s-logs.gz", backup))
}
func (l *ObjectStoreLayout) getRestoreLogKey(restore string) string {
return path.Join(l.subdirs["restores"], restore, fmt.Sprintf("restore-%s-logs.gz", restore))
}
func (l *ObjectStoreLayout) getRestoreResultsKey(restore string) string {
return path.Join(l.subdirs["restores"], restore, fmt.Sprintf("restore-%s-results.gz", restore))
}

View File

@@ -53,7 +53,7 @@ func newObjectBackupStoreTestHarness(bucket, prefix string) *objectBackupStoreTe
objectBackupStore: &objectBackupStore{
objectStore: objectStore,
bucket: bucket,
layout: newObjectStoreLayout(prefix),
layout: NewObjectStoreLayout(prefix),
logger: arktest.NewLogger(),
},
objectStore: objectStore,
@@ -363,7 +363,7 @@ func TestDeleteBackup(t *testing.T) {
backupStore := &objectBackupStore{
objectStore: objectStore,
bucket: "test-bucket",
layout: newObjectStoreLayout(test.prefix),
layout: NewObjectStoreLayout(test.prefix),
logger: arktest.NewLogger(),
}
defer objectStore.AssertExpectations(t)