Skip to content

Commit

Permalink
Add backup-version file in backup tarball
Browse files Browse the repository at this point in the history
Signed-off-by: Wayne Witzel III <wwitzel3@vmware.com>
  • Loading branch information
Wayne Witzel III committed Jan 9, 2019
1 parent 5464b3d commit f6ae1e7
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
1 change: 1 addition & 0 deletions changelogs/unreleased/1117-wwitzel3
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add backup-version file in backup tarball.
4 changes: 4 additions & 0 deletions pkg/apis/ark/v1/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ const (
// for each resource type in the backup.
ResourcesDir = "resources"

// MetadataDir is a top-level directory expected in backups which contains
// files that store metadata about the backup, such as the backup version.
MetadataDir = "metadata"

// RestoreLabelKey is the label key that's applied to all resources that
// are created during a restore. This is applied for ease of identification
// of restored resources. The value will be the restore's name.
Expand Down
28 changes: 28 additions & 0 deletions pkg/backup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"context"
"fmt"
"io"
"path/filepath"
"time"

"github.com/pkg/errors"
Expand All @@ -41,6 +42,9 @@ import (
kubeutil "github.com/heptio/ark/pkg/util/kube"
)

// BackupVersion is the current backup version for Ark.
const BackupVersion = 1

// Backupper performs backups.
type Backupper interface {
// Backup takes a backup using the specification in the api.Backup and writes backup and log data
Expand Down Expand Up @@ -221,6 +225,10 @@ func (kb *kubernetesBackupper) Backup(logger logrus.FieldLogger, backupRequest *
log := logger.WithField("backup", kubeutil.NamespaceAndName(backupRequest))
log.Info("Starting backup")

if err := kb.writeBackupVersion(tw); err != nil {
return errors.WithStack(err)
}

backupRequest.NamespaceIncludesExcludes = getNamespaceIncludesExcludes(backupRequest.Backup)
log.Infof("Including namespaces: %s", backupRequest.NamespaceIncludesExcludes.IncludesString())
log.Infof("Excluding namespaces: %s", backupRequest.NamespaceIncludesExcludes.ExcludesString())
Expand Down Expand Up @@ -292,6 +300,26 @@ func (kb *kubernetesBackupper) Backup(logger logrus.FieldLogger, backupRequest *
return err
}

func (kb *kubernetesBackupper) writeBackupVersion(tw *tar.Writer) error {
versionFile := filepath.Join(api.MetadataDir, "version")
versionString := fmt.Sprintf("%d\n", BackupVersion)

hdr := &tar.Header{
Name: versionFile,
Size: int64(len(versionString)),
Typeflag: tar.TypeReg,
Mode: 0644,
ModTime: time.Now(),
}
if err := tw.WriteHeader(hdr); err != nil {
return errors.Wrap(err)
}
if _, err := tw.Write([]byte(versionString)); err != nil {
return errors.Wrap(err)
}
return nil
}

type tarWriter interface {
io.Closer
Write([]byte) (int, error)
Expand Down
4 changes: 1 addition & 3 deletions pkg/controller/backup_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ import (
"github.com/heptio/ark/pkg/volume"
)

const backupVersion = 1

type backupController struct {
*genericController

Expand Down Expand Up @@ -245,7 +243,7 @@ func (c *backupController) prepareBackupRequest(backup *api.Backup) *pkgbackup.R
}

// set backup version
request.Status.Version = backupVersion
request.Status.Version = pkgbackup.BackupVersion

// calculate expiration
if request.Spec.TTL.Duration > 0 {
Expand Down

0 comments on commit f6ae1e7

Please sign in to comment.