Skip to content

Commit

Permalink
chore(BackupSchedule): enhancement for MaxRetentionDays, MaxBackups
Browse files Browse the repository at this point in the history
  • Loading branch information
ravi-shankar-sap committed Feb 22, 2025
1 parent 506001a commit 77c7d0b
Show file tree
Hide file tree
Showing 12 changed files with 245 additions and 32 deletions.
36 changes: 35 additions & 1 deletion api/cloud-resources/v1beta1/awsnfsbackupschedule_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,32 @@ type AwsNfsBackupScheduleSpec struct {
EndTime *metav1.Time `json:"endTime,omitempty"`

// MaxRetentionDays specifies the maximum number of days to retain the backup
// If not provided, backup will be retained indefinitely
// If not provided, it will be defaulted to 375 days.
// If the DeleteCascade is true for this schedule,
// then all the backups will be deleted when the schedule is deleted irrespective of the MaxRetentionDay configuration.
// +optional
// +kubebuilder:default=375
// +kubebuilder:validation:Minimum=1
MaxRetentionDays int `json:"maxRetentionDays,omitempty"`

// MaxReadyBackups specifies the maximum number of backups in "Ready" state to be retained.
// If not provided, it will be defaulted to 100 active backups.
// If the DeleteCascade is true for this schedule,
// then all the backups will be deleted when the schedule is deleted irrespective of the MaxReadyBackups configuration.
// +optional
// +kubebuilder:default=100
// +kubebuilder:validation:Minimum=1
MaxReadyBackups int `json:"maxReadyBackups,omitempty"`

// MaxFailedBackups specifies the maximum number of backups in "Failed" state to be retained.
// If not provided, it will be defaulted to 5 failed backups.
// If the DeleteCascade is true for this schedule,
// then all the backups will be deleted when the schedule is deleted irrespective of the MaxFailedBackups configuration.
// +optional
// +kubebuilder:default=5
// +kubebuilder:validation:Minimum=1
MaxFailedBackups int `json:"maxFailedBackups,omitempty"`

// Suspend specifies whether the schedule should be suspended
// By default, suspend will be false
// +kubebuilder:default=false
Expand Down Expand Up @@ -211,6 +231,20 @@ func (sc *AwsNfsBackupSchedule) SetDeleteCascade(cascade bool) {
sc.Spec.DeleteCascade = cascade
}

func (sc *AwsNfsBackupSchedule) GetMaxReadyBackups() int {
return sc.Spec.MaxReadyBackups
}
func (sc *AwsNfsBackupSchedule) SetMaxReadyBackups(count int) {
sc.Spec.MaxReadyBackups = count
}

func (sc *AwsNfsBackupSchedule) GetMaxFailedBackups() int {
return sc.Spec.MaxFailedBackups
}
func (sc *AwsNfsBackupSchedule) SetMaxFailedBackups(count int) {
sc.Spec.MaxFailedBackups = count
}

func (sc *AwsNfsBackupSchedule) GetNextRunTimes() []string {
return sc.Status.NextRunTimes
}
Expand Down
36 changes: 35 additions & 1 deletion api/cloud-resources/v1beta1/gcpnfsbackupschedule_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,32 @@ type GcpNfsBackupScheduleSpec struct {
EndTime *metav1.Time `json:"endTime,omitempty"`

// MaxRetentionDays specifies the maximum number of days to retain the backup
// If not provided, backup will be retained indefinitely
// If not provided, it will be defaulted to 375 days.
// If the DeleteCascade is true for this schedule,
// then all the backups will be deleted when the schedule is deleted irrespective of the MaxRetentionDay configuration.
// +optional
// +kubebuilder:default=375
// +kubebuilder:validation:Minimum=1
MaxRetentionDays int `json:"maxRetentionDays,omitempty"`

// MaxReadyBackups specifies the maximum number of backups in "Ready" state to be retained.
// If not provided, it will be defaulted to 100 active backups.
// If the DeleteCascade is true for this schedule,
// then all the backups will be deleted when the schedule is deleted irrespective of the MaxRetentionDay configuration.
// +optional
// +kubebuilder:default=100
// +kubebuilder:validation:Minimum=1
MaxReadyBackups int `json:"maxReadyBackups,omitempty"`

// MaxFailedBackups specifies the maximum number of backups in "Failed" state to be retained.
// If not provided, it will be defaulted to 5 failed backups.
// If the DeleteCascade is true for this schedule,
// then all the backups will be deleted when the schedule is deleted irrespective of the MaxRetentionDay configuration.
// +optional
// +kubebuilder:default=5
// +kubebuilder:validation:Minimum=1
MaxFailedBackups int `json:"maxFailedBackups,omitempty"`

// Suspend specifies whether the schedule should be suspended
// By default, suspend will be false
// +kubebuilder:default=false
Expand Down Expand Up @@ -215,6 +235,20 @@ func (sc *GcpNfsBackupSchedule) SetDeleteCascade(cascade bool) {
sc.Spec.DeleteCascade = cascade
}

func (sc *GcpNfsBackupSchedule) GetMaxReadyBackups() int {
return sc.Spec.MaxReadyBackups
}
func (sc *GcpNfsBackupSchedule) SetMaxReadyBackups(count int) {
sc.Spec.MaxReadyBackups = count
}

func (sc *GcpNfsBackupSchedule) GetMaxFailedBackups() int {
return sc.Spec.MaxFailedBackups
}
func (sc *GcpNfsBackupSchedule) SetMaxFailedBackups(count int) {
sc.Spec.MaxFailedBackups = count
}

func (sc *GcpNfsBackupSchedule) GetNextRunTimes() []string {
return sc.Status.NextRunTimes
}
Expand Down
8 changes: 4 additions & 4 deletions api/cloud-resources/v1beta1/gcpnfsvolumebackup_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,12 @@ type GcpNfsVolumeBackup struct {
Status GcpNfsVolumeBackupStatus `json:"status,omitempty"`
}

func (in *GcpNfsVolumeBackup) State() GcpNfsBackupState {
return in.Status.State
func (in *GcpNfsVolumeBackup) State() string {
return string(in.Status.State)
}

func (in *GcpNfsVolumeBackup) SetState(v GcpNfsBackupState) {
in.Status.State = v
func (in *GcpNfsVolumeBackup) SetState(v string) {
in.Status.State = GcpNfsBackupState(v)
}

func (in *GcpNfsVolumeBackup) Conditions() *[]metav1.Condition {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,32 @@ spec:
If not provided, schedule will run indefinitely
format: date-time
type: string
maxActiveBackups:
default: 100
description: |-
MaxActiveBackups specifies the maximum number of backups in "Active" state to be retained.
If not provided, it will be defaulted to 100 active backups.
If the DeleteCascade is true for this schedule,
then all the backups will be deleted when the schedule is deleted irrespective of the MaxRetentionDay configuration.
minimum: 1
type: integer
maxFailedBackups:
default: 5
description: |-
MaxFailedBackups specifies the maximum number of backups in "Failed" state to be retained.
If not provided, it will be defaulted to 5 failed backups.
If the DeleteCascade is true for this schedule,
then all the backups will be deleted when the schedule is deleted irrespective of the MaxRetentionDay configuration.
minimum: 1
type: integer
maxRetentionDays:
default: 375
description: |-
MaxRetentionDays specifies the maximum number of days to retain the backup
If not provided, backup will be retained indefinitely
If not provided, it will be defaulted to 375 days.
If the DeleteCascade is true for this schedule,
then all the backups will be deleted when the schedule is deleted irrespective of the MaxRetentionDay configuration.
minimum: 1
type: integer
nfsVolumeRef:
description: NfsVolumeRef specifies the SourceRef resource that a backup has to be made of.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,32 @@ spec:
location:
description: Location specifies the location where the backup has to be stored.
type: string
maxActiveBackups:
default: 100
description: |-
MaxActiveBackups specifies the maximum number of backups in "Active" state to be retained.
If not provided, it will be defaulted to 100 active backups.
If the DeleteCascade is true for this schedule,
then all the backups will be deleted when the schedule is deleted irrespective of the MaxRetentionDay configuration.
minimum: 1
type: integer
maxFailedBackups:
default: 5
description: |-
MaxFailedBackups specifies the maximum number of backups in "Failed" state to be retained.
If not provided, it will be defaulted to 5 failed backups.
If the DeleteCascade is true for this schedule,
then all the backups will be deleted when the schedule is deleted irrespective of the MaxRetentionDay configuration.
minimum: 1
type: integer
maxRetentionDays:
default: 375
description: |-
MaxRetentionDays specifies the maximum number of days to retain the backup
If not provided, backup will be retained indefinitely
If not provided, it will be defaulted to 375 days.
If the DeleteCascade is true for this schedule,
then all the backups will be deleted when the schedule is deleted irrespective of the MaxRetentionDay configuration.
minimum: 1
type: integer
nfsVolumeRef:
description: NfsVolumeRef specifies the SourceRef resource that a backup has to be made of.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,32 @@ spec:
If not provided, schedule will run indefinitely
format: date-time
type: string
maxActiveBackups:
default: 100
description: |-
MaxActiveBackups specifies the maximum number of backups in "Active" state to be retained.
If not provided, it will be defaulted to 100 active backups.
If the DeleteCascade is true for this schedule,
then all the backups will be deleted when the schedule is deleted irrespective of the MaxRetentionDay configuration.
minimum: 1
type: integer
maxFailedBackups:
default: 5
description: |-
MaxFailedBackups specifies the maximum number of backups in "Failed" state to be retained.
If not provided, it will be defaulted to 5 failed backups.
If the DeleteCascade is true for this schedule,
then all the backups will be deleted when the schedule is deleted irrespective of the MaxRetentionDay configuration.
minimum: 1
type: integer
maxRetentionDays:
default: 375
description: |-
MaxRetentionDays specifies the maximum number of days to retain the backup
If not provided, backup will be retained indefinitely
If not provided, it will be defaulted to 375 days.
If the DeleteCascade is true for this schedule,
then all the backups will be deleted when the schedule is deleted irrespective of the MaxRetentionDay configuration.
minimum: 1
type: integer
nfsVolumeRef:
description: NfsVolumeRef specifies the SourceRef resource that a backup has to be made of.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,32 @@ spec:
location:
description: Location specifies the location where the backup has to be stored.
type: string
maxActiveBackups:
default: 100
description: |-
MaxActiveBackups specifies the maximum number of backups in "Active" state to be retained.
If not provided, it will be defaulted to 100 active backups.
If the DeleteCascade is true for this schedule,
then all the backups will be deleted when the schedule is deleted irrespective of the MaxRetentionDay configuration.
minimum: 1
type: integer
maxFailedBackups:
default: 5
description: |-
MaxFailedBackups specifies the maximum number of backups in "Failed" state to be retained.
If not provided, it will be defaulted to 5 failed backups.
If the DeleteCascade is true for this schedule,
then all the backups will be deleted when the schedule is deleted irrespective of the MaxRetentionDay configuration.
minimum: 1
type: integer
maxRetentionDays:
default: 375
description: |-
MaxRetentionDays specifies the maximum number of days to retain the backup
If not provided, backup will be retained indefinitely
If not provided, it will be defaulted to 375 days.
If the DeleteCascade is true for this schedule,
then all the backups will be deleted when the schedule is deleted irrespective of the MaxRetentionDay configuration.
minimum: 1
type: integer
nfsVolumeRef:
description: NfsVolumeRef specifies the SourceRef resource that a backup has to be made of.
Expand Down
4 changes: 2 additions & 2 deletions config/patchAfterMakeManifests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ yq -i '.metadata.annotations."cloud-resources.kyma-project.io/version" = "v0.0.2
yq -i '.metadata.annotations."cloud-resources.kyma-project.io/version" = "v0.0.55"' $SCRIPT_DIR/crd/bases/cloud-resources.kyma-project.io_azureredisinstances.yaml
yq -i '.metadata.annotations."cloud-resources.kyma-project.io/version" = "v0.0.4"' $SCRIPT_DIR/crd/bases/cloud-resources.kyma-project.io_gcpnfsvolumebackups.yaml
yq -i '.metadata.annotations."cloud-resources.kyma-project.io/version" = "v0.0.3"' $SCRIPT_DIR/crd/bases/cloud-resources.kyma-project.io_gcpnfsvolumerestores.yaml
yq -i '.metadata.annotations."cloud-resources.kyma-project.io/version" = "v0.0.4"' $SCRIPT_DIR/crd/bases/cloud-resources.kyma-project.io_gcpnfsbackupschedules.yaml
yq -i '.metadata.annotations."cloud-resources.kyma-project.io/version" = "v0.0.5"' $SCRIPT_DIR/crd/bases/cloud-resources.kyma-project.io_gcpnfsbackupschedules.yaml
yq -i '.metadata.annotations."cloud-resources.kyma-project.io/version" = "v0.0.3"' $SCRIPT_DIR/crd/bases/cloud-resources.kyma-project.io_gcpvpcpeerings.yaml
yq -i '.metadata.annotations."cloud-resources.kyma-project.io/version" = "v0.0.3"' $SCRIPT_DIR/crd/bases/cloud-resources.kyma-project.io_awsvpcpeerings.yaml
yq -i '.metadata.annotations."cloud-resources.kyma-project.io/version" = "v0.0.1"' $SCRIPT_DIR/crd/bases/cloud-resources.kyma-project.io_cloudresources.yaml
yq -i '.metadata.annotations."cloud-resources.kyma-project.io/version" = "v0.0.3"' $SCRIPT_DIR/crd/bases/cloud-resources.kyma-project.io_awsnfsbackupschedules.yaml
yq -i '.metadata.annotations."cloud-resources.kyma-project.io/version" = "v0.0.4"' $SCRIPT_DIR/crd/bases/cloud-resources.kyma-project.io_awsnfsbackupschedules.yaml
yq -i '.metadata.annotations."cloud-resources.kyma-project.io/version" = "v0.0.1"' $SCRIPT_DIR/crd/bases/cloud-resources.kyma-project.io_azurerwxvolumebackups.yaml
4 changes: 4 additions & 0 deletions pkg/skr/backupschedule/backupschedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ type BackupScheduleSpec interface {
SetSuspend(suspend bool)
GetDeleteCascade() bool
SetDeleteCascade(cascade bool)
GetMaxReadyBackups() int
SetMaxReadyBackups(count int)
GetMaxFailedBackups() int
SetMaxFailedBackups(count int)
}

type BackupScheduleStatus interface {
Expand Down
21 changes: 19 additions & 2 deletions pkg/skr/backupschedule/deleteBackups.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package backupschedule
import (
"context"
"fmt"
"github.com/kyma-project/cloud-manager/api/cloud-resources/v1beta1"
"time"

"github.com/kyma-project/cloud-manager/pkg/composed"
Expand Down Expand Up @@ -48,12 +49,20 @@ func deleteBackups(ctx context.Context, st composed.State) (error, context.Conte

nextDeleteTimes := map[string]string{}
var lastDeleted []corev1.ObjectReference
for _, backup := range state.Backups {
readyCount, failedCount := 0, 0
for _, bk := range state.Backups {
backup, okay := bk.(composed.ObjWithConditionsAndState)
if !okay {
logger.WithValues("BackupSchedule", schedule.GetName()).Info(fmt.Sprintf("%t is not of type composed.ObjWithConditionsAndState", bk))
continue
}

//Check if the backup object should be deleted
toRetain := time.Duration(schedule.GetMaxRetentionDays()) * 24 * time.Hour
elapsed := time.Since(backup.GetCreationTimestamp().Time)
if elapsed > toRetain {
if elapsed > toRetain ||
(backup.State() == v1beta1.StateReady && readyCount >= schedule.GetMaxReadyBackups()) ||
(backup.State() == v1beta1.StateFailed && failedCount >= schedule.GetMaxFailedBackups()) {
logger.WithValues("Backup", backup.GetName()).Info("Deleting backup object")
err := state.Cluster().K8sClient().Delete(ctx, backup)
if err != nil {
Expand All @@ -64,6 +73,14 @@ func deleteBackups(ctx context.Context, st composed.State) (error, context.Conte
Name: backup.GetName(),
Namespace: backup.GetNamespace(),
})
} else {
//Increment counters
switch backup.State() {
case v1beta1.StateReady:
readyCount++
case v1beta1.StateFailed:
failedCount++
}
}
if len(nextDeleteTimes) < MaxSchedules {
backupName := fmt.Sprintf("%s/%s", backup.GetNamespace(), backup.GetName())
Expand Down
Loading

0 comments on commit 77c7d0b

Please sign in to comment.