Skip to content

Commit

Permalink
add prune schedule
Browse files Browse the repository at this point in the history
  • Loading branch information
Elbehery committed Jul 9, 2024
1 parent 99b5168 commit 08917b6
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
48 changes: 48 additions & 0 deletions pkg/cmd/backuprestore/backupnoconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

backupv1alpha1 "github.com/openshift/api/config/v1alpha1"
configversionedclientv1alpha1 "github.com/openshift/client-go/config/clientset/versioned/typed/config/v1alpha1"
prune_backups "github.com/openshift/cluster-etcd-operator/pkg/cmd/prune-backups"

v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/clientcmd"
Expand Down Expand Up @@ -69,6 +70,7 @@ func (b *backupNoConfig) Run() error {
}

go b.scheduleBackup()
go b.scheduleBackupPrune()
return nil
}

Expand Down Expand Up @@ -164,6 +166,52 @@ func (b *backupNoConfig) copySnapshot() error {
return nil
}

func (b *backupNoConfig) pruneBackups() error {
switch b.retention.RetentionType {
case prune_backups.RetentionTypeNone:
klog.Info("no retention policy specified")
return nil
case prune_backups.RetentionTypeNumber:
if b.retention.RetentionNumber == nil {
err := fmt.Errorf("retention policy RetentionTypeNumber requires RetentionNumberConfig")
klog.Error(err)
return err
}
return prune_backups.Retain(b.retention)
case prune_backups.RetentionTypeSize:
if b.retention.RetentionSize == nil {
err := fmt.Errorf("retention policy RetentionTypeSize requires RetentionSizeConfig")
klog.Error(err)
return err
}
return prune_backups.Retain(b.retention)
default:
err := fmt.Errorf("illegal retention policy type: [%v]", b.retention.RetentionType)
klog.Error(err)
return err
}

return nil
}

func (b *backupNoConfig) scheduleBackupPrune() error {
s, _ := gcron.NewScheduler()
defer func() { _ = s.Shutdown() }()

if _, err := s.NewJob(
gcron.CronJob(
b.schedule,
false,
),
gcron.NewTask(b.pruneBackups()),
); err != nil {
return err
}

s.Start()
return nil
}

func getCpArgs(src, dst string) []string {
return strings.Split(fmt.Sprintf("--verbose --recursive --preserve --reflink=auto %s %s", src, dst), " ")
}
14 changes: 14 additions & 0 deletions pkg/cmd/prune-backups/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package prune_backups
import (
goflag "flag"
"fmt"
"github.com/openshift/api/config/v1alpha1"
"github.com/spf13/cobra"
"io/fs"
"k8s.io/klog/v2"
Expand Down Expand Up @@ -109,6 +110,19 @@ func (r *pruneOpts) Run() error {
return nil
}

func Retain(policy v1alpha1.RetentionPolicy) error {
switch policy.RetentionType {
case RetentionTypeNone:
klog.Infof("nothing to do, retention type is none")
return nil
case RetentionTypeNumber:
return retainByNumber(policy.RetentionNumber.MaxNumberOfBackups)
case RetentionTypeSize:
return retainBySizeGb(policy.RetentionSize.MaxSizeOfBackupsGb)
}
return nil
}

func retainBySizeGb(sizeInGb int) error {
folders, err := listAllBackupFolders()
if err != nil {
Expand Down

0 comments on commit 08917b6

Please sign in to comment.