Skip to content

Commit

Permalink
Enabled compactor command to save snapshots in snapstore.
Browse files Browse the repository at this point in the history
  • Loading branch information
abdasgupta committed Jun 3, 2021
1 parent ccfe181 commit 53a08a7
Show file tree
Hide file tree
Showing 16 changed files with 388 additions and 442 deletions.
10 changes: 4 additions & 6 deletions cmd/compact.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package cmd

import (
"context"
"fmt"

"github.com/gardener/etcd-backup-restore/pkg/compactor"
"github.com/sirupsen/logrus"
Expand All @@ -30,7 +29,7 @@ func NewCompactCommand(ctx context.Context) *cobra.Command {
compactCmd := &cobra.Command{
Use: "compact",
Short: "compacts multiple incremental snapshots in etcd backup into a single full snapshot",
Long: fmt.Sprintf(`Compacts an existing backup stored in snapshot store.`),
Long: "Compacts an existing backup stored in snapshot store.",
Run: func(cmd *cobra.Command, args []string) {
/* Compact operation
- Restore from all the latest snapshots (Base + Delta).
Expand All @@ -40,19 +39,18 @@ func NewCompactCommand(ctx context.Context) *cobra.Command {
*/
logger := logrus.New()

options, store, err := BuildRestoreOptionsAndStore(opts)
options, store, err := BuildRestoreOptionsAndStore(opts.restorerOptions)
if err != nil {
return
}

cp := compactor.NewCompactor(store, logrus.NewEntry(logger))
res, err := cp.Compact(options, opts.needDefragmentation)
snapshot, err := cp.Compact(options, opts.needDefragmentation)
if err != nil {
logger.Fatalf("Failed to restore snapshot: %v", err)
return
}
// logger.Infof("Compacted snapshot is in: %v", filepath.Join(opts.snapstoreConfig.Container, res.Snapshot.SnapDir, res.Snapshot.SnapName))
logger.Infof("Compacted snapshot is in: %v", res.Path)
logger.Infof("Compacted snapshot name : %v", snapshot.SnapName)

},
}
Expand Down
12 changes: 6 additions & 6 deletions cmd/miscellaneous.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,25 @@ func printVersionInfo() {
}

// BuildRestoreOptionsAndStore forms the RestoreOptions and Store object
func BuildRestoreOptionsAndStore(opts restoreOpts) (*brtypes.RestoreOptions, brtypes.SnapStore, error) {
func BuildRestoreOptionsAndStore(opts *restorerOptions) (*brtypes.RestoreOptions, brtypes.SnapStore, error) {
if err := opts.validate(); err != nil {
logger.Fatalf("failed to validate the options: %v", err)
return nil, nil, err
}

opts.complete()

clusterUrlsMap, err := types.NewURLsMap(opts.getRestorationConfig().InitialCluster)
clusterUrlsMap, err := types.NewURLsMap(opts.restorationConfig.InitialCluster)
if err != nil {
logger.Fatalf("failed creating url map for restore cluster: %v", err)
}

peerUrls, err := types.NewURLs(opts.getRestorationConfig().InitialAdvertisePeerURLs)
peerUrls, err := types.NewURLs(opts.restorationConfig.InitialAdvertisePeerURLs)
if err != nil {
logger.Fatalf("failed parsing peers urls for restore cluster: %v", err)
}

store, err := snapstore.GetSnapstore(opts.getSnapstoreConfig())
store, err := snapstore.GetSnapstore(opts.snapstoreConfig)
if err != nil {
logger.Fatalf("failed to create restore snapstore from configured storage provider: %v", err)
}
Expand All @@ -64,11 +64,11 @@ func BuildRestoreOptionsAndStore(opts restoreOpts) (*brtypes.RestoreOptions, brt

if baseSnap == nil {
logger.Infof("No base snapshot found. Will do nothing.")
return nil, nil, fmt.Errorf("No base snapshot found")
return nil, nil, fmt.Errorf("no base snapshot found")
}

return &brtypes.RestoreOptions{
Config: opts.getRestorationConfig(),
Config: opts.restorationConfig,
BaseSnapshot: baseSnap,
DeltaSnapList: deltaSnapList,
ClusterURLs: clusterUrlsMap,
Expand Down
43 changes: 1 addition & 42 deletions cmd/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,6 @@ func (c *initializerOptions) complete() {
c.restorerOptions.complete()
}

type restoreOpts interface {
getRestorationConfig() *brtypes.RestorationConfig
getSnapstoreConfig() *brtypes.SnapstoreConfig
validate() error
complete()
}

type compactOptions struct {
*restorerOptions
needDefragmentation bool
Expand All @@ -150,39 +143,13 @@ func newCompactOptions() *compactOptions {
}
}

func (c *compactOptions) getRestorationConfig() *brtypes.RestorationConfig {
return c.restorationConfig
}

func (c *compactOptions) getSnapstoreConfig() *brtypes.SnapstoreConfig {
return c.snapstoreConfig
}

// AddFlags adds the flags to flagset.
func (c *compactOptions) addFlags(fs *flag.FlagSet) {
c.restorationConfig.AddFlags(fs)
c.snapstoreConfig.AddFlags(fs)
fs.BoolVar(&c.needDefragmentation, "defragment", c.needDefragmentation, "defragment after compaction")
}

// Validate validates the config.
func (c *compactOptions) validate() error {
if err := c.snapstoreConfig.Validate(); err != nil {
return err
}

if err := c.snapstoreConfig.Validate(); err != nil {
return err
}

return c.restorationConfig.Validate()
}

// complete completes the config.
func (c *compactOptions) complete() {
c.snapstoreConfig.Complete()
}

type restorerOptions struct {
restorationConfig *brtypes.RestorationConfig
snapstoreConfig *brtypes.SnapstoreConfig
Expand All @@ -196,14 +163,6 @@ func newRestorerOptions() *restorerOptions {
}
}

func (c *restorerOptions) getRestorationConfig() *brtypes.RestorationConfig {
return c.restorationConfig
}

func (c *restorerOptions) getSnapstoreConfig() *brtypes.SnapstoreConfig {
return c.snapstoreConfig
}

// AddFlags adds the flags to flagset.
func (c *restorerOptions) addFlags(fs *flag.FlagSet) {
c.restorationConfig.AddFlags(fs)
Expand All @@ -221,7 +180,7 @@ func (c *restorerOptions) validate() error {

// complete completes the config.
func (c *restorerOptions) complete() {
//c.snapstoreConfig.Complete()
c.snapstoreConfig.Complete()
}

type validatorOptions struct {
Expand Down
5 changes: 2 additions & 3 deletions cmd/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package cmd

import (
"context"
"fmt"

"github.com/gardener/etcd-backup-restore/pkg/snapshot/restorer"
"github.com/sirupsen/logrus"
Expand All @@ -30,7 +29,7 @@ func NewRestoreCommand(ctx context.Context) *cobra.Command {
restoreCmd := &cobra.Command{
Use: "restore",
Short: "restores an etcd member data directory from snapshots",
Long: fmt.Sprintf(`Restores an etcd member data directory from existing backup stored in snapshot store.`),
Long: "Restores an etcd member data directory from existing backup stored in snapshot store.",
Run: func(cmd *cobra.Command, args []string) {
/* Restore operation
- Find the latest snapshot.
Expand All @@ -44,7 +43,7 @@ func NewRestoreCommand(ctx context.Context) *cobra.Command {
}

rs := restorer.NewRestorer(store, logrus.NewEntry(logger))
if err := rs.Restore(*options); err != nil {
if err := rs.RestoreAndStopEtcd(*options); err != nil {
logger.Fatalf("Failed to restore snapshot: %v", err)
return
}
Expand Down
Loading

0 comments on commit 53a08a7

Please sign in to comment.