diff --git a/pkg/exposer/csi_snapshot.go b/pkg/exposer/csi_snapshot.go index 637d0805d..fac00732c 100644 --- a/pkg/exposer/csi_snapshot.go +++ b/pkg/exposer/csi_snapshot.go @@ -118,41 +118,6 @@ func (e *csiSnapshotExposer) Expose(ctx context.Context, ownerObject corev1.Obje curLog.WithField("vsc name", vsc.Name).WithField("vs name", volumeSnapshot.Name).Infof("Got VSC from VS in namespace %s", volumeSnapshot.Namespace) - retained, err := csi.RetainVSC(ctx, e.csiSnapshotClient, vsc) - if err != nil { - return errors.Wrap(err, "error to retain volume snapshot content") - } - - curLog.WithField("vsc name", vsc.Name).WithField("retained", (retained != nil)).Info("Finished to retain VSC") - - defer func() { - if retained != nil { - csi.DeleteVolumeSnapshotContentIfAny(ctx, e.csiSnapshotClient, retained.Name, curLog) - } - }() - - err = csi.EnsureDeleteVS(ctx, e.csiSnapshotClient, volumeSnapshot.Name, volumeSnapshot.Namespace, csiExposeParam.OperationTimeout) - if err != nil { - return errors.Wrap(err, "error to delete volume snapshot") - } - - curLog.WithField("vs name", volumeSnapshot.Name).Infof("VS is deleted in namespace %s", volumeSnapshot.Namespace) - - err = csi.RemoveVSCProtect(ctx, e.csiSnapshotClient, vsc.Name, csiExposeParam.ExposeTimeout) - if err != nil { - return errors.Wrap(err, "error to remove protect from volume snapshot content") - } - - curLog.WithField("vsc name", vsc.Name).Infof("Removed protect from VSC") - - err = csi.EnsureDeleteVSC(ctx, e.csiSnapshotClient, vsc.Name, csiExposeParam.OperationTimeout) - if err != nil { - return errors.Wrap(err, "error to delete volume snapshot content") - } - - curLog.WithField("vsc name", vsc.Name).Infof("VSC is deleted") - retained = nil - backupVS, err := e.createBackupVS(ctx, ownerObject, volumeSnapshot) if err != nil { return errors.Wrap(err, "error to create backup volume snapshot") @@ -173,6 +138,27 @@ func (e *csiSnapshotExposer) Expose(ctx context.Context, ownerObject corev1.Obje curLog.WithField("vsc name", backupVSC.Name).Infof("Backup VSC is created from %s", vsc.Name) + retained, err := csi.RetainVSC(ctx, e.csiSnapshotClient, vsc) + if err != nil { + return errors.Wrap(err, "error to retain volume snapshot content") + } + + curLog.WithField("vsc name", vsc.Name).WithField("retained", (retained != nil)).Info("Finished to retain VSC") + + err = csi.EnsureDeleteVS(ctx, e.csiSnapshotClient, volumeSnapshot.Name, volumeSnapshot.Namespace, csiExposeParam.OperationTimeout) + if err != nil { + return errors.Wrap(err, "error to delete volume snapshot") + } + + curLog.WithField("vs name", volumeSnapshot.Name).Infof("VS is deleted in namespace %s", volumeSnapshot.Namespace) + + err = csi.EnsureDeleteVSC(ctx, e.csiSnapshotClient, vsc.Name, csiExposeParam.OperationTimeout) + if err != nil { + return errors.Wrap(err, "error to delete volume snapshot content") + } + + curLog.WithField("vsc name", vsc.Name).Infof("VSC is deleted") + var volumeSize resource.Quantity if volumeSnapshot.Status.RestoreSize != nil && !volumeSnapshot.Status.RestoreSize.IsZero() { volumeSize = *volumeSnapshot.Status.RestoreSize diff --git a/pkg/util/csi/volume_snapshot.go b/pkg/util/csi/volume_snapshot.go index a9a88fd32..9cf195144 100644 --- a/pkg/util/csi/volume_snapshot.go +++ b/pkg/util/csi/volume_snapshot.go @@ -99,7 +99,7 @@ func GetVolumeSnapshotContentForVolumeSnapshot(volSnap *snapshotv1api.VolumeSnap return vsc, nil } -// RetainVSC updates the VSC's deletion policy to Retain and add a finalier and then return the update VSC +// RetainVSC updates the VSC's deletion policy to Retain and then return the update VSC func RetainVSC(ctx context.Context, snapshotClient snapshotter.SnapshotV1Interface, vsc *snapshotv1api.VolumeSnapshotContent) (*snapshotv1api.VolumeSnapshotContent, error) { if vsc.Spec.DeletionPolicy == snapshotv1api.VolumeSnapshotContentRetain { @@ -108,7 +108,6 @@ func RetainVSC(ctx context.Context, snapshotClient snapshotter.SnapshotV1Interfa return patchVSC(ctx, snapshotClient, vsc, func(updated *snapshotv1api.VolumeSnapshotContent) { updated.Spec.DeletionPolicy = snapshotv1api.VolumeSnapshotContentRetain - updated.Finalizers = append(updated.Finalizers, volumeSnapshotContentProtectFinalizer) }) }