Update report state rpc API

Ironic agent needs ability to delete baremetal agents when ironic node
is deleted. Change modifies report rpc API to fetch agents by filters
and adds `delete_agent` method to delete agent.

Bump version to 1.4

Co-Authored-By: Harald Jensås <hjensas@redhat.com>
Related-Bug: 2086640
Change-Id: I9db166e9b610f0bafc2be5351f3b79c4c2251664
This commit is contained in:
aarefiev
2025-03-14 06:41:20 -07:00
committed by Harald Jensås
parent ea83e86347
commit 397f48d310
4 changed files with 77 additions and 2 deletions

View File

@@ -81,7 +81,7 @@ class PluginReportStateAPI:
doc/source/contributor/internals/rpc_api.rst.
"""
def __init__(self, topic):
target = oslo_messaging.Target(topic=topic, version='1.3',
target = oslo_messaging.Target(topic=topic, version='1.4',
namespace=constants.RPC_NAMESPACE_STATE)
self.client = lib_rpc.get_client(target)
self.timeout = cfg.CONF.AGENT.report_interval
@@ -104,6 +104,14 @@ class PluginReportStateAPI:
method = cctxt.call if use_call else cctxt.cast
return method(context, 'report_state', **kwargs)
def get_agents(self, context, **filters):
cctxt = self.client.prepare(timeout=self.timeout)
return cctxt.call(context, 'get_agents', **filters)
def delete_agent(self, context, **kwargs):
cctxt = self.client.prepare(timeout=self.timeout)
return cctxt.call(context, 'delete_agent', **kwargs)
class PluginApi:
"""Agent side of the rpc API.

View File

@@ -469,9 +469,10 @@ class AgentExtRpcCallback:
1.1 - report_state now returns agent state.
1.2 - add method has_alive_neutron_server.
1.3 - has_alive_neutron_server tests db connection.
1.4 - add methods get_agents and delete_agent.
"""
target = oslo_messaging.Target(version='1.3',
target = oslo_messaging.Target(version='1.4',
namespace=constants.RPC_NAMESPACE_STATE)
START_TIME = timeutils.utcnow()
@@ -528,6 +529,41 @@ class AgentExtRpcCallback:
self._update_local_agent_resource_versions(context, agent_state)
return agent_status
@db_api.retry_if_session_inactive()
def delete_agent(self, context, **kwargs):
"""Delete agent on server
Deletes the agent on the server, if it exists.
"""
try:
host = kwargs['host']
agent_type = kwargs['agent_type']
except KeyError:
LOG.warning("Insufficient arguments: %s for delete_agent; "
"both 'host' and 'agent_type' are mandatory.",
kwargs)
return
agent = agent_obj.Agent.get_object(context, **kwargs)
if not agent:
LOG.debug("No agent found for host: %(host)s with agent_type: "
"%(agent_type)s, host already removed",
{'host': host, 'agent_type': agent_type})
return
agent.delete()
@db_api.retry_if_session_inactive()
def get_agents(self, context, **filters):
"""Get filtered list of agents.
Returns list of agents
"""
is_active = filters.pop('is_active', None)
agents = agent_obj.Agent.get_objects(context, **filters)
if is_active is not None:
is_active = converters.convert_to_boolean(is_active)
agents = [a for a in agents if a.is_active == is_active]
return agents
def _update_local_agent_resource_versions(self, context, agent_state):
resource_versions_dict = agent_state.get('resource_versions')
if not resource_versions_dict:

View File

@@ -385,3 +385,22 @@ class TestAgentExtRpcCallback(TestAgentsDbBase):
def test_has_alive_neutron_server(self):
alive = self.callback.has_alive_neutron_server(self.context)
self.assertTrue(alive)
def test_delete_agent(self):
with mock.patch.object(agent_obj, 'Agent',
autospec=True) as mock_agent:
fake_agent = mock.Mock()
mock_agent.get_object.return_value = fake_agent
kwargs = {'host': 'test-host',
'agent_type': 'test-agent-type'}
self.callback.delete_agent(self.context, **kwargs)
mock_agent.get_object.assert_called_once_with(
self.context, **kwargs)
fake_agent.delete.assert_called_once()
def test_get_agents(self):
with mock.patch.object(agent_obj.Agent, 'get_objects') as mock_get:
host = 'test-host'
self.callback.get_agents(self.context, host=host,
is_active=False)
mock_get.assert_called_once_with(self.context, host=host)

View File

@@ -0,0 +1,12 @@
---
features:
- |
Add new `get_agents` method to PluginReportStateAPI which fetches list of
agents depending on provided filters.
- |
Add new `delete_agent` method to PluginReportStateAPI which deletes the
agent.
upgrade:
- |
PluginReportStateAPI has a new version 1.4.