DVR: Do not check HA state on DVR-only routers

Trying to check HA state on a DVR-only compute node
can trigger:

AttributeError: 'DvrLocalRouter' object has no attribute 'ha_state'

Also moved the mode assignment outside of the loops
since it only needs to be done once.

Co-Authored-By: Sean Redmond <sean.redmond1@gmail.com>
Closes-bug: #1691427
Change-Id: I3e48e06e76325939fbc9533b0198924bc96d600e
This commit is contained in:
Brian Haley 2017-05-17 16:51:18 -04:00
parent ced89e3329
commit b748f2e9b9
2 changed files with 22 additions and 3 deletions

View File

@ -563,6 +563,10 @@ class L3NATAgent(ha.AgentMixin,
timestamp = timeutils.utcnow()
router_ids = []
chunk = []
is_snat_agent = (self.conf.agent_mode ==
lib_const.L3_AGENT_MODE_DVR_SNAT)
is_dvr_only_agent = (self.conf.agent_mode ==
lib_const.L3_AGENT_MODE_DVR)
try:
router_ids = self.plugin_rpc.get_router_ids(context)
# fetch routers by chunks to reduce the load on server and to
@ -578,14 +582,12 @@ class L3NATAgent(ha.AgentMixin,
# need to keep fip namespaces as well
ext_net_id = (r['external_gateway_info'] or {}).get(
'network_id')
is_snat_agent = (self.conf.agent_mode ==
lib_const.L3_AGENT_MODE_DVR_SNAT)
if ext_net_id:
ns_manager.keep_ext_net(ext_net_id)
elif is_snat_agent and not r.get('ha'):
ns_manager.ensure_snat_cleanup(r['id'])
# For HA routers check that DB state matches actual state
if r.get('ha'):
if r.get('ha') and not is_dvr_only_agent:
self.check_ha_state_for_router(
r['id'], r.get(l3_constants.HA_ROUTER_STATE_KEY))
update = queue.RouterUpdate(

View File

@ -275,6 +275,23 @@ class TestBasicRouterOperations(BasicRouterOperationsFramework):
check.assert_called_once_with(ha_id,
n_const.HA_ROUTER_STATE_STANDBY)
def test_periodic_sync_routers_task_not_check_ha_state_for_router(self):
# DVR-only agent should not trigger ha state check
self.conf.set_override('agent_mode', lib_constants.L3_AGENT_MODE_DVR)
agent = l3_agent.L3NATAgentWithStateReport(HOSTNAME, self.conf)
ha_id = _uuid()
active_routers = [
{'id': ha_id,
n_const.HA_ROUTER_STATE_KEY: n_const.HA_ROUTER_STATE_STANDBY,
'ha': True},
{'id': _uuid()}]
self.plugin_api.get_router_ids.return_value = [r['id'] for r
in active_routers]
self.plugin_api.get_routers.return_value = active_routers
with mock.patch.object(agent, 'check_ha_state_for_router') as check:
agent.periodic_sync_routers_task(agent.context)
self.assertFalse(check.called)
def test_periodic_sync_routers_task_raise_exception(self):
agent = l3_agent.L3NATAgent(HOSTNAME, self.conf)
self.plugin_api.get_router_ids.return_value = ['fake_id']