Skip to content

Commit fd99ed4

Browse files
authored
Merge pull request velero-io#9696 from adam-jian-zhang/fix-restore-pvr-scope-1.18
Fix PodVolumeBackup list scope during restore
2 parents 5ad4e60 + 0291c53 commit fd99ed4

5 files changed

Lines changed: 58 additions & 2 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix issue #9681, fix restores and podvolumerestores list options to only list in installed namespace

pkg/controller/restore_controller.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,7 @@ func (r *restoreReconciler) runValidatedRestore(restore *api.Restore, info backu
529529
LabelSelector: labels.Set(map[string]string{
530530
api.BackupNameLabel: label.GetValidName(restore.Spec.BackupName),
531531
}).AsSelector(),
532+
Namespace: restore.Namespace,
532533
}
533534

534535
podVolumeBackupList := &api.PodVolumeBackupList{}

pkg/controller/restore_controller_test.go

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,8 @@ func TestRestoreReconcile(t *testing.T) {
238238
expectedFinalPhase string
239239
addValidFinalizer bool
240240
emptyVolumeInfo bool
241+
podVolumeBackups []*velerov1api.PodVolumeBackup
242+
expectedPVBCount int
241243
}{
242244
{
243245
name: "restore with both namespace in both includedNamespaces and excludedNamespaces fails validation",
@@ -357,6 +359,22 @@ func TestRestoreReconcile(t *testing.T) {
357359
expectedCompletedTime: &timestamp,
358360
expectedRestorerCall: NewRestore("foo", "bar", "backup-1", "ns-1", "", velerov1api.RestorePhaseInProgress).Result(),
359361
},
362+
{
363+
name: "valid restore gets executed and only includes pod volume backups from restore namespace",
364+
location: defaultStorageLocation,
365+
restore: NewRestore("foo", "bar2", "backup-1", "ns-1", "", velerov1api.RestorePhaseNew).Result(),
366+
backup: defaultBackup().StorageLocation("default").Result(),
367+
podVolumeBackups: []*velerov1api.PodVolumeBackup{
368+
builder.ForPodVolumeBackup("foo", "pvb-1").ObjectMeta(builder.WithLabels(velerov1api.BackupNameLabel, "backup-1")).Result(),
369+
builder.ForPodVolumeBackup("other-ns", "pvb-2").ObjectMeta(builder.WithLabels(velerov1api.BackupNameLabel, "backup-1")).Result(),
370+
},
371+
expectedPVBCount: 1,
372+
expectedErr: false,
373+
expectedPhase: string(velerov1api.RestorePhaseInProgress),
374+
expectedStartTime: &timestamp,
375+
expectedCompletedTime: &timestamp,
376+
expectedRestorerCall: NewRestore("foo", "bar2", "backup-1", "ns-1", "", velerov1api.RestorePhaseInProgress).Result(),
377+
},
360378
{
361379
name: "restoration of nodes is not supported",
362380
location: defaultStorageLocation,
@@ -501,6 +519,13 @@ func TestRestoreReconcile(t *testing.T) {
501519
defaultStorageLocation.ObjectMeta.ResourceVersion = ""
502520
}()
503521

522+
if test.podVolumeBackups != nil {
523+
for _, pvb := range test.podVolumeBackups {
524+
err := fakeClient.Create(t.Context(), pvb)
525+
require.NoError(t, err)
526+
}
527+
}
528+
504529
r := NewRestoreReconciler(
505530
t.Context(),
506531
velerov1api.DefaultNamespace,
@@ -670,6 +695,10 @@ func TestRestoreReconcile(t *testing.T) {
670695
// the mock stores the pointer, which gets modified after
671696
assert.Equal(t, test.expectedRestorerCall.Spec, restorer.calledWithArg.Spec)
672697
assert.Equal(t, test.expectedRestorerCall.Status.Phase, restorer.calledWithArg.Status.Phase)
698+
699+
if test.podVolumeBackups != nil {
700+
assert.Len(t, restorer.calledWithPVBs, test.expectedPVBCount)
701+
}
673702
})
674703
}
675704
}
@@ -1021,8 +1050,9 @@ func NewRestore(ns, name, backup, includeNS, includeResource string, phase veler
10211050

10221051
type fakeRestorer struct {
10231052
mock.Mock
1024-
calledWithArg velerov1api.Restore
1025-
kbClient client.Client
1053+
calledWithArg velerov1api.Restore
1054+
calledWithPVBs []*velerov1api.PodVolumeBackup
1055+
kbClient client.Client
10261056
}
10271057

10281058
func (r *fakeRestorer) Restore(
@@ -1045,6 +1075,7 @@ func (r *fakeRestorer) RestoreWithResolvers(req *pkgrestore.Request,
10451075
r.kbClient, volumeSnapshotterGetter)
10461076

10471077
r.calledWithArg = *req.Restore
1078+
r.calledWithPVBs = req.PodVolumeBackups
10481079

10491080
return res.Get(0).(results.Result), res.Get(1).(results.Result)
10501081
}

pkg/restore/actions/pod_volume_restore_action.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ func (a *PodVolumeRestoreAction) Execute(input *velero.RestoreItemActionExecuteI
101101

102102
opts := &ctrlclient.ListOptions{
103103
LabelSelector: label.NewSelectorForBackup(input.Restore.Spec.BackupName),
104+
Namespace: input.Restore.Namespace,
104105
}
105106
podVolumeBackupList := new(velerov1api.PodVolumeBackupList)
106107
if err := a.crClient.List(context.TODO(), podVolumeBackupList, opts); err != nil {

pkg/restore/actions/pod_volume_restore_action_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,28 @@ func TestPodVolumeRestoreActionExecute(t *testing.T) {
350350
VolumeMounts(builder.ForVolumeMount("myvol", "/restores/myvol").Result()).
351351
Command([]string{"/velero-restore-helper"}).Result()).Result(),
352352
},
353+
{
354+
name: "pod volume backups in a different namespace are ignored when looking for matches due to namespace scoping",
355+
pod: builder.ForPod("ns-1", "my-pod").
356+
Volumes(
357+
builder.ForVolume("myvol").PersistentVolumeClaimSource("pvc-1").Result(),
358+
).
359+
Result(),
360+
podVolumeBackups: []runtime.Object{
361+
builder.ForPodVolumeBackup("other-ns", "pvb-1").
362+
PodName("my-pod").
363+
PodNamespace("ns-1").
364+
Volume("myvol").
365+
ObjectMeta(builder.WithLabels(velerov1api.BackupNameLabel, backupName)).
366+
SnapshotID("foo").
367+
Result(),
368+
},
369+
want: builder.ForPod("ns-1", "my-pod").
370+
Volumes(
371+
builder.ForVolume("myvol").PersistentVolumeClaimSource("pvc-1").Result(),
372+
).
373+
Result(),
374+
},
353375
}
354376

355377
veleroDeployment := &appsv1api.Deployment{

0 commit comments

Comments
 (0)