[L3HA] Don't update HA router's ports if router isn't active on agents

In case when HA router isn't active on any L3 agent,
_ensure_host_set_on_port method shouldn't try to update port's host to
the host from which there was an rpc message sent, as this can be host
on which router is in the "standby" mode.
This method should only update port's host to the router's "active_host"
if there is such active_host found already.

Depends-On: https://review.opendev.org/c/openstack/requirements/+/841489

Closes-Bug: #1973162
Closes-Bug: #1942190
Change-Id: Ib3945d294601b35f9b268c25841cd284b52c4ca3
(cherry picked from commit cd8bf18150)
This commit is contained in:
Slawek Kaplonski 2022-05-13 11:56:02 +02:00 committed by liuyulong
parent 23df8f3575
commit 9562726b1b
2 changed files with 19 additions and 10 deletions

View File

@ -218,22 +218,21 @@ class L3RpcCallback(object):
active_host = ( active_host = (
self.l3plugin.get_active_host_for_ha_router( self.l3plugin.get_active_host_for_ha_router(
context, router_id)) context, router_id))
if active_host: if not active_host:
host = active_host LOG.debug("Router %(router)s is not active on any "
# If there is currently no active router instance (For "host. Port %(port)s will not be updated "
# example it's a new router), the host that requested "now.",
# the routers (Essentially a random host) will do. The {'router': router_id, 'port': port['id']})
# port binding will be corrected when an active is return
# elected.
try: try:
LOG.debug("Updating router %(router)s port %(port)s " LOG.debug("Updating router %(router)s port %(port)s "
"binding host %(host)s", "binding host %(host)s",
{"router": router_id, "port": port['id'], {"router": router_id, "port": port['id'],
"host": host}) "host": active_host})
self.plugin.update_port( self.plugin.update_port(
context, context,
port['id'], port['id'],
{'port': {portbindings.HOST_ID: host}}) {'port': {portbindings.HOST_ID: active_host}})
except exceptions.PortNotFound: except exceptions.PortNotFound:
LOG.debug("Port %(port)s not found while updating " LOG.debug("Port %(port)s not found while updating "
"agent binding for router %(router)s.", "agent binding for router %(router)s.",

View File

@ -1304,9 +1304,19 @@ class L3HAModeDbTestCase(L3HATestFramework):
self.plugin.list_active_sync_routers_on_active_l3_agent( self.plugin.list_active_sync_routers_on_active_l3_agent(
self.admin_ctx, self.agent1['host'], [router['id']]))[0] self.admin_ctx, self.agent1['host'], [router['id']]))[0]
# ensure_host_set_on_ports binds an unbound port
callback = l3_rpc.L3RpcCallback() callback = l3_rpc.L3RpcCallback()
callback._l3plugin = self.plugin callback._l3plugin = self.plugin
# First ensure that port is not bound if router is not active on any
# agent
callback._ensure_host_set_on_ports(
self.admin_ctx, self.agent1['host'], [router])
port = self._get_first_interface(router['id'])
self.assertEqual('', port[portbindings.HOST_ID])
# Now update router to be active on agent1
# and ensure_host_set_on_ports binds an unbound port
self.plugin.update_routers_states(
self.admin_ctx, {router['id']: 'active'}, self.agent1['host'])
callback._ensure_host_set_on_ports( callback._ensure_host_set_on_ports(
self.admin_ctx, self.agent1['host'], [router]) self.admin_ctx, self.agent1['host'], [router])
port = self._get_first_interface(router['id']) port = self._get_first_interface(router['id'])