Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ebs br: add gc immune flag #5567

Merged
merged 3 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/api-references/federation-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,18 @@ VolumeBackupStatus
<td>
</td>
</tr>
<tr>
<td>
<code>skipGC</code></br>
<em>
bool
</em>
</td>
<td>
<em>(Optional)</em>
<p>SkipGC indicates whether this EBS volume snapshot backup is immune to GC</p>
</td>
</tr>
</tbody>
</table>
<h3 id="volumebackupschedule">VolumeBackupSchedule</h3>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ spec:
type: string
metadata:
type: object
skipGC:
default: false
type: boolean
spec:
properties:
clusters:
Expand Down
3 changes: 3 additions & 0 deletions manifests/federation-crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ spec:
type: string
metadata:
type: object
skipGC:
default: false
type: boolean
spec:
properties:
clusters:
Expand Down
7 changes: 7 additions & 0 deletions pkg/apis/federation/pingcap/v1alpha1/openapi_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions pkg/apis/federation/pingcap/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ type VolumeBackup struct {

// +k8s:openapi-gen=false
Status VolumeBackupStatus `json:"status,omitempty"`

// SkipGC indicates whether this EBS volume snapshot backup is immune to GC
// +optional
// +kubebuilder:default=false
SkipGC bool `json:"skipGC,omitempty"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
Expand Down
34 changes: 23 additions & 11 deletions pkg/fedvolumebackup/backupschedule/backup_schedule_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,17 @@ func (bm *backupScheduleManager) getBackupList(bs *v1alpha1.VolumeBackupSchedule
return nil, fmt.Errorf("get backup schedule %s/%s backup list failed, selector: %s, err: %v", ns, bsName, selector, err)
}

return backupsList, nil
// We can filter out backup immune to GC
var backupListCandidates []*v1alpha1.VolumeBackup

for _, backup := range backupsList {
if backup.SkipGC {
continue
}
backupListCandidates = append(backupListCandidates, backup)
}

return backupListCandidates, nil
}

func (bm *backupScheduleManager) backupGCByMaxBackups(vbs *v1alpha1.VolumeBackupSchedule) {
Expand All @@ -352,21 +362,23 @@ func (bm *backupScheduleManager) backupGCByMaxBackups(vbs *v1alpha1.VolumeBackup
// Delete the oldest expired backup
if len(backupsList) > int(*vbs.Spec.MaxBackups) {
backup := backupsList[len(backupsList)-1]

if backup.DeletionTimestamp != nil {
klog.Infof("Deletion is ongoing for backup schedule %s/%s, backup %s", ns, bsName, backup.GetName())
return
} else {
if err = bm.deps.FedVolumeBackupControl.DeleteVolumeBackup(backup); err != nil {
klog.Errorf("backup schedule %s/%s gc backup %s failed, err %v", ns, bsName, backup.GetName(), err)
return
}
klog.Infof("backup schedule %s/%s gc backup %s success", ns, bsName, backup.GetName())
}

if len(backupsList) == 1 {
// All backups have been deleted, so the last backup information in the backupSchedule should be reset
bm.resetLastBackup(vbs)
}
if err = bm.deps.FedVolumeBackupControl.DeleteVolumeBackup(backup); err != nil {
klog.Errorf("backup schedule %s/%s gc backup %s failed, err %v", ns, bsName, backup.GetName(), err)
return
}
klog.Infof("backup schedule %s/%s gc backup %s success", ns, bsName, backup.GetName())

if len(backupsList) == 1 {
// All backups have been deleted, so the last backup information in the backupSchedule should be reset
bm.resetLastBackup(vbs)
}

}
}

Expand Down
22 changes: 12 additions & 10 deletions pkg/fedvolumebackup/backupschedule/backup_schedule_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,16 +239,14 @@ func TestCalculateExpiredBackups(t *testing.T) {

testCases := []*testCase{
// no backup should be deleted
/*
{
backups: []*v1alpha1.VolumeBackup{
fakeBackup(&last10Min),
},
reservedTime: 24 * time.Hour,
expectedDeleteBackupCount: 0,
{
backups: []*v1alpha1.VolumeBackup{
fakeBackup(&last10Min),
},
*/
// 2 backup should be deleted
reservedTime: 24 * time.Hour,
expectedDeleteBackupCount: 0,
},
// 3 backups should be deleted
{
backups: []*v1alpha1.VolumeBackup{
fakeBackup(&last3Day),
Expand All @@ -262,7 +260,7 @@ func TestCalculateExpiredBackups(t *testing.T) {
}

for _, tc := range testCases {
deletedBackups, err := calculateExpiredBackups(tc.backups, tc.reservedTime)
deletedBackups, err := calculateExpiredBackups(sortSnapshotBackups(tc.backups), tc.reservedTime)
g.Expect(err).Should(BeNil())
g.Expect(len(deletedBackups)).Should(Equal(tc.expectedDeleteBackupCount))
}
Expand Down Expand Up @@ -395,6 +393,10 @@ func fakeBackup(ts *time.Time) *v1alpha1.VolumeBackup {
return backup
}
backup.CreationTimestamp = metav1.Time{Time: *ts}
backup.Status.Conditions = append(backup.Status.Conditions, v1alpha1.VolumeBackupCondition{
Type: v1alpha1.VolumeBackupComplete,
Status: v1.ConditionTrue,
})
return backup
}

Expand Down