From fc244cfab0901988df86d5849514b45efb9a3d27 Mon Sep 17 00:00:00 2001 From: James Lu Date: Mon, 13 Jan 2025 12:18:30 +0800 Subject: [PATCH] fix(backup): modify backup APIs for backup target and volume name Ensure backups have backup target name and volume name in the returning information of API `BackupList` and `BackupGet`. Backups in state `pending`, `in-progress`, and `error` will not have the backup target name and volume name in `b.Status`. ref: longhorn/longhorn 10182 Signed-off-by: James Lu (cherry picked from commit 7c50e4f35c6926ed085f7765389142d4c3453ace) --- api/model.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/api/model.go b/api/model.go index 73c278f224..b89361d700 100644 --- a/api/model.go +++ b/api/model.go @@ -1951,6 +1951,19 @@ func toBackupResource(b *longhorn.Backup) *Backup { return nil } + // For frontend, it will use the backup target name and volume name to tell this backup is belong to which backup volume. + // The backup not in `completed` state will not have the backup target name and volume name in `Status`. + backupTargetName := b.Status.BackupTargetName + volumeName := b.Status.VolumeName + if b.Labels != nil { + if backupTargetName == "" { + backupTargetName = b.Labels[types.LonghornLabelBackupTarget] + } + if volumeName == "" { + volumeName = b.Labels[types.LonghornLabelBackupVolume] + } + } + getSnapshotNameFromBackup := func(b *longhorn.Backup) string { if b.Spec.SnapshotName != "" { return b.Spec.SnapshotName @@ -1976,14 +1989,14 @@ func toBackupResource(b *longhorn.Backup) *Backup { Labels: b.Status.Labels, BackupMode: b.Spec.BackupMode, Messages: b.Status.Messages, - VolumeName: b.Status.VolumeName, + VolumeName: volumeName, VolumeSize: b.Status.VolumeSize, VolumeCreated: b.Status.VolumeCreated, VolumeBackingImageName: b.Status.VolumeBackingImageName, CompressionMethod: string(b.Status.CompressionMethod), NewlyUploadedDataSize: b.Status.NewlyUploadedDataSize, ReUploadedDataSize: b.Status.ReUploadedDataSize, - BackupTargetName: b.Status.BackupTargetName, + BackupTargetName: backupTargetName, } // Set the volume name from backup CR's label if it's empty. // This field is empty probably because the backup state is not Ready