Skip to content

Commit d6fe9ff

Browse files
authored
Merge pull request #107 from stackhpc/upstream/yoga-2024-01-08
Synchronise yoga with upstream
2 parents 6d935fa + 9ce8220 commit d6fe9ff

File tree

9 files changed

+136
-27
lines changed

9 files changed

+136
-27
lines changed

neutron/agent/metadata/driver.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,13 @@ def spawn_monitored_metadata_proxy(cls, monitor, ns_name, port, conf,
294294
pm = cls._get_metadata_proxy_process_manager(uuid, conf,
295295
ns_name=ns_name,
296296
callback=callback)
297-
pm.enable()
297+
try:
298+
pm.enable()
299+
except exceptions.ProcessExecutionError as exec_err:
300+
LOG.error("Encountered process execution error %(err)s while "
301+
"starting process in namespace %(ns)s",
302+
{"err": exec_err, "ns": ns_name})
303+
return
298304
monitor.register(uuid, METADATA_SERVICE_NAME, pm)
299305
cls.monitors[router_id] = pm
300306

neutron/agent/ovn/metadata/driver.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,13 @@ def spawn_monitored_metadata_proxy(cls, monitor, ns_name, port, conf,
197197
pm = cls._get_metadata_proxy_process_manager(uuid, conf,
198198
ns_name=ns_name,
199199
callback=callback)
200-
pm.enable()
200+
try:
201+
pm.enable()
202+
except exceptions.ProcessExecutionError as exec_err:
203+
LOG.error("Encountered process execution error %(err)s while "
204+
"starting process in namespace %(ns)s",
205+
{"err": exec_err, "ns": ns_name})
206+
return
201207
monitor.register(uuid, METADATA_SERVICE_NAME, pm)
202208
cls.monitors[router_id] = pm
203209

neutron/api/rpc/handlers/securitygroups_rpc.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,10 @@ def _select_sg_ids_for_ports(self, context, ports):
431431
for sg_id in p['security_group_ids']))
432432
return [(sg_id, ) for sg_id in sg_ids]
433433

434-
def _is_security_group_stateful(self, context, sg_id):
435-
sg = self.rcache.get_resource_by_id(resources.SECURITYGROUP, sg_id)
436-
return sg.stateful
434+
def _get_sgs_stateful_flag(self, context, sg_ids):
435+
sgs_stateful = {}
436+
for sg_id in sg_ids:
437+
sg = self.rcache.get_resource_by_id(resources.SECURITYGROUP, sg_id)
438+
sgs_stateful[sg_id] = sg.stateful
439+
440+
return sgs_stateful

neutron/db/securitygroups_rpc_base.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -211,12 +211,10 @@ def security_group_info_for_ports(self, context, ports):
211211
# this set will be serialized into a list by rpc code
212212
remote_address_group_info[remote_ag_id][ethertype] = set()
213213
direction = rule_in_db['direction']
214-
stateful = self._is_security_group_stateful(context,
215-
security_group_id)
216214
rule_dict = {
217215
'direction': direction,
218216
'ethertype': ethertype,
219-
'stateful': stateful}
217+
}
220218

221219
for key in ('protocol', 'port_range_min', 'port_range_max',
222220
'remote_ip_prefix', 'remote_group_id',
@@ -234,6 +232,13 @@ def security_group_info_for_ports(self, context, ports):
234232
if rule_dict not in sg_info['security_groups'][security_group_id]:
235233
sg_info['security_groups'][security_group_id].append(
236234
rule_dict)
235+
236+
# Populate the security group "stateful" flag in the SGs list of rules.
237+
for sg_id, stateful in self._get_sgs_stateful_flag(
238+
context, sg_info['security_groups'].keys()).items():
239+
for rule in sg_info['security_groups'][sg_id]:
240+
rule['stateful'] = stateful
241+
237242
# Update the security groups info if they don't have any rules
238243
sg_ids = self._select_sg_ids_for_ports(context, ports)
239244
for (sg_id, ) in sg_ids:
@@ -427,13 +432,13 @@ def _select_sg_ids_for_ports(self, context, ports):
427432
"""
428433
raise NotImplementedError()
429434

430-
def _is_security_group_stateful(self, context, sg_id):
431-
"""Return whether the security group is stateful or not.
435+
def _get_sgs_stateful_flag(self, context, sg_id):
436+
"""Return the security groups stateful flag.
432437
433-
Return True if the security group associated with the given ID
434-
is stateful, else False.
438+
Returns a dictionary with the SG ID as key and the stateful flag:
439+
{sg_1: True, sg_2: False, ...}
435440
"""
436-
return True
441+
raise NotImplementedError()
437442

438443

439444
class SecurityGroupServerRpcMixin(SecurityGroupInfoAPIMixin,
@@ -526,5 +531,5 @@ def _select_ips_for_remote_address_group(self, context,
526531
return ips_by_group
527532

528533
@db_api.retry_if_session_inactive()
529-
def _is_security_group_stateful(self, context, sg_id):
530-
return sg_obj.SecurityGroup.get_sg_by_id(context, sg_id).stateful
534+
def _get_sgs_stateful_flag(self, context, sg_ids):
535+
return sg_obj.SecurityGroup.get_sgs_stateful_flag(context, sg_ids)

neutron/objects/securitygroup.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# under the License.
1212

1313
from neutron_lib import context as context_lib
14+
from neutron_lib.db import api as db_api
1415
from neutron_lib.objects import common_types
1516
from neutron_lib.utils import net as net_utils
1617
from oslo_utils import versionutils
@@ -132,6 +133,13 @@ def get_bound_project_ids(cls, context, obj_id):
132133
security_group_ids=[obj_id])
133134
return {port.project_id for port in port_objs}
134135

136+
@classmethod
137+
@db_api.CONTEXT_READER
138+
def get_sgs_stateful_flag(cls, context, sg_ids):
139+
query = context.session.query(cls.db_model.id, cls.db_model.stateful)
140+
query = query.filter(cls.db_model.id.in_(sg_ids))
141+
return dict(query.all())
142+
135143

136144
@base.NeutronObjectRegistry.register
137145
class DefaultSecurityGroup(base.NeutronDbObject):

neutron/tests/unit/agent/dhcp/test_agent.py

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -875,23 +875,37 @@ def _process_manager_constructor_call(self, ns=FAKE_NETWORK_DHCP_NS):
875875
def _enable_dhcp_helper(self, network, enable_isolated_metadata=False,
876876
is_isolated_network=False, is_ovn_network=False):
877877
self.dhcp._process_monitor = mock.Mock()
878+
# The disable() call
879+
gmppm_expected_calls = [mock.call(FAKE_NETWORK_UUID, cfg.CONF,
880+
ns_name=FAKE_NETWORK_DHCP_NS)]
878881
if enable_isolated_metadata:
879882
cfg.CONF.set_override('enable_isolated_metadata', True)
883+
if is_isolated_network:
884+
# The enable() call
885+
gmppm_expected_calls.append(
886+
mock.call(FAKE_NETWORK_UUID, cfg.CONF,
887+
ns_name=FAKE_NETWORK_DHCP_NS,
888+
callback=mock.ANY))
880889
self.plugin.get_network_info.return_value = network
881-
self.dhcp.enable_dhcp_helper(network.id)
890+
process_instance = mock.Mock(active=False)
891+
with mock.patch.object(metadata_driver.MetadataDriver,
892+
'_get_metadata_proxy_process_manager',
893+
return_value=process_instance) as gmppm:
894+
self.dhcp.enable_dhcp_helper(network.id)
895+
gmppm.assert_has_calls(gmppm_expected_calls)
882896
self.plugin.assert_has_calls([
883897
mock.call.get_network_info(network.id)])
884898
self.call_driver.assert_called_once_with('enable', network)
885899
self.cache.assert_has_calls([mock.call.put(network)])
886900
if (is_isolated_network and enable_isolated_metadata and not
887901
is_ovn_network):
888-
self.external_process.assert_has_calls([
889-
self._process_manager_constructor_call(),
890-
mock.call().enable()], any_order=True)
902+
process_instance.assert_has_calls([
903+
mock.call.disable(sig=str(int(signal.SIGTERM))),
904+
mock.call.get_pid_file_name(),
905+
mock.call.enable()])
891906
else:
892-
self.external_process.assert_has_calls([
893-
self._process_manager_constructor_call(),
894-
mock.call().disable(sig=str(int(signal.SIGTERM)))])
907+
process_instance.assert_has_calls([
908+
mock.call.disable(sig=str(int(signal.SIGTERM)))])
895909

896910
def test_enable_dhcp_helper_enable_metadata_isolated_network(self):
897911
self._enable_dhcp_helper(isolated_network,
@@ -1065,11 +1079,16 @@ def test_disable_dhcp_helper_driver_failure(self):
10651079

10661080
def test_enable_isolated_metadata_proxy(self):
10671081
self.dhcp._process_monitor = mock.Mock()
1068-
self.dhcp.enable_isolated_metadata_proxy(fake_network)
1069-
self.external_process.assert_has_calls([
1070-
self._process_manager_constructor_call(),
1071-
mock.call().enable()
1072-
], any_order=True)
1082+
process_instance = mock.Mock(active=False)
1083+
with mock.patch.object(metadata_driver.MetadataDriver,
1084+
'_get_metadata_proxy_process_manager',
1085+
return_value=process_instance) as gmppm:
1086+
self.dhcp.enable_isolated_metadata_proxy(fake_network)
1087+
gmppm.assert_called_with(FAKE_NETWORK_UUID,
1088+
cfg.CONF,
1089+
ns_name=FAKE_NETWORK_DHCP_NS,
1090+
callback=mock.ANY)
1091+
process_instance.enable.assert_called_once()
10731092

10741093
def test_disable_isolated_metadata_proxy(self):
10751094
method_path = ('neutron.agent.metadata.driver.MetadataDriver'

neutron/tests/unit/agent/metadata/test_driver.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from unittest import mock
1919

2020
from neutron_lib import constants
21+
from neutron_lib import exceptions as lib_exceptions
2122
from neutron_lib import fixture as lib_fixtures
2223
from oslo_config import cfg
2324
from oslo_utils import uuidutils
@@ -241,6 +242,26 @@ def test_spawn_metadata_proxy(self):
241242
def test_spawn_metadata_proxy_dad_failed(self):
242243
self._test_spawn_metadata_proxy(dad_failed=True)
243244

245+
@mock.patch.object(metadata_driver.LOG, 'error')
246+
def test_spawn_metadata_proxy_handles_process_exception(self, error_log):
247+
process_instance = mock.Mock(active=False)
248+
process_instance.enable.side_effect = (
249+
lib_exceptions.ProcessExecutionError('Something happened', -1))
250+
with mock.patch.object(metadata_driver.MetadataDriver,
251+
'_get_metadata_proxy_process_manager',
252+
return_value=process_instance):
253+
process_monitor = mock.Mock()
254+
network_id = 123456
255+
metadata_driver.MetadataDriver.spawn_monitored_metadata_proxy(
256+
process_monitor,
257+
'dummy_namespace',
258+
self.METADATA_PORT,
259+
cfg.CONF,
260+
network_id=network_id)
261+
error_log.assert_called_once()
262+
process_monitor.register.assert_not_called()
263+
self.assertNotIn(network_id, metadata_driver.MetadataDriver.monitors)
264+
244265
def test_create_config_file_wrong_user(self):
245266
with mock.patch('pwd.getpwnam', side_effect=KeyError):
246267
config = metadata_driver.HaproxyConfigurator(_uuid(),

neutron/tests/unit/agent/ovn/metadata/test_driver.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import os
1717
from unittest import mock
1818

19+
from neutron_lib import exceptions as lib_exceptions
1920
from neutron_lib import fixture as lib_fixtures
2021
from oslo_config import cfg
2122
from oslo_utils import uuidutils
@@ -108,6 +109,29 @@ def test_spawn_metadata_proxy(self):
108109
run_as_root=True)
109110
])
110111

112+
@mock.patch.object(metadata_driver.LOG, 'error')
113+
def test_spawn_metadata_proxy_handles_process_exception(self, error_log):
114+
process_instance = mock.Mock(active=False)
115+
process_instance.enable.side_effect = (
116+
lib_exceptions.ProcessExecutionError('Something happened', -1))
117+
118+
with mock.patch.object(metadata_driver.MetadataDriver,
119+
'_get_metadata_proxy_process_manager',
120+
return_value=process_instance):
121+
process_monitor = mock.Mock()
122+
network_id = 123456
123+
124+
metadata_driver.MetadataDriver.spawn_monitored_metadata_proxy(
125+
process_monitor,
126+
'dummy_namespace',
127+
self.METADATA_PORT,
128+
cfg.CONF,
129+
network_id=network_id)
130+
131+
error_log.assert_called_once()
132+
process_monitor.register.assert_not_called()
133+
self.assertNotIn(network_id, metadata_driver.MetadataDriver.monitors)
134+
111135
def test_create_config_file_wrong_user(self):
112136
with mock.patch('pwd.getpwnam', side_effect=KeyError):
113137
config = metadata_driver.HaproxyConfigurator(mock.ANY, mock.ANY,

neutron/tests/unit/objects/test_securitygroup.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,22 @@ def test_get_objects_no_synth(self):
210210
self.assertEqual(len(sg_obj.rules), 0)
211211
self.assertIsNone(listed_objs[0].rules)
212212

213+
def test_get_sgs_stateful_flag(self):
214+
for obj in self.objs:
215+
obj.create()
216+
217+
sg_ids = tuple(sg.id for sg in self.objs)
218+
sgs_stateful = securitygroup.SecurityGroup.get_sgs_stateful_flag(
219+
self.context, sg_ids)
220+
for sg_id, stateful in sgs_stateful.items():
221+
for obj in (obj for obj in self.objs if obj.id == sg_id):
222+
self.assertEqual(obj.stateful, stateful)
223+
224+
sg_ids = sg_ids + ('random_id_not_present', )
225+
sgs_stateful = securitygroup.SecurityGroup.get_sgs_stateful_flag(
226+
self.context, sg_ids)
227+
self.assertEqual(len(self.objs), len(sgs_stateful))
228+
213229

214230
class DefaultSecurityGroupIfaceObjTestCase(test_base.BaseObjectIfaceTestCase):
215231

0 commit comments

Comments
 (0)