From: Hong Hui Xiao Date: Sun, 15 Nov 2015 09:06:45 +0000 (-0500) Subject: Don't add default route to HA router if there is no gateway ip X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=14c09fdfc4fbda874508c170ac6a7d6ead0bfd2e;p=openstack-build%2Fneutron-build.git Don't add default route to HA router if there is no gateway ip When adding an external network with no gateway ip to a HA router, l3 agent will report exception. The exception comes from the code of adding default route to HA router. However, if there is no gateway ip in the external network, there is no need to add such route. Change-Id: I41d6a292c903758f408d3d93a64dca7adeeb5769 Closes-Bug: #1515209 --- diff --git a/neutron/agent/l3/ha_router.py b/neutron/agent/l3/ha_router.py index d215ddd02..d3ef8a68f 100644 --- a/neutron/agent/l3/ha_router.py +++ b/neutron/agent/l3/ha_router.py @@ -192,13 +192,16 @@ class HaRouter(router.RouterInfo): self.routes = new_routes def _add_default_gw_virtual_route(self, ex_gw_port, interface_name): - default_gw_rts = [] gateway_ips = self._get_external_gw_ips(ex_gw_port) + if not gateway_ips: + return + + default_gw_rts = [] + instance = self._get_keepalived_instance() for gw_ip in gateway_ips: # TODO(Carl) This is repeated everywhere. A method would # be nice. default_gw = n_consts.IP_ANY[netaddr.IPAddress(gw_ip).version] - instance = self._get_keepalived_instance() default_gw_rts.append(keepalived.KeepalivedVirtualRoute( default_gw, gw_ip, interface_name)) instance.virtual_routes.gateway_routes = default_gw_rts diff --git a/neutron/tests/unit/agent/l3/test_ha_router.py b/neutron/tests/unit/agent/l3/test_ha_router.py index 99a46ade5..e44b2138b 100644 --- a/neutron/tests/unit/agent/l3/test_ha_router.py +++ b/neutron/tests/unit/agent/l3/test_ha_router.py @@ -46,3 +46,27 @@ class TestBasicRouterOperations(base.BaseTestCase): addresses = ['15.1.2.2/24', '15.1.2.3/32'] ri._get_cidrs_from_keepalived = mock.MagicMock(return_value=addresses) self.assertEqual(set(addresses), ri.get_router_cidrs(device)) + + def test__add_default_gw_virtual_route(self): + ri = self._create_router() + mock_instance = mock.Mock() + mock_instance.virtual_routes.gateway_routes = [] + ri._get_keepalived_instance = mock.Mock(return_value=mock_instance) + subnets = [{'id': _uuid(), + 'cidr': '20.0.0.0/24', + 'gateway_ip': None}] + ex_gw_port = {'fixed_ips': [], + 'subnets': subnets, + 'extra_subnets': [], + 'id': _uuid(), + 'network_id': _uuid(), + 'mac_address': 'ca:fe:de:ad:be:ef'} + # Make sure no exceptional code + ri._add_default_gw_virtual_route(ex_gw_port, 'qg-abc') + self.assertEqual(0, len(mock_instance.virtual_routes.gateway_routes)) + + subnets.append({'id': _uuid(), + 'cidr': '30.0.0.0/24', + 'gateway_ip': '30.0.0.1'}) + ri._add_default_gw_virtual_route(ex_gw_port, 'qg-abc') + self.assertEqual(1, len(mock_instance.virtual_routes.gateway_routes))