From 226370d0352348a12a06014321207e900d7b90e6 Mon Sep 17 00:00:00 2001 From: Xun Jiang Date: Wed, 27 Nov 2024 18:45:03 +0800 Subject: [PATCH] Add nil check for updating DataUpload VolumeInfo in finalizing phase. Signed-off-by: Xun Jiang --- changelogs/unreleased/8471-blackpiglet | 1 + pkg/backup/backup.go | 3 ++- pkg/backup/backup_test.go | 36 ++++++++++++++++++++++++-- 3 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 changelogs/unreleased/8471-blackpiglet diff --git a/changelogs/unreleased/8471-blackpiglet b/changelogs/unreleased/8471-blackpiglet new file mode 100644 index 0000000000..0245025ffc --- /dev/null +++ b/changelogs/unreleased/8471-blackpiglet @@ -0,0 +1 @@ +Add nil check for updating DataUpload VolumeInfo in finalizing phase diff --git a/pkg/backup/backup.go b/pkg/backup/backup.go index 18a1f181e9..25d6ba3544 100644 --- a/pkg/backup/backup.go +++ b/pkg/backup/backup.go @@ -1059,7 +1059,8 @@ func updateVolumeInfos( for index := range volumeInfos { if volumeInfos[index].PVCName == dataUpload.Spec.SourcePVC && - volumeInfos[index].PVCNamespace == dataUpload.Spec.SourceNamespace { + volumeInfos[index].PVCNamespace == dataUpload.Spec.SourceNamespace && + volumeInfos[index].SnapshotDataMovementInfo != nil { if dataUpload.Status.CompletionTimestamp != nil { volumeInfos[index].CompletionTimestamp = dataUpload.Status.CompletionTimestamp } diff --git a/pkg/backup/backup_test.go b/pkg/backup/backup_test.go index 78ba0b0cc9..de22901de3 100644 --- a/pkg/backup/backup_test.go +++ b/pkg/backup/backup_test.go @@ -5510,6 +5510,36 @@ func TestUpdateVolumeInfos(t *testing.T) { }, }, }, + { + // This is an error case. No crash happen here is good enough. + name: "VolumeInfo doesn't have SnapshotDataMovementInfo when there is a matching DataUpload", + operations: []*itemoperation.BackupOperation{}, + dataUpload: builder.ForDataUpload("velero", "du-1"). + CompletionTimestamp(&now). + CSISnapshot(&velerov2alpha1.CSISnapshotSpec{VolumeSnapshot: "vs-1"}). + SnapshotID("snapshot-id"). + Progress(shared.DataMoveOperationProgress{TotalBytes: 1000}). + Phase(velerov2alpha1.DataUploadPhaseCompleted). + SourceNamespace("ns-1"). + SourcePVC("pvc-1"). + Result(), + volumeInfos: []*volume.BackupVolumeInfo{ + { + PVCName: "pvc-1", + PVCNamespace: "ns-1", + CompletionTimestamp: &metav1.Time{}, + SnapshotDataMovementInfo: nil, + }, + }, + expectedVolumeInfos: []*volume.BackupVolumeInfo{ + { + PVCName: "pvc-1", + PVCNamespace: "ns-1", + CompletionTimestamp: &metav1.Time{}, + SnapshotDataMovementInfo: nil, + }, + }, + }, } for _, tc := range tests { @@ -5526,8 +5556,10 @@ func TestUpdateVolumeInfos(t *testing.T) { } require.NoError(t, updateVolumeInfos(tc.volumeInfos, unstructures, tc.operations, logger)) - require.Equal(t, tc.expectedVolumeInfos[0].CompletionTimestamp, tc.volumeInfos[0].CompletionTimestamp) - require.Equal(t, tc.expectedVolumeInfos[0].SnapshotDataMovementInfo, tc.volumeInfos[0].SnapshotDataMovementInfo) + if len(tc.expectedVolumeInfos) > 0 { + require.Equal(t, tc.expectedVolumeInfos[0].CompletionTimestamp, tc.volumeInfos[0].CompletionTimestamp) + require.Equal(t, tc.expectedVolumeInfos[0].SnapshotDataMovementInfo, tc.volumeInfos[0].SnapshotDataMovementInfo) + } }) } }