Skip to content

Commit

Permalink
Clean up snapshotDir create/exists logic
Browse files Browse the repository at this point in the history
Signed-off-by: Brad Davidson <[email protected]>
  • Loading branch information
brandond committed Mar 4, 2024
1 parent b164d7a commit 3576ed4
Showing 1 changed file with 32 additions and 25 deletions.
57 changes: 32 additions & 25 deletions pkg/etcd/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,26 +74,38 @@ var (
)

// snapshotDir ensures that the snapshot directory exists, and then returns its path.
// Only the default snapshot directory will be created; user-specified non-default
// snapshot directories must already exist.
func snapshotDir(config *config.Control, create bool) (string, error) {
if config.EtcdSnapshotDir == "" {
// we have to create the snapshot dir if we are using
// the default snapshot dir if it doesn't exist
defaultSnapshotDir := filepath.Join(config.DataDir, "db", "snapshots")
s, err := os.Stat(defaultSnapshotDir)
if err != nil {
if create && os.IsNotExist(err) {
if err := os.MkdirAll(defaultSnapshotDir, 0700); err != nil {
return "", err
}
return defaultSnapshotDir, nil
defaultSnapshotDir := filepath.Join(config.DataDir, "db", "snapshots")
snapshotDir := config.EtcdSnapshotDir

if snapshotDir == "" {
snapshotDir = defaultSnapshotDir
}

// Disable creation if not using the default snapshot dir.
// Non-default snapshot dirs must be created by the user.
if snapshotDir != defaultSnapshotDir {
create = false
}

s, err := os.Stat(snapshotDir)
if err != nil {
if os.IsNotExist(err) && create {
if err := os.MkdirAll(snapshotDir, 0700); err != nil {
return "", err
}
return "", err
}
if s.IsDir() {
return defaultSnapshotDir, nil
return snapshotDir, nil
}
return "", err
}

if !s.IsDir() {
return "", fmt.Errorf("%s is not a directory", snapshotDir)
}
return config.EtcdSnapshotDir, nil

return snapshotDir, nil
}

// preSnapshotSetup checks to see if the necessary components are in place
Expand Down Expand Up @@ -248,12 +260,6 @@ func (e *ETCD) Snapshot(ctx context.Context) error {
return errors.Wrap(err, "failed to get etcd-snapshot-dir")
}

if info, err := os.Stat(snapshotDir); err != nil {
return errors.Wrapf(err, "failed to stat etcd-snapshot-dir %s", snapshotDir)
} else if !info.IsDir() {
return fmt.Errorf("etcd-snapshot-dir %s is not a directory", snapshotDir)
}

cfg, err := getClientConfig(ctx, e.config)
if err != nil {
return errors.Wrap(err, "failed to get config for etcd snapshot")
Expand Down Expand Up @@ -438,7 +444,7 @@ func (e *ETCD) listLocalSnapshots() (map[string]snapshotFile, error) {
snapshots := make(map[string]snapshotFile)
snapshotDir, err := snapshotDir(e.config, true)
if err != nil {
return snapshots, errors.Wrap(err, "failed to get the snapshot dir")
return snapshots, errors.Wrap(err, "failed to get etcd-snapshot-dir")
}

if err := filepath.Walk(snapshotDir, func(path string, file os.FileInfo, err error) error {
Expand Down Expand Up @@ -502,8 +508,9 @@ func (e *ETCD) initS3IfNil(ctx context.Context) error {
func (e *ETCD) PruneSnapshots(ctx context.Context) error {
snapshotDir, err := snapshotDir(e.config, false)
if err != nil {
return errors.Wrap(err, "failed to get the snapshot dir")
return errors.Wrap(err, "failed to get etcd-snapshot-dir")
}

if err := snapshotRetention(e.config.EtcdSnapshotRetention, e.config.EtcdSnapshotName, snapshotDir); err != nil {
logrus.Errorf("Error applying snapshot retention policy: %v", err)
}
Expand Down Expand Up @@ -551,7 +558,7 @@ func (e *ETCD) ListSnapshots(ctx context.Context) (map[string]snapshotFile, erro
func (e *ETCD) DeleteSnapshots(ctx context.Context, snapshots []string) error {
snapshotDir, err := snapshotDir(e.config, false)
if err != nil {
return errors.Wrap(err, "failed to get the snapshot dir")
return errors.Wrap(err, "failed to get etcd-snapshot-dir")
}
if e.config.EtcdS3 {
if err := e.initS3IfNil(ctx); err != nil {
Expand Down

0 comments on commit 3576ed4

Please sign in to comment.