Skip to content

Commit a238f33

Browse files
authored
Add retention flag specific for s3
* Add retention flag specific for s3 * Add retention for the unit tests: Signed-off-by: Vitor Savian <[email protected]>
1 parent 02c898d commit a238f33

File tree

10 files changed

+107
-42
lines changed

10 files changed

+107
-42
lines changed

pkg/cli/cmds/etcd_snapshot.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,13 @@ var EtcdSnapshotFlags = []cli.Flag{
124124
Destination: &ServerConfig.EtcdS3Region,
125125
Value: "us-east-1",
126126
},
127+
&cli.IntFlag{
128+
Name: "s3-retention",
129+
Aliases: []string{"etcd-s3-retention"},
130+
Usage: "(db) Number of s3 snapshots to retain.",
131+
Destination: &ServerConfig.EtcdS3Retention,
132+
Value: defaultSnapshotRentention,
133+
},
127134
&cli.StringFlag{
128135
Name: "s3-folder",
129136
Aliases: []string{"etcd-s3-folder"},

pkg/cli/cmds/server.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ type Server struct {
105105
EtcdS3SecretKey string
106106
EtcdS3SessionToken string
107107
EtcdS3BucketName string
108+
EtcdS3Retention int
108109
EtcdS3BucketLookupType string
109110
EtcdS3Region string
110111
EtcdS3Folder string
@@ -480,6 +481,12 @@ var ServerFlags = []cli.Flag{
480481
Usage: "(db) S3 folder",
481482
Destination: &ServerConfig.EtcdS3Folder,
482483
},
484+
&cli.IntFlag{
485+
Name: "etcd-s3-retention",
486+
Usage: "(db) S3 retention limit",
487+
Destination: &ServerConfig.EtcdS3Retention,
488+
Value: defaultSnapshotRentention,
489+
},
483490
&cli.StringFlag{
484491
Name: "etcd-s3-proxy",
485492
Usage: "(db) Proxy server to use when connecting to S3, overriding any proxy-releated environment variables",

pkg/cli/etcdsnapshot/etcd_snapshot.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ func commandSetup(app *cli.Context, cfg *cmds.Server) (*etcd.SnapshotRequest, *c
6565
Region: cfg.EtcdS3Region,
6666
SecretKey: cfg.EtcdS3SecretKey,
6767
SkipSSLVerify: cfg.EtcdS3SkipSSLVerify,
68+
Retention: cfg.EtcdS3Retention,
6869
Timeout: metav1.Duration{Duration: cfg.EtcdS3Timeout},
6970
}
7071
// extend request timeout to allow the S3 operation to complete

pkg/cli/server/server.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ func run(app *cli.Context, cfg *cmds.Server, leaderControllers server.CustomCont
210210
SecretKey: cfg.EtcdS3SecretKey,
211211
SessionToken: cfg.EtcdS3SessionToken,
212212
SkipSSLVerify: cfg.EtcdS3SkipSSLVerify,
213+
Retention: cfg.EtcdS3Retention,
213214
Timeout: metav1.Duration{Duration: cfg.EtcdS3Timeout},
214215
}
215216
}

pkg/daemons/config/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ type EtcdS3 struct {
7676
SessionToken string `json:"sessionToken,omitempty"`
7777
Insecure bool `json:"insecure,omitempty"`
7878
SkipSSLVerify bool `json:"skipSSLVerify,omitempty"`
79+
Retention int `json:"retention,omitempty"`
7980
Timeout metav1.Duration `json:"timeout,omitempty"`
8081
}
8182

pkg/etcd/s3/config_secret.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ func (c *Controller) getConfigFromSecret(secretName string) (*config.EtcdS3, err
5353
Region: defaultEtcdS3.Region,
5454
SecretKey: string(secret.Data["etcd-s3-secret-key"]),
5555
SessionToken: string(secret.Data["etcd-s3-session-token"]),
56+
Retention: defaultEtcdS3.Retention,
5657
Timeout: *defaultEtcdS3.Timeout.DeepCopy(),
5758
}
5859

@@ -75,6 +76,14 @@ func (c *Controller) getConfigFromSecret(secretName string) (*config.EtcdS3, err
7576
}
7677
}
7778

79+
if v, ok := secret.Data["etcd-s3-retention"]; ok {
80+
if retention, err := strconv.Atoi(string(v)); err != nil {
81+
logrus.Warnf("Failed to parse etcd-s3-retention value from S3 config secret %s: %v", secretName, err)
82+
} else {
83+
etcdS3.Retention = retention
84+
}
85+
}
86+
7887
// configure ssl verification, if value can be parsed
7988
if v, ok := secret.Data["etcd-s3-skip-ssl-verify"]; ok {
8089
if b, err := strconv.ParseBool(string(v)); err != nil {

pkg/etcd/s3/s3.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ var defaultEtcdS3 = &config.EtcdS3{
4848
Timeout: metav1.Duration{
4949
Duration: 5 * time.Minute,
5050
},
51+
Retention: 5,
5152
}
5253

5354
var (
@@ -386,13 +387,13 @@ func (c *Client) downloadSnapshotMetadata(ctx context.Context, key, file string)
386387

387388
// SnapshotRetention prunes snapshots in the configured S3 compatible backend for this specific node.
388389
// Returns a list of pruned snapshot names.
389-
func (c *Client) SnapshotRetention(ctx context.Context, retention int, prefix string) ([]string, error) {
390-
if retention < 1 {
390+
func (c *Client) SnapshotRetention(ctx context.Context, prefix string) ([]string, error) {
391+
if c.etcdS3.Retention < 1 {
391392
return nil, nil
392393
}
393394

394395
prefix = path.Join(c.etcdS3.Folder, prefix)
395-
logrus.Infof("Applying snapshot retention=%d to snapshots stored in s3://%s/%s", retention, c.etcdS3.Bucket, prefix)
396+
logrus.Infof("Applying snapshot retention=%d to snapshots stored in s3://%s/%s", c.etcdS3.Retention, c.etcdS3.Bucket, prefix)
396397

397398
var snapshotFiles []minio.ObjectInfo
398399

@@ -416,7 +417,7 @@ func (c *Client) SnapshotRetention(ctx context.Context, retention int, prefix st
416417
snapshotFiles = append(snapshotFiles, info)
417418
}
418419

419-
if len(snapshotFiles) <= retention {
420+
if len(snapshotFiles) <= c.etcdS3.Retention {
420421
return nil, nil
421422
}
422423

@@ -426,7 +427,7 @@ func (c *Client) SnapshotRetention(ctx context.Context, retention int, prefix st
426427
})
427428

428429
deleted := []string{}
429-
for _, df := range snapshotFiles[retention:] {
430+
for _, df := range snapshotFiles[c.etcdS3.Retention:] {
430431
logrus.Infof("Removing S3 snapshot: s3://%s/%s", c.etcdS3.Bucket, df.Key)
431432

432433
key := path.Base(df.Key)

0 commit comments

Comments
 (0)