From 0f253ea9c5f3f3d97bc049f00d1bcdf2761f0736 Mon Sep 17 00:00:00 2001 From: Artom Lifshitz Date: Mon, 15 Jan 2018 15:36:40 -0500 Subject: [PATCH] Refactor _build_device_metadata This patch splits out interface and disk metadata generation into its own function each, in preparation for handling hostdev metadata generation in a subsequent patch. It also adds some debug logging. NOTE(artom): Conflicts in nova/virt/libvirt/driver.py due to trusted VFs not being present in Queens. Related-bug: 1743458 Change-Id: I318b9118c97d7bf2b61bc45698443e8d0255ff6f (cherry picked from commit d16d0d553b20fd45a83cf7c68bc477b97aece9a1) --- nova/virt/libvirt/driver.py | 81 ++++++++++++++++++++++++------------- 1 file changed, 53 insertions(+), 28 deletions(-) diff --git a/nova/virt/libvirt/driver.py b/nova/virt/libvirt/driver.py index df20d85825e6..404a096171d7 100644 --- a/nova/virt/libvirt/driver.py +++ b/nova/virt/libvirt/driver.py @@ -8627,6 +8627,54 @@ class LibvirtDriver(driver.ComputeDriver): bus.address = address return bus + def _build_interface_metadata(self, dev, vifs_to_expose, vlans_by_mac): + """Builds a metadata object for a network interface + + :param dev: The LibvirtConfigGuestInterface to build metadata for. + :param vifs_to_expose: The list of tagged and/or vlan'ed + VirtualInterface objects. + :param vlans_by_mac: A dictionary of mac address -> vlan associations. + :return: A NetworkInterfaceMetadata object, or None. + """ + vif = vifs_to_expose.get(dev.mac_addr) + if not vif: + LOG.debug('No VIF found with MAC %s, not building metadata', + dev.mac_addr) + return None + bus = self._prepare_device_bus(dev) + device = objects.NetworkInterfaceMetadata(mac=vif.address) + if 'tag' in vif and vif.tag: + device.tags = [vif.tag] + if bus: + device.bus = bus + vlan = vlans_by_mac.get(vif.address) + if vlan: + device.vlan = int(vlan) + return device + + def _build_disk_metadata(self, dev, tagged_bdms): + """Builds a metadata object for a disk + + :param dev: The vconfig.LibvirtConfigGuestDisk to build metadata for. + :param tagged_bdms: The list of tagged BlockDeviceMapping objects. + :return: A DiskMetadata object, or None. + """ + bdm = tagged_bdms.get(dev.target_dev) + if not bdm: + LOG.debug('No BDM found with device name %s, not building ' + 'metadata.', dev.target_dev) + return None + bus = self._prepare_device_bus(dev) + device = objects.DiskMetadata(tags=[bdm.tag]) + # NOTE(artom) Setting the serial (which corresponds to + # volume_id in BlockDeviceMapping) in DiskMetadata allows us to + # find the disks's BlockDeviceMapping object when we detach the + # volume and want to clean up its metadata. + device.serial = bdm.volume_id + if bus: + device.bus = bus + return device + def _build_device_metadata(self, context, instance): """Builds a metadata object for instance devices, that maps the user provided tag to the hypervisor assigned device address. @@ -8656,36 +8704,13 @@ class LibvirtDriver(driver.ComputeDriver): guest_config.parse_dom(xml_dom) for dev in guest_config.devices: - # Build network interfaces related metadata + device = None if isinstance(dev, vconfig.LibvirtConfigGuestInterface): - vif = vifs_to_expose.get(dev.mac_addr) - if not vif: - continue - bus = self._prepare_device_bus(dev) - device = objects.NetworkInterfaceMetadata(mac=vif.address) - if 'tag' in vif and vif.tag: - device.tags = [vif.tag] - if bus: - device.bus = bus - vlan = vlans_by_mac.get(vif.address) - if vlan: - device.vlan = int(vlan) - devices.append(device) - - # Build disks related metadata + device = self._build_interface_metadata(dev, vifs_to_expose, + vlans_by_mac) if isinstance(dev, vconfig.LibvirtConfigGuestDisk): - bdm = tagged_bdms.get(dev.target_dev) - if not bdm: - continue - bus = self._prepare_device_bus(dev) - device = objects.DiskMetadata(tags=[bdm.tag]) - # NOTE(artom) Setting the serial (which corresponds to - # volume_id in BlockDeviceMapping) in DiskMetadata allows us to - # find the disks's BlockDeviceMapping object when we detach the - # volume and want to clean up its metadata. - device.serial = bdm.volume_id - if bus: - device.bus = bus + device = self._build_disk_metadata(dev, tagged_bdms) + if device: devices.append(device) if devices: dev_meta = objects.InstanceDeviceMetadata(devices=devices)