diff --git a/pkg/cli/cmds/etcd_snapshot.go b/pkg/cli/cmds/etcd_snapshot.go index 8995ea1584..b216c090a7 100644 --- a/pkg/cli/cmds/etcd_snapshot.go +++ b/pkg/cli/cmds/etcd_snapshot.go @@ -124,6 +124,13 @@ var EtcdSnapshotFlags = []cli.Flag{ Destination: &ServerConfig.EtcdS3Region, Value: "us-east-1", }, + &cli.IntFlag{ + Name: "s3-retention", + Aliases: []string{"etcd-s3-retention"}, + Usage: "(db) Number of s3 snapshots to retain.", + Destination: &ServerConfig.EtcdS3Retention, + Value: defaultSnapshotRentention, + }, &cli.StringFlag{ Name: "s3-folder", Aliases: []string{"etcd-s3-folder"}, diff --git a/pkg/cli/cmds/server.go b/pkg/cli/cmds/server.go index b1a905470d..352721d676 100644 --- a/pkg/cli/cmds/server.go +++ b/pkg/cli/cmds/server.go @@ -105,6 +105,7 @@ type Server struct { EtcdS3SecretKey string EtcdS3SessionToken string EtcdS3BucketName string + EtcdS3Retention int EtcdS3BucketLookupType string EtcdS3Region string EtcdS3Folder string @@ -480,6 +481,12 @@ var ServerFlags = []cli.Flag{ Usage: "(db) S3 folder", Destination: &ServerConfig.EtcdS3Folder, }, + &cli.IntFlag{ + Name: "etcd-s3-retention", + Usage: "(db) S3 retention limit", + Destination: &ServerConfig.EtcdS3Retention, + Value: defaultSnapshotRentention, + }, &cli.StringFlag{ Name: "etcd-s3-proxy", Usage: "(db) Proxy server to use when connecting to S3, overriding any proxy-releated environment variables", diff --git a/pkg/cli/etcdsnapshot/etcd_snapshot.go b/pkg/cli/etcdsnapshot/etcd_snapshot.go index 148f11b68a..1f20a06f23 100644 --- a/pkg/cli/etcdsnapshot/etcd_snapshot.go +++ b/pkg/cli/etcdsnapshot/etcd_snapshot.go @@ -65,6 +65,7 @@ func commandSetup(app *cli.Context, cfg *cmds.Server) (*etcd.SnapshotRequest, *c Region: cfg.EtcdS3Region, SecretKey: cfg.EtcdS3SecretKey, SkipSSLVerify: cfg.EtcdS3SkipSSLVerify, + Retention: cfg.EtcdS3Retention, Timeout: metav1.Duration{Duration: cfg.EtcdS3Timeout}, } // extend request timeout to allow the S3 operation to complete diff --git a/pkg/cli/server/server.go b/pkg/cli/server/server.go index ea93fcbc55..d82a303aa2 100644 --- a/pkg/cli/server/server.go +++ b/pkg/cli/server/server.go @@ -210,6 +210,7 @@ func run(app *cli.Context, cfg *cmds.Server, leaderControllers server.CustomCont SecretKey: cfg.EtcdS3SecretKey, SessionToken: cfg.EtcdS3SessionToken, SkipSSLVerify: cfg.EtcdS3SkipSSLVerify, + Retention: cfg.EtcdS3Retention, Timeout: metav1.Duration{Duration: cfg.EtcdS3Timeout}, } } diff --git a/pkg/daemons/config/types.go b/pkg/daemons/config/types.go index a9b46d9698..0d814c608a 100644 --- a/pkg/daemons/config/types.go +++ b/pkg/daemons/config/types.go @@ -75,6 +75,7 @@ type EtcdS3 struct { SessionToken string `json:"sessionToken,omitempty"` Insecure bool `json:"insecure,omitempty"` SkipSSLVerify bool `json:"skipSSLVerify,omitempty"` + Retention int `json:"retention,omitempty"` Timeout metav1.Duration `json:"timeout,omitempty"` } diff --git a/pkg/etcd/s3/config_secret.go b/pkg/etcd/s3/config_secret.go index a7c0ae0365..e7c8462c23 100644 --- a/pkg/etcd/s3/config_secret.go +++ b/pkg/etcd/s3/config_secret.go @@ -53,6 +53,7 @@ func (c *Controller) getConfigFromSecret(secretName string) (*config.EtcdS3, err Region: defaultEtcdS3.Region, SecretKey: string(secret.Data["etcd-s3-secret-key"]), SessionToken: string(secret.Data["etcd-s3-session-token"]), + Retention: defaultEtcdS3.Retention, Timeout: *defaultEtcdS3.Timeout.DeepCopy(), } @@ -75,6 +76,14 @@ func (c *Controller) getConfigFromSecret(secretName string) (*config.EtcdS3, err } } + if v, ok := secret.Data["etcd-s3-retention"]; ok { + if retention, err := strconv.Atoi(string(v)); err != nil { + logrus.Warnf("Failed to parse etcd-s3-retention value from S3 config secret %s: %v", secretName, err) + } else { + etcdS3.Retention = retention + } + } + // configure ssl verification, if value can be parsed if v, ok := secret.Data["etcd-s3-skip-ssl-verify"]; ok { if b, err := strconv.ParseBool(string(v)); err != nil { diff --git a/pkg/etcd/s3/s3.go b/pkg/etcd/s3/s3.go index 33a4d1d048..f1aa7aa1cb 100644 --- a/pkg/etcd/s3/s3.go +++ b/pkg/etcd/s3/s3.go @@ -48,6 +48,7 @@ var defaultEtcdS3 = &config.EtcdS3{ Timeout: metav1.Duration{ Duration: 5 * time.Minute, }, + Retention: 5, } var ( @@ -386,13 +387,13 @@ func (c *Client) downloadSnapshotMetadata(ctx context.Context, key, file string) // SnapshotRetention prunes snapshots in the configured S3 compatible backend for this specific node. // Returns a list of pruned snapshot names. -func (c *Client) SnapshotRetention(ctx context.Context, retention int, prefix string) ([]string, error) { - if retention < 1 { +func (c *Client) SnapshotRetention(ctx context.Context, prefix string) ([]string, error) { + if c.etcdS3.Retention < 1 { return nil, nil } prefix = path.Join(c.etcdS3.Folder, prefix) - logrus.Infof("Applying snapshot retention=%d to snapshots stored in s3://%s/%s", retention, c.etcdS3.Bucket, prefix) + logrus.Infof("Applying snapshot retention=%d to snapshots stored in s3://%s/%s", c.etcdS3.Retention, c.etcdS3.Bucket, prefix) var snapshotFiles []minio.ObjectInfo @@ -416,7 +417,7 @@ func (c *Client) SnapshotRetention(ctx context.Context, retention int, prefix st snapshotFiles = append(snapshotFiles, info) } - if len(snapshotFiles) <= retention { + if len(snapshotFiles) <= c.etcdS3.Retention { return nil, nil } @@ -426,7 +427,7 @@ func (c *Client) SnapshotRetention(ctx context.Context, retention int, prefix st }) deleted := []string{} - for _, df := range snapshotFiles[retention:] { + for _, df := range snapshotFiles[c.etcdS3.Retention:] { logrus.Infof("Removing S3 snapshot: s3://%s/%s", c.etcdS3.Bucket, df.Key) key := path.Base(df.Key) diff --git a/pkg/etcd/s3/s3_test.go b/pkg/etcd/s3/s3_test.go index 4451bd54a2..2bbdd4950d 100644 --- a/pkg/etcd/s3/s3_test.go +++ b/pkg/etcd/s3/s3_test.go @@ -105,6 +105,7 @@ func Test_UnitControllerGetClient(t *testing.T) { Endpoint: listenerAddr, Insecure: true, Region: defaultEtcdS3.Region, + Retention: defaultEtcdS3.Retention, Timeout: *defaultEtcdS3.Timeout.DeepCopy(), }, }, @@ -130,6 +131,7 @@ func Test_UnitControllerGetClient(t *testing.T) { Endpoint: listenerAddr, Insecure: true, Region: defaultEtcdS3.Region, + Retention: defaultEtcdS3.Retention, Timeout: *defaultEtcdS3.Timeout.DeepCopy(), }, }, @@ -153,6 +155,7 @@ func Test_UnitControllerGetClient(t *testing.T) { Endpoint: defaultEtcdS3.Endpoint, Region: defaultEtcdS3.Region, ConfigSecret: "my-etcd-s3-config-secret", + Retention: defaultEtcdS3.Retention, Timeout: *defaultEtcdS3.Timeout.DeepCopy(), }, }, @@ -179,6 +182,7 @@ func Test_UnitControllerGetClient(t *testing.T) { Endpoint: defaultEtcdS3.Endpoint, Region: defaultEtcdS3.Region, ConfigSecret: "my-etcd-s3-config-secret", + Retention: defaultEtcdS3.Retention, Timeout: *defaultEtcdS3.Timeout.DeepCopy(), }, }, @@ -204,6 +208,7 @@ func Test_UnitControllerGetClient(t *testing.T) { "etcd-s3-region": []byte("us-west-2"), "etcd-s3-timeout": []byte("1m"), "etcd-s3-endpoint-ca": cert, + "etcd-s3-retention": []byte("5"), }, }, nil }) @@ -218,6 +223,7 @@ func Test_UnitControllerGetClient(t *testing.T) { Endpoint: defaultEtcdS3.Endpoint, Region: defaultEtcdS3.Region, ConfigSecret: "my-etcd-s3-config-secret", + Retention: defaultEtcdS3.Retention, Timeout: *defaultEtcdS3.Timeout.DeepCopy(), }, }, @@ -244,6 +250,7 @@ func Test_UnitControllerGetClient(t *testing.T) { "etcd-s3-timeout": []byte("1m"), "etcd-s3-endpoint-ca-name": []byte("my-etcd-s3-ca"), "etcd-s3-skip-ssl-verify": []byte("false"), + "etcd-s3-retention": []byte("5"), }, }, nil }) @@ -272,6 +279,7 @@ func Test_UnitControllerGetClient(t *testing.T) { Endpoint: defaultEtcdS3.Endpoint, Region: defaultEtcdS3.Region, ConfigSecret: "my-etcd-s3-config-secret", + Retention: defaultEtcdS3.Retention, Timeout: *defaultEtcdS3.Timeout.DeepCopy(), }, }, @@ -301,6 +309,7 @@ func Test_UnitControllerGetClient(t *testing.T) { "etcd-s3-endpoint-ca-name": []byte("my-etcd-s3-ca"), "etcd-s3-skip-ssl-verify": []byte("invalid"), "etcd-s3-insecure": []byte("invalid"), + "etcd-s3-retention": []byte("5"), }, }, nil }) @@ -321,6 +330,7 @@ func Test_UnitControllerGetClient(t *testing.T) { ConfigSecret: "my-etcd-s3-config-secret", Endpoint: listenerAddr, Insecure: true, + Retention: defaultEtcdS3.Retention, Timeout: *defaultEtcdS3.Timeout.DeepCopy(), }, }, @@ -346,6 +356,7 @@ func Test_UnitControllerGetClient(t *testing.T) { ConfigSecret: "my-etcd-s3-config-secret", Endpoint: listenerTLSAddr, SkipSSLVerify: true, + Retention: defaultEtcdS3.Retention, Timeout: *defaultEtcdS3.Timeout.DeepCopy(), }, }, @@ -370,6 +381,7 @@ func Test_UnitControllerGetClient(t *testing.T) { Region: "us-west-2", Endpoint: listenerAddr, Insecure: true, + Retention: defaultEtcdS3.Retention, Timeout: *defaultEtcdS3.Timeout.DeepCopy(), }, }, @@ -392,6 +404,7 @@ func Test_UnitControllerGetClient(t *testing.T) { Endpoint: defaultEtcdS3.Endpoint, Region: defaultEtcdS3.Region, ConfigSecret: "my-etcd-s3-config-secret", + Retention: defaultEtcdS3.Retention, Timeout: *defaultEtcdS3.Timeout.DeepCopy(), }, }, @@ -409,6 +422,7 @@ func Test_UnitControllerGetClient(t *testing.T) { Endpoint: listenerAddr, Insecure: true, Region: defaultEtcdS3.Region, + Retention: defaultEtcdS3.Retention, Timeout: *defaultEtcdS3.Timeout.DeepCopy(), } f.clientCache.Add(*c.etcdS3, c) @@ -425,6 +439,7 @@ func Test_UnitControllerGetClient(t *testing.T) { "etcd-s3-bucket": []byte("testbucket"), "etcd-s3-endpoint": []byte(listenerAddr), "etcd-s3-insecure": []byte("true"), + "etcd-s3-retention": []byte("5"), }, }, nil }) @@ -442,6 +457,7 @@ func Test_UnitControllerGetClient(t *testing.T) { ConfigSecret: "my-etcd-s3-config-secret", Endpoint: listenerAddr, Insecure: true, + Retention: defaultEtcdS3.Retention, Timeout: *defaultEtcdS3.Timeout.DeepCopy(), }, }, @@ -470,6 +486,7 @@ func Test_UnitControllerGetClient(t *testing.T) { ConfigSecret: "my-etcd-s3-config-secret", Endpoint: listenerAddr, Insecure: true, + Retention: defaultEtcdS3.Retention, Timeout: *defaultEtcdS3.Timeout.DeepCopy(), Proxy: "http://" + listenerAddr, }, @@ -496,6 +513,7 @@ func Test_UnitControllerGetClient(t *testing.T) { ConfigSecret: "my-etcd-s3-config-secret", Endpoint: listenerAddr, Insecure: true, + Retention: defaultEtcdS3.Retention, Timeout: *defaultEtcdS3.Timeout.DeepCopy(), Proxy: "http://%invalid", }, @@ -523,6 +541,7 @@ func Test_UnitControllerGetClient(t *testing.T) { ConfigSecret: "my-etcd-s3-config-secret", Endpoint: listenerAddr, Insecure: true, + Retention: defaultEtcdS3.Retention, Timeout: *defaultEtcdS3.Timeout.DeepCopy(), Proxy: "/proxy", }, @@ -550,6 +569,7 @@ func Test_UnitControllerGetClient(t *testing.T) { ConfigSecret: "my-etcd-s3-config-secret", Endpoint: listenerTLSAddr, EndpointCA: certFile, + Retention: defaultEtcdS3.Retention, Timeout: *defaultEtcdS3.Timeout.DeepCopy(), }, }, @@ -575,6 +595,7 @@ func Test_UnitControllerGetClient(t *testing.T) { ConfigSecret: "my-etcd-s3-config-secret", Endpoint: listenerTLSAddr, EndpointCA: "/does/not/exist", + Retention: defaultEtcdS3.Retention, Timeout: *defaultEtcdS3.Timeout.DeepCopy(), }, }, @@ -696,6 +717,7 @@ func Test_UnitClientUpload(t *testing.T) { Endpoint: listenerAddr, Insecure: true, Region: defaultEtcdS3.Region, + Retention: defaultEtcdS3.Retention, Timeout: *defaultEtcdS3.Timeout.DeepCopy(), }, }, @@ -717,6 +739,7 @@ func Test_UnitClientUpload(t *testing.T) { Folder: "testfolder", Insecure: true, Region: defaultEtcdS3.Region, + Retention: defaultEtcdS3.Retention, Timeout: *defaultEtcdS3.Timeout.DeepCopy(), }, }, @@ -737,6 +760,7 @@ func Test_UnitClientUpload(t *testing.T) { Endpoint: listenerAddr, Insecure: true, Region: defaultEtcdS3.Region, + Retention: defaultEtcdS3.Retention, Timeout: *defaultEtcdS3.Timeout.DeepCopy(), }, }, @@ -758,6 +782,7 @@ func Test_UnitClientUpload(t *testing.T) { Endpoint: listenerAddr, Insecure: true, Region: defaultEtcdS3.Region, + Retention: defaultEtcdS3.Retention, Timeout: *defaultEtcdS3.Timeout.DeepCopy(), }, }, @@ -858,6 +883,7 @@ func Test_UnitClientDownload(t *testing.T) { Endpoint: listenerAddr, Insecure: true, Region: defaultEtcdS3.Region, + Retention: defaultEtcdS3.Retention, Timeout: *defaultEtcdS3.Timeout.DeepCopy(), }, }, @@ -877,6 +903,7 @@ func Test_UnitClientDownload(t *testing.T) { Endpoint: listenerAddr, Insecure: true, Region: defaultEtcdS3.Region, + Retention: defaultEtcdS3.Retention, Timeout: *defaultEtcdS3.Timeout.DeepCopy(), }, }, @@ -897,6 +924,7 @@ func Test_UnitClientDownload(t *testing.T) { Endpoint: listenerAddr, Insecure: true, Region: defaultEtcdS3.Region, + Retention: defaultEtcdS3.Retention, Timeout: *defaultEtcdS3.Timeout.DeepCopy(), }, }, @@ -917,6 +945,7 @@ func Test_UnitClientDownload(t *testing.T) { Endpoint: listenerAddr, Insecure: true, Region: defaultEtcdS3.Region, + Retention: defaultEtcdS3.Retention, Timeout: *defaultEtcdS3.Timeout.DeepCopy(), }, }, @@ -999,6 +1028,7 @@ func Test_UnitClientListSnapshots(t *testing.T) { Endpoint: listenerAddr, Insecure: true, Region: defaultEtcdS3.Region, + Retention: defaultEtcdS3.Retention, Timeout: *defaultEtcdS3.Timeout.DeepCopy(), }, controller: controller, @@ -1017,6 +1047,7 @@ func Test_UnitClientListSnapshots(t *testing.T) { Folder: "testfolder", Insecure: true, Region: defaultEtcdS3.Region, + Retention: defaultEtcdS3.Retention, Timeout: *defaultEtcdS3.Timeout.DeepCopy(), }, controller: controller, @@ -1034,6 +1065,7 @@ func Test_UnitClientListSnapshots(t *testing.T) { Endpoint: listenerAddr, Insecure: true, Region: defaultEtcdS3.Region, + Retention: defaultEtcdS3.Retention, Timeout: *defaultEtcdS3.Timeout.DeepCopy(), }, controller: controller, @@ -1052,6 +1084,7 @@ func Test_UnitClientListSnapshots(t *testing.T) { Endpoint: listenerAddr, Insecure: true, Region: defaultEtcdS3.Region, + Retention: defaultEtcdS3.Retention, Timeout: *defaultEtcdS3.Timeout.DeepCopy(), }, controller: controller, @@ -1133,6 +1166,7 @@ func Test_UnitClientDeleteSnapshot(t *testing.T) { Endpoint: listenerAddr, Insecure: true, Region: defaultEtcdS3.Region, + Retention: defaultEtcdS3.Retention, Timeout: *defaultEtcdS3.Timeout.DeepCopy(), }, controller: controller, @@ -1151,6 +1185,7 @@ func Test_UnitClientDeleteSnapshot(t *testing.T) { Endpoint: listenerAddr, Insecure: true, Region: defaultEtcdS3.Region, + Retention: defaultEtcdS3.Retention, Timeout: *defaultEtcdS3.Timeout.DeepCopy(), }, controller: controller, @@ -1170,6 +1205,7 @@ func Test_UnitClientDeleteSnapshot(t *testing.T) { Endpoint: listenerAddr, Insecure: true, Region: defaultEtcdS3.Region, + Retention: defaultEtcdS3.Retention, Timeout: *defaultEtcdS3.Timeout.DeepCopy(), }, controller: controller, @@ -1189,6 +1225,7 @@ func Test_UnitClientDeleteSnapshot(t *testing.T) { Endpoint: listenerAddr, Insecure: true, Region: defaultEtcdS3.Region, + Retention: defaultEtcdS3.Retention, Timeout: *defaultEtcdS3.Timeout.DeepCopy(), }, controller: controller, @@ -1249,9 +1286,8 @@ func Test_UnitClientSnapshotRetention(t *testing.T) { controller *Controller } type args struct { - ctx context.Context - retention int - prefix string + ctx context.Context + prefix string } tests := []struct { name string @@ -1269,14 +1305,14 @@ func Test_UnitClientSnapshotRetention(t *testing.T) { Endpoint: listenerAddr, Insecure: true, Region: defaultEtcdS3.Region, + Retention: 10, Timeout: *defaultEtcdS3.Timeout.DeepCopy(), }, controller: controller, }, args: args{ - ctx: ctx, - retention: 10, - prefix: "snapshot-", + ctx: ctx, + prefix: "snapshot-", }, }, { @@ -1288,14 +1324,14 @@ func Test_UnitClientSnapshotRetention(t *testing.T) { Endpoint: listenerAddr, Insecure: true, Region: defaultEtcdS3.Region, + Retention: 2, Timeout: *defaultEtcdS3.Timeout.DeepCopy(), }, controller: controller, }, args: args{ - ctx: ctx, - retention: 2, - prefix: "snapshot-", + ctx: ctx, + prefix: "snapshot-", }, want: []string{"snapshot-03"}, }, @@ -1308,14 +1344,14 @@ func Test_UnitClientSnapshotRetention(t *testing.T) { Endpoint: listenerAddr, Insecure: true, Region: defaultEtcdS3.Region, + Retention: 1, Timeout: *defaultEtcdS3.Timeout.DeepCopy(), }, controller: controller, }, args: args{ - ctx: ctx, - retention: 1, - prefix: "snapshot-", + ctx: ctx, + prefix: "snapshot-", }, want: []string{"snapshot-02", "snapshot-03"}, }, @@ -1329,14 +1365,14 @@ func Test_UnitClientSnapshotRetention(t *testing.T) { Folder: "testfolder", Insecure: true, Region: defaultEtcdS3.Region, + Retention: 10, Timeout: *defaultEtcdS3.Timeout.DeepCopy(), }, controller: controller, }, args: args{ - ctx: ctx, - retention: 10, - prefix: "snapshot-", + ctx: ctx, + prefix: "snapshot-", }, }, { @@ -1349,14 +1385,14 @@ func Test_UnitClientSnapshotRetention(t *testing.T) { Folder: "testfolder", Insecure: true, Region: defaultEtcdS3.Region, + Retention: 2, Timeout: *defaultEtcdS3.Timeout.DeepCopy(), }, controller: controller, }, args: args{ - ctx: ctx, - retention: 2, - prefix: "snapshot-", + ctx: ctx, + prefix: "snapshot-", }, want: []string{"snapshot-06"}, }, @@ -1370,14 +1406,14 @@ func Test_UnitClientSnapshotRetention(t *testing.T) { Folder: "testfolder", Insecure: true, Region: defaultEtcdS3.Region, + Retention: 1, Timeout: *defaultEtcdS3.Timeout.DeepCopy(), }, controller: controller, }, args: args{ - ctx: ctx, - retention: 1, - prefix: "snapshot-", + ctx: ctx, + prefix: "snapshot-", }, want: []string{"snapshot-05", "snapshot-06"}, }, @@ -1390,14 +1426,14 @@ func Test_UnitClientSnapshotRetention(t *testing.T) { Endpoint: listenerAddr, Insecure: true, Region: defaultEtcdS3.Region, + Retention: 1, Timeout: *defaultEtcdS3.Timeout.DeepCopy(), }, controller: controller, }, args: args{ - ctx: ctx, - retention: 1, - prefix: "snapshot-", + ctx: ctx, + prefix: "snapshot-", }, wantErr: true, }, @@ -1410,14 +1446,14 @@ func Test_UnitClientSnapshotRetention(t *testing.T) { Endpoint: listenerAddr, Insecure: true, Region: defaultEtcdS3.Region, + Retention: 1, Timeout: *defaultEtcdS3.Timeout.DeepCopy(), }, controller: controller, }, args: args{ - ctx: ctx, - retention: 1, - prefix: "snapshot-", + ctx: ctx, + prefix: "snapshot-", }, wantErr: true, }, @@ -1431,7 +1467,7 @@ func Test_UnitClientSnapshotRetention(t *testing.T) { } return } - got, err := c.SnapshotRetention(tt.args.ctx, tt.args.retention, tt.args.prefix) + got, err := c.SnapshotRetention(tt.args.ctx, tt.args.prefix) t.Logf("Got snapshots=%#v err=%v", got, err) if (err != nil) != tt.wantErr { t.Errorf("Client.SnapshotRetention() error = %v, wantErr %v", err, tt.wantErr) diff --git a/pkg/etcd/snapshot.go b/pkg/etcd/snapshot.go index 81376e99a8..765718d8cf 100644 --- a/pkg/etcd/snapshot.go +++ b/pkg/etcd/snapshot.go @@ -373,7 +373,7 @@ func (e *ETCD) snapshot(ctx context.Context) (_ *managed.SnapshotResult, rerr er // Attempt to apply retention even if the upload failed; failure may be due to bucket // being full or some other condition that retention policy would resolve. // Snapshot retention may prune some files before returning an error. Failing to prune is not fatal. - deleted, err := s3client.SnapshotRetention(ctx, e.config.EtcdSnapshotRetention, e.config.EtcdSnapshotName) + deleted, err := s3client.SnapshotRetention(ctx, e.config.EtcdSnapshotName) res.Deleted = append(res.Deleted, deleted...) if err != nil { logrus.Warnf("Failed to apply s3 snapshot retention policy: %v", err) @@ -482,7 +482,7 @@ func (e *ETCD) PruneSnapshots(ctx context.Context) (*managed.SnapshotResult, err if s3client, err := e.getS3Client(ctx); err != nil { logrus.Warnf("Unable to initialize S3 client: %v", err) } else { - deleted, err := s3client.SnapshotRetention(ctx, e.config.EtcdSnapshotRetention, e.config.EtcdSnapshotName) + deleted, err := s3client.SnapshotRetention(ctx, e.config.EtcdSnapshotName) if err != nil { logrus.Errorf("Error applying S3 snapshot retention policy: %v", err) } diff --git a/tests/e2e/s3/s3_test.go b/tests/e2e/s3/s3_test.go index 93ea3f00f6..0bbf227b34 100644 --- a/tests/e2e/s3/s3_test.go +++ b/tests/e2e/s3/s3_test.go @@ -75,7 +75,8 @@ var _ = Describe("Verify Create", Ordered, func() { "--etcd-s3-folder=test-folder " + "--etcd-s3-endpoint=localhost:9090 " + "--etcd-s3-skip-ssl-verify=true " + - "--etcd-s3-access-key=test ") + "--etcd-s3-access-key=test " + + "--etcd-s3-retention=1 ") Expect(err).NotTo(HaveOccurred()) Expect(res).To(ContainSubstring("Snapshot on-demand-server-0")) }) @@ -86,7 +87,8 @@ var _ = Describe("Verify Create", Ordered, func() { "--from-literal=etcd-s3-folder=test-folder " + "--from-literal=etcd-s3-endpoint=localhost:9090 " + "--from-literal=etcd-s3-skip-ssl-verify=true " + - "--from-literal=etcd-s3-access-key=test ") + "--from-literal=etcd-s3-access-key=test " + + "--from-literal=etcd-s3-retention=1 ") Expect(err).NotTo(HaveOccurred()) Expect(res).To(ContainSubstring("secret/k3s-etcd-s3-config created")) }) @@ -132,12 +134,12 @@ var _ = Describe("Verify Create", Ordered, func() { _, err = tc.Servers[0].RunCmdOnNode("k3s etcd-snapshot save") Expect(err).NotTo(HaveOccurred()) time.Sleep(time.Second) - res, err := tc.Servers[0].RunCmdOnNode("k3s etcd-snapshot prune") + _, err = tc.Servers[0].RunCmdOnNode("k3s etcd-snapshot prune") Expect(err).NotTo(HaveOccurred()) - // There should now be 4 on-demand snapshots - 2 local, and 2 on s3 - res, err = tc.Servers[0].RunCmdOnNode("k3s etcd-snapshot ls 2>/dev/null | grep on-demand | wc -l") + // There should now be 4 on-demand snapshots - 2 local, and 1 on s3 + res, err := tc.Servers[0].RunCmdOnNode("k3s etcd-snapshot ls 2>/dev/null | grep on-demand | wc -l") Expect(err).NotTo(HaveOccurred()) - Expect(strings.TrimSpace(res)).To(Equal("4")) + Expect(strings.TrimSpace(res)).To(Equal("3")) }) It("ensure snapshots retention is working in s3 and local", func() { // Wait until the retention works with 3 minutes @@ -145,7 +147,7 @@ var _ = Describe("Verify Create", Ordered, func() { time.Sleep(3 * time.Minute) res, err := tc.Servers[0].RunCmdOnNode("k3s etcd-snapshot ls 2>/dev/null | grep etcd-snapshot | wc -l") Expect(err).NotTo(HaveOccurred()) - Expect(strings.TrimSpace(res)).To(Equal("4")) + Expect(strings.TrimSpace(res)).To(Equal("3")) }) }) })