Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

virt-handler: Refactor claimDeviceOwnership method #6760

Merged
merged 1 commit into from
Nov 15, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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