Skip to content
This repository has been archived by the owner on Jan 20, 2022. It is now read-only.

Use env vars to customize backup retention #322

Merged
merged 1 commit into from
May 27, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 55 additions & 4 deletions pkg/backupcontroller/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,66 @@ package backupcontroller
import (
"context"
"fmt"
"os"
"strconv"
"strings"
"time"

"k8s.io/klog"

"kope.io/etcd-manager/pkg/backup"
)

// HourlyBackupsRetention controls how long hourly backups are kept.
var HourlyBackupsRetention = 24 * 7 * time.Hour

// DailyBackupsRetention controls how long daily backups are kept.
var DailyBackupsRetention = 24 * 7 * 365 * time.Hour

// ParseHumanDuration parses a go-style duration string, but
// recognizes additional suffixes: d means "day" and is interpreted as
// 24 hours; y means "year" and is interpreted as 365 days.
func ParseHumanDuration(s string) (time.Duration, error) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was shamelessly copied from certs.go. I couldn't find some good common utils place for it.

func ParseHumanDuration(s string) (time.Duration, error) {

if strings.HasSuffix(s, "y") {
s = strings.TrimSuffix(s, "y")
n, err := strconv.Atoi(s)
if err != nil {
return time.Duration(0), err
}
return time.Duration(n) * 365 * 24 * time.Hour, nil
}

if strings.HasSuffix(s, "d") {
s = strings.TrimSuffix(s, "d")
n, err := strconv.Atoi(s)
if err != nil {
return time.Duration(0), err
}
return time.Duration(n) * 24 * time.Hour, nil
}

return time.ParseDuration(s)

}

func init() {
if s := os.Getenv("ETCD_MANAGER_HOURLY_BACKUPS_RETENTION"); s != "" {
v, err := ParseHumanDuration(s)
if err != nil {
klog.Fatalf("failed to parse ETCD_MANAGER_HOURLY_BACKUPS_RETENTION=%q", s)
}
HourlyBackupsRetention = v
}

if s := os.Getenv("ETCD_MANAGER_DAILY_BACKUPS_RETENTION"); s != "" {
v, err := ParseHumanDuration(s)
if err != nil {
klog.Fatalf("failed to parse ETCD_MANAGER_DAILY_BACKUPS_RETENTION=%q", s)
}
DailyBackupsRetention = v
}
}

// BackupCleanup encapsulates the logic around periodically removing old backups
type BackupCleanup struct {
backupStore backup.Store
Expand Down Expand Up @@ -43,8 +96,6 @@ func (m *BackupCleanup) MaybeDoBackupMaintenance(ctx context.Context) error {
}

minRetention := time.Hour
hourly := time.Hour * 24 * 7
daily := time.Hour * 24 * 7 * 365

backups := make(map[time.Time]string)
retain := make(map[string]bool)
Expand All @@ -70,7 +121,7 @@ func (m *BackupCleanup) MaybeDoBackupMaintenance(ctx context.Context) error {
continue
}

if age < hourly {
if age < HourlyBackupsRetention {
bucketed := t.Truncate(time.Hour)
existing := buckets[bucketed]
if existing.IsZero() || existing.After(t) {
Expand All @@ -79,7 +130,7 @@ func (m *BackupCleanup) MaybeDoBackupMaintenance(ctx context.Context) error {
continue
}

if age < daily {
if age < DailyBackupsRetention {
bucketed := t.Truncate(time.Hour * 24)
existing := buckets[bucketed]
if existing.IsZero() || existing.After(t) {
Expand Down