Skip to content

Commit

Permalink
fix(system-backup): handle snapshot of last backup removed
Browse files Browse the repository at this point in the history
The snapshot of the last backup of the volume might be removed.
Therefore, volume backup will be not up-to-date and it needs to
create a new backup.

ref: longhorn/longhorn 10215

Signed-off-by: James Lu <james.lu@suse.com>
  • Loading branch information
mantissahz committed Jan 16, 2025
1 parent 7c50e4f commit e6b40bc
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
8 changes: 8 additions & 0 deletions controller/system_backup_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -915,11 +915,19 @@ func (c *SystemBackupController) isVolumeBackupUpToDate(volume *longhorn.Volume,
// Retrieve last backup and its snapshot.
lastBackup, err := c.ds.GetBackup(volume.Status.LastBackup)
if err != nil {
if apierrors.IsNotFound(err) {
log.Warnf("Last Backup %v not found for volume %v", volume.Status.LastBackup, volume.Name)
return false, nil
}
return false, err
}

lastBackupSnapshot, err := c.ds.GetSnapshot(lastBackup.Status.SnapshotName)
if err != nil {
if apierrors.IsNotFound(err) {
log.Warnf("Snapshot %v not found for backup %v", lastBackup.Status.SnapshotName, lastBackup.Name)
return false, nil
}
return false, err
}

Expand Down
31 changes: 31 additions & 0 deletions controller/system_backup_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type SystemBackupTestCase struct {
existPersistentVolumes map[SystemRolloutCRName]*corev1.PersistentVolume
existVolumes map[SystemRolloutCRName]*longhorn.Volume
existBackingImages map[SystemRolloutCRName]*longhorn.BackingImage
existBackups map[string]*longhorn.Backup

expectError bool
expectErrorConditionMessage string
Expand Down Expand Up @@ -102,6 +103,15 @@ func (s *TestSuite) TestReconcileSystemBackup(c *C) {
},
},
},
existBackups: map[string]*longhorn.Backup{
"exists": {
Status: longhorn.BackupStatus{
State: longhorn.BackupStateCompleted,
SnapshotName: "exists",
VolumeName: TestVolumeName,
},
},
},
expectState: longhorn.SystemBackupStateBackingImageBackup,
expectNewVolumBackupCount: 0,
},
Expand All @@ -115,6 +125,15 @@ func (s *TestSuite) TestReconcileSystemBackup(c *C) {
},
},
},
existBackups: map[string]*longhorn.Backup{
"exists": {
Status: longhorn.BackupStatus{
State: longhorn.BackupStateCompleted,
SnapshotName: "exists",
VolumeName: TestVolumeName,
},
},
},
expectState: longhorn.SystemBackupStateBackingImageBackup,
expectNewVolumBackupCount: 1,
},
Expand Down Expand Up @@ -235,6 +254,7 @@ func (s *TestSuite) TestReconcileSystemBackup(c *C) {
fakeSystemRolloutStorageClassesDefault(c, informerFactories.KubeInformerFactory, kubeClient)

fakeSystemRolloutVolumes(tc.existVolumes, c, informerFactories.LhInformerFactory, lhClient)
fakeSystemRolloutBackups(tc.existBackups, c, informerFactories.LhInformerFactory, lhClient)
fakeSystemRolloutBackingImages(tc.existBackingImages, c, informerFactories.LhInformerFactory, lhClient)
fakeSystemRolloutPersistentVolumes(tc.existPersistentVolumes, c, informerFactories.KubeInformerFactory, kubeClient)

Expand All @@ -255,6 +275,17 @@ func (s *TestSuite) TestReconcileSystemBackup(c *C) {

switch systemBackup.Status.State {
case longhorn.SystemBackupStateVolumeBackup:
if tc.existBackups != nil {
existBackupSnap := &longhorn.Snapshot{
ObjectMeta: metav1.ObjectMeta{Name: "exists"},
Spec: longhorn.SnapshotSpec{Volume: TestVolumeName},
Status: longhorn.SnapshotStatus{
ReadyToUse: true,
CreationTime: metav1.Now().Time.Format(time.RFC3339),
},
}
fakeSystemRolloutSnapshot(existBackupSnap, c, informerFactories.LhInformerFactory, lhClient)
}
backups, _ := systemBackupController.BackupVolumes(systemBackup)

for _, backup := range backups {
Expand Down

0 comments on commit e6b40bc

Please sign in to comment.