Skip to content

Commit 09c1c61

Browse files
committed
Make get_ports RPC method common for the DHCP and Metadata agent
This patch is the initial implementation on the suggestion from this patch[1]. The DHCP agent can query the existing `get_ports` RPC method because this method is already exposed in the MetadataRpcCallback(server side) which runs under the same topic(PLUGIN) and namespace(None). The benefit here is that there is no change needed to the API, however it does go against how we historically setup the RPC layer between a server and client. [1] https://review.opendev.org/c/openstack/neutron/+/903572/comments/3d4e0453_4b4d2ab6 Related-Bug: #1982569 Change-Id: Icd7c55d2a5103bdbd90907b1dbfb9ccfe34c020a (cherry picked from commit 3b6e642)
1 parent 5033488 commit 09c1c61

File tree

5 files changed

+61
-23
lines changed

5 files changed

+61
-23
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copyright 2024 Red Hat, Inc.
2+
# All Rights Reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
5+
# not use this file except in compliance with the License. You may obtain
6+
# a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
# License for the specific language governing permissions and limitations
14+
# under the License.
15+
16+
from neutron_lib import rpc as n_rpc
17+
from oslo_messaging import Target
18+
19+
20+
class BasePluginApi(object):
21+
"""Base agent side of the rpc API"""
22+
def __init__(self, topic, namespace, version):
23+
target = Target(
24+
topic=topic,
25+
namespace=namespace,
26+
version=version)
27+
self.client = n_rpc.get_client(target)
28+
29+
def get_ports(self, context, port_filters):
30+
# NOTE(mtomaska): The MetadataRpcCallback (server side) API version 1.0
31+
# exposes get_ports, under the PLUGIN topic and None namespace.
32+
cctxt = self.client.prepare(version='1.0')
33+
return cctxt.call(context, 'get_ports', filters=port_filters)

neutron/agent/dhcp/agent.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
from neutron_lib import constants
2626
from neutron_lib import context
2727
from neutron_lib import exceptions
28-
from neutron_lib import rpc as n_rpc
2928
from oslo_concurrency import lockutils
3029
from oslo_config import cfg
3130
from oslo_log import helpers as log_helpers
@@ -38,6 +37,7 @@
3837
from oslo_utils import timeutils
3938

4039
from neutron._i18n import _
40+
from neutron.agent.common import base_agent_rpc
4141
from neutron.agent.common import resource_processing_queue as queue
4242
from neutron.agent.linux import dhcp
4343
from neutron.agent.linux import external_process
@@ -838,7 +838,7 @@ def disable_isolated_metadata_proxy(self, network):
838838
del self._metadata_routers[network.id]
839839

840840

841-
class DhcpPluginApi(object):
841+
class DhcpPluginApi(base_agent_rpc.BasePluginApi):
842842
"""Agent side of the dhcp rpc API.
843843
844844
This class implements the client side of an rpc interface. The server side
@@ -858,11 +858,10 @@ class DhcpPluginApi(object):
858858

859859
def __init__(self, topic, host):
860860
self.host = host
861-
target = oslo_messaging.Target(
861+
super().__init__(
862862
topic=topic,
863863
namespace=constants.RPC_NAMESPACE_DHCP_PLUGIN,
864864
version='1.0')
865-
self.client = n_rpc.get_client(target)
866865

867866
@property
868867
def context(self):
@@ -918,6 +917,11 @@ def get_dhcp_port(self, port_id):
918917
if port:
919918
return dhcp.DictModel(port)
920919

920+
def get_ports(self, port_filters):
921+
ports = super().get_ports(self.context, port_filters)
922+
if ports:
923+
return [dhcp.DictModel(port) for port in ports]
924+
921925
def dhcp_ready_on_ports(self, port_ids):
922926
"""Notify the server that DHCP is configured for the port."""
923927
cctxt = self.client.prepare(version='1.5')

neutron/agent/linux/dhcp.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,11 +1210,15 @@ def _output_opts_file(self):
12101210
return name
12111211

12121212
def _get_ovn_metadata_port_ip(self, subnet):
1213-
m_ports = [port for port in self.network.ports if
1214-
self._is_ovn_metadata_port(port, self.network.id)]
1215-
if m_ports:
1216-
port = self.device_manager.plugin.get_dhcp_port(m_ports[0].id)
1217-
for fixed_ip in port.fixed_ips:
1213+
"""Check if provided subnet contains OVN metadata port"""
1214+
ports_result = self.device_manager.plugin.get_ports(
1215+
port_filters={
1216+
'device_owner': [constants.DEVICE_OWNER_DISTRIBUTED],
1217+
'device_id': ['ovnmeta-' + self.network.id]
1218+
},
1219+
)
1220+
if ports_result:
1221+
for fixed_ip in ports_result[0].get('fixed_ips', []):
12181222
if fixed_ip.subnet_id == subnet.id:
12191223
return fixed_ip.ip_address
12201224

neutron/agent/metadata/agent.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,16 @@
1919
from neutron_lib.agent import topics
2020
from neutron_lib import constants
2121
from neutron_lib import context
22-
from neutron_lib import rpc as n_rpc
2322
from neutron_lib.utils import host
2423
from oslo_config import cfg
2524
from oslo_log import log as logging
26-
import oslo_messaging
2725
from oslo_service import loopingcall
2826
from oslo_utils import netutils
2927
import requests
3028
import webob
3129

3230
from neutron._i18n import _
31+
from neutron.agent.common import base_agent_rpc
3332
from neutron.agent.linux import utils as agent_utils
3433
from neutron.agent import rpc as agent_rpc
3534
from neutron.common import cache_utils as cache
@@ -46,7 +45,7 @@
4645
}
4746

4847

49-
class MetadataPluginAPI(object):
48+
class MetadataPluginAPI(base_agent_rpc.BasePluginApi):
5049
"""Agent-side RPC for metadata agent-to-plugin interaction.
5150
5251
This class implements the client side of an rpc interface used by the
@@ -61,15 +60,10 @@ class MetadataPluginAPI(object):
6160
"""
6261

6362
def __init__(self, topic):
64-
target = oslo_messaging.Target(
63+
super().__init__(
6564
topic=topic,
6665
namespace=constants.RPC_NAMESPACE_METADATA,
6766
version='1.0')
68-
self.client = n_rpc.get_client(target)
69-
70-
def get_ports(self, context, filters):
71-
cctxt = self.client.prepare()
72-
return cctxt.call(context, 'get_ports', filters=filters)
7367

7468

7569
class MetadataProxyHandler(object):

neutron/tests/unit/agent/linux/test_dhcp.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,11 @@ def __str__(self):
7373
# A base class where class attributes can also be accessed by treating
7474
# an instance as a dict.
7575
class Dictable(object):
76-
def __getitem__(self, k):
77-
return self.__class__.__dict__.get(k)
76+
def __getitem__(self, k, default_value=None):
77+
return self.__dict__.get(k, default_value)
78+
79+
def get(self, k, default_value=None):
80+
return self.__getitem__(k, default_value)
7881

7982

8083
class FakeDhcpPort(object):
@@ -90,7 +93,7 @@ def __init__(self):
9093
self.extra_dhcp_opts = []
9194

9295

93-
class FakeOvnMetadataPort(object):
96+
class FakeOvnMetadataPort(Dictable):
9497
def __init__(self):
9598
self.id = 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaa'
9699
self.admin_state_up = True
@@ -3205,8 +3208,8 @@ def test__generate_opts_per_subnet_no_metadata(self):
32053208
def test__generate_opts_per_subnet_with_metadata_port(self):
32063209
config = {'enable_isolated_metadata': False,
32073210
'force_metadata': False}
3208-
self.mock_mgr.return_value.plugin.get_dhcp_port.return_value = \
3209-
FakeOvnMetadataPort()
3211+
self.mock_mgr.return_value.plugin.get_ports.return_value = \
3212+
[FakeOvnMetadataPort()]
32103213
self._test__generate_opts_per_subnet_helper(config, True,
32113214
network_class=FakeNetworkDhcpandOvnMetadataPort)
32123215

0 commit comments

Comments
 (0)