Refactor route clearing to linux interface

The code for clearing routes currently lives inside the
service_instance module but it belongs better inside the linux
interface ones.

This patch refactors it into there, which can allow for a true
noop driver as all network operations would happen inside that
section.

Change-Id: I9fa29e9e5ed7dd2c620b56efab559f4cec25aced
This commit is contained in:
Mohammed Naser 2020-01-30 16:50:46 +01:00
parent a8f9174ca6
commit e7858ad084
4 changed files with 15 additions and 6 deletions

View File

@ -65,7 +65,7 @@ class LinuxInterfaceDriver(object):
self.conf = CONF
@device_name_synchronized
def init_l3(self, device_name, ip_cidrs, namespace=None):
def init_l3(self, device_name, ip_cidrs, namespace=None, clear_cidrs=[]):
"""Set the L3 settings for the interface using data from the port.
ip_cidrs: list of 'X.X.X.X/YY' strings
@ -73,6 +73,9 @@ class LinuxInterfaceDriver(object):
device = ip_lib.IPDevice(device_name,
namespace=namespace)
for cidr in clear_cidrs:
device.route.clear_outdated_routes(cidr)
previous = {}
for address in device.addr.list(scope='global', filters=['permanent']):
previous[address['cidr']] = address['ip_version']

View File

@ -977,17 +977,19 @@ class NeutronNetworkHelper(BaseNetworkhelper):
LOG.debug("Plug interface into host - interface_name: %s, "
"device: %s, port: %s", interface_name, device, port)
self.vif_driver.plug(interface_name, port['id'], port['mac_address'])
cidrs_to_clear = []
ip_cidrs = []
for fixed_ip in port['fixed_ips']:
subnet = self.neutron_api.get_subnet(fixed_ip['subnet_id'])
if clear_outdated_routes:
device.route.clear_outdated_routes(subnet['cidr'])
cidrs_to_clear.append(subnet['cidr'])
net = netaddr.IPNetwork(subnet['cidr'])
ip_cidr = '%s/%s' % (fixed_ip['ip_address'], net.prefixlen)
ip_cidrs.append(ip_cidr)
self.vif_driver.init_l3(interface_name, ip_cidrs)
self.vif_driver.init_l3(interface_name, ip_cidrs,
clear_cidrs=cidrs_to_clear)
@utils.synchronized("service_instance_get_service_port", external=True)
def _get_service_port(self, network_id, subnet_id, device_id):

View File

@ -98,9 +98,11 @@ class TestABCDriver(TestBase):
self.mock_object(bc, '_remove_outdated_interfaces')
ns = '12345678-1234-5678-90ab-ba0987654321'
bc.init_l3('tap0', ['192.168.1.2/24'], namespace=ns)
bc.init_l3('tap0', ['192.168.1.2/24'], namespace=ns,
clear_cidrs=['192.168.0.0/16'])
self.ip_dev.assert_has_calls(
[mock.call('tap0', namespace=ns),
mock.call().route.clear_outdated_routes('192.168.0.0/16'),
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'),

View File

@ -2126,8 +2126,10 @@ class NeutronNetworkHelperTestCase(test.TestCase):
mock.call(fake_subnet_admin['id'])])
instance.vif_driver.init_l3.assert_has_calls([
mock.call(interface_name_service,
['10.254.0.2/%s' % fake_division_mask]),
mock.call(interface_name_admin, ['10.0.0.4/24'])])
['10.254.0.2/%s' % fake_division_mask],
clear_cidrs=[]),
mock.call(interface_name_admin, ['10.0.0.4/24'],
clear_cidrs=[fake_subnet_admin['cidr']])])
service_instance.ip_lib.IPDevice.assert_has_calls([
mock.call(interface_name_service),
mock.call(interface_name_admin)])