Merge "port_forwarding: extend support for OVN usage (cont.)"

This commit is contained in:
Zuul 2020-06-10 18:45:10 +00:00 committed by Gerrit Code Review
commit 76b2ace079
2 changed files with 57 additions and 33 deletions

View File

@ -56,15 +56,22 @@ PORT_FORWARDING_FLOATINGIP_KEY = '_pf_floatingips'
def _required_service_plugins(): def _required_service_plugins():
supported_svc_plugins = [l3.ROUTER, 'ovn-router'] SvcPlugin = collections.namedtuple('SvcPlugin', 'plugin uses_rpc')
l3_router = SvcPlugin(l3.ROUTER, True)
supported_svc_plugins = [l3_router, SvcPlugin('ovn-router', False)]
plugins = []
rpc_required = False
try: try:
plugins = [svc for svc in supported_svc_plugins if for svc in supported_svc_plugins:
svc in cfg.CONF.service_plugins] if svc.plugin in cfg.CONF.service_plugins:
plugins.append(svc.plugin)
rpc_required |= svc.uses_rpc
except cfg.NoSuchOptError: except cfg.NoSuchOptError:
plugins = None
pass pass
# Use l3.ROUTER as required service plugin if no other was provided. if plugins:
return plugins or [l3.ROUTER] return plugins, rpc_required
# Use l3_router as required service plugin if no other was provided.
return [l3_router.plugin], l3_router.uses_rpc
@resource_extend.has_resource_extenders @resource_extend.has_resource_extenders
@ -75,7 +82,8 @@ class PortForwardingPlugin(fip_pf.PortForwardingPluginBase):
This class implements a Port Forwarding plugin. This class implements a Port Forwarding plugin.
""" """
required_service_plugins = _required_service_plugins() required_service_plugins, _rpc_notifications_required = \
_required_service_plugins()
supported_extension_aliases = [apidef.ALIAS, supported_extension_aliases = [apidef.ALIAS,
expose_port_forwarding_in_fip.ALIAS, expose_port_forwarding_in_fip.ALIAS,
@ -87,7 +95,8 @@ class PortForwardingPlugin(fip_pf.PortForwardingPluginBase):
def __init__(self): def __init__(self):
super(PortForwardingPlugin, self).__init__() super(PortForwardingPlugin, self).__init__()
self.push_api = resources_rpc.ResourcesPushRpcApi() self.push_api = resources_rpc.ResourcesPushRpcApi() \
if self._rpc_notifications_required else None
self.l3_plugin = directory.get_plugin(constants.L3) self.l3_plugin = directory.get_plugin(constants.L3)
self.core_plugin = directory.get_plugin() self.core_plugin = directory.get_plugin()
registry.publish(pf_consts.PORT_FORWARDING_PLUGIN, events.AFTER_INIT, registry.publish(pf_consts.PORT_FORWARDING_PLUGIN, events.AFTER_INIT,
@ -241,6 +250,7 @@ class PortForwardingPlugin(fip_pf.PortForwardingPluginBase):
pf_resource.delete() pf_resource.delete()
remove_port_forwarding_list.append(pf_resource) remove_port_forwarding_list.append(pf_resource)
if self._rpc_notifications_required:
self.push_api.push(context, remove_port_forwarding_list, self.push_api.push(context, remove_port_forwarding_list,
rpc_events.DELETED) rpc_events.DELETED)
registry_notify_payload = [ registry_notify_payload = [
@ -369,6 +379,7 @@ class PortForwardingPlugin(fip_pf.PortForwardingPluginBase):
raise lib_exc.BadRequest(resource=apidef.RESOURCE_NAME, raise lib_exc.BadRequest(resource=apidef.RESOURCE_NAME,
msg=message) msg=message)
if self._rpc_notifications_required:
self.push_api.push(context, [pf_obj], rpc_events.CREATED) self.push_api.push(context, [pf_obj], rpc_events.CREATED)
registry.notify(pf_consts.PORT_FORWARDING, events.AFTER_CREATE, registry.notify(pf_consts.PORT_FORWARDING, events.AFTER_CREATE,
self, self,
@ -423,6 +434,7 @@ class PortForwardingPlugin(fip_pf.PortForwardingPluginBase):
"are %s") % conflict_params "are %s") % conflict_params
raise lib_exc.BadRequest(resource=apidef.RESOURCE_NAME, raise lib_exc.BadRequest(resource=apidef.RESOURCE_NAME,
msg=message) msg=message)
if self._rpc_notifications_required:
self.push_api.push(context, [pf_obj], rpc_events.UPDATED) self.push_api.push(context, [pf_obj], rpc_events.UPDATED)
registry.notify(pf_consts.PORT_FORWARDING, events.AFTER_UPDATE, self, registry.notify(pf_consts.PORT_FORWARDING, events.AFTER_UPDATE, self,
payload=[callbacks.PortForwardingPayload(context, payload=[callbacks.PortForwardingPayload(context,
@ -524,6 +536,7 @@ class PortForwardingPlugin(fip_pf.PortForwardingPluginBase):
fip_obj.update_fields({'router_id': None}) fip_obj.update_fields({'router_id': None})
fip_obj.update() fip_obj.update()
pf_obj.delete() pf_obj.delete()
if self._rpc_notifications_required:
self.push_api.push(context, [pf_obj], rpc_events.DELETED) self.push_api.push(context, [pf_obj], rpc_events.DELETED)
registry.notify(pf_consts.PORT_FORWARDING, events.AFTER_DELETE, self, registry.notify(pf_consts.PORT_FORWARDING, events.AFTER_DELETE, self,
payload=[callbacks.PortForwardingPayload( payload=[callbacks.PortForwardingPayload(

View File

@ -395,37 +395,48 @@ class TestPortForwardingPlugin(testlib_api.SqlTestCase):
self.ctxt, 'fake-pf-id', 'fip_id_2', **pf_input) self.ctxt, 'fake-pf-id', 'fip_id_2', **pf_input)
def test_service_plugins_values(self): def test_service_plugins_values(self):
exp_default = ['router'] exp_default_plugins = ['router']
supported_plugins = ['router', 'ovn-router'] supported_plugins = ['router', 'ovn-router']
same_as_input = 'same_as_input' same_as_input = 'same_as_input'
TC = namedtuple('TC', 'input expected description') TC = namedtuple('TC', 'input exp_plugins exp_uses_rpc description')
test_cases = [ test_cases = [
TC([], exp_default, "default from empty cfg"), TC([], exp_default_plugins, True, "default from empty cfg"),
TC(['foo'], exp_default, "default from unexpected cfg"), TC(['foo'], exp_default_plugins, True,
TC(['foo', 123], exp_default, "default from unexpected cfg"), "default from unexpected cfg"),
TC(['foo', 'router'], exp_default, "default from valid cfg"), TC(['foo', 123], exp_default_plugins, True,
TC(['router'], same_as_input, "valid cfg 1"), "default from unexpected cfg"),
TC(['router'], same_as_input, "valid cfg 1"), TC(['foo', 'router'], exp_default_plugins, True,
TC(['ovn-router'], same_as_input, "valid cfg 2"), "default from valid cfg"),
TC(['ovn-router', 'router'], supported_plugins, "valid cfg 3"), TC(['router'], same_as_input, True, "valid cfg 1"),
TC(['router', 'ovn-router'], supported_plugins, "valid cfg 4"), TC(['router'], same_as_input, True, "valid cfg 1"),
TC(['bar', 'router', 'foo'], ['router'], "valid cfg 5"), TC(['ovn-router'], same_as_input, False, "valid cfg 2"),
TC(['bar', 'ovn-router', 'foo'], ['ovn-router'], "valid cfg 6"), TC(['ovn-router', 'router'], supported_plugins, True,
"valid cfg 3"),
TC(['router', 'ovn-router'], supported_plugins, True,
"valid cfg 4"),
TC(['bar', 'router', 'foo'], ['router'], True, "valid cfg 5"),
TC(['bar', 'ovn-router', 'foo'], ['ovn-router'], False,
"valid cfg 6"),
TC(['bar', 'router', 123, 'ovn-router', 'foo', 'kitchen', 'sink'], TC(['bar', 'router', 123, 'ovn-router', 'foo', 'kitchen', 'sink'],
supported_plugins, "valid cfg 7"), supported_plugins, True, "valid cfg 7"),
] ]
for tc in test_cases: for tc in test_cases:
cfg.CONF.set_override("service_plugins", tc.input) cfg.CONF.set_override("service_plugins", tc.input)
result = pf_plugin._required_service_plugins() plugins, rpc_required = pf_plugin._required_service_plugins()
if tc.expected == same_as_input: if tc.exp_plugins == same_as_input:
self.assertEqual(tc.input, result, tc.description) self.assertEqual(
(tc.input, tc.exp_uses_rpc), (plugins, rpc_required),
tc.description)
else: else:
self.assertEqual(tc.expected, result, tc.description) self.assertEqual(
(tc.exp_plugins, tc.exp_uses_rpc), (plugins, rpc_required),
tc.description)
@mock.patch.object(cfg.ConfigOpts, '__getattr__') @mock.patch.object(cfg.ConfigOpts, '__getattr__')
def test_service_plugins_no_such_opt(self, mock_config_opts_get): def test_service_plugins_no_such_opt(self, mock_config_opts_get):
description = "test cfg.NoSuchOptError exception" description = "test cfg.NoSuchOptError exception"
mock_config_opts_get.side_effect = cfg.NoSuchOptError('test_svc_plug') mock_config_opts_get.side_effect = cfg.NoSuchOptError('test_svc_plug')
result = pf_plugin._required_service_plugins() plugins, rpc_required = pf_plugin._required_service_plugins()
mock_config_opts_get.assert_called_once() mock_config_opts_get.assert_called_once()
self.assertEqual(['router'], result, description) self.assertEqual(
(['router'], True), (plugins, rpc_required), description)