Skip to content

Commit 270f1da

Browse files
authored
Merge pull request #18 from stackhpc/upstream/yoga-2022-12-19
Synchronise yoga with upstream
2 parents fb72afa + f49cf59 commit 270f1da

File tree

10 files changed

+107
-28
lines changed

10 files changed

+107
-28
lines changed

neutron/db/availability_zone/router.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,4 @@ def _process_az_request(self, resource, event, trigger, payload):
4949
if az_hints:
5050
self.validate_availability_zones(context, 'router',
5151
az_hints)
52-
self.set_extra_attr_value(context, router_db, az_def.AZ_HINTS,
53-
az_hints)
52+
self.set_extra_attr_value(router_db, az_def.AZ_HINTS, az_hints)

neutron/db/l3_attrs_db.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,20 @@ def _extend_extra_router_dict(router_res, router_db):
4545
from_db = info.get('transform_from_db', lambda x: x)
4646
router_res[name] = from_db(extra_attrs.get(name, info['default']))
4747

48-
def _ensure_extra_attr_model(self, context, router_db):
49-
if not router_db['extra_attributes']:
50-
kwargs = {k: v['default'] for k, v in get_attr_info().items()}
51-
kwargs['router_id'] = router_db['id']
52-
new = l3_attrs.RouterExtraAttributes(**kwargs)
53-
context.session.add(new)
54-
router_db['extra_attributes'] = new
48+
@staticmethod
49+
def add_extra_attr(context, router_db):
50+
kwargs = {k: v['default'] for k, v in get_attr_info().items()}
51+
kwargs['router_id'] = router_db['id']
52+
new = l3_attrs.RouterExtraAttributes(**kwargs)
53+
context.session.add(new)
54+
router_db['extra_attributes'] = new
5555

56-
def set_extra_attr_value(self, context, router_db, key, value):
56+
@staticmethod
57+
def set_extra_attr_value(router_db, key, value):
5758
# set a single value explicitly
5859
if key in get_attr_info():
5960
info = get_attr_info()[key]
6061
to_db = info.get('transform_to_db', lambda x: x)
61-
self._ensure_extra_attr_model(context, router_db)
6262
router_db['extra_attributes'].update({key: to_db(value)})
6363
return
6464
raise RuntimeError(_("Tried to set a key '%s' that doesn't exist "

neutron/db/l3_db.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
from neutron.common import ipv6_utils
4949
from neutron.common import utils
5050
from neutron.db import _utils as db_utils
51+
from neutron.db import l3_attrs_db
5152
from neutron.db.models import l3 as l3_models
5253
from neutron.db.models import l3_attrs as l3_attrs_models
5354
from neutron.db import models_v2
@@ -248,6 +249,7 @@ def _create_router_db(self, context, router, tenant_id):
248249
status=constants.ACTIVE,
249250
description=router.get('description'))
250251
context.session.add(router_db)
252+
l3_attrs_db.ExtraAttributesMixin.add_extra_attr(context, router_db)
251253

252254
registry.publish(resources.ROUTER, events.PRECOMMIT_CREATE, self,
253255
payload=events.DBEventPayload(
@@ -283,6 +285,8 @@ def _get_stripped_router(self, router_body):
283285
def create_router(self, context, router):
284286
r = router['router']
285287
gw_info = r.get(EXTERNAL_GW_INFO, None)
288+
# TODO(ralonsoh): migrate "tenant_id" to "project_id"
289+
# https://blueprints.launchpad.net/neutron/+spec/keystone-v3
286290
create = functools.partial(self._create_router_db, context, r,
287291
r['tenant_id'])
288292
delete = functools.partial(self.delete_router, context)

neutron/db/l3_dvr_db.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,11 @@ def l3plugin(self):
103103
priority_group.PRIORITY_ROUTER_EXTENDED_ATTRIBUTE)
104104
def _set_distributed_flag(self, resource, event, trigger, payload):
105105
"""Event handler to set distributed flag on creation."""
106-
context = payload.context
107106
router = payload.latest_state
108107
router_db = payload.metadata['router_db']
109108
dist = is_distributed_router(router)
110109
router['distributed'] = dist
111-
self.l3plugin.set_extra_attr_value(context, router_db, 'distributed',
112-
dist)
110+
self.l3plugin.set_extra_attr_value(router_db, 'distributed', dist)
113111

114112
def _validate_router_migration(self, context, router_db, router_res,
115113
old_router=None):
@@ -203,8 +201,7 @@ def _handle_distributed_migration(self, resource, event,
203201
payload.context, payload.resource_id,
204202
agent['id'])
205203
self.l3plugin.set_extra_attr_value(
206-
payload.context, payload.desired_state,
207-
'distributed', migrating_to_distributed)
204+
payload.desired_state, 'distributed', migrating_to_distributed)
208205

209206
@registry.receives(resources.ROUTER, [events.AFTER_UPDATE],
210207
priority_group.PRIORITY_ROUTER_EXTENDED_ATTRIBUTE)

neutron/db/l3_hamode_db.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ def _precommit_router_create(self, resource, event, trigger, payload):
379379
router_db = payload.metadata['router_db']
380380
is_ha = self._is_ha(router)
381381
router['ha'] = is_ha
382-
self.set_extra_attr_value(context, router_db, 'ha', is_ha)
382+
self.set_extra_attr_value(router_db, 'ha', is_ha)
383383
if not is_ha:
384384
return
385385
# This will throw an exception if there aren't enough agents to
@@ -453,14 +453,14 @@ def _validate_migration(self, resource, event, trigger, payload=None):
453453
payload.desired_state.extra_attributes.ha_vr_id = None
454454
if (payload.request_body.get('distributed') or
455455
payload.states[0]['distributed']):
456-
self.set_extra_attr_value(payload.context, payload.desired_state,
456+
self.set_extra_attr_value(payload.desired_state,
457457
'ha', requested_ha_state)
458458
return
459459
self._migrate_router_ports(
460460
payload.context, payload.desired_state,
461461
old_owner=old_owner, new_owner=new_owner)
462462
self.set_extra_attr_value(
463-
payload.context, payload.desired_state, 'ha', requested_ha_state)
463+
payload.desired_state, 'ha', requested_ha_state)
464464

465465
@registry.receives(resources.ROUTER, [events.AFTER_UPDATE],
466466
priority_group.PRIORITY_ROUTER_EXTENDED_ATTRIBUTE)

neutron/objects/router.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717
from neutron_lib.api.definitions import availability_zone as az_def
1818
from neutron_lib.api.validators import availability_zone as az_validator
1919
from neutron_lib import constants as n_const
20+
from neutron_lib.db import api as db_api
2021
from neutron_lib.objects import common_types
2122
from neutron_lib.utils import net as net_utils
2223
from oslo_utils import versionutils
2324
from oslo_versionedobjects import fields as obj_fields
2425
from sqlalchemy import func
26+
from sqlalchemy import sql
2527

2628
from neutron.db.models import dvr as dvr_models
2729
from neutron.db.models import l3
@@ -230,6 +232,16 @@ def check_routers_not_owned_by_projects(cls, context, gw_ports, projects):
230232

231233
return bool(query.count())
232234

235+
@staticmethod
236+
@db_api.CONTEXT_READER
237+
def get_router_ids_without_router_std_attrs(context):
238+
r_attrs = l3_attrs.RouterExtraAttributes
239+
query = context.session.query(l3.Router)
240+
query = query.join(r_attrs, r_attrs.router_id == l3.Router.id,
241+
isouter=True)
242+
query = query.filter(r_attrs.router_id == sql.null())
243+
return [r.id for r in query.all()]
244+
233245

234246
@base.NeutronObjectRegistry.register
235247
class FloatingIP(base.NeutronDbObject):

neutron/plugins/ml2/drivers/ovn/mech_driver/ovsdb/maintenance.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from neutron_lib.api.definitions import segment as segment_def
2727
from neutron_lib import constants as n_const
2828
from neutron_lib import context as n_context
29+
from neutron_lib.db import api as db_api
2930
from neutron_lib import exceptions as n_exc
3031
from oslo_config import cfg
3132
from oslo_log import log
@@ -35,9 +36,11 @@
3536
from neutron.common.ovn import constants as ovn_const
3637
from neutron.common.ovn import utils
3738
from neutron.conf.plugins.ml2.drivers.ovn import ovn_conf
39+
from neutron.db import l3_attrs_db
3840
from neutron.db import ovn_hash_ring_db as hash_ring_db
3941
from neutron.db import ovn_revision_numbers_db as revision_numbers_db
4042
from neutron.db import segments_db
43+
from neutron.objects import router as router_obj
4144
from neutron.plugins.ml2.drivers.ovn.mech_driver.ovsdb import ovn_db_sync
4245

4346

@@ -893,6 +896,31 @@ def check_baremetal_ports_dhcp_options(self):
893896

894897
raise periodics.NeverAgain()
895898

899+
# TODO(ralonsoh): Remove this in the Antelope+4 cycle
900+
@periodics.periodic(spacing=600, run_immediately=True)
901+
def create_router_extra_attributes_registers(self):
902+
"""Create missing ``RouterExtraAttributes`` registers.
903+
904+
ML2/OVN L3 plugin does not inherit the ``ExtraAttributesMixin`` class.
905+
Before LP#1995974, the L3 plugin was not creating a
906+
``RouterExtraAttributes`` register per ``Routers`` register. This one
907+
only execution method finds those ``Routers`` registers without the
908+
child one and creates one with the default values.
909+
"""
910+
911+
if not self.has_lock:
912+
return
913+
914+
context = n_context.get_admin_context()
915+
for router_id in router_obj.Router.\
916+
get_router_ids_without_router_std_attrs(context):
917+
with db_api.CONTEXT_WRITER.using(context):
918+
router_db = {'id': router_id}
919+
l3_attrs_db.ExtraAttributesMixin.add_extra_attr(context,
920+
router_db)
921+
922+
raise periodics.NeverAgain()
923+
896924

897925
class HashRingHealthCheckPeriodics(object):
898926

neutron/tests/unit/db/test_l3_db.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
from neutron.db import extraroute_db
3838
from neutron.db import l3_db
3939
from neutron.db.models import l3 as l3_models
40+
from neutron.db.models import l3_attrs
4041
from neutron.db import models_v2
4142
from neutron.extensions import segment as segment_ext
4243
from neutron.objects import base as base_obj
@@ -869,6 +870,20 @@ def test_create_router_notify(self):
869870
]
870871
mock_publish.assert_has_calls(expected_calls)
871872

873+
def test_create_router_extra_attr(self):
874+
router_args = {'router': {'name': 'foo_router',
875+
'admin_state_up': True,
876+
'tenant_id': 'foo_tenant'}
877+
}
878+
router_dict = self.create_router(router_args)
879+
with db_api.CONTEXT_READER.using(self.ctx) as session:
880+
r_extra_attrs = session.query(
881+
l3_attrs.RouterExtraAttributes).filter(
882+
l3_attrs.RouterExtraAttributes.router_id ==
883+
router_dict['id']).all()
884+
self.assertEqual(1, len(r_extra_attrs))
885+
self.assertEqual(router_dict['id'], r_extra_attrs[0].router_id)
886+
872887
def test_update_router_notify(self):
873888
with mock.patch.object(l3_db.registry, 'publish') as mock_publish:
874889
self.mixin.update_router(self.ctx, self.router['id'],

neutron/tests/unit/extensions/test_l3.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -614,9 +614,15 @@ def setUp(self):
614614
self.mixin = l3_attrs_db.ExtraAttributesMixin()
615615
directory.add_plugin(plugin_constants.L3, self.mixin)
616616
self.ctx = context.get_admin_context()
617-
self.router = l3_models.Router()
618-
with db_api.CONTEXT_WRITER.using(self.ctx):
619-
self.ctx.session.add(self.router)
617+
self.router = self._new_router(self.ctx)
618+
619+
@staticmethod
620+
@db_api.CONTEXT_WRITER
621+
def _new_router(ctx):
622+
router = l3_models.Router()
623+
ctx.session.add(router)
624+
l3_attrs_db.ExtraAttributesMixin.add_extra_attr(ctx, router)
625+
return router
620626

621627
def _get_default_api_values(self):
622628
return {k: v.get('transform_from_db', lambda x: x)(v['default'])
@@ -625,8 +631,7 @@ def _get_default_api_values(self):
625631
def test_set_extra_attr_key_bad(self):
626632
with testtools.ExpectedException(RuntimeError):
627633
with db_api.CONTEXT_WRITER.using(self.ctx):
628-
self.mixin.set_extra_attr_value(self.ctx, self.router,
629-
'bad', 'value')
634+
self.mixin.set_extra_attr_value(self.router, 'bad', 'value')
630635

631636
def test__extend_extra_router_dict_defaults(self):
632637
rdict = {}
@@ -635,9 +640,8 @@ def test__extend_extra_router_dict_defaults(self):
635640

636641
def test_set_attrs_and_extend(self):
637642
with db_api.CONTEXT_WRITER.using(self.ctx):
638-
self.mixin.set_extra_attr_value(self.ctx, self.router,
639-
'ha_vr_id', 99)
640-
self.mixin.set_extra_attr_value(self.ctx, self.router,
643+
self.mixin.set_extra_attr_value(self.router, 'ha_vr_id', 99)
644+
self.mixin.set_extra_attr_value(self.router,
641645
'availability_zone_hints',
642646
['x', 'y', 'z'])
643647
expected = self._get_default_api_values()
@@ -647,7 +651,7 @@ def test_set_attrs_and_extend(self):
647651
self.mixin._extend_extra_router_dict(rdict, self.router)
648652
self.assertEqual(expected, rdict)
649653

650-
self.mixin.set_extra_attr_value(self.ctx, self.router,
654+
self.mixin.set_extra_attr_value(self.router,
651655
'availability_zone_hints',
652656
['z', 'y', 'z'])
653657
expected['availability_zone_hints'] = ['z', 'y', 'z']

neutron/tests/unit/objects/test_router.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414

1515
from unittest import mock
1616

17+
from neutron_lib.db import api as db_api
1718
from oslo_utils import uuidutils
1819

20+
from neutron.db import l3_attrs_db
1921
from neutron.objects.qos import binding as qos_binding
2022
from neutron.objects.qos import policy
2123
from neutron.objects import router
@@ -127,6 +129,24 @@ def test_check_routers_not_owned_by_projects(self):
127129
[project, new_project])
128130
self.assertFalse(router_exist)
129131

132+
def test_get_router_ids_without_router_std_attrs(self):
133+
def create_r_attr_reg(idx):
134+
with db_api.CONTEXT_WRITER.using(self.context):
135+
router_db = {'id': self.objs[idx].id}
136+
l3_attrs_db.ExtraAttributesMixin.add_extra_attr(self.context,
137+
router_db)
138+
139+
for idx in range(3):
140+
self.objs[idx].create()
141+
expected_router_ids = [r.id for r in self.objs]
142+
143+
for idx in range(3):
144+
router_ids = router.Router.\
145+
get_router_ids_without_router_std_attrs(self.context)
146+
self.assertEqual(expected_router_ids, router_ids)
147+
create_r_attr_reg(idx)
148+
expected_router_ids = expected_router_ids[1:]
149+
130150

131151
class RouterPortIfaceObjectTestCase(obj_test_base.BaseObjectIfaceTestCase):
132152

0 commit comments

Comments
 (0)