mirror of
https://github.com/vmware-tanzu/velero.git
synced 2026-09-13 11:34:54 +00:00
Add in-place restore pre-flight check: PVC must be bound to the backed-up PV
An in-place restore onto a different volume than the one backed up is unsafe: an incremental (CBT) restore computes deltas against a different volume lineage, and even a full restore would patch and write into an unrelated volume. Verify the existing PVC is bound and still bound to the PV recorded at backup time before any side effect, on both the CSI data mover path (using the backed-up PVC's volume name) and the file system path (using the PVC-to-PV mapping from the backup volume info). The PV comparison is skipped for namespace-mapped restores, where the target PVC is necessarily bound to a different PV (the documented cross-namespace clone-and-restore workflow). Signed-off-by: chlins <chlins.zhang@gmail.com>
This commit is contained in:
@@ -333,7 +333,8 @@ func createPVObj(index int, withHostPath bool) *corev1api.PersistentVolume {
|
||||
}
|
||||
|
||||
func createPVCObj(index int) *corev1api.PersistentVolumeClaim {
|
||||
pvcObj := builder.ForPersistentVolumeClaim("fake-ns", fmt.Sprintf("fake-pvc-%d", index)).VolumeName(fmt.Sprintf("fake-pv-%d", index)).Result()
|
||||
pvcObj := builder.ForPersistentVolumeClaim("fake-ns", fmt.Sprintf("fake-pvc-%d", index)).VolumeName(fmt.Sprintf("fake-pv-%d", index)).
|
||||
Phase(corev1api.ClaimBound).Result()
|
||||
return pvcObj
|
||||
}
|
||||
|
||||
|
||||
@@ -49,6 +49,9 @@ type RestoreData struct {
|
||||
Pod *corev1api.Pod
|
||||
PodVolumeBackups []*velerov1api.PodVolumeBackup
|
||||
SourceNamespace, BackupLocation string
|
||||
// BackupVolumeInfos is the backup's volume info keyed by PV name, used by
|
||||
// the in-place restore pre-flight checks.
|
||||
BackupVolumeInfos map[string]volume.BackupVolumeInfo
|
||||
}
|
||||
|
||||
// Restorer can execute pod volume restores of volumes in a pod.
|
||||
@@ -186,6 +189,10 @@ func (r *restorer) RestorePodVolumes(data RestoreData, tracker *volume.RestoreVo
|
||||
// to write into, and they cannot write to it themselves until this
|
||||
// restore's PodVolumeRestores complete.
|
||||
if data.Restore.IsVolumeDataInplaceRestore() && pvc != nil {
|
||||
if err := inplace.CheckPVCBoundToBackedUpPV(pvc, backedUpPVName(data.BackupVolumeInfos, data.SourceNamespace, pvc.Name), data.SourceNamespace); err != nil {
|
||||
errs = append(errs, err)
|
||||
continue
|
||||
}
|
||||
if err := inplace.CheckPVCNotInUse(r.ctx, r.crClient, pvc, data.Restore.UID); err != nil {
|
||||
errs = append(errs, err)
|
||||
continue
|
||||
@@ -317,6 +324,17 @@ func newPodVolumeRestore(restore *velerov1api.Restore, pod *corev1api.Pod, backu
|
||||
return pvr
|
||||
}
|
||||
|
||||
// backedUpPVName returns the name of the PV the given source-namespace PVC was
|
||||
// bound to at backup time, or "" if unknown.
|
||||
func backedUpPVName(infos map[string]volume.BackupVolumeInfo, pvcNamespace, pvcName string) string {
|
||||
for pvName, info := range infos {
|
||||
if info.PVCNamespace == pvcNamespace && info.PVCName == pvcName {
|
||||
return pvName
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func getVolumesRepositoryType(volumes map[string]volumeBackupInfo) (string, error) {
|
||||
if len(volumes) == 0 {
|
||||
return "", errors.New("empty volume list")
|
||||
|
||||
@@ -197,6 +197,7 @@ func TestRestorePodVolumes(t *testing.T) {
|
||||
pvbs []*velerov1api.PodVolumeBackup
|
||||
restoredPod *corev1api.Pod
|
||||
sourceNamespace string
|
||||
volumeInfos map[string]volume.BackupVolumeInfo
|
||||
inplace bool
|
||||
errs []expectError
|
||||
}{
|
||||
@@ -413,6 +414,31 @@ func TestRestorePodVolumes(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "in-place restore blocked when the PVC is bound to a different PV than at backup time",
|
||||
pvbs: []*velerov1api.PodVolumeBackup{
|
||||
createPVBObj(true, true, 1, "kopia"),
|
||||
},
|
||||
inplace: true,
|
||||
kubeClientObj: []runtime.Object{
|
||||
createNodeAgentDaemonset(),
|
||||
createPVCObj(1),
|
||||
},
|
||||
ctlClientObj: []runtime.Object{
|
||||
createBackupRepoObj(),
|
||||
},
|
||||
restoredPod: createPodObj(true, true, true, 1),
|
||||
sourceNamespace: "fake-ns",
|
||||
bsl: "fake-bsl",
|
||||
volumeInfos: map[string]volume.BackupVolumeInfo{"some-other-pv": {PVCNamespace: "fake-ns", PVCName: "fake-pvc-1"}},
|
||||
runtimeScheme: scheme,
|
||||
errs: []expectError{
|
||||
{
|
||||
err: "in-place restore pre-flight check failed",
|
||||
prefixOnly: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "in-place restore proceeds when the PVC is only used by the gated restored pod",
|
||||
pvbs: []*velerov1api.PodVolumeBackup{
|
||||
@@ -481,11 +507,12 @@ func TestRestorePodVolumes(t *testing.T) {
|
||||
}()
|
||||
|
||||
errs := rs.RestorePodVolumes(RestoreData{
|
||||
Restore: restoreObj,
|
||||
Pod: test.restoredPod,
|
||||
PodVolumeBackups: test.pvbs,
|
||||
SourceNamespace: test.sourceNamespace,
|
||||
BackupLocation: test.bsl,
|
||||
Restore: restoreObj,
|
||||
Pod: test.restoredPod,
|
||||
PodVolumeBackups: test.pvbs,
|
||||
SourceNamespace: test.sourceNamespace,
|
||||
BackupLocation: test.bsl,
|
||||
BackupVolumeInfos: test.volumeInfos,
|
||||
}, volume.NewRestoreVolInfoTracker(restoreObj, logrus.New(), fakeCRClient))
|
||||
|
||||
if errs == nil {
|
||||
|
||||
Reference in New Issue
Block a user