Browse Source

Use dict .get() to avoid a KeyError in the segment plugin

On subnet delete, the supplied subnet in the post hook
could contain a subnet without certain items, leading to
a KeyError in the segment plugin.  Fix a number of these
occurences so this cannot happen.

Also fixed similar code in the segment tests.

Change-Id: I645610febde446b78ed6edd868e699673648a4de
Closes-bug: #1868724
(cherry picked from commit 07b015d789)
(cherry picked from commit b019821abd)
changes/45/749945/1
Brian Haley 1 year ago
committed by Slawek Kaplonski
parent
commit
97f6e2569e
  1. 23
      neutron/services/segments/plugin.py
  2. 4
      neutron/tests/unit/extensions/test_segment.py

23
neutron/services/segments/plugin.py

@ -260,10 +260,10 @@ class NovaSegmentNotifier(object):
total += int(netaddr.IPAddress(pool['end']) -
netaddr.IPAddress(pool['start'])) + 1
if total:
if subnet['gateway_ip']:
if subnet.get('gateway_ip'):
total += 1
reserved += 1
if subnet['enable_dhcp']:
if subnet.get('enable_dhcp'):
reserved += 1
return total, reserved
@ -561,7 +561,7 @@ class SegmentHostRoutes(object):
context=context,
ip_version=netaddr.IPNetwork(subnet['cidr']).version,
network_id=subnet['network_id'],
segment_id=subnet['segment_id'],
segment_id=segment_id,
host_routes=copy.deepcopy(host_routes),
gateway_ip=gateway_ip)
if (not host_routes or
@ -573,11 +573,14 @@ class SegmentHostRoutes(object):
def host_routes_before_update(self, resource, event, trigger, **kwargs):
context = kwargs['context']
subnet, original_subnet = kwargs['request'], kwargs['original_subnet']
segment_id = subnet.get('segment_id', original_subnet['segment_id'])
gateway_ip = subnet.get('gateway_ip', original_subnet['gateway_ip'])
host_routes = subnet.get('host_routes', original_subnet['host_routes'])
if (segment_id and (host_routes != original_subnet['host_routes'] or
gateway_ip != original_subnet['gateway_ip'])):
orig_segment_id = original_subnet.get('segment_id')
segment_id = subnet.get('segment_id', orig_segment_id)
orig_gateway_ip = original_subnet.get('gateway_ip')
gateway_ip = subnet.get('gateway_ip', orig_gateway_ip)
orig_host_routes = original_subnet.get('host_routes')
host_routes = subnet.get('host_routes', orig_host_routes)
if (segment_id and (host_routes != orig_host_routes or
gateway_ip != orig_gateway_ip)):
calc_host_routes = self._calculate_routed_network_host_routes(
context=context,
ip_version=netaddr.IPNetwork(original_subnet['cidr']).version,
@ -585,8 +588,8 @@ class SegmentHostRoutes(object):
segment_id=segment_id,
host_routes=copy.deepcopy(host_routes),
gateway_ip=gateway_ip,
old_gateway_ip=original_subnet['gateway_ip'] if (
gateway_ip != original_subnet['gateway_ip']) else None)
old_gateway_ip=orig_gateway_ip if (
gateway_ip != orig_gateway_ip) else None)
if self._host_routes_need_update(host_routes, calc_host_routes):
subnet['host_routes'] = calc_host_routes

4
neutron/tests/unit/extensions/test_segment.py

@ -1700,10 +1700,10 @@ class TestNovaSegmentNotifier(SegmentAwareIpamTestCase):
total += int(netaddr.IPAddress(pool['end']) -
netaddr.IPAddress(pool['start'])) + 1
if total:
if subnet['gateway_ip']:
if subnet.get('gateway_ip'):
total += 1
reserved += 1
if subnet['enable_dhcp']:
if subnet.get('enable_dhcp'):
reserved += 1
return total, reserved

Loading…
Cancel
Save