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

Add backup-version file in backup tarball #1117

Merged
merged 1 commit into from
Jan 22, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.WithStack(err)
}
if _, err := tw.Write([]byte(versionString)); err != nil {
wwitzel3 marked this conversation as resolved.
Show resolved Hide resolved
return errors.WithStack(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