From b4eca61aeb833724dd0bb277bff8982e7d9ee9b7 Mon Sep 17 00:00:00 2001 From: Derek Nola Date: Mon, 9 Aug 2021 09:04:18 -0700 Subject: [PATCH] Prevent snapshot commands from creating empty snapshot directory (#3783) Signed-off-by: dereknola --- pkg/etcd/etcd.go | 14 +++++++------- pkg/etcd/s3.go | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/pkg/etcd/etcd.go b/pkg/etcd/etcd.go index e4f819f835..dce1725818 100644 --- a/pkg/etcd/etcd.go +++ b/pkg/etcd/etcd.go @@ -761,14 +761,14 @@ members: } // snapshotDir ensures that the snapshot directory exists, and then returns its path. -func snapshotDir(config *config.Control) (string, error) { +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 os.IsNotExist(err) { + if create && os.IsNotExist(err) { if err := os.MkdirAll(defaultSnapshotDir, 0700); err != nil { return "", err } @@ -821,7 +821,7 @@ func (e *ETCD) Snapshot(ctx context.Context, config *config.Control) error { return nil } - snapshotDir, err := snapshotDir(e.config) + snapshotDir, err := snapshotDir(e.config, true) if err != nil { return errors.Wrap(err, "failed to get the snapshot dir") } @@ -979,7 +979,7 @@ func (e *ETCD) initS3IfNil(ctx context.Context) error { // PruneSnapshots perfrorms a retention run with the given // retention duration and removes expired snapshots. func (e *ETCD) PruneSnapshots(ctx context.Context) error { - snapshotDir, err := snapshotDir(e.config) + snapshotDir, err := snapshotDir(e.config, false) if err != nil { return errors.Wrap(err, "failed to get the snapshot dir") } @@ -998,7 +998,7 @@ func (e *ETCD) PruneSnapshots(ctx context.Context) error { // ListSnapshots is an exported wrapper method that wraps an // unexported method of the same name. func (e *ETCD) ListSnapshots(ctx context.Context) ([]SnapshotFile, error) { - snapshotDir, err := snapshotDir(e.config) + snapshotDir, err := snapshotDir(e.config, false) if err != nil { return nil, errors.Wrap(err, "failed to get the snapshot dir") } @@ -1009,7 +1009,7 @@ func (e *ETCD) ListSnapshots(ctx context.Context) ([]SnapshotFile, error) { // deleteSnapshots removes the given snapshots from // either local storage or S3. func (e *ETCD) DeleteSnapshots(ctx context.Context, snapshots []string) error { - snapshotDir, err := snapshotDir(e.config) + snapshotDir, err := snapshotDir(e.config, false) if err != nil { return errors.Wrap(err, "failed to get the snapshot dir") } @@ -1106,7 +1106,7 @@ func updateSnapshotData(data map[string]string, snapshotFiles []SnapshotFile) er func (e *ETCD) StoreSnapshotData(ctx context.Context) error { logrus.Infof("Saving current etcd snapshot set to %s ConfigMap", snapshotConfigMapName) - snapshotDir, err := snapshotDir(e.config) + snapshotDir, err := snapshotDir(e.config, true) if err != nil { return errors.Wrap(err, "failed to get the snapshot dir") } diff --git a/pkg/etcd/s3.go b/pkg/etcd/s3.go index 5f7a93456a..bf481949c1 100644 --- a/pkg/etcd/s3.go +++ b/pkg/etcd/s3.go @@ -127,7 +127,7 @@ func (s *S3) Download(ctx context.Context) error { } defer r.Close() - snapshotDir, err := snapshotDir(s.config) + snapshotDir, err := snapshotDir(s.config, true) if err != nil { return errors.Wrap(err, "failed to get the snapshot dir") }