diff --git a/changelogs/unreleased/156-blackpiglet b/changelogs/unreleased/156-blackpiglet new file mode 100644 index 00000000..a91ada0a --- /dev/null +++ b/changelogs/unreleased/156-blackpiglet @@ -0,0 +1 @@ +When restorePVs is false, CSI should restore the PVC. \ No newline at end of file diff --git a/internal/restore/pvc_action.go b/internal/restore/pvc_action.go index 9d65d256..81f1f757 100644 --- a/internal/restore/pvc_action.go +++ b/internal/restore/pvc_action.go @@ -103,10 +103,6 @@ func (p *PVCRestoreItemAction) Execute(input *velero.RestoreItemActionExecuteInp return nil, errors.WithStack(err) } p.Log.Infof("Starting PVCRestoreItemAction for PVC %s/%s", pvc.Namespace, pvc.Name) - if boolptr.IsSetToFalse(input.Restore.Spec.RestorePVs) { - p.Log.Infof("Restore did not request for PVs to be restored %s/%s", input.Restore.Namespace, input.Restore.Name) - return &velero.RestoreItemActionExecuteOutput{SkipRestore: true}, nil - } removePVCAnnotations(&pvc, []string{AnnBindCompleted, AnnBoundByController, AnnStorageProvisioner, AnnSelectedNode}) @@ -125,32 +121,39 @@ func (p *PVCRestoreItemAction) Execute(input *velero.RestoreItemActionExecuteInp }, nil } - _, snapClient, err := util.GetClients() - if err != nil { - return nil, errors.WithStack(err) - } - - vs, err := snapClient.SnapshotV1().VolumeSnapshots(pvc.Namespace).Get(context.TODO(), volumeSnapshotName, metav1.GetOptions{}) - if err != nil { - return nil, errors.Wrapf(err, fmt.Sprintf("Failed to get Volumesnapshot %s/%s to restore PVC %s/%s", pvc.Namespace, volumeSnapshotName, pvc.Namespace, pvc.Name)) - } + if boolptr.IsSetToFalse(input.Restore.Spec.RestorePVs) { + p.Log.Infof("Restore did not request for PVs to be restored from snapshot %s/%s.", input.Restore.Namespace, input.Restore.Name) + pvc.Spec.VolumeName = "" + pvc.Spec.DataSource = nil + pvc.Spec.DataSourceRef = nil + } else { + _, snapClient, err := util.GetClients() + if err != nil { + return nil, errors.WithStack(err) + } - if _, exists := vs.Annotations[util.VolumeSnapshotRestoreSize]; exists { - restoreSize, err := resource.ParseQuantity(vs.Annotations[util.VolumeSnapshotRestoreSize]) + vs, err := snapClient.SnapshotV1().VolumeSnapshots(pvc.Namespace).Get(context.TODO(), volumeSnapshotName, metav1.GetOptions{}) if err != nil { - return nil, errors.Wrapf(err, fmt.Sprintf("Failed to parse %s from annotation on Volumesnapshot %s/%s into restore size", - vs.Annotations[util.VolumeSnapshotRestoreSize], vs.Namespace, vs.Name)) + return nil, errors.Wrapf(err, fmt.Sprintf("Failed to get Volumesnapshot %s/%s to restore PVC %s/%s", pvc.Namespace, volumeSnapshotName, pvc.Namespace, pvc.Name)) } - // It is possible that the volume provider allocated a larger capacity volume than what was requested in the backed up PVC. - // In this scenario the volumesnapshot of the PVC will endup being larger than its requested storage size. - // Such a PVC, on restore as-is, will be stuck attempting to use a Volumesnapshot as a data source for a PVC that - // is not large enough. - // To counter that, here we set the storage request on the PVC to the larger of the PVC's storage request and the size of the - // VolumeSnapshot - setPVCStorageResourceRequest(&pvc, restoreSize, p.Log) - } - resetPVCSpec(&pvc, volumeSnapshotName) + if _, exists := vs.Annotations[util.VolumeSnapshotRestoreSize]; exists { + restoreSize, err := resource.ParseQuantity(vs.Annotations[util.VolumeSnapshotRestoreSize]) + if err != nil { + return nil, errors.Wrapf(err, fmt.Sprintf("Failed to parse %s from annotation on Volumesnapshot %s/%s into restore size", + vs.Annotations[util.VolumeSnapshotRestoreSize], vs.Namespace, vs.Name)) + } + // It is possible that the volume provider allocated a larger capacity volume than what was requested in the backed up PVC. + // In this scenario the volumesnapshot of the PVC will endup being larger than its requested storage size. + // Such a PVC, on restore as-is, will be stuck attempting to use a Volumesnapshot as a data source for a PVC that + // is not large enough. + // To counter that, here we set the storage request on the PVC to the larger of the PVC's storage request and the size of the + // VolumeSnapshot + setPVCStorageResourceRequest(&pvc, restoreSize, p.Log) + } + + resetPVCSpec(&pvc, volumeSnapshotName) + } pvcMap, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&pvc) if err != nil {