Merge "Add Octavia OVN DBs sync cmd"

This commit is contained in:
Zuul 2025-01-09 11:01:19 +00:00 committed by Gerrit Code Review
commit 29d17aa2b3
5 changed files with 123 additions and 0 deletions

View File

View File

@ -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.")

View File

@ -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

View File

@ -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)

View File

@ -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