Skip to content

Commit

Permalink
virt-handler: Refactor claimDeviceOwnership method
Browse files Browse the repository at this point in the history
Pass to the method the isolation data it needs to avoid re-calculating
it multiple times in the setup flow.

Signed-off-by: Edward Haas <edwardh@redhat.com>
  • Loading branch information
EdDev committed Nov 14, 2021
1 parent aaa7286 commit f6e8bd4
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions pkg/virt-handler/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,8 @@ func (d *VirtualMachineController) setPodNetworkPhase1(vmi *v1.VirtualMachineIns
}

if virtutil.IsNonRootVMI(vmi) && virtutil.WantVirtioNetDevice(vmi) {
err := d.claimDeviceOwnership(vmi, "vhost-net")
rootMount := res.MountRoot()
err := d.claimDeviceOwnership(rootMount, "vhost-net")
if err != nil {
return neterrors.CreateCriticalNetworkError(fmt.Errorf("failed to set up vhost-net device, %s", err))
}
Expand Down Expand Up @@ -2540,21 +2541,22 @@ func (d *VirtualMachineController) vmUpdateHelperMigrationTarget(origVMI *v1.Vir
return fmt.Errorf("failed to configure vmi network for migration target: %w", err)
}

err = d.claimDeviceOwnership(vmi, "kvm")
isolationRes, err := d.podIsolationDetector.Detect(vmi)
if err != nil {
return fmt.Errorf("failed to set up file ownership for /dev/kvm: %v", err)
return fmt.Errorf("failed to detect isolation for launcher pod: %v", err)
}
virtLauncherRootMount := isolationRes.MountRoot()

res, err := d.podIsolationDetector.Detect(vmi)
err = d.claimDeviceOwnership(virtLauncherRootMount, "kvm")
if err != nil {
return err
return fmt.Errorf("failed to set up file ownership for /dev/kvm: %v", err)
}

lessPVCSpaceToleration := d.clusterConfig.GetLessPVCSpaceToleration()
minimumPVCReserveBytes := d.clusterConfig.GetMinimumReservePVCBytes()

// initialize disks images for empty PVC
hostDiskCreator := hostdisk.NewHostDiskCreator(d.recorder, lessPVCSpaceToleration, minimumPVCReserveBytes, res.MountRoot())
hostDiskCreator := hostdisk.NewHostDiskCreator(d.recorder, lessPVCSpaceToleration, minimumPVCReserveBytes, virtLauncherRootMount)
err = hostDiskCreator.Create(vmi)
if err != nil {
return fmt.Errorf("preparing host-disks failed: %v", err)
Expand Down Expand Up @@ -2628,21 +2630,22 @@ func (d *VirtualMachineController) vmUpdateHelperDefault(origVMI *v1.VirtualMach
return fmt.Errorf("failed to configure vmi network: %w", err)
}

err = d.claimDeviceOwnership(vmi, "kvm")
isolationRes, err := d.podIsolationDetector.Detect(vmi)
if err != nil {
return fmt.Errorf("failed to set up file ownership for /dev/kvm: %v", err)
return fmt.Errorf("failed to detect isolation for launcher pod: %v", err)
}
virtLauncherRootMount := isolationRes.MountRoot()

res, err := d.podIsolationDetector.Detect(vmi)
err = d.claimDeviceOwnership(virtLauncherRootMount, "kvm")
if err != nil {
return err
return fmt.Errorf("failed to set up file ownership for /dev/kvm: %v", err)
}

lessPVCSpaceToleration := d.clusterConfig.GetLessPVCSpaceToleration()
minimumPVCReserveBytes := d.clusterConfig.GetMinimumReservePVCBytes()

// initialize disks images for empty PVC
hostDiskCreator := hostdisk.NewHostDiskCreator(d.recorder, lessPVCSpaceToleration, minimumPVCReserveBytes, res.MountRoot())
hostDiskCreator := hostdisk.NewHostDiskCreator(d.recorder, lessPVCSpaceToleration, minimumPVCReserveBytes, virtLauncherRootMount)
err = hostDiskCreator.Create(vmi)
if err != nil {
return fmt.Errorf("preparing host-disks failed: %v", err)
Expand Down Expand Up @@ -2912,13 +2915,8 @@ func (d *VirtualMachineController) isHostModelMigratable(vmi *v1.VirtualMachineI
return nil
}

func (d *VirtualMachineController) claimDeviceOwnership(vmi *v1.VirtualMachineInstance, deviceName string) error {
isolation, err := d.podIsolationDetector.Detect(vmi)
if err != nil {
return err
}

kvmPath := filepath.Join(isolation.MountRoot(), "dev", deviceName)
func (d *VirtualMachineController) claimDeviceOwnership(virtLauncherRootMount, deviceName string) error {
kvmPath := filepath.Join(virtLauncherRootMount, "dev", deviceName)

softwareEmulation, err := util.UseSoftwareEmulationForDevice(kvmPath, d.clusterConfig.AllowEmulation())
if err != nil || softwareEmulation {
Expand Down

0 comments on commit f6e8bd4

Please sign in to comment.