From: Jakub Libosvar Date: Thu, 3 Apr 2014 12:52:59 +0000 (+0200) Subject: Set ns_name in RouterInfo as attribute X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=90039e87503767bafef27b90189fa47dfb7f5b21;p=openstack-build%2Fneutron-build.git Set ns_name in RouterInfo as attribute Closes-Bug: #1302007 Change-Id: I02a9b92eea06010569d3d9c5a987e69f89b23be5 (cherry picked from commit ceee7b4ac9d027499dfd2568868f1192bfb77879) --- diff --git a/neutron/agent/l3_agent.py b/neutron/agent/l3_agent.py index 5b7d709d6..a1c217c65 100644 --- a/neutron/agent/l3_agent.py +++ b/neutron/agent/l3_agent.py @@ -109,11 +109,11 @@ class RouterInfo(object): self.use_namespaces = use_namespaces # Invoke the setter for establishing initial SNAT action self.router = router + self.ns_name = NS_PREFIX + router_id if use_namespaces else None self.iptables_manager = iptables_manager.IptablesManager( root_helper=root_helper, #FIXME(danwent): use_ipv6=True, - namespace=self.ns_name()) - + namespace=self.ns_name) self.routes = [] @property @@ -135,10 +135,6 @@ class RouterInfo(object): # Gateway port was removed, remove rules self._snat_action = 'remove_rules' - def ns_name(self): - if self.use_namespaces: - return NS_PREFIX + self.router_id - def perform_snat_action(self, snat_callback, *args): # Process SNAT rules for attached subnets if self._snat_action: @@ -309,7 +305,7 @@ class L3NATAgent(firewall_l3_agent.FWaaSL3AgentRpcCallback, manager.Manager): def _create_router_namespace(self, ri): ip_wrapper_root = ip_lib.IPWrapper(self.root_helper) - ip_wrapper = ip_wrapper_root.ensure_namespace(ri.ns_name()) + ip_wrapper = ip_wrapper_root.ensure_namespace(ri.ns_name) ip_wrapper.netns.execute(['sysctl', '-w', 'net.ipv4.ip_forward=1']) def _fetch_external_net_id(self, force=False): @@ -353,7 +349,7 @@ class L3NATAgent(firewall_l3_agent.FWaaSL3AgentRpcCallback, manager.Manager): ri.iptables_manager.apply() super(L3NATAgent, self).process_router_add(ri) if self.conf.enable_metadata_proxy: - self._spawn_metadata_proxy(ri.router_id, ri.ns_name()) + self._spawn_metadata_proxy(ri.router_id, ri.ns_name) def _router_removed(self, router_id): ri = self.router_info.get(router_id) @@ -371,9 +367,9 @@ class L3NATAgent(firewall_l3_agent.FWaaSL3AgentRpcCallback, manager.Manager): ri.iptables_manager.ipv4['nat'].remove_rule(c, r) ri.iptables_manager.apply() if self.conf.enable_metadata_proxy: - self._destroy_metadata_proxy(ri.router_id, ri.ns_name()) + self._destroy_metadata_proxy(ri.router_id, ri.ns_name) del self.router_info[router_id] - self._destroy_router_namespace(ri.ns_name()) + self._destroy_router_namespace(ri.ns_name) def _spawn_metadata_proxy(self, router_id, ns_name): def callback(pid_file): @@ -416,7 +412,7 @@ class L3NATAgent(firewall_l3_agent.FWaaSL3AgentRpcCallback, manager.Manager): def _get_existing_devices(self, ri): ip_wrapper = ip_lib.IPWrapper(root_helper=self.root_helper, - namespace=ri.ns_name()) + namespace=ri.ns_name) ip_devs = ip_wrapper.get_devices(exclude_loopback=True) return [ip_dev.name for ip_dev in ip_devs] @@ -452,7 +448,7 @@ class L3NATAgent(firewall_l3_agent.FWaaSL3AgentRpcCallback, manager.Manager): LOG.debug(_('Deleting stale internal router device: %s'), stale_dev) self.driver.unplug(stale_dev, - namespace=ri.ns_name(), + namespace=ri.ns_name, prefix=INTERNAL_DEV_PREFIX) internal_cidrs = [p['ip_cidr'] for p in ri.internal_ports] @@ -480,7 +476,7 @@ class L3NATAgent(firewall_l3_agent.FWaaSL3AgentRpcCallback, manager.Manager): stale_dev) self.driver.unplug(stale_dev, bridge=self.conf.external_network_bridge, - namespace=ri.ns_name(), + namespace=ri.ns_name, prefix=EXTERNAL_DEV_PREFIX) # Process static routes for router @@ -569,7 +565,7 @@ class L3NATAgent(firewall_l3_agent.FWaaSL3AgentRpcCallback, manager.Manager): fip_statuses = {} interface_name = self.get_external_device_name(ex_gw_port['id']) device = ip_lib.IPDevice(interface_name, self.root_helper, - namespace=ri.ns_name()) + namespace=ri.ns_name) existing_cidrs = set([addr['cidr'] for addr in device.addr.list()]) new_cidrs = set() @@ -617,7 +613,7 @@ class L3NATAgent(firewall_l3_agent.FWaaSL3AgentRpcCallback, manager.Manager): ip_address] try: ip_wrapper = ip_lib.IPWrapper(self.root_helper, - namespace=ri.ns_name()) + namespace=ri.ns_name) ip_wrapper.netns.execute(arping_cmd, check_exit_code=True) except Exception as e: LOG.error(_("Failed sending gratuitous ARP: %s"), str(e)) @@ -639,7 +635,7 @@ class L3NATAgent(firewall_l3_agent.FWaaSL3AgentRpcCallback, manager.Manager): ex_gw_port['id'], interface_name, ex_gw_port['mac_address'], bridge=self.conf.external_network_bridge, - namespace=ri.ns_name(), + namespace=ri.ns_name, prefix=EXTERNAL_DEV_PREFIX) # Compute a list of addresses this router is supposed to have. @@ -650,7 +646,7 @@ class L3NATAgent(firewall_l3_agent.FWaaSL3AgentRpcCallback, manager.Manager): for ip in floating_ips] self.driver.init_l3(interface_name, [ex_gw_port['ip_cidr']], - namespace=ri.ns_name(), + namespace=ri.ns_name, preserve_ips=preserve_ips) ip_address = ex_gw_port['ip_cidr'].split('/')[0] self._send_gratuitous_arp_packet(ri, interface_name, ip_address) @@ -659,7 +655,7 @@ class L3NATAgent(firewall_l3_agent.FWaaSL3AgentRpcCallback, manager.Manager): if ex_gw_port['subnet']['gateway_ip']: cmd = ['route', 'add', 'default', 'gw', gw_ip] ip_wrapper = ip_lib.IPWrapper(self.root_helper, - namespace=ri.ns_name()) + namespace=ri.ns_name) ip_wrapper.netns.execute(cmd, check_exit_code=False) def external_gateway_removed(self, ri, ex_gw_port, @@ -667,7 +663,7 @@ class L3NATAgent(firewall_l3_agent.FWaaSL3AgentRpcCallback, manager.Manager): self.driver.unplug(interface_name, bridge=self.conf.external_network_bridge, - namespace=ri.ns_name(), + namespace=ri.ns_name, prefix=EXTERNAL_DEV_PREFIX) def metadata_filter_rules(self): @@ -701,13 +697,13 @@ class L3NATAgent(firewall_l3_agent.FWaaSL3AgentRpcCallback, manager.Manager): interface_name = self.get_internal_device_name(port_id) if not ip_lib.device_exists(interface_name, root_helper=self.root_helper, - namespace=ri.ns_name()): + namespace=ri.ns_name): self.driver.plug(network_id, port_id, interface_name, mac_address, - namespace=ri.ns_name(), + namespace=ri.ns_name, prefix=INTERNAL_DEV_PREFIX) self.driver.init_l3(interface_name, [internal_cidr], - namespace=ri.ns_name()) + namespace=ri.ns_name) ip_address = internal_cidr.split('/')[0] self._send_gratuitous_arp_packet(ri, interface_name, ip_address) @@ -715,8 +711,8 @@ class L3NATAgent(firewall_l3_agent.FWaaSL3AgentRpcCallback, manager.Manager): interface_name = self.get_internal_device_name(port_id) if ip_lib.device_exists(interface_name, root_helper=self.root_helper, - namespace=ri.ns_name()): - self.driver.unplug(interface_name, namespace=ri.ns_name(), + namespace=ri.ns_name): + self.driver.unplug(interface_name, namespace=ri.ns_name, prefix=INTERNAL_DEV_PREFIX) def internal_network_nat_rules(self, ex_gw_ip, internal_cidr): @@ -874,7 +870,7 @@ class L3NATAgent(firewall_l3_agent.FWaaSL3AgentRpcCallback, manager.Manager): cmd = ['ip', 'route', operation, 'to', route['destination'], 'via', route['nexthop']] ip_wrapper = ip_lib.IPWrapper(self.root_helper, - namespace=ri.ns_name()) + namespace=ri.ns_name) ip_wrapper.netns.execute(cmd, check_exit_code=False) def routes_updated(self, ri): diff --git a/neutron/services/firewall/agents/l3reference/firewall_l3_agent.py b/neutron/services/firewall/agents/l3reference/firewall_l3_agent.py index a3646c6bb..865051835 100644 --- a/neutron/services/firewall/agents/l3reference/firewall_l3_agent.py +++ b/neutron/services/firewall/agents/l3reference/firewall_l3_agent.py @@ -102,7 +102,7 @@ class FWaaSL3AgentRpcCallback(api.FWaaSAgentRpcCallbackMixin): if rid not in self.router_info: continue if self.router_info[rid].use_namespaces: - router_ns = self.router_info[rid].ns_name() + router_ns = self.router_info[rid].ns_name if router_ns in local_ns_list: router_info_list.append(self.router_info[rid]) else: diff --git a/neutron/services/firewall/agents/varmour/varmour_router.py b/neutron/services/firewall/agents/varmour/varmour_router.py index 25cf02ffa..0ca357229 100755 --- a/neutron/services/firewall/agents/varmour/varmour_router.py +++ b/neutron/services/firewall/agents/varmour/varmour_router.py @@ -308,15 +308,15 @@ class vArmourL3NATAgent(l3_agent.L3NATAgent, if not ip_lib.device_exists(interface_name, root_helper=self.root_helper, - namespace=ri.ns_name()): + namespace=ri.ns_name): self.driver.plug(ex_gw_port['network_id'], ex_gw_port['id'], interface_name, ex_gw_port['mac_address'], bridge=self.conf.external_network_bridge, - namespace=ri.ns_name(), + namespace=ri.ns_name, prefix=l3_agent.EXTERNAL_DEV_PREFIX) self.driver.init_l3(interface_name, [ex_gw_port['ip_cidr']], - namespace=ri.ns_name()) + namespace=ri.ns_name) def _update_routing_table(self, ri, operation, route): return diff --git a/neutron/services/metering/drivers/iptables/iptables_driver.py b/neutron/services/metering/drivers/iptables/iptables_driver.py index 33a4d27b6..3eb17c246 100644 --- a/neutron/services/metering/drivers/iptables/iptables_driver.py +++ b/neutron/services/metering/drivers/iptables/iptables_driver.py @@ -70,16 +70,13 @@ class RouterWithMetering(object): self.id = router['id'] self.router = router self.root_helper = config.get_root_helper(self.conf) + self.ns_name = NS_PREFIX + self.id if conf.use_namespaces else None self.iptables_manager = iptables_manager.IptablesManager( root_helper=self.root_helper, - namespace=self.ns_name(), + namespace=self.ns_name, binary_name=WRAP_NAME) self.metering_labels = {} - def ns_name(self): - if self.conf.use_namespaces: - return NS_PREFIX + self.router['id'] - class IptablesMeteringDriver(abstract_driver.MeteringAbstractDriver): diff --git a/neutron/services/vpn/agent.py b/neutron/services/vpn/agent.py index ffb4d74b6..8c666b497 100644 --- a/neutron/services/vpn/agent.py +++ b/neutron/services/vpn/agent.py @@ -67,7 +67,7 @@ class VPNAgent(l3_agent.L3NATAgentWithStateReport): router_info = self.router_info.get(router_id) if not router_info: return - return router_info.ns_name() + return router_info.ns_name def add_nat_rule(self, router_id, chain, rule, top=False): """Add nat rule in namespace. diff --git a/neutron/tests/unit/services/firewall/agents/l3reference/test_firewall_l3_agent.py b/neutron/tests/unit/services/firewall/agents/l3reference/test_firewall_l3_agent.py index f638daa27..934e0b1e7 100644 --- a/neutron/tests/unit/services/firewall/agents/l3reference/test_firewall_l3_agent.py +++ b/neutron/tests/unit/services/firewall/agents/l3reference/test_firewall_l3_agent.py @@ -321,7 +321,7 @@ class TestFwaasL3AgentRpcCallback(base.BaseTestCase): self.api.router_info = {ri.router_id: ri} with mock.patch.object(ip_lib.IPWrapper, 'get_namespaces') as mock_get_namespaces: - mock_get_namespaces.return_value = ri.ns_name() + mock_get_namespaces.return_value = ri.ns_name router_info_list = self.api._get_router_info_list_for_tenant( routers, ri.router['tenant_id']) @@ -376,7 +376,7 @@ class TestFwaasL3AgentRpcCallback(base.BaseTestCase): ri_expected.append(ri) with mock.patch.object(ip_lib.IPWrapper, 'get_namespaces') as mock_get_namespaces: - mock_get_namespaces.return_value = ri.ns_name() + mock_get_namespaces.return_value = ri.ns_name router_info_list = self.api._get_router_info_list_for_tenant( routers, ri.router['tenant_id']) diff --git a/neutron/tests/unit/test_l3_agent.py b/neutron/tests/unit/test_l3_agent.py index d390ac29b..89e3e4320 100644 --- a/neutron/tests/unit/test_l3_agent.py +++ b/neutron/tests/unit/test_l3_agent.py @@ -96,7 +96,7 @@ class TestBasicRouterOperations(base.BaseTestCase): ri = l3_agent.RouterInfo(id, self.conf.root_helper, self.conf.use_namespaces, None) - self.assertTrue(ri.ns_name().endswith(id)) + self.assertTrue(ri.ns_name.endswith(id)) def test_router_info_create_with_router(self): id = _uuid() @@ -113,7 +113,7 @@ class TestBasicRouterOperations(base.BaseTestCase): 'gw_port': ex_gw_port} ri = l3_agent.RouterInfo(id, self.conf.root_helper, self.conf.use_namespaces, router) - self.assertTrue(ri.ns_name().endswith(id)) + self.assertTrue(ri.ns_name.endswith(id)) self.assertEqual(ri.router, router) def test_agent_create(self): @@ -838,7 +838,7 @@ class TestBasicRouterOperations(base.BaseTestCase): self.assertEqual(self.mock_driver.unplug.call_count, len(stale_devnames)) calls = [mock.call(stale_devname, - namespace=ri.ns_name(), + namespace=ri.ns_name, prefix=l3_agent.INTERNAL_DEV_PREFIX) for stale_devname in stale_devnames] self.mock_driver.unplug.assert_has_calls(calls, any_order=True) @@ -867,7 +867,7 @@ class TestBasicRouterOperations(base.BaseTestCase): self.mock_driver.unplug.assert_called_with( stale_devnames[0], bridge="br-ex", - namespace=ri.ns_name(), + namespace=ri.ns_name, prefix=l3_agent.EXTERNAL_DEV_PREFIX) def test_routers_with_admin_state_down(self):