From dabe21ccbc9df5166607bbd4ab51c2a131bad19c Mon Sep 17 00:00:00 2001 From: ricolin Date: Tue, 6 Aug 2024 17:07:10 +0800 Subject: [PATCH] Add Octavia OVN DBs sync cmd This is initial patch of a chain to allow ovn_octavia_provider to be able to sync Octavia DB to OVN NB DB to sync content, getting as source of true the Octavia DB one. This patch add command sync command `octavia-ovn-db-sync-util` Related-Bug: #2045415 Co-authored-by: Fernando Royo Co-authored-by: Rico Lin Change-Id: I0b1fee7a75e0a2a837e89766ea3c3198e0929823 --- ovn_octavia_provider/cmd/__init__.py | 0 .../cmd/octavia_ovn_db_sync_util.py | 59 +++++++++++++++++++ ovn_octavia_provider/driver.py | 5 ++ ovn_octavia_provider/tests/unit/test_cmd.py | 56 ++++++++++++++++++ setup.cfg | 3 + 5 files changed, 123 insertions(+) create mode 100644 ovn_octavia_provider/cmd/__init__.py create mode 100644 ovn_octavia_provider/cmd/octavia_ovn_db_sync_util.py create mode 100644 ovn_octavia_provider/tests/unit/test_cmd.py diff --git a/ovn_octavia_provider/cmd/__init__.py b/ovn_octavia_provider/cmd/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/ovn_octavia_provider/cmd/octavia_ovn_db_sync_util.py b/ovn_octavia_provider/cmd/octavia_ovn_db_sync_util.py new file mode 100644 index 00000000..b42e7ac6 --- /dev/null +++ b/ovn_octavia_provider/cmd/octavia_ovn_db_sync_util.py @@ -0,0 +1,59 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import sys + +from oslo_config import cfg +from oslo_log import log as logging +from ovn_octavia_provider.common import config as ovn_conf +from ovn_octavia_provider import driver + +CONF = cfg.CONF +LOG = logging.getLogger(__name__) + + +def setup_conf(): + conf = cfg.CONF + ovn_conf.register_opts() + logging.register_options(CONF) + + try: + CONF(project='octavia') + except TypeError: + LOG.error('Error parsing the configuration values. Please verify.') + raise + return conf + + +def main(): + """Main method for syncing Octavia LBs (OVN provider) with OVN NB DB. + + This script provides a utility for syncing the OVN Northbound Database + with the Octavia database. + + """ + setup_conf() + logging.setup(CONF, 'octavia_ovn_db_sync_util') + + # Method can be call like `octavia-ovn-db-sync-util --debug` + LOG.info("OVN Octavia DB sync start.") + args = sys.argv[1:] + lb_filters = {'provider': 'ovn'} + if '--debug' in args: + cfg.CONF.set_override('debug', True) + args.remove('--debug') + else: + cfg.CONF.set_override('debug', False) + + ovn_driver = driver.OvnProviderDriver() + ovn_driver.do_sync(**lb_filters) + LOG.info("OVN Octavia DB sync finish.") diff --git a/ovn_octavia_provider/driver.py b/ovn_octavia_provider/driver.py index 50a82428..5e60b34f 100644 --- a/ovn_octavia_provider/driver.py +++ b/ovn_octavia_provider/driver.py @@ -585,3 +585,8 @@ class OvnProviderDriver(driver_base.ProviderDriver): request = {'type': ovn_const.REQ_TYPE_HM_DELETE, 'info': request_info} self._ovn_helper.add_request(request) + + def do_sync(self, **lb_filters): + LOG.info(f"Starting sync OVN DB with Loadbalancer filter {lb_filters}") + # TODO(froyo): get LBs from Octavia DB through openstack sdk client and + # call to helper methods to sync diff --git a/ovn_octavia_provider/tests/unit/test_cmd.py b/ovn_octavia_provider/tests/unit/test_cmd.py new file mode 100644 index 00000000..43f49adc --- /dev/null +++ b/ovn_octavia_provider/tests/unit/test_cmd.py @@ -0,0 +1,56 @@ +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +import sys +from unittest import mock + +from oslo_config import cfg +from oslo_log import log + +from ovn_octavia_provider.cmd import octavia_ovn_db_sync_util +from ovn_octavia_provider import driver +from ovn_octavia_provider.tests.unit import base as ovn_base + + +class TestCMD(ovn_base.TestOvnOctaviaBase): + + def setUp(self): + super().setUp() + mock.patch.object(log, 'register_options').start() + self.m_cfg = mock.patch.object( + cfg.ConfigOpts, '__call__').start() + + @mock.patch.object(driver.OvnProviderDriver, 'do_sync') + def test_octavia_ovn_db_sync_util(self, m_sync): + octavia_ovn_db_sync_util.main() + m_sync.assert_called_once_with(provider='ovn') + + @mock.patch.object(cfg.CONF, 'set_override') + @mock.patch.object(driver.OvnProviderDriver, 'do_sync') + def test_octavia_ovn_db_sync_util_with_debug(self, m_sync, m_cfg_or): + return_value = ['octavia-ovn-db-sync-util', + '--debug'] + return_value_no_debug = ['octavia-ovn-db-sync-util'] + with mock.patch.object(sys, 'argv', return_value): + octavia_ovn_db_sync_util.main() + with mock.patch.object(sys, 'argv', return_value_no_debug): + octavia_ovn_db_sync_util.main() + m_cfg_or.assert_has_calls([mock.call('debug', True), + mock.call('debug', False)]) + + @mock.patch.object(octavia_ovn_db_sync_util, 'LOG') + def test_octavia_ovn_db_sync_util_config_error(self, m_log): + self.m_cfg.side_effect = [TypeError()] + self.assertRaises(TypeError, octavia_ovn_db_sync_util.main) + msg = ("Error parsing the configuration values. Please verify.") + m_log.error.assert_called_once_with(msg) diff --git a/setup.cfg b/setup.cfg index 124d0db4..4f5b59d5 100644 --- a/setup.cfg +++ b/setup.cfg @@ -32,6 +32,9 @@ setup_hooks = octavia.api.drivers = ovn = ovn_octavia_provider.driver:OvnProviderDriver +console_scripts = + octavia-ovn-db-sync-util = ovn_octavia_provider.cmd.octavia_ovn_db_sync_util:main + octavia.driver_agent.provider_agents = ovn = ovn_octavia_provider.agent:OvnProviderAgent