Skip to content

Commit ddb3e3d

Browse files
authored
Merge pull request velero-io#9092 from blackpiglet/8116_fix
Avoid checking the VS and VSC status in the backup finalizing phase.
2 parents 768c342 + a61a073 commit ddb3e3d

5 files changed

Lines changed: 52 additions & 24 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Avoid checking the VS and VSC status in the backup finalizing phase.

pkg/backup/actions/csi/volumesnapshot_action.go

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,16 @@ func (p *volumeSnapshotBackupItemAction) Execute(
9595
)
9696
}
9797

98+
if backup.Status.Phase == velerov1api.BackupPhaseFinalizing ||
99+
backup.Status.Phase == velerov1api.BackupPhaseFinalizingPartiallyFailed {
100+
p.log.
101+
WithField("Backup", fmt.Sprintf("%s/%s", backup.Namespace, backup.Name)).
102+
WithField("BackupPhase", backup.Status.Phase).Debugf("Cleaning VolumeSnapshots.")
103+
104+
csi.DeleteReadyVolumeSnapshot(*vs, p.crClient, p.log)
105+
return item, nil, "", nil, nil
106+
}
107+
98108
p.log.Infof("Getting VolumesnapshotContent for Volumesnapshot %s/%s",
99109
vs.Namespace, vs.Name)
100110

@@ -109,20 +119,6 @@ func (p *volumeSnapshotBackupItemAction) Execute(
109119
return nil, nil, "", nil, errors.WithStack(err)
110120
}
111121

112-
if backup.Status.Phase == velerov1api.BackupPhaseFinalizing ||
113-
backup.Status.Phase == velerov1api.BackupPhaseFinalizingPartiallyFailed {
114-
p.log.
115-
WithField("Backup", fmt.Sprintf("%s/%s", backup.Namespace, backup.Name)).
116-
WithField("BackupPhase", backup.Status.Phase).Debugf("Cleaning VolumeSnapshots.")
117-
118-
if vsc == nil {
119-
vsc = &snapshotv1api.VolumeSnapshotContent{}
120-
}
121-
122-
csi.DeleteReadyVolumeSnapshot(*vs, *vsc, p.crClient, p.log)
123-
return item, nil, "", nil, nil
124-
}
125-
126122
annotations := make(map[string]string)
127123

128124
if vsc != nil {

pkg/backup/actions/csi/volumesnapshot_action_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,34 @@ func TestVSExecute(t *testing.T) {
118118
},
119119
},
120120
},
121+
{
122+
name: "Backup in finalizing phase - skip VSC lookup",
123+
backup: builder.ForBackup("velero", "backup").
124+
Phase(velerov1api.BackupPhaseFinalizing).Result(),
125+
vs: builder.ForVolumeSnapshot("velero", "vs").
126+
ObjectMeta(builder.WithLabels(
127+
velerov1api.BackupNameLabel, "backup")).
128+
Status().
129+
BoundVolumeSnapshotContentName("vsc").Result(),
130+
vsc: nil, // VSC won't be created/fetched
131+
expectedErr: "",
132+
expectedAdditionalItems: nil,
133+
expectedItemToUpdate: nil,
134+
},
135+
{
136+
name: "Backup in finalizing partially failed phase - skip VSC lookup",
137+
backup: builder.ForBackup("velero", "backup").
138+
Phase(velerov1api.BackupPhaseFinalizingPartiallyFailed).Result(),
139+
vs: builder.ForVolumeSnapshot("velero", "vs").
140+
ObjectMeta(builder.WithLabels(
141+
velerov1api.BackupNameLabel, "backup")).
142+
Status().
143+
BoundVolumeSnapshotContentName("vsc").Result(),
144+
vsc: nil, // VSC won't be created/fetched
145+
expectedErr: "",
146+
expectedAdditionalItems: nil,
147+
expectedItemToUpdate: nil,
148+
},
121149
}
122150

123151
for _, tc := range tests {

pkg/util/csi/volume_snapshot.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -489,16 +489,16 @@ func SetVolumeSnapshotContentDeletionPolicy(
489489
vscName string,
490490
crClient crclient.Client,
491491
policy snapshotv1api.DeletionPolicy,
492-
) error {
492+
) (*snapshotv1api.VolumeSnapshotContent, error) {
493493
vsc := new(snapshotv1api.VolumeSnapshotContent)
494494
if err := crClient.Get(context.TODO(), crclient.ObjectKey{Name: vscName}, vsc); err != nil {
495-
return err
495+
return nil, err
496496
}
497497

498498
originVSC := vsc.DeepCopy()
499499
vsc.Spec.DeletionPolicy = policy
500500

501-
return crClient.Patch(context.TODO(), vsc, crclient.MergeFrom(originVSC))
501+
return vsc, crClient.Patch(context.TODO(), vsc, crclient.MergeFrom(originVSC))
502502
}
503503

504504
// CleanupVolumeSnapshot deletes the VolumeSnapshot and the associated VolumeSnapshotContent. It will make sure the
@@ -523,7 +523,7 @@ func CleanupVolumeSnapshot(
523523
if vs.Status != nil && vs.Status.BoundVolumeSnapshotContentName != nil {
524524
// we patch the DeletionPolicy of the VolumeSnapshotContent to set it to Delete.
525525
// This ensures that the volume snapshot in the storage provider is also deleted.
526-
err := SetVolumeSnapshotContentDeletionPolicy(
526+
_, err := SetVolumeSnapshotContentDeletionPolicy(
527527
*vs.Status.BoundVolumeSnapshotContentName,
528528
crClient,
529529
snapshotv1api.VolumeSnapshotContentDelete,
@@ -544,7 +544,6 @@ func CleanupVolumeSnapshot(
544544

545545
func DeleteReadyVolumeSnapshot(
546546
vs snapshotv1api.VolumeSnapshot,
547-
vsc snapshotv1api.VolumeSnapshotContent,
548547
client crclient.Client,
549548
logger logrus.FieldLogger,
550549
) {
@@ -557,11 +556,15 @@ func DeleteReadyVolumeSnapshot(
557556
return
558557
}
559558

559+
var vsc *snapshotv1api.VolumeSnapshotContent
560+
560561
if vs.Status != nil && vs.Status.BoundVolumeSnapshotContentName != nil {
562+
var err error
563+
561564
// Patch the DeletionPolicy of the VolumeSnapshotContent to set it to Retain.
562565
// This ensures that the volume snapshot in the storage provider is kept.
563-
if err := SetVolumeSnapshotContentDeletionPolicy(
564-
vsc.Name,
566+
if vsc, err = SetVolumeSnapshotContentDeletionPolicy(
567+
*vs.Status.BoundVolumeSnapshotContentName,
565568
client,
566569
snapshotv1api.VolumeSnapshotContentRetain,
567570
); err != nil {
@@ -570,7 +573,7 @@ func DeleteReadyVolumeSnapshot(
570573
return
571574
}
572575

573-
if err := client.Delete(context.TODO(), &vsc); err != nil {
576+
if err := client.Delete(context.TODO(), vsc); err != nil {
574577
logger.WithError(err).Warnf("Failed to delete the VolumeSnapshotContent %s", vsc.Name)
575578
}
576579
}

pkg/util/csi/volume_snapshot_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1439,7 +1439,7 @@ func TestSetVolumeSnapshotContentDeletionPolicy(t *testing.T) {
14391439
for _, tc := range testCases {
14401440
t.Run(tc.name, func(t *testing.T) {
14411441
fakeClient := velerotest.NewFakeControllerRuntimeClient(t, tc.objs...)
1442-
err := SetVolumeSnapshotContentDeletionPolicy(tc.inputVSCName, fakeClient, tc.policy)
1442+
_, err := SetVolumeSnapshotContentDeletionPolicy(tc.inputVSCName, fakeClient, tc.policy)
14431443
if tc.expectError {
14441444
assert.Error(t, err)
14451445
} else {
@@ -1496,7 +1496,7 @@ func TestDeleteVolumeSnapshots(t *testing.T) {
14961496
)
14971497
logger := logging.DefaultLogger(logrus.DebugLevel, logging.FormatText)
14981498

1499-
DeleteReadyVolumeSnapshot(tc.vs, tc.vsc, client, logger)
1499+
DeleteReadyVolumeSnapshot(tc.vs, client, logger)
15001500

15011501
vsList := new(snapshotv1api.VolumeSnapshotList)
15021502
err := client.List(

0 commit comments

Comments
 (0)