From d9b8ba8d7109ca098c379d170eb412879c5ee94e Mon Sep 17 00:00:00 2001 From: Vitor Savian Date: Thu, 6 Jun 2024 17:31:01 -0300 Subject: [PATCH] Add snapshot retention etcd-s3-folder fix * Add snapshot retention folder fix Signed-off-by: Vitor Savian * Add snapshot retention E2E test Signed-off-by: Vitor Savian --------- Signed-off-by: Vitor Savian --- pkg/etcd/s3.go | 6 ++++-- tests/e2e/s3/Vagrantfile | 2 ++ tests/e2e/s3/s3_test.go | 11 ++++++++++- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/pkg/etcd/s3.go b/pkg/etcd/s3.go index 6af4c4d27a8a..52671e5967d4 100644 --- a/pkg/etcd/s3.go +++ b/pkg/etcd/s3.go @@ -321,10 +321,12 @@ func (s *S3) snapshotRetention(ctx context.Context) ([]string, error) { deleted := []string{} for _, df := range snapshotFiles[s.config.EtcdSnapshotRetention:] { logrus.Infof("Removing S3 snapshot: s3://%s/%s", s.config.EtcdS3BucketName, df.Key) - if err := s.deleteSnapshot(ctx, df.Key); err != nil { + + key := path.Base(df.Key) + if err := s.deleteSnapshot(ctx, key); err != nil { return deleted, err } - deleted = append(deleted, df.Key) + deleted = append(deleted, key) } return deleted, nil diff --git a/tests/e2e/s3/Vagrantfile b/tests/e2e/s3/Vagrantfile index 35df26a8abe1..652a990c12c5 100644 --- a/tests/e2e/s3/Vagrantfile +++ b/tests/e2e/s3/Vagrantfile @@ -44,6 +44,8 @@ def provision(vm, role, role_num, node_num) node-external-ip: #{NETWORK_PREFIX}.100 flannel-iface: eth1 cluster-init: true + etcd-snapshot-schedule-cron: '*/1 * * * *' + etcd-snapshot-retention: 2 etcd-s3-insecure: true etcd-s3-bucket: test-bucket etcd-s3-folder: test-folder diff --git a/tests/e2e/s3/s3_test.go b/tests/e2e/s3/s3_test.go index 271a650a421d..f1aee914a21a 100644 --- a/tests/e2e/s3/s3_test.go +++ b/tests/e2e/s3/s3_test.go @@ -6,6 +6,7 @@ import ( "os" "strings" "testing" + "time" "github.com/k3s-io/k3s/tests/e2e" . "github.com/onsi/ginkgo/v2" @@ -126,13 +127,21 @@ var _ = Describe("Verify Create", Ordered, func() { Expect(err).NotTo(HaveOccurred()) _, err = e2e.RunCmdOnNode("k3s etcd-snapshot save", serverNodeNames[0]) Expect(err).NotTo(HaveOccurred()) - res, err := e2e.RunCmdOnNode("k3s etcd-snapshot prune --snapshot-retention 2", serverNodeNames[0]) + res, err := e2e.RunCmdOnNode("k3s etcd-snapshot prune", serverNodeNames[0]) Expect(err).NotTo(HaveOccurred()) // There should now be 4 on-demand snapshots - 2 local, and 2 on s3 res, err = e2e.RunCmdOnNode("k3s etcd-snapshot ls 2>/dev/null | grep on-demand | wc -l", serverNodeNames[0]) Expect(err).NotTo(HaveOccurred()) Expect(strings.TrimSpace(res)).To(Equal("4")) }) + It("ensure snapshots retention is working in s3 and local", func() { + // Wait until the retention works with 3 minutes + fmt.Printf("\nWaiting 3 minutes until retention works\n") + time.Sleep(3 * time.Minute) + res, err := e2e.RunCmdOnNode("k3s etcd-snapshot ls 2>/dev/null | grep etcd-snapshot | wc -l", serverNodeNames[0]) + Expect(err).NotTo(HaveOccurred()) + Expect(strings.TrimSpace(res)).To(Equal("4")) + }) }) })