From 705bc440944eba641c48d61fa2a9412ddcc1d1cd Mon Sep 17 00:00:00 2001 From: Roman Dobosz Date: Wed, 18 Dec 2019 20:40:21 +0100 Subject: [PATCH] Stop passing around neutron client object. In couple of drivers, in some methods, neutron client object is passing down to through the series of methods call, just to eventually be called with some object. Since we already make the client a singleton, calling clients.get_neutron_client method will immediately return instance, which is possibly is already created. There is no need to pass this down, while it can be easily reached by destination method. Implements: blueprint switch-to-openstacksdk Change-Id: I0a08f4740e94f9d7ae5ee003aa8b0a2e0a9b6fac --- .../controller/drivers/nested_macvlan_vif.py | 23 +++---- .../controller/drivers/nested_vif.py | 8 ++- .../controller/drivers/nested_vlan_vif.py | 22 ++++--- .../controller/drivers/vif_pool.py | 16 +++-- .../drivers/test_nested_macvlan_vif.py | 63 +++++++++---------- .../controller/drivers/test_nested_vif.py | 12 ++-- .../drivers/test_nested_vlan_vif.py | 46 ++++++-------- .../unit/controller/drivers/test_vif_pool.py | 24 +++---- 8 files changed, 102 insertions(+), 112 deletions(-) diff --git a/kuryr_kubernetes/controller/drivers/nested_macvlan_vif.py b/kuryr_kubernetes/controller/drivers/nested_macvlan_vif.py index 3d613638c..29229e105 100755 --- a/kuryr_kubernetes/controller/drivers/nested_macvlan_vif.py +++ b/kuryr_kubernetes/controller/drivers/nested_macvlan_vif.py @@ -40,7 +40,7 @@ class NestedMacvlanPodVIFDriver(nested_vif.NestedPodVIFDriver): attempts = kuryr_config.CONF.pod_vif_nested.rev_update_attempts container_port = None while attempts > 0: - vm_port = self._get_parent_port(neutron, pod) + vm_port = self._get_parent_port(pod) if not container_port: container_port = neutron.create_port(req).get('port') @@ -51,7 +51,7 @@ class NestedMacvlanPodVIFDriver(nested_vif.NestedPodVIFDriver): container_port['fixed_ips']) attempts = self._try_update_port( - attempts, self._add_to_allowed_address_pairs, neutron, vm_port, + attempts, self._add_to_allowed_address_pairs, vm_port, container_ips, container_mac) return ovu.neutron_to_osvif_vif_nested_macvlan(container_port, subnets) @@ -71,9 +71,9 @@ class NestedMacvlanPodVIFDriver(nested_vif.NestedPodVIFDriver): container_mac = container_port['mac_address'] container_ips = frozenset(entry['ip_address'] for entry in container_port['fixed_ips']) - vm_port = self._get_parent_port(neutron, pod) + vm_port = self._get_parent_port(pod) attempts = self._try_update_port( - attempts, self._remove_from_allowed_address_pairs, neutron, + attempts, self._remove_from_allowed_address_pairs, vm_port, container_ips, container_mac) try: @@ -90,7 +90,7 @@ class NestedMacvlanPodVIFDriver(nested_vif.NestedPodVIFDriver): # immediately to let the CNI driver make progress. vif.active = True - def _add_to_allowed_address_pairs(self, neutron, port, ip_addresses, + def _add_to_allowed_address_pairs(self, port, ip_addresses, mac_address=None): if not ip_addresses: raise k_exc.IntegrityError( @@ -120,13 +120,13 @@ class NestedMacvlanPodVIFDriver(nested_vif.NestedPodVIFDriver): address_pairs.append({'ip_address': ip, 'mac_address': mac}) self._update_port_address_pairs( - neutron, port['id'], address_pairs, + port['id'], address_pairs, revision_number=port['revision_number']) LOG.debug("Added allowed_address_pair %s %s" % (str(ip_addresses,), mac_address)) - def _remove_from_allowed_address_pairs(self, neutron, port, ip_addresses, + def _remove_from_allowed_address_pairs(self, port, ip_addresses, mac_address=None): if not ip_addresses: raise k_exc.IntegrityError( @@ -149,12 +149,13 @@ class NestedMacvlanPodVIFDriver(nested_vif.NestedPodVIFDriver): if updated: self._update_port_address_pairs( - neutron, port['id'], + port['id'], address_pairs, revision_number=port['revision_number']) - def _update_port_address_pairs(self, neutron, port_id, address_pairs, + def _update_port_address_pairs(self, port_id, address_pairs, revision_number=None): + neutron = clients.get_neutron_client() neutron.update_port( port_id, {'port': {'allowed_address_pairs': address_pairs}}, @@ -162,10 +163,10 @@ class NestedMacvlanPodVIFDriver(nested_vif.NestedPodVIFDriver): ) def _try_update_port(self, attempts, f, - neutron, vm_port, container_ips, container_mac): + vm_port, container_ips, container_mac): try: with self.lock: - f(neutron, vm_port, container_ips, container_mac) + f(vm_port, container_ips, container_mac) attempts = 0 except n_exc.NeutronClientException: attempts -= 1 diff --git a/kuryr_kubernetes/controller/drivers/nested_vif.py b/kuryr_kubernetes/controller/drivers/nested_vif.py index 435afb2c2..40bc4c5fa 100644 --- a/kuryr_kubernetes/controller/drivers/nested_vif.py +++ b/kuryr_kubernetes/controller/drivers/nested_vif.py @@ -20,6 +20,7 @@ from neutronclient.common import exceptions as n_exc from oslo_config import cfg as oslo_cfg from oslo_log import log as logging +from kuryr_kubernetes import clients from kuryr_kubernetes.controller.drivers import neutron_vif @@ -30,7 +31,8 @@ LOG = logging.getLogger(__name__) class NestedPodVIFDriver(neutron_vif.NeutronPodVIFDriver): """Skeletal handler driver for VIFs for Nested Pods.""" - def _get_parent_port_by_host_ip(self, neutron, node_fixed_ip): + def _get_parent_port_by_host_ip(self, node_fixed_ip): + neutron = clients.get_neutron_client() node_subnet_id = oslo_cfg.CONF.pod_vif_nested.worker_nodes_subnet if not node_subnet_id: raise oslo_cfg.RequiredOptError( @@ -52,7 +54,7 @@ class NestedPodVIFDriver(neutron_vif.NeutronPodVIFDriver): " not found!", fixed_ips) raise kl_exc.NoResourceException - def _get_parent_port(self, neutron, pod): + def _get_parent_port(self, pod): try: # REVISIT(vikasc): Assumption is being made that hostIP is the IP # of trunk interface on the node(vm). @@ -63,4 +65,4 @@ class NestedPodVIFDriver(neutron_vif.NeutronPodVIFDriver): LOG.error("Failed to get parent vm port ip") raise - return self._get_parent_port_by_host_ip(neutron, node_fixed_ip) + return self._get_parent_port_by_host_ip(node_fixed_ip) diff --git a/kuryr_kubernetes/controller/drivers/nested_vlan_vif.py b/kuryr_kubernetes/controller/drivers/nested_vlan_vif.py index abed3cf04..1be03fd03 100644 --- a/kuryr_kubernetes/controller/drivers/nested_vlan_vif.py +++ b/kuryr_kubernetes/controller/drivers/nested_vlan_vif.py @@ -39,13 +39,13 @@ class NestedVlanPodVIFDriver(nested_vif.NestedPodVIFDriver): def request_vif(self, pod, project_id, subnets, security_groups): neutron = clients.get_neutron_client() - parent_port = self._get_parent_port(neutron, pod) + parent_port = self._get_parent_port(pod) trunk_id = self._get_trunk_id(parent_port) rq = self._get_port_request(pod, project_id, subnets, security_groups) port = neutron.create_port(rq).get('port') utils.tag_neutron_resources('ports', [port['id']]) - vlan_id = self._add_subport(neutron, trunk_id, port['id']) + vlan_id = self._add_subport(trunk_id, port['id']) return ovu.neutron_to_osvif_vif_nested_vlan(port, subnets, vlan_id) @@ -66,9 +66,9 @@ class NestedVlanPodVIFDriver(nested_vif.NestedPodVIFDriver): """ neutron = clients.get_neutron_client() if trunk_ip: - parent_port = self._get_parent_port_by_host_ip(neutron, trunk_ip) + parent_port = self._get_parent_port_by_host_ip(trunk_ip) else: - parent_port = self._get_parent_port(neutron, pod) + parent_port = self._get_parent_port(pod) trunk_id = self._get_trunk_id(parent_port) port_rq, subports_info = self._create_subports_info( @@ -114,9 +114,9 @@ class NestedVlanPodVIFDriver(nested_vif.NestedPodVIFDriver): def release_vif(self, pod, vif, project_id=None, security_groups=None): neutron = clients.get_neutron_client() - parent_port = self._get_parent_port(neutron, pod) + parent_port = self._get_parent_port(pod) trunk_id = self._get_trunk_id(parent_port) - self._remove_subport(neutron, trunk_id, vif.id) + self._remove_subport(trunk_id, vif.id) self._release_vlan_id(vif.vlan_id) try: neutron.delete_port(vif.id) @@ -175,7 +175,7 @@ class NestedVlanPodVIFDriver(nested_vif.NestedPodVIFDriver): "with a Neutron vlan trunk") raise k_exc.K8sNodeTrunkPortFailure - def _add_subport(self, neutron, trunk_id, subport): + def _add_subport(self, trunk_id, subport): """Adds subport port to Neutron trunk This method gets vlanid allocated from kuryr segmentation driver. @@ -186,6 +186,7 @@ class NestedVlanPodVIFDriver(nested_vif.NestedPodVIFDriver): """ # TODO(vikasc): Better approach for retrying in case of # vlan-id conflict. + neutron = clients.get_neutron_client() retry_count = 1 while True: try: @@ -218,7 +219,8 @@ class NestedVlanPodVIFDriver(nested_vif.NestedPodVIFDriver): raise return vlan_id - def _remove_subports(self, neutron, trunk_id, subports_id): + def _remove_subports(self, trunk_id, subports_id): + neutron = clients.get_neutron_client() subports_body = [] for subport_id in set(subports_id): subports_body.append({'port_id': subport_id}) @@ -230,8 +232,8 @@ class NestedVlanPodVIFDriver(nested_vif.NestedPodVIFDriver): "trunk %s", trunk_id) raise - def _remove_subport(self, neutron, trunk_id, subport_id): - self._remove_subports(neutron, trunk_id, [subport_id]) + def _remove_subport(self, trunk_id, subport_id): + self._remove_subports(trunk_id, [subport_id]) def _get_vlan_id(self, trunk_id): vlan_ids = self._get_in_use_vlan_ids_set(trunk_id) diff --git a/kuryr_kubernetes/controller/drivers/vif_pool.py b/kuryr_kubernetes/controller/drivers/vif_pool.py index 44b185e53..9aeb647cd 100644 --- a/kuryr_kubernetes/controller/drivers/vif_pool.py +++ b/kuryr_kubernetes/controller/drivers/vif_pool.py @@ -850,10 +850,9 @@ class NestedVIFPool(BaseVIFPool): pool_key, {}).setdefault( sg_current.get(port_id), []).append(port_id) else: - trunk_id = self._get_trunk_id(neutron, pool_key) + trunk_id = self._get_trunk_id(pool_key) try: - self._drv_vif._remove_subport(neutron, trunk_id, - port_id) + self._drv_vif._remove_subport(trunk_id, port_id) self._drv_vif._release_vlan_id( self._existing_vifs[port_id].vlan_id) del self._existing_vifs[port_id] @@ -871,11 +870,10 @@ class NestedVIFPool(BaseVIFPool): except KeyError: LOG.debug('Port already recycled: %s', port_id) - def _get_trunk_id(self, neutron, pool_key): + def _get_trunk_id(self, pool_key): trunk_id = self._known_trunk_ids.get(pool_key, None) if not trunk_id: - p_port = self._drv_vif._get_parent_port_by_host_ip( - neutron, pool_key[0]) + p_port = self._drv_vif._get_parent_port_by_host_ip(pool_key[0]) trunk_id = self._drv_vif._get_trunk_id(p_port) self._known_trunk_ids[pool_key] = trunk_id return trunk_id @@ -965,7 +963,7 @@ class NestedVIFPool(BaseVIFPool): elif action == 'free': try: - self._drv_vif._remove_subport(neutron, trunk_id, + self._drv_vif._remove_subport(trunk_id, kuryr_subport['id']) neutron.delete_port(kuryr_subport['id']) self._drv_vif._release_vlan_id( @@ -1050,11 +1048,11 @@ class NestedVIFPool(BaseVIFPool): for pool_key, ports in list(self._available_ports_pools.items()): if self._get_pool_key_net(pool_key) != net_id: continue - trunk_id = self._get_trunk_id(neutron, pool_key) + trunk_id = self._get_trunk_id(pool_key) ports_id = [p_id for sg_ports in ports.values() for p_id in sg_ports] try: - self._drv_vif._remove_subports(neutron, trunk_id, ports_id) + self._drv_vif._remove_subports(trunk_id, ports_id) except n_exc.NeutronClientException: LOG.exception('Error removing subports from trunk: %s', trunk_id) diff --git a/kuryr_kubernetes/tests/unit/controller/drivers/test_nested_macvlan_vif.py b/kuryr_kubernetes/tests/unit/controller/drivers/test_nested_macvlan_vif.py index ad256d3d3..f4fb20cd7 100644 --- a/kuryr_kubernetes/tests/unit/controller/drivers/test_nested_macvlan_vif.py +++ b/kuryr_kubernetes/tests/unit/controller/drivers/test_nested_macvlan_vif.py @@ -59,7 +59,7 @@ class TestNestedMacvlanPodVIFDriver(test_base.TestCase): m_driver._get_port_request.assert_called_once_with( pod, project_id, subnets, security_groups) neutron.create_port.assert_called_once_with(port_request) - m_driver._get_parent_port.assert_called_once_with(neutron, pod) + m_driver._get_parent_port.assert_called_once_with(pod) m_driver._try_update_port.assert_called_once() m_to_vif.assert_called_once_with(container_port['port'], subnets) @@ -114,7 +114,7 @@ class TestNestedMacvlanPodVIFDriver(test_base.TestCase): m_driver._get_port_request.assert_called_once_with( pod, project_id, subnets, security_groups) neutron.create_port.assert_not_called() - m_driver._get_parent_port.assert_called_once_with(neutron, pod) + m_driver._get_parent_port.assert_called_once_with(pod) m_driver._try_update_port.assert_not_called() m_to_vif.assert_not_called() @@ -142,7 +142,7 @@ class TestNestedMacvlanPodVIFDriver(test_base.TestCase): cls.release_vif(m_driver, pod, vif) neutron.show_port.assert_called_once_with(port_id) - m_driver._get_parent_port.assert_called_once_with(neutron, pod) + m_driver._get_parent_port.assert_called_once_with(pod) m_driver._try_update_port.assert_called_once() neutron.delete_port.assert_called_once_with(vif.id) @@ -185,7 +185,7 @@ class TestNestedMacvlanPodVIFDriver(test_base.TestCase): m_driver, pod, vif) neutron.show_port.assert_called_with(port_id) self.assertEqual(neutron.show_port.call_count, 1) - m_driver._get_parent_port.assert_called_with(neutron, pod) + m_driver._get_parent_port.assert_called_with(pod) self.assertEqual(m_driver._get_parent_port.call_count, 1) m_driver._remove_from_allowed_address_pairs.assert_not_called() neutron.delete_port.assert_not_called() @@ -215,7 +215,7 @@ class TestNestedMacvlanPodVIFDriver(test_base.TestCase): cls.release_vif(m_driver, pod, vif) neutron.show_port.assert_called_once_with(port_id) - m_driver._get_parent_port.assert_called_once_with(neutron, pod) + m_driver._get_parent_port.assert_called_once_with(pod) m_driver._try_update_port.assert_called_once() neutron.delete_port.assert_called_once_with(vif.id) @@ -235,7 +235,7 @@ class TestNestedMacvlanPodVIFDriver(test_base.TestCase): def test_add_to_allowed_address_pairs(self, m_mac): cls = nested_macvlan_vif.NestedMacvlanPodVIFDriver m_driver = mock.Mock(spec=cls) - neutron = self.useFixture(k_fix.MockNeutronClient()).client + self.useFixture(k_fix.MockNeutronClient()).client port_id = lib_utils.get_hash() vm_port = self._get_fake_port(port_id)['port'] @@ -255,28 +255,28 @@ class TestNestedMacvlanPodVIFDriver(test_base.TestCase): 'mac_address': m_mac if m_mac else vm_port['mac_address']} ) - cls._add_to_allowed_address_pairs(m_driver, neutron, vm_port, + cls._add_to_allowed_address_pairs(m_driver, vm_port, frozenset([ip_addr]), m_mac) m_driver._update_port_address_pairs.assert_called_once_with( - neutron, port_id, address_pairs, revision_number=1) + port_id, address_pairs, revision_number=1) def test_add_to_allowed_address_pairs_no_ip_addresses(self): cls = nested_macvlan_vif.NestedMacvlanPodVIFDriver m_driver = mock.Mock(spec=cls) - neutron = self.useFixture(k_fix.MockNeutronClient()).client + self.useFixture(k_fix.MockNeutronClient()).client port_id = lib_utils.get_hash() vm_port = self._get_fake_port(port_id)['port'] self.assertRaises(k_exc.IntegrityError, cls._add_to_allowed_address_pairs, m_driver, - neutron, vm_port, frozenset()) + vm_port, frozenset()) def test_add_to_allowed_address_pairs_same_ip(self): cls = nested_macvlan_vif.NestedMacvlanPodVIFDriver m_driver = mock.Mock(spec=cls) - neutron = self.useFixture(k_fix.MockNeutronClient()).client + self.useFixture(k_fix.MockNeutronClient()).client port_id = lib_utils.get_hash() vm_port = self._get_fake_port(port_id)['port'] @@ -292,16 +292,16 @@ class TestNestedMacvlanPodVIFDriver(test_base.TestCase): ip_addr = '10.0.0.30' address_pairs.append({'ip_address': ip_addr, 'mac_address': mac_addr}) - cls._add_to_allowed_address_pairs(m_driver, neutron, vm_port, + cls._add_to_allowed_address_pairs(m_driver, vm_port, frozenset([ip_addr]), mac_addr) m_driver._update_port_address_pairs.assert_called_once_with( - neutron, port_id, address_pairs, revision_number=1) + port_id, address_pairs, revision_number=1) def test_add_to_allowed_address_pairs_already_present(self): cls = nested_macvlan_vif.NestedMacvlanPodVIFDriver m_driver = mock.Mock(spec=cls) - neutron = self.useFixture(k_fix.MockNeutronClient()).client + self.useFixture(k_fix.MockNeutronClient()).client port_id = lib_utils.get_hash() vm_port = self._get_fake_port(port_id)['port'] @@ -317,14 +317,14 @@ class TestNestedMacvlanPodVIFDriver(test_base.TestCase): ip_addr = '10.0.0.30' self.assertRaises(k_exc.AllowedAddressAlreadyPresent, - cls._add_to_allowed_address_pairs, m_driver, neutron, + cls._add_to_allowed_address_pairs, m_driver, vm_port, frozenset([ip_addr]), mac_addr) @ddt.data((None), ('fa:16:3e:71:cb:80')) def test_remove_from_allowed_address_pairs(self, m_mac): cls = nested_macvlan_vif.NestedMacvlanPodVIFDriver m_driver = mock.Mock(spec=cls) - neutron = self.useFixture(k_fix.MockNeutronClient()).client + self.useFixture(k_fix.MockNeutronClient()).client port_id = lib_utils.get_hash() vm_port = self._get_fake_port(port_id)['port'] @@ -345,28 +345,28 @@ class TestNestedMacvlanPodVIFDriver(test_base.TestCase): ) cls._remove_from_allowed_address_pairs( - m_driver, neutron, vm_port, frozenset([ip_addr]), m_mac) + m_driver, vm_port, frozenset([ip_addr]), m_mac) m_driver._update_port_address_pairs.assert_called_once_with( - neutron, port_id, address_pairs, revision_number=1) + port_id, address_pairs, revision_number=1) def test_remove_from_allowed_address_pairs_no_ip_addresses(self): cls = nested_macvlan_vif.NestedMacvlanPodVIFDriver m_driver = mock.Mock(spec=cls) - neutron = self.useFixture(k_fix.MockNeutronClient()).client + self.useFixture(k_fix.MockNeutronClient()).client port_id = lib_utils.get_hash() vm_port = self._get_fake_port(port_id)['port'] self.assertRaises(k_exc.IntegrityError, cls._remove_from_allowed_address_pairs, m_driver, - neutron, vm_port, frozenset()) + vm_port, frozenset()) @ddt.data((None), ('fa:16:3e:71:cb:80')) def test_remove_from_allowed_address_pairs_missing(self, m_mac): cls = nested_macvlan_vif.NestedMacvlanPodVIFDriver m_driver = mock.Mock(spec=cls) - neutron = self.useFixture(k_fix.MockNeutronClient()).client + self.useFixture(k_fix.MockNeutronClient()).client port_id = lib_utils.get_hash() vm_port = self._get_fake_port(port_id)['port'] @@ -385,16 +385,16 @@ class TestNestedMacvlanPodVIFDriver(test_base.TestCase): ip_addr = ['10.0.0.29', '10.0.0.28'] cls._remove_from_allowed_address_pairs( - m_driver, neutron, vm_port, frozenset(ip_addr), m_mac) + m_driver, vm_port, frozenset(ip_addr), m_mac) m_driver._update_port_address_pairs.assert_called_once_with( - neutron, port_id, address_pairs, revision_number=1) + port_id, address_pairs, revision_number=1) @ddt.data((None), ('fa:16:3e:71:cb:80')) def test_remove_from_allowed_address_pairs_no_update(self, m_mac): cls = nested_macvlan_vif.NestedMacvlanPodVIFDriver m_driver = mock.Mock(spec=cls) - neutron = self.useFixture(k_fix.MockNeutronClient()).client + self.useFixture(k_fix.MockNeutronClient()).client port_id = lib_utils.get_hash() vm_port = self._get_fake_port(port_id)['port'] @@ -411,7 +411,7 @@ class TestNestedMacvlanPodVIFDriver(test_base.TestCase): ip_addr = ['10.0.0.29'] cls._remove_from_allowed_address_pairs( - m_driver, neutron, vm_port, frozenset(ip_addr), m_mac) + m_driver, vm_port, frozenset(ip_addr), m_mac) m_driver._update_port_address_pairs.assert_not_called() @@ -423,7 +423,7 @@ class TestNestedMacvlanPodVIFDriver(test_base.TestCase): port_id = lib_utils.get_hash() pairs = mock.sentinel.allowed_address_pairs - cls._update_port_address_pairs(m_driver, neutron, port_id, pairs, + cls._update_port_address_pairs(m_driver, port_id, pairs, revision_number=1) neutron.update_port.assert_called_with( @@ -441,7 +441,7 @@ class TestNestedMacvlanPodVIFDriver(test_base.TestCase): neutron.update_port.side_effect = n_exc.NeutronClientException self.assertRaises(n_exc.NeutronClientException, - cls._update_port_address_pairs, m_driver, neutron, + cls._update_port_address_pairs, m_driver, port_id, pairs, revision_number=1) neutron.update_port.assert_called_with( @@ -455,7 +455,7 @@ class TestNestedMacvlanPodVIFDriver(test_base.TestCase): cls = nested_macvlan_vif.NestedMacvlanPodVIFDriver m_driver = mock.Mock(spec=cls) m_driver.lock = mock.MagicMock(spec=threading.Lock()) - neutron = self.useFixture(k_fix.MockNeutronClient()).client + self.useFixture(k_fix.MockNeutronClient()).client port_id = lib_utils.get_hash() vm_port = self._get_fake_port(port_id)['port'] @@ -472,8 +472,7 @@ class TestNestedMacvlanPodVIFDriver(test_base.TestCase): ip_addr = ['10.0.0.29'] attempts = cls._try_update_port(m_driver, 3, cls._add_to_allowed_address_pairs, - neutron, vm_port, frozenset(ip_addr), - mac_addr) + vm_port, frozenset(ip_addr), mac_addr) self.assertEqual(attempts, 0) aaapf_mock.assert_called_once() @@ -483,7 +482,7 @@ class TestNestedMacvlanPodVIFDriver(test_base.TestCase): cls = nested_macvlan_vif.NestedMacvlanPodVIFDriver m_driver = mock.Mock(spec=cls) m_driver.lock = mock.MagicMock(spec=threading.Lock()) - neutron = self.useFixture(k_fix.MockNeutronClient()).client + self.useFixture(k_fix.MockNeutronClient()).client port_id = lib_utils.get_hash() vm_port = self._get_fake_port(port_id)['port'] @@ -503,7 +502,7 @@ class TestNestedMacvlanPodVIFDriver(test_base.TestCase): self.assertRaises(n_exc.NeutronClientException, cls._try_update_port, m_driver, 1, cls._add_to_allowed_address_pairs, - neutron, vm_port, frozenset(ip_addr), mac_addr) + vm_port, frozenset(ip_addr), mac_addr) # TODO(garyloug) consider exending and moving to a parent class def _get_fake_port(self, port_id=None, ip_address=None, mac_address=None): diff --git a/kuryr_kubernetes/tests/unit/controller/drivers/test_nested_vif.py b/kuryr_kubernetes/tests/unit/controller/drivers/test_nested_vif.py index f371b7205..438ca4846 100644 --- a/kuryr_kubernetes/tests/unit/controller/drivers/test_nested_vif.py +++ b/kuryr_kubernetes/tests/unit/controller/drivers/test_nested_vif.py @@ -25,7 +25,7 @@ class TestNestedPodVIFDriver(test_base.TestCase): def test_get_parent_port(self): cls = nested_vif.NestedPodVIFDriver m_driver = mock.Mock(spec=cls) - neutron = self.useFixture(k_fix.MockNeutronClient()).client + self.useFixture(k_fix.MockNeutronClient()).client node_fixed_ip = mock.sentinel.node_fixed_ip pod_status = mock.MagicMock() @@ -37,7 +37,7 @@ class TestNestedPodVIFDriver(test_base.TestCase): m_driver._get_parent_port_by_host_ip.return_value = parent_port - cls._get_parent_port(m_driver, neutron, pod) + cls._get_parent_port(m_driver, pod) m_driver._get_parent_port_by_host_ip.assert_called_once() def test_get_parent_port_by_host_ip(self): @@ -57,7 +57,7 @@ class TestNestedPodVIFDriver(test_base.TestCase): neutron.list_ports.return_value = ports self.assertEqual(port, cls._get_parent_port_by_host_ip( - m_driver, neutron, node_fixed_ip)) + m_driver, node_fixed_ip)) fixed_ips = ['subnet_id=%s' % str(node_subnet_id), 'ip_address=%s' % str(node_fixed_ip)] neutron.list_ports.assert_called_once_with(fixed_ips=fixed_ips) @@ -65,14 +65,14 @@ class TestNestedPodVIFDriver(test_base.TestCase): def test_get_parent_port_by_host_ip_subnet_id_not_configured(self): cls = nested_vif.NestedPodVIFDriver m_driver = mock.Mock(spec=cls) - neutron = self.useFixture(k_fix.MockNeutronClient()).client + self.useFixture(k_fix.MockNeutronClient()).client oslo_cfg.CONF.set_override('worker_nodes_subnet', '', group='pod_vif_nested') node_fixed_ip = mock.sentinel.node_fixed_ip self.assertRaises(oslo_cfg.RequiredOptError, cls._get_parent_port_by_host_ip, - m_driver, neutron, node_fixed_ip) + m_driver, node_fixed_ip) def test_get_parent_port_by_host_ip_trunk_not_found(self): cls = nested_vif.NestedPodVIFDriver @@ -91,7 +91,7 @@ class TestNestedPodVIFDriver(test_base.TestCase): neutron.list_ports.return_value = ports self.assertRaises(kl_exc.NoResourceException, - cls._get_parent_port_by_host_ip, m_driver, neutron, + cls._get_parent_port_by_host_ip, m_driver, node_fixed_ip) fixed_ips = ['subnet_id=%s' % str(node_subnet_id), 'ip_address=%s' % str(node_fixed_ip)] diff --git a/kuryr_kubernetes/tests/unit/controller/drivers/test_nested_vlan_vif.py b/kuryr_kubernetes/tests/unit/controller/drivers/test_nested_vlan_vif.py index 0f00041a6..94a61a12c 100644 --- a/kuryr_kubernetes/tests/unit/controller/drivers/test_nested_vlan_vif.py +++ b/kuryr_kubernetes/tests/unit/controller/drivers/test_nested_vlan_vif.py @@ -59,14 +59,12 @@ class TestNestedVlanPodVIFDriver(test_base.TestCase): self.assertEqual(vif, cls.request_vif(m_driver, pod, project_id, subnets, security_groups)) - m_driver._get_parent_port.assert_called_once_with(neutron, pod) + m_driver._get_parent_port.assert_called_once_with(pod) m_driver._get_trunk_id.assert_called_once_with(parent_port) m_driver._get_port_request.assert_called_once_with( pod, project_id, subnets, security_groups) neutron.create_port.assert_called_once_with(port_request) - m_driver._add_subport.assert_called_once_with(neutron, - trunk_id, - port_id) + m_driver._add_subport.assert_called_once_with(trunk_id, port_id) m_to_vif.assert_called_once_with(port, subnets, vlan_id) @mock.patch( @@ -106,7 +104,7 @@ class TestNestedVlanPodVIFDriver(test_base.TestCase): self.assertEqual([vif, vif], cls.request_vifs( m_driver, pod, project_id, subnets, security_groups, num_ports)) - m_driver._get_parent_port.assert_called_once_with(neutron, pod) + m_driver._get_parent_port.assert_called_once_with(pod) m_driver._get_trunk_id.assert_called_once_with(parent_port) m_driver._create_subports_info.assert_called_once_with( pod, project_id, subnets, security_groups, trunk_id, num_ports, @@ -123,7 +121,7 @@ class TestNestedVlanPodVIFDriver(test_base.TestCase): def test_request_vifs_no_vlans(self): cls = nested_vlan_vif.NestedVlanPodVIFDriver m_driver = mock.Mock(spec=cls) - neutron = self.useFixture(k_fix.MockNeutronClient()).client + self.useFixture(k_fix.MockNeutronClient()).client pod = mock.sentinel.pod project_id = mock.sentinel.project_id @@ -145,7 +143,7 @@ class TestNestedVlanPodVIFDriver(test_base.TestCase): subnets, security_groups, num_ports)) - m_driver._get_parent_port.assert_called_once_with(neutron, pod) + m_driver._get_parent_port.assert_called_once_with(pod) m_driver._get_trunk_id.assert_called_once_with(parent_port) m_driver._create_subports_info.assert_called_once_with( pod, project_id, subnets, security_groups, @@ -184,7 +182,7 @@ class TestNestedVlanPodVIFDriver(test_base.TestCase): n_exc.NeutronClientException, cls.request_vifs, m_driver, pod, project_id, subnets, security_groups, num_ports) - m_driver._get_parent_port.assert_called_once_with(neutron, pod) + m_driver._get_parent_port.assert_called_once_with(pod) m_driver._get_trunk_id.assert_called_once_with(parent_port) m_driver._create_subports_info.assert_called_once_with( pod, project_id, subnets, security_groups, @@ -225,7 +223,7 @@ class TestNestedVlanPodVIFDriver(test_base.TestCase): self.assertEqual([], cls.request_vifs(m_driver, pod, project_id, subnets, security_groups, num_ports)) - m_driver._get_parent_port.assert_called_once_with(neutron, pod) + m_driver._get_parent_port.assert_called_once_with(pod) m_driver._get_trunk_id.assert_called_once_with(parent_port) m_driver._create_subports_info.assert_called_once_with( pod, project_id, subnets, security_groups, @@ -269,7 +267,7 @@ class TestNestedVlanPodVIFDriver(test_base.TestCase): self.assertEqual([], cls.request_vifs(m_driver, pod, project_id, subnets, security_groups, num_ports)) - m_driver._get_parent_port.assert_called_once_with(neutron, pod) + m_driver._get_parent_port.assert_called_once_with(pod) m_driver._get_trunk_id.assert_called_once_with(parent_port) m_driver._create_subports_info.assert_called_once_with( pod, project_id, subnets, security_groups, @@ -293,10 +291,9 @@ class TestNestedVlanPodVIFDriver(test_base.TestCase): cls.release_vif(m_driver, pod, vif) - m_driver._get_parent_port.assert_called_once_with(neutron, pod) + m_driver._get_parent_port.assert_called_once_with(pod) m_driver._get_trunk_id.assert_called_once_with(parent_port) - m_driver._remove_subport.assert_called_once_with( - neutron, trunk_id, vif.id) + m_driver._remove_subport.assert_called_once_with(trunk_id, vif.id) neutron.delete_port.assert_called_once_with(vif.id) def test_release_vif_not_found(self): @@ -317,10 +314,9 @@ class TestNestedVlanPodVIFDriver(test_base.TestCase): cls.release_vif(m_driver, pod, vif) - m_driver._get_parent_port.assert_called_once_with(neutron, pod) + m_driver._get_parent_port.assert_called_once_with(pod) m_driver._get_trunk_id.assert_called_once_with(parent_port) - m_driver._remove_subport.assert_called_once_with( - neutron, trunk_id, vif.id) + m_driver._remove_subport.assert_called_once_with(trunk_id, vif.id) neutron.delete_port.assert_called_once_with(vif.id) def _test_get_port_request(self, m_to_fips, security_groups, @@ -523,8 +519,8 @@ class TestNestedVlanPodVIFDriver(test_base.TestCase): 'port_id': subport, 'segmentation_type': 'vlan'}] nested_vlan_vif.DEFAULT_MAX_RETRY_COUNT = 1 - self.assertEqual(vlan_id, cls._add_subport(m_driver, - neutron, trunk_id, subport)) + self.assertEqual(vlan_id, cls._add_subport(m_driver, trunk_id, + subport)) m_driver._get_vlan_id.assert_called_once_with(trunk_id) neutron.trunk_add_subports.assert_called_once_with( trunk_id, {'sub_ports': subport_dict}) @@ -532,14 +528,13 @@ class TestNestedVlanPodVIFDriver(test_base.TestCase): def test_add_subport_get_vlanid_failure(self): cls = nested_vlan_vif.NestedVlanPodVIFDriver m_driver = mock.Mock(spec=cls) - neutron = self.useFixture(k_fix.MockNeutronClient()).client + self.useFixture(k_fix.MockNeutronClient()).client trunk_id = mock.sentinel.trunk_id subport = mock.sentinel.subport m_driver._get_vlan_id.side_effect = n_exc.NeutronClientException nested_vlan_vif.DEFAULT_MAX_RETRY_COUNT = 1 - self.assertRaises( - n_exc.NeutronClientException, cls._add_subport, - m_driver, neutron, trunk_id, subport) + self.assertRaises(n_exc.NeutronClientException, cls._add_subport, + m_driver, trunk_id, subport) m_driver._get_vlan_id.assert_called_once_with(trunk_id) @@ -557,7 +552,7 @@ class TestNestedVlanPodVIFDriver(test_base.TestCase): neutron.trunk_add_subports.side_effect = n_exc.Conflict nested_vlan_vif.DEFAULT_MAX_RETRY_COUNT = 1 self.assertRaises(n_exc.Conflict, cls._add_subport, m_driver, - neutron, trunk_id, subport) + trunk_id, subport) neutron.trunk_add_subports.assert_called_once_with( trunk_id, {'sub_ports': subport_dict}) @@ -569,7 +564,7 @@ class TestNestedVlanPodVIFDriver(test_base.TestCase): trunk_id = mock.sentinel.trunk_id subport_id = mock.sentinel.subport_id subportid_dict = [{'port_id': subport_id}] - cls._remove_subports(m_driver, neutron, trunk_id, [subport_id]) + cls._remove_subports(m_driver, trunk_id, [subport_id]) neutron.trunk_remove_subports.assert_called_once_with( trunk_id, {'sub_ports': subportid_dict}) @@ -581,8 +576,7 @@ class TestNestedVlanPodVIFDriver(test_base.TestCase): trunk_id = mock.sentinel.trunk_id subport_id = mock.sentinel.subport_id subportid_dict = [{'port_id': subport_id}] - cls._remove_subports(m_driver, neutron, trunk_id, [subport_id, - subport_id]) + cls._remove_subports(m_driver, trunk_id, [subport_id, subport_id]) neutron.trunk_remove_subports.assert_called_once_with( trunk_id, {'sub_ports': subportid_dict}) diff --git a/kuryr_kubernetes/tests/unit/controller/drivers/test_vif_pool.py b/kuryr_kubernetes/tests/unit/controller/drivers/test_vif_pool.py index b03a5a62f..4e001a2c5 100644 --- a/kuryr_kubernetes/tests/unit/controller/drivers/test_vif_pool.py +++ b/kuryr_kubernetes/tests/unit/controller/drivers/test_vif_pool.py @@ -1369,8 +1369,7 @@ class NestedVIFPool(test_base.TestCase): neutron.update_port.assert_not_called() neutron.delete_port.assert_called_once_with(port_id) m_driver._get_trunk_id.assert_called_once() - m_driver._drv_vif._remove_subport.assert_called_once_with(neutron, - trunk_id, + m_driver._drv_vif._remove_subport.assert_called_once_with(trunk_id, port_id) @mock.patch('kuryr_kubernetes.controller.drivers.utils.get_ports_by_attrs') @@ -1440,8 +1439,7 @@ class NestedVIFPool(test_base.TestCase): neutron.update_port.assert_not_called() m_driver._get_trunk_id.assert_called_once() - m_driver._drv_vif._remove_subport.assert_called_once_with(neutron, - trunk_id, + m_driver._drv_vif._remove_subport.assert_called_once_with(trunk_id, port_id) neutron.delete_port.assert_called_once_with(port_id) @@ -1475,8 +1473,7 @@ class NestedVIFPool(test_base.TestCase): neutron.update_port.assert_not_called() m_driver._get_trunk_id.assert_called_once() - m_driver._drv_vif._remove_subport.assert_called_once_with(neutron, - trunk_id, + m_driver._drv_vif._remove_subport.assert_called_once_with(trunk_id, port_id) neutron.delete_port.assert_not_called() @@ -1895,9 +1892,8 @@ class NestedVIFPool(test_base.TestCase): m_driver._trigger_return_to_pool.assert_called_once() m_driver._get_pool_key_net.assert_called_once() - m_driver._get_trunk_id.assert_called_once_with(neutron, pool_key) - m_driver._drv_vif._remove_subports.assert_called_once_with(neutron, - trunk_id, + m_driver._get_trunk_id.assert_called_once_with(pool_key) + m_driver._drv_vif._remove_subports.assert_called_once_with(trunk_id, [port_id]) m_driver._drv_vif._release_vlan_id.assert_called_once_with(vlan_id) neutron.delete_port.assert_called_once_with(port_id) @@ -1952,9 +1948,8 @@ class NestedVIFPool(test_base.TestCase): m_driver._trigger_return_to_pool.assert_called_once() m_driver._get_pool_key_net.assert_called_once() - m_driver._get_trunk_id.assert_called_once_with(neutron, pool_key) - m_driver._drv_vif._remove_subports.assert_called_once_with(neutron, - trunk_id, + m_driver._get_trunk_id.assert_called_once_with(pool_key) + m_driver._drv_vif._remove_subports.assert_called_once_with(trunk_id, [port_id]) m_driver._drv_vif._release_vlan_id.assert_not_called() neutron.delete_port.assert_not_called() @@ -1988,9 +1983,8 @@ class NestedVIFPool(test_base.TestCase): m_driver._trigger_return_to_pool.assert_called_once() m_driver._get_pool_key_net.assert_called_once() - m_driver._get_trunk_id.assert_called_once_with(neutron, pool_key) - m_driver._drv_vif._remove_subports.assert_called_once_with(neutron, - trunk_id, + m_driver._get_trunk_id.assert_called_once_with(pool_key) + m_driver._drv_vif._remove_subports.assert_called_once_with(trunk_id, [port_id]) m_driver._drv_vif._release_vlan_id.assert_not_called() neutron.delete_port.assert_called_once_with(port_id)