Browse Source

Merge "[OVN] Simplify connection creation logic" into stable/train

changes/39/792539/1
Zuul 3 weeks ago
committed by Gerrit Code Review
parent
commit
5b81ebeccd
6 changed files with 45 additions and 43 deletions
  1. +4
    -4
      networking_ovn/cmd/neutron_ovn_db_sync_util.py
  2. +1
    -2
      networking_ovn/ml2/mech_driver.py
  3. +25
    -24
      networking_ovn/ovsdb/impl_idl_ovn.py
  4. +12
    -11
      networking_ovn/tests/unit/cmd/test_neutron_ovn_db_sync_util.py
  5. +1
    -1
      networking_ovn/tests/unit/ovsdb/test_impl_idl_ovn.py
  6. +2
    -1
      networking_ovn/tests/unit/test_ovn_db_sync.py

+ 4
- 4
networking_ovn/cmd/neutron_ovn_db_sync_util.py View File

@ -26,6 +26,7 @@ from networking_ovn.common import config as ovn_config
from networking_ovn.ml2 import mech_driver
from networking_ovn import ovn_db_sync
from networking_ovn.ovsdb import impl_idl_ovn
from networking_ovn.ovsdb import worker
LOG = logging.getLogger(__name__)
@ -184,16 +185,15 @@ def main():
LOG.error('Invalid core plugin : ["%s"].', cfg.CONF.core_plugin)
return
mech_worker = worker.MaintenanceWorker
try:
conn = impl_idl_ovn.get_connection(impl_idl_ovn.OvsdbNbOvnIdl)
ovn_api = impl_idl_ovn.OvsdbNbOvnIdl(conn)
ovn_api = impl_idl_ovn.OvsdbNbOvnIdl.from_worker(mech_worker)
except RuntimeError:
LOG.error('Invalid --ovn-ovn_nb_connection parameter provided.')
return
try:
sb_conn = impl_idl_ovn.get_connection(impl_idl_ovn.OvsdbSbOvnIdl)
ovn_sb_api = impl_idl_ovn.OvsdbSbOvnIdl(sb_conn)
ovn_sb_api = impl_idl_ovn.OvsdbSbOvnIdl.from_worker(mech_worker)
except RuntimeError:
LOG.error('Invalid --ovn-ovn_sb_connection parameter provided.')
return


+ 1
- 2
networking_ovn/ml2/mech_driver.py View File

@ -230,8 +230,7 @@ class OVNMechanismDriver(api.MechanismDriver):
if not is_maintenance:
self.node_uuid = db_hash_ring.add_node(self.hash_ring_group)
self._nb_ovn, self._sb_ovn = impl_idl_ovn.get_ovn_idls(
self, trigger, binding_events=not is_maintenance)
self._nb_ovn, self._sb_ovn = impl_idl_ovn.get_ovn_idls(self, trigger)
if self._sb_ovn.is_table_present('Chassis_Private'):
self.agent_chassis_table = 'Chassis_Private'


+ 25
- 24
networking_ovn/ovsdb/impl_idl_ovn.py View File

@ -33,6 +33,7 @@ from networking_ovn.common import exceptions as ovn_exc
from networking_ovn.common import utils
from networking_ovn.ovsdb import commands as cmd
from networking_ovn.ovsdb import ovsdb_monitor
from networking_ovn.ovsdb import worker
LOG = log.getLogger(__name__)
@ -135,45 +136,35 @@ class OvsdbConnectionUnavailable(n_exc.ServiceUnavailable):
# Retry forever to get the OVN NB and SB IDLs. Wait 2^x * 1 seconds between
# each retry, up to 'max_interval' seconds, then interval will be fixed
# to 'max_interval' seconds afterwards. The default 'max_interval' is 180.
def get_ovn_idls(driver, trigger, binding_events=False):
def get_ovn_idls(driver, trigger):
@tenacity.retry(
wait=tenacity.wait_exponential(
max=cfg.get_ovn_ovsdb_retry_max_interval()),
reraise=True)
def get_ovn_idl_retry(cls):
def get_ovn_idl_retry(api_cls):
trigger_class = utils.get_method_class(trigger)
LOG.info('Getting %(cls)s for %(trigger)s with retry',
{'cls': cls.__name__, 'trigger': trigger_class.__name__})
return cls(get_connection(cls, trigger, driver, binding_events))
{'cls': api_cls.__name__, 'trigger': trigger_class.__name__})
return api_cls.from_worker(trigger_class, driver)
vlog.use_python_logger(max_level=cfg.get_ovn_ovsdb_log_level())
return tuple(get_ovn_idl_retry(c) for c in (OvsdbNbOvnIdl, OvsdbSbOvnIdl))
def get_connection(db_class, trigger=None, driver=None, binding_events=False):
if db_class == OvsdbNbOvnIdl:
args = (cfg.get_ovn_nb_connection(), 'OVN_Northbound')
elif db_class == OvsdbSbOvnIdl:
args = (cfg.get_ovn_sb_connection(), 'OVN_Southbound')
if binding_events:
if db_class == OvsdbNbOvnIdl:
idl_ = ovsdb_monitor.OvnNbIdl.from_server(*args, driver=driver)
else:
idl_ = ovsdb_monitor.OvnSbIdl.from_server(*args, driver=driver)
else:
if db_class == OvsdbNbOvnIdl:
idl_ = ovsdb_monitor.BaseOvnIdl.from_server(*args)
else:
idl_ = ovsdb_monitor.BaseOvnSbIdl.from_server(*args)
return connection.Connection(idl_, timeout=cfg.get_ovn_ovsdb_timeout())
class OvsdbNbOvnIdl(nb_impl_idl.OvnNbApiIdlImpl, Backend):
def __init__(self, connection):
super(OvsdbNbOvnIdl, self).__init__(connection)
@classmethod
def from_worker(cls, worker_class, driver=None):
args = (cfg.get_ovn_nb_connection(), 'OVN_Northbound')
if worker_class == worker.MaintenanceWorker:
idl_ = ovsdb_monitor.BaseOvnIdl.from_server(*args)
else:
idl_ = ovsdb_monitor.OvnNbIdl.from_server(*args, driver=driver)
conn = connection.Connection(idl_, timeout=cfg.get_ovn_ovsdb_timeout())
return cls(conn)
@property
def nb_global(self):
return next(iter(self.tables['NB_Global'].rows.values()))
@ -734,6 +725,16 @@ class OvsdbSbOvnIdl(sb_impl_idl.OvnSbApiIdlImpl, Backend):
def __init__(self, connection):
super(OvsdbSbOvnIdl, self).__init__(connection)
@classmethod
def from_worker(cls, worker_class, driver=None):
args = (cfg.get_ovn_sb_connection(), 'OVN_Southbound')
if worker_class == worker.MaintenanceWorker:
idl_ = ovsdb_monitor.BaseOvnSbIdl.from_server(*args)
else:
idl_ = ovsdb_monitor.OvnSbIdl.from_server(*args, driver=driver)
conn = connection.Connection(idl_, timeout=cfg.get_ovn_ovsdb_timeout())
return cls(conn)
def _get_chassis_physnets(self, chassis):
bridge_mappings = chassis.external_ids.get('ovn-bridge-mappings', '')
mapping_dict = helpers.parse_mappings(bridge_mappings.split(','),


+ 12
- 11
networking_ovn/tests/unit/cmd/test_neutron_ovn_db_sync_util.py View File

@ -16,7 +16,7 @@ import mock
from networking_ovn.cmd import neutron_ovn_db_sync_util as cmd
from networking_ovn.tests import base
from networking_ovn.ovsdb import impl_idl_ovn
class TestNeutronOVNDBSyncUtil(base.TestCase):
@ -48,11 +48,11 @@ class TestNeutronOVNDBSyncUtil(base.TestCase):
@mock.patch('oslo_log.log.setup')
@mock.patch('networking_ovn.cmd.neutron_ovn_db_sync_util.setup_conf')
@mock.patch('networking_ovn.ovsdb.impl_idl_ovn.get_connection')
def test_main_invalid_nb_idl(self, mock_con, mock_conf, mock_log_setup):
def test_main_invalid_nb_idl(self, mock_conf, mock_log_setup):
with mock.patch('oslo_config.cfg.CONF') as mock_cfg, \
mock.patch('networking_ovn.ovsdb.impl_idl_ovn.OvsdbNbOvnIdl',
side_effect=RuntimeError):
mock.patch('networking_ovn.ovsdb.impl_idl_ovn.OvsdbNbOvnIdl.'
'from_worker',
side_effect=RuntimeError):
self._setup_default_mock_cfg(mock_cfg)
cmd.main()
self.cmd_log.error.assert_called_once_with(
@ -60,11 +60,12 @@ class TestNeutronOVNDBSyncUtil(base.TestCase):
@mock.patch('oslo_log.log.setup')
@mock.patch('networking_ovn.cmd.neutron_ovn_db_sync_util.setup_conf')
@mock.patch('networking_ovn.ovsdb.impl_idl_ovn.get_connection')
def test_main_invalid_sb_idl(self, mock_con, mock_conf, mock_log_setup):
def test_main_invalid_sb_idl(self, mock_conf, mock_log_setup):
with mock.patch('oslo_config.cfg.CONF') as mock_cfg, \
mock.patch('networking_ovn.ovsdb.impl_idl_ovn.OvsdbSbOvnIdl',
side_effect=RuntimeError):
mock.patch.object(impl_idl_ovn.OvsdbNbOvnIdl,'from_worker',
return_value=mock.Mock()), \
mock.patch('networking_ovn.ovsdb.impl_idl_ovn.OvsdbSbOvnIdl.'
'from_worker', side_effect=RuntimeError):
self._setup_default_mock_cfg(mock_cfg)
cmd.main()
self.cmd_log.error.assert_called_once_with(
@ -75,8 +76,8 @@ class TestNeutronOVNDBSyncUtil(base.TestCase):
@mock.patch('networking_ovn.ovsdb.impl_idl_ovn.OvsdbNbOvnIdl')
@mock.patch('oslo_log.log.setup')
@mock.patch('networking_ovn.cmd.neutron_ovn_db_sync_util.setup_conf')
@mock.patch('networking_ovn.ovsdb.impl_idl_ovn.get_connection')
def _test_main(self, mock_con, mock_conf, mock_log_setup, mock_nb_idl,
@mock.patch.object(impl_idl_ovn.OvsdbSbOvnIdl, 'from_worker', mock.Mock())
def _test_main(self, mock_conf, mock_log_setup, mock_nb_idl,
mock_plugin, mock_manager_init):
cmd.main()


+ 1
- 1
networking_ovn/tests/unit/ovsdb/test_impl_idl_ovn.py View File

@ -338,7 +338,7 @@ class TestNBImplIdlOvn(TestDBImplIdlOvn):
self._tables['DHCP_Options'] = self.dhcp_table
self._tables['Address_Set'] = self.address_set_table
with mock.patch.object(impl_idl_ovn, 'get_connection',
with mock.patch.object(impl_idl_ovn.OvsdbNbOvnIdl, 'from_worker',
return_value=mock.Mock()):
impl_idl_ovn.OvsdbNbOvnIdl.ovsdb_connection = None
self.nb_ovn_idl = impl_idl_ovn.OvsdbNbOvnIdl(mock.Mock())


+ 2
- 1
networking_ovn/tests/unit/test_ovn_db_sync.py View File

@ -21,6 +21,7 @@ from networking_ovn.common import constants as ovn_const
from networking_ovn.common import ovn_client
from networking_ovn import ovn_db_sync
from networking_ovn.tests.unit.ml2 import test_mech_driver
from networking_ovn.ovsdb import impl_idl_ovn
OvnPortInfo = collections.namedtuple('OvnPortInfo', ['name'])
@ -547,7 +548,7 @@ class TestOvnNbSyncML2(test_mech_driver.OVNMechanismDriverTestCase):
self._test_mocks_helper(ovn_nb_synchronizer)
ovn_api = ovn_nb_synchronizer.ovn_api
mock.patch("networking_ovn.ovsdb.impl_idl_ovn.get_connection").start()
mock.patch.object(impl_idl_ovn.OvsdbNbOvnIdl, 'from_worker').start()
ovn_nb_synchronizer.do_sync()


Loading…
Cancel
Save