Skip to content

Commit

Permalink
PV rename/remapClaimRefNS were being skipped when there was no snapshot
Browse files Browse the repository at this point in the history
Signed-off-by: Scott Seago <sseago@redhat.com>
(cherry picked from commit b845d23)
  • Loading branch information
sseago committed Sep 22, 2021
1 parent 6d2729e commit a5a9e76
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 0 deletions.
31 changes: 31 additions & 0 deletions pkg/restore/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -1102,6 +1102,37 @@ func (ctx *restoreContext) restoreItem(obj *unstructured.Unstructured, groupReso
default:
ctx.log.Infof("Restoring persistent volume as-is because it doesn't have a snapshot and its reclaim policy is not Delete.")

shouldRenamePV, err := shouldRenamePV(ctx, obj, resourceClient)
if err != nil {
errs.Add(namespace, err)
return warnings, errs
}
if shouldRenamePV {
oldName := obj.GetName()
pvName, err := ctx.pvRenamer(oldName)
if err != nil {
errs.Add(namespace, errors.Wrapf(err, "error renaming PV"))
return warnings, errs
}

ctx.renamedPVs[oldName] = pvName
obj.SetName(pvName)

// add the original PV name as an annotation
annotations := obj.GetAnnotations()
if annotations == nil {
annotations = map[string]string{}
}
annotations["velero.io/original-pv-name"] = oldName
obj.SetAnnotations(annotations)
}

// Check to see if the claimRef.namespace field needs to be remapped, and do so if necessary.
_, err = remapClaimRefNS(ctx, obj)
if err != nil {
errs.Add(namespace, err)
return warnings, errs
}
obj = resetVolumeBindingInfo(obj)
// We call the pvRestorer here to clear out the PV's claimRef.UID,
// so it can be re-claimed when its PVC is restored and gets a new UID.
Expand Down
95 changes: 95 additions & 0 deletions pkg/restore/restore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2191,6 +2191,101 @@ func TestRestorePersistentVolumes(t *testing.T) {
),
},
},
{
name: "when a PV without a snapshot is used by a PVC in a namespace that's being remapped, and the original PV exists in-cluster, the PV is renamed",
restore: defaultRestore().NamespaceMappings("source-ns", "target-ns").Result(),
backup: defaultBackup().Result(),
tarball: test.NewTarWriter(t).
AddItems(
"persistentvolumes",
builder.ForPersistentVolume("source-pv").
//ReclaimPolicy(corev1api.PersistentVolumeReclaimRetain).
AWSEBSVolumeID("source-volume").
ClaimRef("source-ns", "pvc-1").
Result(),
).
AddItems(
"persistentvolumeclaims",
builder.ForPersistentVolumeClaim("source-ns", "pvc-1").VolumeName("source-pv").Result(),
).
Done(),
apiResources: []*test.APIResource{
test.PVs(
builder.ForPersistentVolume("source-pv").
//ReclaimPolicy(corev1api.PersistentVolumeReclaimRetain).
AWSEBSVolumeID("source-volume").
ClaimRef("source-ns", "pvc-1").
Result(),
),
test.PVCs(),
},
want: []*test.APIResource{
test.PVs(
builder.ForPersistentVolume("source-pv").
AWSEBSVolumeID("source-volume").
ClaimRef("source-ns", "pvc-1").
Result(),
builder.ForPersistentVolume("renamed-source-pv").
ObjectMeta(
builder.WithAnnotations("velero.io/original-pv-name", "source-pv"),
builder.WithLabels("velero.io/backup-name", "backup-1", "velero.io/restore-name", "restore-1"),
// the namespace for this PV's claimRef should be the one that the PVC was remapped into.
).ClaimRef("target-ns", "pvc-1").
AWSEBSVolumeID("source-volume").
Result(),
),
test.PVCs(
builder.ForPersistentVolumeClaim("target-ns", "pvc-1").
ObjectMeta(
builder.WithLabels("velero.io/backup-name", "backup-1", "velero.io/restore-name", "restore-1"),
).
VolumeName("renamed-source-pv").
Result(),
),
},
},
{
name: "when a PV without a snapshot is used by a PVC in a namespace that's being remapped, and the original PV does not exist in-cluster, the PV is not renamed",
restore: defaultRestore().NamespaceMappings("source-ns", "target-ns").Result(),
backup: defaultBackup().Result(),
tarball: test.NewTarWriter(t).
AddItems(
"persistentvolumes",
builder.ForPersistentVolume("source-pv").
AWSEBSVolumeID("source-volume").
ClaimRef("source-ns", "pvc-1").
Result(),
).
AddItems(
"persistentvolumeclaims",
builder.ForPersistentVolumeClaim("source-ns", "pvc-1").VolumeName("source-pv").Result(),
).
Done(),
apiResources: []*test.APIResource{
test.PVs(),
test.PVCs(),
},
want: []*test.APIResource{
test.PVs(
builder.ForPersistentVolume("source-pv").
//ReclaimPolicy(corev1api.PersistentVolumeReclaimRetain).
ObjectMeta(
builder.WithLabels("velero.io/backup-name", "backup-1", "velero.io/restore-name", "restore-1"),
).
ClaimRef("target-ns", "pvc-1").
AWSEBSVolumeID("source-volume").
Result(),
),
test.PVCs(
builder.ForPersistentVolumeClaim("target-ns", "pvc-1").
ObjectMeta(
builder.WithLabels("velero.io/backup-name", "backup-1", "velero.io/restore-name", "restore-1"),
).
VolumeName("source-pv").
Result(),
),
},
},
{
name: "when a PV is renamed and the original PV does not exist in-cluster, the PV should be renamed",
restore: defaultRestore().NamespaceMappings("source-ns", "target-ns").Result(),
Expand Down

0 comments on commit a5a9e76

Please sign in to comment.