From 9001ddcb1c74b018fc3d47ddb04a76edf38168e1 Mon Sep 17 00:00:00 2001 From: elajkat Date: Fri, 6 Jun 2025 15:18:55 +0200 Subject: [PATCH] Delete tunnel endpoints if endpoint agent is deleted Closes-Bug: #2084446 Change-Id: I64ad033e600c2c87af4716736c10e11c143afea2 Signed-off-by: lajoskatona --- .../openvswitch/agent/ovs_neutron_agent.py | 2 +- neutron/plugins/ml2/plugin.py | 28 +++++++++ neutron/tests/unit/plugins/ml2/test_plugin.py | 60 +++++++++++++++++++ 3 files changed, 89 insertions(+), 1 deletion(-) diff --git a/neutron/plugins/ml2/drivers/openvswitch/agent/ovs_neutron_agent.py b/neutron/plugins/ml2/drivers/openvswitch/agent/ovs_neutron_agent.py index a3b06aede78..d7b2445deb8 100644 --- a/neutron/plugins/ml2/drivers/openvswitch/agent/ovs_neutron_agent.py +++ b/neutron/plugins/ml2/drivers/openvswitch/agent/ovs_neutron_agent.py @@ -917,7 +917,7 @@ class OVSNeutronAgent(l2population_rpc.L2populationRpcCallBackTunnelMixin, @profiler.trace("rpc") def tunnel_delete(self, context, **kwargs): - LOG.debug("tunnel_delete received") + LOG.debug("tunnel_delete received: %s", kwargs) if not self.enable_tunneling: return tunnel_ip = kwargs.get('tunnel_ip') diff --git a/neutron/plugins/ml2/plugin.py b/neutron/plugins/ml2/plugin.py index 3fe63a3d50a..6ae5d229bf0 100644 --- a/neutron/plugins/ml2/plugin.py +++ b/neutron/plugins/ml2/plugin.py @@ -492,6 +492,34 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2, return True return False + @registry.receives(resources.AGENT, [events.AFTER_DELETE]) + def delete_agent_notified(self, resource, event, trigger, + payload=None): + context = payload.context + agent = payload.states[0] + if agent.binary != const.AGENT_PROCESS_OVS: + return + tunnel_id = payload.resource_id + tunnel_ip = agent.configurations.get('tunneling_ip') + tunnel_types = agent.configurations.get('tunnel_types') + if not tunnel_ip or not tunnel_types: + return + LOG.debug('Deleting tunnel id %s, and endpoints associated with ' + 'it (tunnel_ip: %s tunnel_types: %s)', + tunnel_id, tunnel_ip, tunnel_types) + for t_type in tunnel_types: + self.notifier.tunnel_delete( + context=context, + tunnel_ip=tunnel_ip, + tunnel_type=t_type) + try: + driver = self.type_manager.drivers.get(t_type) + except KeyError: + LOG.warning('Tunnel type %s is not registered, cannot ' + 'delete tunnel endpoint for it.', t_type) + else: + driver.obj.delete_endpoint(tunnel_ip) + @registry.receives(resources.AGENT, [events.AFTER_UPDATE]) def _retry_binding_revived_agents(self, resource, event, trigger, payload=None): diff --git a/neutron/tests/unit/plugins/ml2/test_plugin.py b/neutron/tests/unit/plugins/ml2/test_plugin.py index bb3cd3b4a22..13c094ee763 100644 --- a/neutron/tests/unit/plugins/ml2/test_plugin.py +++ b/neutron/tests/unit/plugins/ml2/test_plugin.py @@ -43,6 +43,7 @@ from neutron_lib.plugins import utils as p_utils from oslo_config import cfg from oslo_db import exception as db_exc from oslo_utils import netutils +from oslo_utils import timeutils from oslo_utils import uuidutils import testtools import webob @@ -64,6 +65,7 @@ from neutron.plugins.ml2.common import constants as ml2_consts from neutron.plugins.ml2.common import exceptions as ml2_exc from neutron.plugins.ml2 import db as ml2_db from neutron.plugins.ml2 import driver_context +from neutron.plugins.ml2.drivers import type_tunnel from neutron.plugins.ml2.drivers import type_vlan from neutron.plugins.ml2 import managers from neutron.plugins.ml2 import models @@ -595,6 +597,64 @@ class TestMl2NetworksV2(test_plugin.TestNetworksV2, self.assertIn("network", res.json['NeutronError']['message']) +class TestMl2AgentNotifications(Ml2PluginV2TestCase): + + class Agent: + def __init__(self, agent_dict): + for field in agent_dict: + setattr(self, field, agent_dict[field]) + + def test_delete_agent_notified(self): + agent_status = {'agent_type': constants.AGENT_TYPE_OVS, + 'binary': constants.AGENT_PROCESS_OVS, + 'host': 'AHOST', + 'topic': 'N/A', + 'configurations': {'tunnel_types': ['vxlan'], + 'tunneling_ip': '100.101.2.3'}} + agent = self.plugin.create_or_update_agent(self.context, + dict(agent_status), + timeutils.utcnow()) + agnt = self.Agent(agent[1]) + with mock.patch.object( + self.plugin.notifier, 'tunnel_delete') as m_t_del: + with mock.patch.object( + type_tunnel.EndpointTunnelTypeDriver, + 'delete_endpoint') as m_del_ep: + self.plugin.delete_agent_notified( + resource='agent', event='after_delete', trigger=None, + payload=events.DBEventPayload( + self.context, states=(agnt,), + resource_id=agent[1]['id'])) + m_t_del.assert_called_once_with( + context=mock.ANY, + tunnel_ip='100.101.2.3', tunnel_type='vxlan') + m_del_ep.assert_called_once_with('100.101.2.3') + + def test_delete_agent_notified_non_ovs(self): + agent_status = {'agent_type': constants.AGENT_TYPE_NIC_SWITCH, + 'binary': constants.AGENT_PROCESS_NIC_SWITCH, + 'host': 'AHOST', + 'topic': 'N/A', + 'configurations': {'tunnel_types': ['vxlan'], + 'tunneling_ip': '100.101.2.3'}} + agent = self.plugin.create_or_update_agent(self.context, + dict(agent_status), + timeutils.utcnow()) + agnt = self.Agent(agent[1]) + with mock.patch.object( + self.plugin.notifier, 'tunnel_delete') as m_t_del: + with mock.patch.object( + type_tunnel.EndpointTunnelTypeDriver, + 'delete_endpoint') as m_del_ep: + self.plugin.delete_agent_notified( + resource='agent', event='after_delete', trigger=None, + payload=events.DBEventPayload( + self.context, states=(agnt,), + resource_id=agent[1]['id'])) + m_t_del.assert_not_called() + m_del_ep.assert_not_called() + + class TestMl2NetworksV2AgentMechDrivers(Ml2PluginV2TestCase): _mechanism_drivers = ['logger', 'test', 'test_with_agent']