Merge "Clean up some of the OVN agent API methods" into stable/ussuri

This commit is contained in:
Zuul 2020-08-29 01:45:02 +00:00 committed by Gerrit Code Review
commit f527a08668
4 changed files with 159 additions and 90 deletions

View File

@ -0,0 +1,119 @@
#
# 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 abc
from oslo_utils import timeutils
from neutron.common.ovn import constants as ovn_const
from neutron.common.ovn import utils as ovn_utils
class NeutronAgent(abc.ABC):
types = {}
def __init_subclass__(cls):
# Register the subclasses to be looked up by their type
for _type in cls.types:
NeutronAgent.types[_type] = cls
def __init__(self, chassis):
self.chassis = chassis
@property
def updated_at(self):
try:
return timeutils.parse_isotime(self.chassis.external_ids[self.key])
except KeyError:
return timeutils.utcnow(with_timezone=True)
def as_dict(self, alive):
return {
'binary': self.binary,
'host': self.chassis.hostname,
'heartbeat_timestamp': timeutils.utcnow(),
'availability_zone': ', '.join(
ovn_utils.get_chassis_availability_zones(self.chassis)),
'topic': 'n/a',
'description': self.description,
'configurations': {
'chassis_name': self.chassis.name,
'bridge-mappings':
self.chassis.external_ids.get('ovn-bridge-mappings', '')},
'start_flag': True,
'agent_type': self.agent_type,
'id': self.agent_id,
'alive': alive,
'admin_state_up': True}
@classmethod
def from_type(cls, _type, chassis):
return cls.types[_type](chassis)
@staticmethod
def agent_types():
return NeutronAgent.__subclasses__()
@property
@abc.abstractmethod
def agent_type(self):
pass
class ControllerAgent(NeutronAgent):
types = [ovn_const.OVN_CONTROLLER_AGENT, ovn_const.OVN_CONTROLLER_GW_AGENT]
binary = 'ovn-controller'
key = ovn_const.OVN_LIVENESS_CHECK_EXT_ID_KEY
@property
def agent_type(self):
if ('enable-chassis-as-gw' in
self.chassis.external_ids.get('ovn-cms-options', [])):
return ovn_const.OVN_CONTROLLER_GW_AGENT
return ovn_const.OVN_CONTROLLER_AGENT
@property
def nb_cfg(self):
return self.chassis.nb_cfg
@property
def agent_id(self):
return self.chassis.name
@property
def description(self):
return self.chassis.external_ids.get(
ovn_const.OVN_AGENT_DESC_KEY, '')
class MetadataAgent(NeutronAgent):
agent_type = ovn_const.OVN_METADATA_AGENT
types = [agent_type]
binary = 'networking-ovn-metadata-agent'
key = ovn_const.METADATA_LIVENESS_CHECK_EXT_ID_KEY
@property
def nb_cfg(self):
return int(self.chassis.external_ids.get(
ovn_const.OVN_AGENT_METADATA_SB_CFG_KEY, 0))
@property
def agent_id(self):
return self.chassis.external_ids.get(
ovn_const.OVN_AGENT_METADATA_ID_KEY)
@property
def description(self):
return self.chassis.external_ids.get(
ovn_const.OVN_AGENT_METADATA_DESC_KEY, '')

View File

@ -48,6 +48,7 @@ from neutron.db import ovn_hash_ring_db
from neutron.db import ovn_revision_numbers_db from neutron.db import ovn_revision_numbers_db
from neutron.db import provisioning_blocks from neutron.db import provisioning_blocks
from neutron.plugins.ml2 import db as ml2_db from neutron.plugins.ml2 import db as ml2_db
from neutron.plugins.ml2.drivers.ovn.agent import neutron_agent as n_agent
from neutron.plugins.ml2.drivers.ovn.mech_driver.ovsdb import impl_idl_ovn from neutron.plugins.ml2.drivers.ovn.mech_driver.ovsdb import impl_idl_ovn
from neutron.plugins.ml2.drivers.ovn.mech_driver.ovsdb import maintenance from neutron.plugins.ml2.drivers.ovn.mech_driver.ovsdb import maintenance
from neutron.plugins.ml2.drivers.ovn.mech_driver.ovsdb import ovn_client from neutron.plugins.ml2.drivers.ovn.mech_driver.ovsdb import ovn_client
@ -1041,87 +1042,35 @@ class OVNMechanismDriver(api.MechanismDriver):
" networking-ovn-metadata-agent status/logs.", " networking-ovn-metadata-agent status/logs.",
port_id) port_id)
def agent_alive(self, chassis, type_, update_db): def agent_alive(self, agent, update_db):
nb_cfg = chassis.nb_cfg
key = ovn_const.OVN_LIVENESS_CHECK_EXT_ID_KEY
if type_ == ovn_const.OVN_METADATA_AGENT:
nb_cfg = int(chassis.external_ids.get(
ovn_const.OVN_AGENT_METADATA_SB_CFG_KEY, 0))
key = ovn_const.METADATA_LIVENESS_CHECK_EXT_ID_KEY
try:
updated_at = timeutils.parse_isotime(chassis.external_ids[key])
except KeyError:
updated_at = timeutils.utcnow(with_timezone=True)
update_db = True
# Allow a maximum of 1 difference between expected and read values # Allow a maximum of 1 difference between expected and read values
# to avoid false positives. # to avoid false positives.
if self._nb_ovn.nb_global.nb_cfg - nb_cfg <= 1: if self._nb_ovn.nb_global.nb_cfg - agent.nb_cfg <= 1:
if update_db: if update_db:
# Update the time of our successful check self.mark_agent_alive(agent)
value = timeutils.utcnow(with_timezone=True).isoformat()
self._sb_ovn.db_set('Chassis', chassis.uuid,
('external_ids', {key: value})).execute(
check_error=True)
return True return True
now = timeutils.utcnow(with_timezone=True)
if (now - updated_at).total_seconds() < cfg.CONF.agent_down_time: now = timeutils.utcnow(with_timezone=True)
if (now - agent.updated_at).total_seconds() < cfg.CONF.agent_down_time:
# down, but not yet timed out # down, but not yet timed out
return True return True
return False return False
def _format_agent_info(self, chassis, binary, agent_id, type_, def mark_agent_alive(self, agent):
description, alive): # Update the time of our successful check
return { value = timeutils.utcnow(with_timezone=True).isoformat()
'binary': binary, self._sb_ovn.db_set('Chassis', agent.chassis.uuid,
'host': chassis.hostname, ('external_ids', {agent.key: value})).execute(
'heartbeat_timestamp': timeutils.utcnow(), check_error=True)
'availability_zone': ', '.join(
ovn_utils.get_chassis_availability_zones(chassis)),
'topic': 'n/a',
'description': description,
'configurations': {
'chassis_name': chassis.name,
'bridge-mappings':
chassis.external_ids.get('ovn-bridge-mappings', '')},
'start_flag': True,
'agent_type': type_,
'id': agent_id,
'alive': alive,
'admin_state_up': True}
def agents_from_chassis(self, chassis, update_db=True): def agents_from_chassis(self, chassis, update_db=True):
agent_dict = {} agent_dict = {}
# Iterate over each unique Agent subclass
# Check for ovn-controller / ovn-controller gateway for agent in [a(chassis) for a in n_agent.NeutronAgent.agent_types()]:
agent_type = ovn_const.OVN_CONTROLLER_AGENT if not agent.agent_id:
# Only the chassis name stays consistent after ovn-controller restart continue
agent_id = chassis.name alive = self.agent_alive(agent, update_db)
if ('enable-chassis-as-gw' in agent_dict[agent.agent_id] = agent.as_dict(alive)
chassis.external_ids.get('ovn-cms-options', [])):
agent_type = ovn_const.OVN_CONTROLLER_GW_AGENT
alive = self.agent_alive(chassis, agent_type, update_db)
description = chassis.external_ids.get(
ovn_const.OVN_AGENT_DESC_KEY, '')
agent_dict[agent_id] = self._format_agent_info(
chassis, 'ovn-controller', agent_id, agent_type, description,
alive)
# Check for the metadata agent
metadata_agent_id = chassis.external_ids.get(
ovn_const.OVN_AGENT_METADATA_ID_KEY)
if metadata_agent_id:
agent_type = ovn_const.OVN_METADATA_AGENT
alive = self.agent_alive(chassis, agent_type, update_db)
description = chassis.external_ids.get(
ovn_const.OVN_AGENT_METADATA_DESC_KEY, '')
agent_dict[metadata_agent_id] = self._format_agent_info(
chassis, 'networking-ovn-metadata-agent',
metadata_agent_id, agent_type, description, alive)
return agent_dict return agent_dict
def patch_plugin_merge(self, method_name, new_fn, op=operator.add): def patch_plugin_merge(self, method_name, new_fn, op=operator.add):
@ -1148,7 +1097,7 @@ class OVNMechanismDriver(api.MechanismDriver):
setattr(self._plugin, method_name, types.MethodType(fn, self._plugin)) setattr(self._plugin, method_name, types.MethodType(fn, self._plugin))
def ping_chassis(self): def ping_all_chassis(self):
"""Update NB_Global.nb_cfg so that Chassis.nb_cfg will increment """Update NB_Global.nb_cfg so that Chassis.nb_cfg will increment
:returns: (bool) True if nb_cfg was updated. False if it was updated :returns: (bool) True if nb_cfg was updated. False if it was updated
@ -1198,7 +1147,7 @@ def populate_agents(driver):
def get_agents(self, context, filters=None, fields=None, _driver=None): def get_agents(self, context, filters=None, fields=None, _driver=None):
update_db = _driver.ping_chassis() update_db = _driver.ping_all_chassis()
filters = filters or {} filters = filters or {}
agent_list = [] agent_list = []
populate_agents(_driver) populate_agents(_driver)

View File

@ -48,6 +48,7 @@ from neutron.db import ovn_revision_numbers_db
from neutron.db import provisioning_blocks from neutron.db import provisioning_blocks
from neutron.db import securitygroups_db from neutron.db import securitygroups_db
from neutron.db import segments_db from neutron.db import segments_db
from neutron.plugins.ml2.drivers.ovn.agent import neutron_agent
from neutron.plugins.ml2.drivers.ovn.mech_driver import mech_driver from neutron.plugins.ml2.drivers.ovn.mech_driver import mech_driver
from neutron.plugins.ml2.drivers.ovn.mech_driver.ovsdb import ovn_client from neutron.plugins.ml2.drivers.ovn.mech_driver.ovsdb import ovn_client
from neutron.plugins.ml2.drivers import type_geneve # noqa from neutron.plugins.ml2.drivers import type_geneve # noqa
@ -1568,14 +1569,14 @@ class TestOVNMechanismDriver(test_plugin.Ml2PluginV2TestCase):
ovn_const.METADATA_LIVENESS_CHECK_EXT_ID_KEY: ovn_const.METADATA_LIVENESS_CHECK_EXT_ID_KEY:
datetime.datetime.isoformat(updated_at)}) datetime.datetime.isoformat(updated_at)})
return chassis return neutron_agent.NeutronAgent.from_type(agent_type, chassis)
def test_agent_alive_true(self): def test_agent_alive_true(self):
for agent_type in (ovn_const.OVN_CONTROLLER_AGENT, for agent_type in (ovn_const.OVN_CONTROLLER_AGENT,
ovn_const.OVN_METADATA_AGENT): ovn_const.OVN_METADATA_AGENT):
self.mech_driver._nb_ovn.nb_global.nb_cfg = 5 self.mech_driver._nb_ovn.nb_global.nb_cfg = 5
chassis = self._add_chassis_agent(5, agent_type) agent = self._add_chassis_agent(5, agent_type)
self.assertTrue(self.mech_driver.agent_alive(chassis, agent_type, self.assertTrue(self.mech_driver.agent_alive(agent,
update_db=True)) update_db=True))
# Assert that each Chassis has been updated in the SB database # Assert that each Chassis has been updated in the SB database
self.assertEqual(2, self.sb_ovn.db_set.call_count) self.assertEqual(2, self.sb_ovn.db_set.call_count)
@ -1588,17 +1589,17 @@ class TestOVNMechanismDriver(test_plugin.Ml2PluginV2TestCase):
self.mech_driver._nb_ovn.nb_global.nb_cfg = 5 self.mech_driver._nb_ovn.nb_global.nb_cfg = 5
now = timeutils.utcnow() now = timeutils.utcnow()
updated_at = now - datetime.timedelta(cfg.CONF.agent_down_time + 1) updated_at = now - datetime.timedelta(cfg.CONF.agent_down_time + 1)
chassis = self._add_chassis_agent(4, agent_type, updated_at) agent = self._add_chassis_agent(4, agent_type, updated_at)
self.assertTrue(self.mech_driver.agent_alive(chassis, agent_type, self.assertTrue(self.mech_driver.agent_alive(agent,
update_db=True)) update_db=True))
def test_agent_alive_not_timed_out(self): def test_agent_alive_not_timed_out(self):
for agent_type in (ovn_const.OVN_CONTROLLER_AGENT, for agent_type in (ovn_const.OVN_CONTROLLER_AGENT,
ovn_const.OVN_METADATA_AGENT): ovn_const.OVN_METADATA_AGENT):
self.mech_driver._nb_ovn.nb_global.nb_cfg = 5 self.mech_driver._nb_ovn.nb_global.nb_cfg = 5
chassis = self._add_chassis_agent(3, agent_type) agent = self._add_chassis_agent(3, agent_type)
self.assertTrue(self.mech_driver.agent_alive( self.assertTrue(self.mech_driver.agent_alive(
chassis, agent_type, update_db=True), agent, update_db=True),
"Agent type %s is not alive" % agent_type) "Agent type %s is not alive" % agent_type)
def test_agent_alive_timed_out(self): def test_agent_alive_timed_out(self):
@ -1607,16 +1608,16 @@ class TestOVNMechanismDriver(test_plugin.Ml2PluginV2TestCase):
self.mech_driver._nb_ovn.nb_global.nb_cfg = 5 self.mech_driver._nb_ovn.nb_global.nb_cfg = 5
now = timeutils.utcnow() now = timeutils.utcnow()
updated_at = now - datetime.timedelta(cfg.CONF.agent_down_time + 1) updated_at = now - datetime.timedelta(cfg.CONF.agent_down_time + 1)
chassis = self._add_chassis_agent(3, agent_type, updated_at) agent = self._add_chassis_agent(3, agent_type, updated_at)
self.assertFalse(self.mech_driver.agent_alive(chassis, agent_type, self.assertFalse(self.mech_driver.agent_alive(agent,
update_db=True)) update_db=True))
def test_agent_alive_true_skip_db_update(self): def test_agent_alive_true_skip_db_update(self):
for agent_type in (ovn_const.OVN_CONTROLLER_AGENT, for agent_type in (ovn_const.OVN_CONTROLLER_AGENT,
ovn_const.OVN_METADATA_AGENT): ovn_const.OVN_METADATA_AGENT):
self.mech_driver._nb_ovn.nb_global.nb_cfg = 5 self.mech_driver._nb_ovn.nb_global.nb_cfg = 5
chassis = self._add_chassis_agent(5, agent_type) agent = self._add_chassis_agent(5, agent_type)
self.assertTrue(self.mech_driver.agent_alive(chassis, agent_type, self.assertTrue(self.mech_driver.agent_alive(agent,
update_db=False)) update_db=False))
self.sb_ovn.db_set.assert_not_called() self.sb_ovn.db_set.assert_not_called()
@ -1689,12 +1690,12 @@ class TestOVNMechanismDriver(test_plugin.Ml2PluginV2TestCase):
expected_opts = {} expected_opts = {}
self._test_update_network_fragmentation(new_mtu, expected_opts) self._test_update_network_fragmentation(new_mtu, expected_opts)
def test_ping_chassis(self): def test_ping_all_chassis(self):
self.nb_ovn.nb_global.external_ids = {} self.nb_ovn.nb_global.external_ids = {}
self.mech_driver.ping_chassis() self.mech_driver.ping_all_chassis()
self.nb_ovn.check_liveness.assert_called_once_with() self.nb_ovn.check_liveness.assert_called_once_with()
def test_ping_chassis_interval_expired(self): def test_ping_all_chassis_interval_expired(self):
timeout = 10 timeout = 10
ovn_conf.cfg.CONF.set_override('agent_down_time', timeout) ovn_conf.cfg.CONF.set_override('agent_down_time', timeout)
@ -1704,14 +1705,14 @@ class TestOVNMechanismDriver(test_plugin.Ml2PluginV2TestCase):
self.nb_ovn.nb_global.external_ids = { self.nb_ovn.nb_global.external_ids = {
ovn_const.OVN_LIVENESS_CHECK_EXT_ID_KEY: str(time)} ovn_const.OVN_LIVENESS_CHECK_EXT_ID_KEY: str(time)}
update_db = self.mech_driver.ping_chassis() update_db = self.mech_driver.ping_all_chassis()
# Since the interval has expired, assert that the "check_liveness" # Since the interval has expired, assert that the "check_liveness"
# command has been invoked # command has been invoked
self.nb_ovn.check_liveness.assert_called_once_with() self.nb_ovn.check_liveness.assert_called_once_with()
# Assert that ping_chassis returned True as it updated the db # Assert that ping_all_chassis returned True as it updated the db
self.assertTrue(update_db) self.assertTrue(update_db)
def test_ping_chassis_interval_not_expired(self): def test_ping_all_chassis_interval_not_expired(self):
ovn_conf.cfg.CONF.set_override('agent_down_time', 10) ovn_conf.cfg.CONF.set_override('agent_down_time', 10)
# Pretend the interval has NOT yet expired # Pretend the interval has NOT yet expired
@ -1719,10 +1720,10 @@ class TestOVNMechanismDriver(test_plugin.Ml2PluginV2TestCase):
self.nb_ovn.nb_global.external_ids = { self.nb_ovn.nb_global.external_ids = {
ovn_const.OVN_LIVENESS_CHECK_EXT_ID_KEY: str(time)} ovn_const.OVN_LIVENESS_CHECK_EXT_ID_KEY: str(time)}
update_db = self.mech_driver.ping_chassis() update_db = self.mech_driver.ping_all_chassis()
# Assert that "check_liveness" wasn't invoked # Assert that "check_liveness" wasn't invoked
self.assertFalse(self.nb_ovn.check_liveness.called) self.assertFalse(self.nb_ovn.check_liveness.called)
# Assert that ping_chassis returned False as it didn't update the db # Assert ping_all_chassis returned False as it didn't update the db
self.assertFalse(update_db) self.assertFalse(update_db)
def test_get_candidates_for_scheduling_availability_zones(self): def test_get_candidates_for_scheduling_availability_zones(self):