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
(cherry picked from commit 14c09fdfc4)
This commit is contained in:
Hong Hui Xiao 2015-11-15 04:06:45 -05:00
parent 79122bde7f
commit 588e9e401d
2 changed files with 29 additions and 2 deletions

View File

@ -186,13 +186,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

View File

@ -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))