From ce85b988584227478a5044ebb47611ccf0905e1c Mon Sep 17 00:00:00 2001 From: Vitor Date: Mon, 14 Aug 2023 19:43:52 -0300 Subject: [PATCH] Fixed the etcd retention to delete orphaned snapshots based on the date Signed-off-by: Vitor --- pkg/etcd/etcd.go | 8 +++++--- pkg/etcd/s3.go | 7 +++++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/pkg/etcd/etcd.go b/pkg/etcd/etcd.go index af65a3eb1d39..1e44c0ec6134 100644 --- a/pkg/etcd/etcd.go +++ b/pkg/etcd/etcd.go @@ -2047,10 +2047,12 @@ func snapshotRetention(retention int, snapshotPrefix string, snapshotDir string) if len(snapshotFiles) <= retention { return nil } - sort.Slice(snapshotFiles, func(i, j int) bool { - return snapshotFiles[i].Name() < snapshotFiles[j].Name() + sort.Slice(snapshotFiles, func(firstSnapshot, secondSnapshot int) bool { + // it takes the name from the snapshot file ex: etcd-snapshot-example-{date}, makes the split using "-" to find the date, takes the date and sort by date + firstSnapshotName, secondSnapshotName := strings.Split(snapshotFiles[firstSnapshot].Name(), "-"), strings.Split(snapshotFiles[secondSnapshot].Name(), "-") + firstSnapshotDate, secondSnapshotDate := firstSnapshotName[len(firstSnapshotName)-1], secondSnapshotName[len(secondSnapshotName)-1] + return firstSnapshotDate < secondSnapshotDate }) - delCount := len(snapshotFiles) - retention for _, df := range snapshotFiles[:delCount] { snapshotPath := filepath.Join(snapshotDir, df.Name()) diff --git a/pkg/etcd/s3.go b/pkg/etcd/s3.go index 1acb9f1c82bd..fe15f8f1f129 100644 --- a/pkg/etcd/s3.go +++ b/pkg/etcd/s3.go @@ -249,8 +249,11 @@ func (s *S3) snapshotRetention(ctx context.Context) error { return nil } - sort.Slice(snapshotFiles, func(i, j int) bool { - return snapshotFiles[i].Key < snapshotFiles[j].Key + sort.Slice(snapshotFiles, func(firstSnapshot, secondSnapshot int) bool { + // it takes the key from the snapshot file ex: etcd-snapshot-example-{date}, makes the split using "-" to find the date, takes the date and sort by date + firstSnapshotName, secondSnapshotName := strings.Split(snapshotFiles[firstSnapshot].Key, "-"), strings.Split(snapshotFiles[secondSnapshot].Key, "-") + firstSnapshotDate, secondSnapshotDate := firstSnapshotName[len(firstSnapshotName)-1], secondSnapshotName[len(secondSnapshotName)-1] + return firstSnapshotDate < secondSnapshotDate }) delCount := len(snapshotFiles) - s.config.EtcdSnapshotRetention