From 542cb5aabd73a7bf9865dacd68f484aea49ae71e Mon Sep 17 00:00:00 2001 From: Hongbin Lu Date: Sun, 28 Oct 2018 22:28:50 +0000 Subject: [PATCH] Search ports with matching subnet On ipam_request_address, it will try to find existing Neutron ports if any. If there is no existing Neutron ports, kuryr will create one. Kuryr will search ports with matching IP address first. If the first search is not successful, it will do a second search to find ports with matching mac address. This commit improve the second search by matching the subnet_id as well as the mac address. This is because things will go wrong if the port is in a different subnet (e.g. an ipv6 address is requested while the port is in a v4 subnet). Change-Id: I1eaa9c87079f1c57babbe08ae52ed69a44cfa9aa Related-Bug: #1800375 --- kuryr_libnetwork/controllers.py | 1 + kuryr_libnetwork/tests/unit/test_kuryr_ipam.py | 10 +++++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/kuryr_libnetwork/controllers.py b/kuryr_libnetwork/controllers.py index 5c7c42a8..9ef71b43 100644 --- a/kuryr_libnetwork/controllers.py +++ b/kuryr_libnetwork/controllers.py @@ -1716,6 +1716,7 @@ def ipam_request_address(): fixed_ips=fixed_ip_existing).get('ports', []) if not filtered_ports: filtered_ports = app.neutron.list_ports( + fixed_ips='subnet_id=%s' % subnet['id'], mac_address=req_mac_address).get('ports', []) num_ports = len(filtered_ports) diff --git a/kuryr_libnetwork/tests/unit/test_kuryr_ipam.py b/kuryr_libnetwork/tests/unit/test_kuryr_ipam.py index e0720ceb..d7b2e425 100644 --- a/kuryr_libnetwork/tests/unit/test_kuryr_ipam.py +++ b/kuryr_libnetwork/tests/unit/test_kuryr_ipam.py @@ -798,7 +798,9 @@ class TestKuryrIpam(base.TestKuryrBase): subnetpool_id=fake_kuryr_subnetpool_id) mock_list_ports.assert_has_calls([ mock.call(fixed_ips=fixed_ip_existing), - mock.call(mac_address=fake_mac_address)]) + mock.call( + mac_address=fake_mac_address, + fixed_ips='subnet_id=%s' % fake_v4_subnet['subnet']['id'])]) mock_create_port.assert_called_with({'port': port_request}) if mock_app.tag_ext: mock_port_add_tag.assert_called() @@ -1030,8 +1032,10 @@ class TestKuryrIpam(base.TestKuryrBase): mock.call(subnetpool_id=fake_kuryr_subnetpool_id), mock.call(subnetpool_id=fake_kuryr_subnetpool_v6_id)]) mock_list_ports.assert_has_calls([ - mock.call(mac_address=requested_mac_address), - mock.call(mac_address=requested_mac_address)]) + mock.call(mac_address=requested_mac_address, + fixed_ips='subnet_id=%s' % subnet_v4_id), + mock.call(mac_address=requested_mac_address, + fixed_ips='subnet_id=%s' % subnet_v6_id)]) mock_update_port.assert_called_with(fake_neutron_port_id, {'port': update_port}) if mock_app.tag_ext: