Browse Source

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

changes/58/748958/1
Zuul 1 month ago
committed by Gerrit Code Review
parent
commit
f527a08668
4 changed files with 159 additions and 90 deletions
  1. +0
    -0
      neutron/plugins/ml2/drivers/ovn/agent/__init__.py
  2. +119
    -0
      neutron/plugins/ml2/drivers/ovn/agent/neutron_agent.py
  3. +20
    -71
      neutron/plugins/ml2/drivers/ovn/mech_driver/mech_driver.py
  4. +20
    -19
      neutron/tests/unit/plugins/ml2/drivers/ovn/mech_driver/test_mech_driver.py

+ 0
- 0
neutron/plugins/ml2/drivers/ovn/agent/__init__.py View File


+ 119
- 0
neutron/plugins/ml2/drivers/ovn/agent/neutron_agent.py 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, '')

+ 20
- 71
neutron/plugins/ml2/drivers/ovn/mech_driver/mech_driver.py 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 provisioning_blocks
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 maintenance
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.",
port_id)

def agent_alive(self, chassis, type_, 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

def agent_alive(self, agent, update_db):
# Allow a maximum of 1 difference between expected and read values
# 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:
# Update the time of our successful check
value = timeutils.utcnow(with_timezone=True).isoformat()
self._sb_ovn.db_set('Chassis', chassis.uuid,
('external_ids', {key: value})).execute(
check_error=True)
self.mark_agent_alive(agent)
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
return True
return False

def _format_agent_info(self, chassis, binary, agent_id, type_,
description, alive):
return {
'binary': binary,
'host': chassis.hostname,
'heartbeat_timestamp': timeutils.utcnow(),
'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 mark_agent_alive(self, agent):
# Update the time of our successful check
value = timeutils.utcnow(with_timezone=True).isoformat()
self._sb_ovn.db_set('Chassis', agent.chassis.uuid,
('external_ids', {agent.key: value})).execute(
check_error=True)

def agents_from_chassis(self, chassis, update_db=True):
agent_dict = {}

# Check for ovn-controller / ovn-controller gateway
agent_type = ovn_const.OVN_CONTROLLER_AGENT
# Only the chassis name stays consistent after ovn-controller restart
agent_id = chassis.name
if ('enable-chassis-as-gw' in
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)

# Iterate over each unique Agent subclass
for agent in [a(chassis) for a in n_agent.NeutronAgent.agent_types()]:
if not agent.agent_id:
continue
alive = self.agent_alive(agent, update_db)
agent_dict[agent.agent_id] = agent.as_dict(alive)
return agent_dict

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))

def ping_chassis(self):
def ping_all_chassis(self):
"""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
@@ -1198,7 +1147,7 @@ def populate_agents(driver):


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 {}
agent_list = []
populate_agents(_driver)


+ 20
- 19
neutron/tests/unit/plugins/ml2/drivers/ovn/mech_driver/test_mech_driver.py View File

@@ -48,6 +48,7 @@ from neutron.db import ovn_revision_numbers_db
from neutron.db import provisioning_blocks
from neutron.db import securitygroups_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.ovsdb import ovn_client
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:
datetime.datetime.isoformat(updated_at)})

return chassis
return neutron_agent.NeutronAgent.from_type(agent_type, chassis)

def test_agent_alive_true(self):
for agent_type in (ovn_const.OVN_CONTROLLER_AGENT,
ovn_const.OVN_METADATA_AGENT):
self.mech_driver._nb_ovn.nb_global.nb_cfg = 5
chassis = self._add_chassis_agent(5, agent_type)
self.assertTrue(self.mech_driver.agent_alive(chassis, agent_type,
agent = self._add_chassis_agent(5, agent_type)
self.assertTrue(self.mech_driver.agent_alive(agent,
update_db=True))
# Assert that each Chassis has been updated in the SB database
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
now = timeutils.utcnow()
updated_at = now - datetime.timedelta(cfg.CONF.agent_down_time + 1)
chassis = self._add_chassis_agent(4, agent_type, updated_at)
self.assertTrue(self.mech_driver.agent_alive(chassis, agent_type,
agent = self._add_chassis_agent(4, agent_type, updated_at)
self.assertTrue(self.mech_driver.agent_alive(agent,
update_db=True))

def test_agent_alive_not_timed_out(self):
for agent_type in (ovn_const.OVN_CONTROLLER_AGENT,
ovn_const.OVN_METADATA_AGENT):
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(
chassis, agent_type, update_db=True),
agent, update_db=True),
"Agent type %s is not alive" % agent_type)

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
now = timeutils.utcnow()
updated_at = now - datetime.timedelta(cfg.CONF.agent_down_time + 1)
chassis = self._add_chassis_agent(3, agent_type, updated_at)
self.assertFalse(self.mech_driver.agent_alive(chassis, agent_type,
agent = self._add_chassis_agent(3, agent_type, updated_at)
self.assertFalse(self.mech_driver.agent_alive(agent,
update_db=True))

def test_agent_alive_true_skip_db_update(self):
for agent_type in (ovn_const.OVN_CONTROLLER_AGENT,
ovn_const.OVN_METADATA_AGENT):
self.mech_driver._nb_ovn.nb_global.nb_cfg = 5
chassis = self._add_chassis_agent(5, agent_type)
self.assertTrue(self.mech_driver.agent_alive(chassis, agent_type,
agent = self._add_chassis_agent(5, agent_type)
self.assertTrue(self.mech_driver.agent_alive(agent,
update_db=False))
self.sb_ovn.db_set.assert_not_called()

@@ -1689,12 +1690,12 @@ class TestOVNMechanismDriver(test_plugin.Ml2PluginV2TestCase):
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.mech_driver.ping_chassis()
self.mech_driver.ping_all_chassis()
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
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 = {
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"
# command has been invoked
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)

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)

# Pretend the interval has NOT yet expired
@@ -1719,10 +1720,10 @@ class TestOVNMechanismDriver(test_plugin.Ml2PluginV2TestCase):
self.nb_ovn.nb_global.external_ids = {
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
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)

def test_get_candidates_for_scheduling_availability_zones(self):


Loading…
Cancel
Save