Skip to content

Commit 8969edd

Browse files
committed
Fixed review comments
1 parent c5a76a8 commit 8969edd

4 files changed

Lines changed: 11 additions & 26 deletions

File tree

kubevirt-datamover-plugin/pvc/backup.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,7 @@ func (p *BackupPlugin) Execute(
9393

9494
p.Log.Infof("[pvc-backup] Processing PersistentVolumeClaim %s/%s", pvc.Namespace, pvc.Name)
9595
// Get VMs for this volume, if any
96-
crClient, err := clients.CRClient()
97-
if err != nil {
98-
return item, nil, "", nil, fmt.Errorf("failed to get controller-runtime client: %w", err)
99-
}
100-
pvcs, err := p.getPVCList(crClient, pvc.Namespace)
96+
pvcs, err := p.getPVCList(p.crClient, pvc.Namespace)
10197
if err != nil {
10298
return item, nil, "", nil, fmt.Errorf("failed to get PVC list: %w", err)
10399
}
@@ -110,7 +106,7 @@ func (p *BackupPlugin) Execute(
110106
vmNames := pvcs[pvc.Name]
111107
for _, vmName := range vmNames {
112108
vm := new(kvcore.VirtualMachine)
113-
err := crClient.Get(context.Background(), crclient.ObjectKey{Name: vmName, Namespace: pvc.Namespace}, vm)
109+
err := p.crClient.Get(context.Background(), crclient.ObjectKey{Name: vmName, Namespace: pvc.Namespace}, vm)
114110
if err != nil {
115111
return item, nil, "", nil, fmt.Errorf("failed to get VM %s: %w", vmName, err)
116112
}

kubevirt-datamover-plugin/pvc/backup_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,6 @@ func createTestPV(name string, pvc *corev1.PersistentVolumeClaim) *corev1.Persis
306306
}
307307
func getFakeClient() crclient.Client {
308308
scheme := runtime.NewScheme()
309-
//_ = velerov2alpha1.AddToScheme(scheme)
310309
_ = kvcore.AddToScheme(scheme)
311310
builder := fake.NewClientBuilder().
312311
WithScheme(scheme)

kubevirt-datamover-plugin/vm/backup.go

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func (p *BackupPlugin) Execute(
146146
p.Log.Infof("[vm-backup] Generated operation ID: %s", operationID)
147147

148148
// Get the first PVC with skip volume policy for SourcePVC (kubevirt datamover trigger)
149-
sourcePVC, err := p.getFirstKubevirtPVC(vm, backup)
149+
sourcePVC, err := p.getFirstKubevirtPVC(vm, backup, vh)
150150
if err != nil {
151151
return nil, nil, "", nil, fmt.Errorf("failed to get source PVC: %w", err)
152152
}
@@ -305,11 +305,8 @@ func CheckPreconditions(vm *kvcore.VirtualMachine, backup *velerov1.Backup, log
305305
}
306306

307307
// Check 4: Volume policy check
308-
// - At least one PVC must have "skip" action (triggers kubevirt datamover)
308+
// - At least one PVC must have "custom" action with "datamover=kubevirt" parameter (triggers kubevirt datamover)
309309
// - No PVC can have "snapshot" action (would conflict with kubevirt datamover)
310-
// TODO(https://github.com/migtools/kubevirt-datamover-plugin/issues/4): Once upstream
311-
// Velero changes are merged, update this to check for "custom" action type and inspect
312-
// the action parameters map to verify it's for kubevirt.
313310
hasKubevirtPolicy, hasConflictingPolicy, err := checkVolumePolicies(vm, backup, log, vh)
314311
if err != nil {
315312
return false, "", fmt.Errorf("failed to check volume policies: %w", err)
@@ -416,7 +413,7 @@ func checkVolumePolicies(vm *kvcore.VirtualMachine, backup *velerov1.Backup, log
416413
// Velero changes merge, update this to return the first PVC with "custom" action type
417414
// and kubevirt-specific parameter set. The volumehelper API will need to expose the
418415
// actual action type (not just shouldSnapshot boolean) for this check.
419-
func (p *BackupPlugin) getFirstKubevirtPVC(vm *kvcore.VirtualMachine, backup *velerov1.Backup) (*corev1.PersistentVolumeClaim, error) {
416+
func (p *BackupPlugin) getFirstKubevirtPVC(vm *kvcore.VirtualMachine, backup *velerov1.Backup, vh vhutil.VolumeHelper) (*corev1.PersistentVolumeClaim, error) {
420417
pvcNames := controllercommon.GetVolumesForVm(vm)
421418
if len(pvcNames) == 0 {
422419
return nil, fmt.Errorf("no PVCs found for VirtualMachine %s/%s", vm.Namespace, vm.Name)
@@ -427,11 +424,6 @@ func (p *BackupPlugin) getFirstKubevirtPVC(vm *kvcore.VirtualMachine, backup *ve
427424
return nil, fmt.Errorf("failed to get core client: %w", err)
428425
}
429426

430-
crClient, err := clients.CRClient()
431-
if err != nil {
432-
return nil, fmt.Errorf("failed to get controller-runtime client: %w", err)
433-
}
434-
435427
for _, pvcName := range pvcNames {
436428
pvc, err := coreClient.PersistentVolumeClaims(vm.Namespace).Get(context.Background(), pvcName, metav1.GetOptions{})
437429
if err != nil {
@@ -451,20 +443,18 @@ func (p *BackupPlugin) getFirstKubevirtPVC(vm *kvcore.VirtualMachine, backup *ve
451443
}
452444
pvcUnstructured := &unstructured.Unstructured{Object: pvcMap}
453445

454-
// Check if this PVC has snapshot policy
455-
shouldSnapshot, err := volumehelper.ShouldPerformSnapshotWithBackup(
446+
// Check if this PVC has custom kubevirt datamover policy
447+
shouldUseKubevirtDm, err := vh.ShouldPerformCustomAction(
456448
pvcUnstructured,
457449
kuberesource.PersistentVolumeClaims,
458-
*backup,
459-
crClient,
460-
p.Log,
450+
kubevirtCustomPolicy,
461451
)
462452
if err != nil {
463453
return nil, fmt.Errorf("failed to check volume policy for PVC %s: %w", pvcName, err)
464454
}
465455

466-
// Return first PVC without snapshot policy
467-
if !shouldSnapshot {
456+
// Return first PVC matching kubevirt dm custom policy action
457+
if shouldUseKubevirtDm {
468458
return pvc, nil
469459
}
470460
}

kubevirt-datamover-plugin/vm/backup_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ func TestBackupPlugin_checkPreconditions(t *testing.T) {
490490

491491
for _, tc := range testCases {
492492
t.Run(tc.name, func(t *testing.T) {
493-
vh, err := plugin.pluginPVCPodCache.GetOrCreateVolumeHelper(tc.backup, getFakeClient(), plugin.Log)
493+
vh, err := plugin.pluginPVCPodCache.GetOrCreateVolumeHelper(tc.backup, plugin.crClient, plugin.Log)
494494
require.NoError(t, err)
495495
eligible, reason, err := CheckPreconditions(tc.vm, tc.backup, plugin.Log, vh)
496496
require.NoError(t, err)

0 commit comments

Comments
 (0)