diff --git a/neutron/plugins/ml2/drivers/ovn/mech_driver/ovsdb/ovn_db_sync.py b/neutron/plugins/ml2/drivers/ovn/mech_driver/ovsdb/ovn_db_sync.py index 11d87839328..8637688f456 100644 --- a/neutron/plugins/ml2/drivers/ovn/mech_driver/ovsdb/ovn_db_sync.py +++ b/neutron/plugins/ml2/drivers/ovn/mech_driver/ovsdb/ovn_db_sync.py @@ -870,10 +870,20 @@ class OvnNbSynchronizer(OvnDbSynchronizer): LOG.warning('DHCP options for subnet %s present in ' 'Neutron but out of sync with OVN NB DB', subnet_id) if self.mode == SYNC_MODE_REPAIR: + # If neutron-server is running we could race and find a + # subnet without a cached network, just skip it to avoid + # a KeyError below. + network_id = utils.ovn_name(subnet['network_id']) + if network_id not in db_networks: + LOG.warning('Network %s for subnet %s not found in OVN NB ' + 'DB network cache, possible race condition, ' + 'please check that neutron-server is stopped! ' + 'Skipping subnet.', network_id, subnet_id) + continue try: LOG.warning('Adding/Updating DHCP options for subnet %s ' 'in OVN NB DB', subnet_id) - network = db_networks[utils.ovn_name(subnet['network_id'])] + network = db_networks[network_id] # _ovn_client._add_subnet_dhcp_options doesn't create # a new row in DHCP_Options if the row already exists. # See commands.AddDHCPOptionsCommand. diff --git a/neutron/tests/unit/plugins/ml2/drivers/ovn/mech_driver/ovsdb/test_ovn_db_sync.py b/neutron/tests/unit/plugins/ml2/drivers/ovn/mech_driver/ovsdb/test_ovn_db_sync.py index 6d9a060b014..67f078ed478 100644 --- a/neutron/tests/unit/plugins/ml2/drivers/ovn/mech_driver/ovsdb/test_ovn_db_sync.py +++ b/neutron/tests/unit/plugins/ml2/drivers/ovn/mech_driver/ovsdb/test_ovn_db_sync.py @@ -104,6 +104,17 @@ class TestOvnNbSyncML2(test_mech_driver.OVNMechanismDriverTestCase): 'gateway_ip': '20.0.0.1', 'dns_nameservers': [], 'host_routes': [], + 'ip_version': 4}, + # A subnet without a known network should be skipped, + # see bug #2045811 + {'id': 'notfound', + 'network_id': 'notfound', + 'enable_dhcp': True, + 'cidr': '30.0.0.0/24', + 'tenant_id': 'tenant1', + 'gateway_ip': '30.0.0.1', + 'dns_nameservers': [], + 'host_routes': [], 'ip_version': 4}] self.security_groups = [