Skip to content

Commit

Permalink
fix(backup): renaming value from backup volume label
Browse files Browse the repository at this point in the history
And correct the way to get the BackupVolume CR.

ref: longhorn/longhorn 5411

Signed-off-by: James Lu <[email protected]>
  • Loading branch information
mantissahz committed Dec 18, 2024
1 parent be9c91b commit 6d836b4
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 20 deletions.
4 changes: 2 additions & 2 deletions api/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -1987,9 +1987,9 @@ func toBackupResource(b *longhorn.Backup) *Backup {
// This field is empty probably because the backup state is not Ready
// or the content of the backup config is empty.
if ret.VolumeName == "" {
backupVolumeName, ok := b.Labels[types.LonghornLabelBackupVolume]
volumeName, ok := b.Labels[types.LonghornLabelBackupVolume]
if ok {
ret.VolumeName = backupVolumeName
ret.VolumeName = volumeName
}
}
return ret
Expand Down
4 changes: 2 additions & 2 deletions controller/backup_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -652,11 +652,11 @@ func (bc *BackupController) getBackupTargetName(backup *longhorn.Backup) (string
}

func (bc *BackupController) getBackupVolumeName(backup *longhorn.Backup) (string, error) {
backupVolumeName, ok := backup.Labels[types.LonghornLabelBackupVolume]
volumeName, ok := backup.Labels[types.LonghornLabelBackupVolume]
if !ok {
return "", fmt.Errorf("cannot find the backup volume label")
}
return backupVolumeName, nil
return volumeName, nil
}

func (bc *BackupController) getEngineBinaryClient(volumeName string) (*engineapi.EngineBinary, error) {
Expand Down
4 changes: 2 additions & 2 deletions controller/uninstall_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ func (c *UninstallController) deleteReplicas(replicas map[string]*longhorn.Repli

// deleteLeftBackups deletes the backup having no backup volume
func (c *UninstallController) deleteLeftBackups(backup *longhorn.Backup) (err error) {
backupVolumeName, ok := backup.Labels[types.LonghornLabelBackupVolume]
volumeName, ok := backup.Labels[types.LonghornLabelBackupVolume]
if !ok {
// directly delete it if there is even no backup volume label
if err = c.ds.DeleteBackup(backup.Name); err != nil {
Expand All @@ -779,7 +779,7 @@ func (c *UninstallController) deleteLeftBackups(backup *longhorn.Backup) (err er
}
}
}
_, err = c.ds.GetBackupVolume(backupVolumeName)
_, err = c.ds.GetBackupVolumeByBackupTargetAndVolumeRO(backup.Status.BackupTargetName, volumeName)
if err != nil && apierrors.IsNotFound(err) {
if err = c.ds.DeleteBackup(backup.Name); err != nil {
if !apierrors.IsNotFound(err) {
Expand Down
3 changes: 3 additions & 0 deletions manager/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,9 @@ func (m *VolumeManager) ListBackupsForVolumeSorted(volumeName string) ([]*longho
func (m *VolumeManager) ListBackupsForBackupVolumeSorted(backupVolumeName string) ([]*longhorn.Backup, error) {
bv, err := m.ds.GetBackupVolumeRO(backupVolumeName)
if err != nil {
if apierrors.IsNotFound(err) {
return []*longhorn.Backup{}, nil
}
return []*longhorn.Backup{}, err
}

Expand Down
19 changes: 13 additions & 6 deletions manager/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"

"github.com/longhorn/backupstore"
"github.com/longhorn/longhorn-manager/datastore"
"github.com/longhorn/longhorn-manager/types"
"github.com/longhorn/longhorn-manager/util"
Expand All @@ -35,12 +36,18 @@ func (m *VolumeManager) PVCreate(name, pvName, fsType, secretNamespace, secretNa
pvName = v.Name
}

if storageClassName == "" {
if backupVolumeName := v.Labels[types.LonghornLabelBackupVolume]; backupVolumeName != "" {
backupVolume, _ := m.ds.GetBackupVolumeRO(backupVolumeName)
if backupVolume != nil {
storageClassName = backupVolume.Status.StorageClassName
}
if storageClassName == "" && v.Spec.FromBackup != "" {
bName, canonicalBVName, _, err := backupstore.DecodeBackupURL(v.Spec.FromBackup)
if err != nil {
return nil, errors.Wrapf(err, "failed to get backup and volume name from backup URL %v", v.Spec.FromBackup)
}
backup, err := m.ds.GetBackupRO(bName)
if err != nil {
return nil, errors.Wrapf(err, "failed to get backup %v", bName)
}
backupVolume, _ := m.ds.GetBackupVolumeByBackupTargetAndVolumeRO(backup.Status.BackupTargetName, canonicalBVName)
if backupVolume != nil {
storageClassName = backupVolume.Status.StorageClassName
}
}

Expand Down
16 changes: 8 additions & 8 deletions manager/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,29 +433,29 @@ func (m *VolumeManager) Activate(volumeName string, frontend string) (v *longhor

func (m *VolumeManager) triggerBackupVolumeToSync(volume *longhorn.Volume) error {
if volume.Spec.BackupTargetName == "" {
return errors.Errorf("cannot find the backup target label for volume: %v", volume.Name)
return errors.Errorf("failed to find the backup target label for volume: %v", volume.Name)
}
backupTargetName := volume.Spec.BackupTargetName

backupVolumeName, isExist := volume.Labels[types.LonghornLabelBackupVolume]
if !isExist || backupVolumeName == "" {
return errors.Errorf("cannot find the backup volume label for volume: %v", volume.Name)
volumeName, isExist := volume.Labels[types.LonghornLabelBackupVolume]
if !isExist || volumeName == "" {
return errors.Errorf("failed to find the backup volume label for volume: %v", volume.Name)
}

backupVolume, err := m.ds.GetBackupVolumeByBackupTargetAndVolume(backupTargetName, backupVolumeName)
backupVolume, err := m.ds.GetBackupVolumeByBackupTargetAndVolume(backupTargetName, volumeName)
if err != nil {
// The backup volume may be deleted already.
// hence it's better not to block the caller to continue the handlings like DR volume activation.
if apierrors.IsNotFound(err) {
logrus.Infof("Cannot find backup volume %v of backup target %s to trigger the sync-up, will skip it", backupVolumeName, backupTargetName)
logrus.Infof("failed to find backup volume %v of backup target %s to trigger the sync-up, will skip it", volumeName, backupTargetName)
return nil
}
return errors.Wrapf(err, "failed to get backup volume: %v", backupVolumeName)
return errors.Wrapf(err, "failed to get backup volume: %v", volumeName)
}
requestSyncTime := metav1.Time{Time: time.Now().UTC()}
backupVolume.Spec.SyncRequestedAt = requestSyncTime
if _, err = m.ds.UpdateBackupVolume(backupVolume); err != nil {
return errors.Wrapf(err, "failed to update backup volume: %v", backupVolumeName)
return errors.Wrapf(err, "failed to update backup volume: %v", backupVolume.Name)
}

return nil
Expand Down

0 comments on commit 6d836b4

Please sign in to comment.