Skip to content

Commit dabe21c

Browse files
committed
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 <[email protected]> Co-authored-by: Rico Lin <[email protected]> Change-Id: I0b1fee7a75e0a2a837e89766ea3c3198e0929823
1 parent 69231fd commit dabe21c

File tree

5 files changed

+123
-0
lines changed

5 files changed

+123
-0
lines changed

ovn_octavia_provider/cmd/__init__.py

Whitespace-only changes.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
2+
# not use this file except in compliance with the License. You may obtain
3+
# a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10+
# License for the specific language governing permissions and limitations
11+
# under the License.
12+
13+
import sys
14+
15+
from oslo_config import cfg
16+
from oslo_log import log as logging
17+
from ovn_octavia_provider.common import config as ovn_conf
18+
from ovn_octavia_provider import driver
19+
20+
CONF = cfg.CONF
21+
LOG = logging.getLogger(__name__)
22+
23+
24+
def setup_conf():
25+
conf = cfg.CONF
26+
ovn_conf.register_opts()
27+
logging.register_options(CONF)
28+
29+
try:
30+
CONF(project='octavia')
31+
except TypeError:
32+
LOG.error('Error parsing the configuration values. Please verify.')
33+
raise
34+
return conf
35+
36+
37+
def main():
38+
"""Main method for syncing Octavia LBs (OVN provider) with OVN NB DB.
39+
40+
This script provides a utility for syncing the OVN Northbound Database
41+
with the Octavia database.
42+
43+
"""
44+
setup_conf()
45+
logging.setup(CONF, 'octavia_ovn_db_sync_util')
46+
47+
# Method can be call like `octavia-ovn-db-sync-util --debug`
48+
LOG.info("OVN Octavia DB sync start.")
49+
args = sys.argv[1:]
50+
lb_filters = {'provider': 'ovn'}
51+
if '--debug' in args:
52+
cfg.CONF.set_override('debug', True)
53+
args.remove('--debug')
54+
else:
55+
cfg.CONF.set_override('debug', False)
56+
57+
ovn_driver = driver.OvnProviderDriver()
58+
ovn_driver.do_sync(**lb_filters)
59+
LOG.info("OVN Octavia DB sync finish.")

ovn_octavia_provider/driver.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,3 +585,8 @@ def health_monitor_delete(self, healthmonitor):
585585
request = {'type': ovn_const.REQ_TYPE_HM_DELETE,
586586
'info': request_info}
587587
self._ovn_helper.add_request(request)
588+
589+
def do_sync(self, **lb_filters):
590+
LOG.info(f"Starting sync OVN DB with Loadbalancer filter {lb_filters}")
591+
# TODO(froyo): get LBs from Octavia DB through openstack sdk client and
592+
# call to helper methods to sync
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#
2+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
3+
# not use this file except in compliance with the License. You may obtain
4+
# a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11+
# License for the specific language governing permissions and limitations
12+
# under the License.
13+
#
14+
import sys
15+
from unittest import mock
16+
17+
from oslo_config import cfg
18+
from oslo_log import log
19+
20+
from ovn_octavia_provider.cmd import octavia_ovn_db_sync_util
21+
from ovn_octavia_provider import driver
22+
from ovn_octavia_provider.tests.unit import base as ovn_base
23+
24+
25+
class TestCMD(ovn_base.TestOvnOctaviaBase):
26+
27+
def setUp(self):
28+
super().setUp()
29+
mock.patch.object(log, 'register_options').start()
30+
self.m_cfg = mock.patch.object(
31+
cfg.ConfigOpts, '__call__').start()
32+
33+
@mock.patch.object(driver.OvnProviderDriver, 'do_sync')
34+
def test_octavia_ovn_db_sync_util(self, m_sync):
35+
octavia_ovn_db_sync_util.main()
36+
m_sync.assert_called_once_with(provider='ovn')
37+
38+
@mock.patch.object(cfg.CONF, 'set_override')
39+
@mock.patch.object(driver.OvnProviderDriver, 'do_sync')
40+
def test_octavia_ovn_db_sync_util_with_debug(self, m_sync, m_cfg_or):
41+
return_value = ['octavia-ovn-db-sync-util',
42+
'--debug']
43+
return_value_no_debug = ['octavia-ovn-db-sync-util']
44+
with mock.patch.object(sys, 'argv', return_value):
45+
octavia_ovn_db_sync_util.main()
46+
with mock.patch.object(sys, 'argv', return_value_no_debug):
47+
octavia_ovn_db_sync_util.main()
48+
m_cfg_or.assert_has_calls([mock.call('debug', True),
49+
mock.call('debug', False)])
50+
51+
@mock.patch.object(octavia_ovn_db_sync_util, 'LOG')
52+
def test_octavia_ovn_db_sync_util_config_error(self, m_log):
53+
self.m_cfg.side_effect = [TypeError()]
54+
self.assertRaises(TypeError, octavia_ovn_db_sync_util.main)
55+
msg = ("Error parsing the configuration values. Please verify.")
56+
m_log.error.assert_called_once_with(msg)

setup.cfg

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ setup_hooks =
3232
octavia.api.drivers =
3333
ovn = ovn_octavia_provider.driver:OvnProviderDriver
3434

35+
console_scripts =
36+
octavia-ovn-db-sync-util = ovn_octavia_provider.cmd.octavia_ovn_db_sync_util:main
37+
3538
octavia.driver_agent.provider_agents =
3639
ovn = ovn_octavia_provider.agent:OvnProviderAgent
3740

0 commit comments

Comments
 (0)