Change agents to use get_devices_with_ip()

Instead of instantiating an IPDevice object just to get
the list of IPs, call get_devices_with_ip() instead since
that's what it's doing anyways.

Trivialfix

Change-Id: I5055d24a40d45f3f3b13b05249d353ea67acf4d5
This commit is contained in:
Brian Haley 2019-01-24 15:42:42 -05:00
parent 418bed5409
commit b083d39a83
6 changed files with 29 additions and 27 deletions

View File

@ -277,9 +277,9 @@ class DvrEdgeRouter(dvr_local_router.DvrLocalRouter):
return set() return set()
interface_name = self.get_snat_external_device_interface_name( interface_name = self.get_snat_external_device_interface_name(
ex_gw_port) ex_gw_port)
device = ip_lib.IPDevice( return set([addr['cidr'] for addr in ip_lib.get_devices_with_ip(
interface_name, namespace=self.snat_namespace.name) self.snat_namespace.name,
return set([addr['cidr'] for addr in device.addr.list()]) name=interface_name)])
def get_router_cidrs(self, device): def get_router_cidrs(self, device):
"""Over-ride the get_router_cidrs function to return the list. """Over-ride the get_router_cidrs function to return the list.

View File

@ -401,10 +401,8 @@ class HaRouter(router.RouterInfo):
pm.disable(sig=str(int(signal.SIGKILL))) pm.disable(sig=str(int(signal.SIGKILL)))
def update_initial_state(self, callback): def update_initial_state(self, callback):
ha_device = ip_lib.IPDevice( addresses = ip_lib.get_devices_with_ip(self.ha_namespace,
self.get_ha_device_name(), name=self.get_ha_device_name())
self.ha_namespace)
addresses = ha_device.addr.list()
cidrs = (address['cidr'] for address in addresses) cidrs = (address['cidr'] for address in addresses)
ha_cidr = self._get_primary_vip() ha_cidr = self._get_primary_vip()
state = 'master' if ha_cidr in cidrs else 'backup' state = 'master' if ha_cidr in cidrs else 'backup'

View File

@ -1066,9 +1066,6 @@ class Dnsmasq(DhcpLocalProcess):
return options return options
def _make_subnet_interface_ip_map(self): def _make_subnet_interface_ip_map(self):
ip_dev = ip_lib.IPDevice(self.interface_name,
namespace=self.network.namespace)
subnet_lookup = dict( subnet_lookup = dict(
(netaddr.IPNetwork(subnet.cidr), subnet.id) (netaddr.IPNetwork(subnet.cidr), subnet.id)
for subnet in self.network.subnets for subnet in self.network.subnets
@ -1076,7 +1073,8 @@ class Dnsmasq(DhcpLocalProcess):
retval = {} retval = {}
for addr in ip_dev.addr.list(): for addr in ip_lib.get_devices_with_ip(self.network.namespace,
name=self.interface_name):
ip_net = netaddr.IPNetwork(addr['cidr']) ip_net = netaddr.IPNetwork(addr['cidr'])
if ip_net in subnet_lookup: if ip_net in subnet_lookup:

View File

@ -26,6 +26,7 @@ import six
from neutron.agent.common import ovs_lib from neutron.agent.common import ovs_lib
from neutron.agent.linux import ip_lib from neutron.agent.linux import ip_lib
from neutron.common import constants as n_const from neutron.common import constants as n_const
from neutron.common import utils
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
@ -224,10 +225,11 @@ class LinuxInterfaceDriver(object):
break break
def get_ipv6_llas(self, device_name, namespace): def get_ipv6_llas(self, device_name, namespace):
device = ip_lib.IPDevice(device_name, kwargs = {'family': utils.get_socket_address_family(
namespace=namespace) constants.IP_VERSION_6),
'scope': 'link'}
return device.addr.list(scope='link', ip_version=6) return ip_lib.get_devices_with_ip(namespace, name=device_name,
**kwargs)
def check_bridge_exists(self, bridge): def check_bridge_exists(self, bridge):
if not ip_lib.device_exists(bridge): if not ip_lib.device_exists(bridge):

View File

@ -2502,10 +2502,9 @@ class TestDnsmasq(TestBase):
self._test_read_leases_file_leases(None, True) self._test_read_leases_file_leases(None, True)
def test_make_subnet_interface_ip_map(self): def test_make_subnet_interface_ip_map(self):
with mock.patch('neutron.agent.linux.ip_lib.IPDevice') as ip_dev: with mock.patch('neutron.agent.linux.ip_lib.'
ip_dev.return_value.addr.list.return_value = [ 'get_devices_with_ip') as list_mock:
{'cidr': '192.168.0.1/24'} list_mock.return_value = [{'cidr': '192.168.0.1/24'}]
]
dm = self._get_dnsmasq(FakeDualNetwork()) dm = self._get_dnsmasq(FakeDualNetwork())
@ -2705,9 +2704,9 @@ class TestDnsmasq(TestBase):
for key, value in config_opts.items(): for key, value in config_opts.items():
self.conf.set_override(key, value) self.conf.set_override(key, value)
dm = self._get_dnsmasq(network_class()) dm = self._get_dnsmasq(network_class())
with mock.patch('neutron.agent.linux.ip_lib.IPDevice') as ipdev_mock: with mock.patch('neutron.agent.linux.ip_lib.'
list_addr = ipdev_mock.return_value.addr.list 'get_devices_with_ip') as list_mock:
list_addr.return_value = [{'cidr': alloc.ip_address + '/24'} list_mock.return_value = [{'cidr': alloc.ip_address + '/24'}
for alloc in FakeDhcpPort().fixed_ips] for alloc in FakeDhcpPort().fixed_ips]
options, idx_map = dm._generate_opts_per_subnet() options, idx_map = dm._generate_opts_per_subnet()

View File

@ -21,6 +21,7 @@ from oslo_utils import excutils
from neutron.agent.common import ovs_lib from neutron.agent.common import ovs_lib
from neutron.agent.linux import interface from neutron.agent.linux import interface
from neutron.agent.linux import ip_lib from neutron.agent.linux import ip_lib
from neutron.common import utils
from neutron.conf.agent import common as config from neutron.conf.agent import common as config
from neutron.tests import base from neutron.tests import base
@ -66,6 +67,9 @@ class TestBase(base.BaseTestCase):
self.ip = self.ip_p.start() self.ip = self.ip_p.start()
self.device_exists_p = mock.patch.object(ip_lib, 'device_exists') self.device_exists_p = mock.patch.object(ip_lib, 'device_exists')
self.device_exists = self.device_exists_p.start() self.device_exists = self.device_exists_p.start()
self.get_devices_with_ip_p = mock.patch.object(ip_lib,
'get_devices_with_ip')
self.get_devices_with_ip = self.get_devices_with_ip_p.start()
class TestABCDriver(TestBase): class TestABCDriver(TestBase):
@ -352,17 +356,18 @@ class TestABCDriver(TestBase):
addresses = [dict(scope='link', addresses = [dict(scope='link',
dynamic=False, dynamic=False,
cidr='fe80:cafe::/64')] cidr='fe80:cafe::/64')]
self.ip_dev().addr.list = mock.Mock(return_value=addresses) self.get_devices_with_ip.return_value = addresses
device_name = self.ip_dev().name device_name = self.ip_dev().name
bc = BaseChild(self.conf) bc = BaseChild(self.conf)
llas = bc.get_ipv6_llas(device_name, ns) llas = bc.get_ipv6_llas(device_name, ns)
self.assertEqual(addresses, llas) self.assertEqual(addresses, llas)
self.ip_dev.assert_has_calls( kwargs = {'family': utils.get_socket_address_family(
[mock.call(device_name, namespace=ns), constants.IP_VERSION_6),
mock.call().addr.list( 'scope': 'link'}
scope='link', ip_version=constants.IP_VERSION_6)]) self.get_devices_with_ip.assert_called_with(
ns, name=device_name, **kwargs)
def test_set_mtu_logs_once(self): def test_set_mtu_logs_once(self):
bc = BaseChild(self.conf) bc = BaseChild(self.conf)