]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Don't add default route to HA router if there is no gateway ip
authorHong Hui Xiao <xiaohhui@cn.ibm.com>
Sun, 15 Nov 2015 09:06:45 +0000 (04:06 -0500)
committerHong Hui Xiao <xiaohhui@cn.ibm.com>
Tue, 17 Nov 2015 05:17:32 +0000 (00:17 -0500)
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

neutron/agent/l3/ha_router.py
neutron/tests/unit/agent/l3/test_ha_router.py

index d215ddd029b6fbedcfdc15ab2efa4ba45d6f87d8..d3ef8a68f3a9ebb6cbccb3703f236a78777c861b 100644 (file)
@@ -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
index 99a46ade5a1d20b7f5fd4536dce756f231a9b382..e44b2138b195c8d2b91a7fc492657d39533fe021 100644 (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))