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
This commit is contained in:
Hongbin Lu 2018-10-28 22:28:50 +00:00
parent f0e48ebaa0
commit 542cb5aabd
2 changed files with 8 additions and 3 deletions

View File

@ -1716,6 +1716,7 @@ def ipam_request_address():
fixed_ips=fixed_ip_existing).get('ports', []) fixed_ips=fixed_ip_existing).get('ports', [])
if not filtered_ports: if not filtered_ports:
filtered_ports = app.neutron.list_ports( filtered_ports = app.neutron.list_ports(
fixed_ips='subnet_id=%s' % subnet['id'],
mac_address=req_mac_address).get('ports', []) mac_address=req_mac_address).get('ports', [])
num_ports = len(filtered_ports) num_ports = len(filtered_ports)

View File

@ -798,7 +798,9 @@ class TestKuryrIpam(base.TestKuryrBase):
subnetpool_id=fake_kuryr_subnetpool_id) subnetpool_id=fake_kuryr_subnetpool_id)
mock_list_ports.assert_has_calls([ mock_list_ports.assert_has_calls([
mock.call(fixed_ips=fixed_ip_existing), 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}) mock_create_port.assert_called_with({'port': port_request})
if mock_app.tag_ext: if mock_app.tag_ext:
mock_port_add_tag.assert_called() 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_id),
mock.call(subnetpool_id=fake_kuryr_subnetpool_v6_id)]) mock.call(subnetpool_id=fake_kuryr_subnetpool_v6_id)])
mock_list_ports.assert_has_calls([ 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, mock_update_port.assert_called_with(fake_neutron_port_id,
{'port': update_port}) {'port': update_port})
if mock_app.tag_ext: if mock_app.tag_ext: