Browse Source

generic: Refactor network functions to l3_init

There's a few functions which are used inside service_instance
which do networking that can live inside the linux interface
driver code.

This patch refactors them into there as this helps allow for
the noop driver.

Change-Id: Ib79ae3f08843414a552c89cd9a254b5e562123b8
(cherry picked from commit e8adbf3cd4)
changes/49/706749/1
Mohammed Naser 3 weeks ago
parent
commit
64b7712ab2
4 changed files with 70 additions and 72 deletions
  1. +33
    -0
      manila/network/linux/interface.py
  2. +0
    -33
      manila/share/drivers/service_instance.py
  3. +37
    -1
      manila/tests/network/linux/test_interface.py
  4. +0
    -38
      manila/tests/share/drivers/test_service_instance.py

+ 33
- 0
manila/network/linux/interface.py View File

@@ -91,6 +91,39 @@ class LinuxInterfaceDriver(object):
for ip_cidr, ip_version in previous.items():
device.addr.delete(ip_version, ip_cidr)

# ensure that interface is first in the list
device.route.pullup_route(device_name)

# here we are checking for garbage devices from removed service port
self._remove_outdated_interfaces(device)

def _remove_outdated_interfaces(self, device):
"""Finds and removes unused network device."""
device_cidr_set = self._get_set_of_device_cidrs(device)
for dev in ip_lib.IPWrapper().get_devices():
if dev.name != device.name and dev.name[:3] == device.name[:3]:
cidr_set = self._get_set_of_device_cidrs(dev)
if device_cidr_set & cidr_set:
self.unplug(dev.name)

def _get_set_of_device_cidrs(self, device):
cidrs = set()
addr_list = []
try:
# NOTE(ganso): I could call ip_lib.device_exists here, but since
# this is a concurrency problem, it would not fix the problem.
addr_list = device.addr.list()
except Exception as e:
if 'does not exist' in six.text_type(e):
LOG.warning(
"Device %s does not exist anymore.", device.name)
else:
raise
for addr in addr_list:
if addr['ip_version'] == 4:
cidrs.add(six.text_type(netaddr.IPNetwork(addr['cidr']).cidr))
return cidrs

def check_bridge_exists(self, bridge):
if not ip_lib.device_exists(bridge):
raise exception.BridgeDoesNotExist(bridge=bridge)

+ 0
- 33
manila/share/drivers/service_instance.py View File

@@ -994,39 +994,6 @@ class NeutronNetworkHelper(BaseNetworkhelper):

self.vif_driver.init_l3(interface_name, ip_cidrs)

# ensure that interface is first in the list
device.route.pullup_route(interface_name)

# here we are checking for garbage devices from removed service port
self._remove_outdated_interfaces(device)

def _remove_outdated_interfaces(self, device):
"""Finds and removes unused network device."""
device_cidr_set = self._get_set_of_device_cidrs(device)
for dev in ip_lib.IPWrapper().get_devices():
if dev.name != device.name and dev.name[:3] == device.name[:3]:
cidr_set = self._get_set_of_device_cidrs(dev)
if device_cidr_set & cidr_set:
self.vif_driver.unplug(dev.name)

def _get_set_of_device_cidrs(self, device):
cidrs = set()
addr_list = []
try:
# NOTE(ganso): I could call ip_lib.device_exists here, but since
# this is a concurrency problem, it would not fix the problem.
addr_list = device.addr.list()
except Exception as e:
if 'does not exist' in six.text_type(e):
LOG.warning(
"Device %s does not exist anymore.", device.name)
else:
raise
for addr in addr_list:
if addr['ip_version'] == 4:
cidrs.add(six.text_type(netaddr.IPNetwork(addr['cidr']).cidr))
return cidrs

@utils.synchronized("service_instance_get_service_port", external=True)
def _get_service_port(self, network_id, subnet_id, device_id):
"""Find or creates service neutron port.

+ 37
- 1
manila/tests/network/linux/test_interface.py View File

@@ -20,6 +20,7 @@ from manila.network.linux import interface
from manila.network.linux import ip_lib
from manila import test
from manila.tests import conf_fixture
from manila.tests import fake_network
from manila import utils


@@ -94,13 +95,48 @@ class TestABCDriver(TestBase):
self.ip_dev().addr.list = mock.Mock(return_value=addresses)

bc = BaseChild()
self.mock_object(bc, '_remove_outdated_interfaces')

ns = '12345678-1234-5678-90ab-ba0987654321'
bc.init_l3('tap0', ['192.168.1.2/24'], namespace=ns)
self.ip_dev.assert_has_calls(
[mock.call('tap0', namespace=ns),
mock.call().addr.list(scope='global', filters=['permanent']),
mock.call().addr.add(4, '192.168.1.2/24', '192.168.1.255'),
mock.call().addr.delete(4, '172.16.77.240/24')])
mock.call().addr.delete(4, '172.16.77.240/24'),
mock.call().route.pullup_route('tap0')])
bc._remove_outdated_interfaces.assert_called_with(self.ip_dev())

def test__remove_outdated_interfaces(self):
device = fake_network.FakeDevice(
'foobarquuz', [dict(ip_version=4, cidr='1.0.0.0/27')])
devices = [fake_network.FakeDevice('foobar')]
self.ip().get_devices = mock.Mock(return_value=devices)

bc = BaseChild()
self.mock_object(bc, 'unplug')

bc._remove_outdated_interfaces(device)
bc.unplug.assert_called_once_with('foobar')

def test__get_set_of_device_cidrs(self):
device = fake_network.FakeDevice('foo')
expected = set(('1.0.0.0/27', '2.0.0.0/27'))

bc = BaseChild()
result = bc._get_set_of_device_cidrs(device)

self.assertEqual(expected, result)

def test__get_set_of_device_cidrs_exception(self):
device = fake_network.FakeDevice('foo')
self.mock_object(device.addr, 'list', mock.Mock(
side_effect=Exception('foo does not exist')))

bc = BaseChild()
result = bc._get_set_of_device_cidrs(device)

self.assertEqual(set(), result)


class TestOVSInterfaceDriver(TestBase):

+ 0
- 38
manila/tests/share/drivers/test_service_instance.py View File

@@ -2128,7 +2128,6 @@ class NeutronNetworkHelperTestCase(test.TestCase):
mock.Mock(side_effect=[fake_subnet_service,
fake_subnet_admin,
fake_subnet_admin]))
self.mock_object(instance, '_remove_outdated_interfaces')
self.mock_object(instance.vif_driver, 'plug')
device_mock = mock.Mock()
self.mock_object(service_instance.ip_lib, 'IPDevice',
@@ -2158,43 +2157,6 @@ class NeutronNetworkHelperTestCase(test.TestCase):
service_instance.ip_lib.IPDevice.assert_has_calls([
mock.call(interface_name_service),
mock.call(interface_name_admin)])
device_mock.route.pullup_route.assert_has_calls([
mock.call(interface_name_service),
mock.call(interface_name_admin)])
instance._remove_outdated_interfaces.assert_called_with(device_mock)

def test__get_set_of_device_cidrs(self):
device = fake_network.FakeDevice('foo')
expected = set(('1.0.0.0/27', '2.0.0.0/27'))
instance = self._init_neutron_network_plugin()

result = instance._get_set_of_device_cidrs(device)

self.assertEqual(expected, result)

def test__get_set_of_device_cidrs_exception(self):
device = fake_network.FakeDevice('foo')
self.mock_object(device.addr, 'list', mock.Mock(
side_effect=Exception('foo does not exist')))
instance = self._init_neutron_network_plugin()

result = instance._get_set_of_device_cidrs(device)

self.assertEqual(set(), result)

def test__remove_outdated_interfaces(self):
device = fake_network.FakeDevice(
'foobarquuz', [dict(ip_version=4, cidr='1.0.0.0/27')])
devices = [fake_network.FakeDevice('foobar')]
instance = self._init_neutron_network_plugin()
self.mock_object(instance.vif_driver, 'unplug')
self.mock_object(
service_instance.ip_lib.IPWrapper, 'get_devices',
mock.Mock(return_value=devices))

instance._remove_outdated_interfaces(device)

instance.vif_driver.unplug.assert_called_once_with('foobar')

def test__get_service_port_none_exist(self):
instance = self._init_neutron_network_plugin()

Loading…
Cancel
Save