virt-handler: Export masquerade interface check for migration to function
This commit change the code to make it easier to understand.
@@ -1895,18 +1895,38 @@ func (d *VirtualMachineController) isPreMigrationTarget(vmi *v1.VirtualMachineIn
}
func (d *VirtualMachineController) checkNetworkInterfacesForMigration(vmi *v1.VirtualMachineInstance) error {
- networks := map[string]*v1.Network{}
- for _, network := range vmi.Spec.Networks {
- networks[network.Name] = network.DeepCopy()
+ err := validatePodNetworkInterfaceUsesMasqueradeBinding(vmi)
+ if err != nil {
+ return err
+ }
+
+ return nil
}
+
+func validatePodNetworkInterfaceUsesMasqueradeBinding(vmi *v1.VirtualMachineInstance) error {
+ interfacesByName := map[string]v1.Interface{}
for _, iface := range vmi.Spec.Domain.Devices.Interfaces {
- if iface.Masquerade == nil && networks[iface.Name].Pod != nil {
- return fmt.Errorf("cannot migrate VMI which does not use masquerade to connect to the pod network")
+ interfacesByName[iface.Name] = iface
}
+
+ vmiPodNetworkName := lookupVMIPodNetworkName(vmi.Spec.Networks)
+ if vmiPodNetworkName != "" && interfacesByName[vmiPodNetworkName].Masquerade == nil {
+ return fmt.Errorf("cannot migrate VMI which does not use masquerade to connect to the pod network")
}
+
return nil
}
+func lookupVMIPodNetworkName(networks []v1.Network) string {
+ for _, network := range networks {
+ if network.Pod != nil {
+ return network.Name
+ }
+ }
+
+ return ""
+}
+
func (d *VirtualMachineController) checkVolumesForMigration(vmi *v1.VirtualMachineInstance) (blockMigrate bool, err error) {
// Check if all VMI volumes can be shared between the source and the destination
// of a live migration. blockMigrate will be returned as false, only if all volumes
