Skip to content

Commit

Permalink
When restorePVs is false, CSI should restore the PVC.
Browse files Browse the repository at this point in the history
The PVC's data source and referenced volume should all be cleaned for restore to let k8s to create a new PV and volume.

Signed-off-by: Xun Jiang <[email protected]>
  • Loading branch information
Xun Jiang committed Apr 13, 2023
1 parent 43080a7 commit 2ec5bf3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 26 deletions.
1 change: 1 addition & 0 deletions changelogs/unreleased/156-blackpiglet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
When restorePVs is false, CSI should restore the PVC.
55 changes: 29 additions & 26 deletions internal/restore/pvc_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand All @@ -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 {
Expand Down

0 comments on commit 2ec5bf3

Please sign in to comment.