Calculate IPv4 DHCP subnets once for metadata
When enabling metadata, we iterate through the subnets on a network multiple times. Do it only once at the beginning and return early if there are no candidates. Follow-on to comments in an earlier review, https://review.openstack.org/#/c/293237 Had to fix a few tests that were creating "fake" subnets without an ip_version attribute or passing a network mock instead of a fake one. Change-Id: I57dfeec339a072e78242373bf793dbbf04e8e4c3
This commit is contained in:
parent
310f83bc4e
commit
b5223453bc
|
@ -1069,26 +1069,27 @@ class Dnsmasq(DhcpLocalProcess):
|
|||
providing access to the metadata service via logical routers built
|
||||
with 3rd party backends.
|
||||
"""
|
||||
if conf.force_metadata:
|
||||
# Only ipv4 subnet, with dhcp enabled, will use metadata proxy.
|
||||
return any(s for s in network.subnets
|
||||
if s.ip_version == 4 and s.enable_dhcp)
|
||||
# Only IPv4 subnets, with dhcp enabled, will use the metadata proxy.
|
||||
v4_dhcp_subnets = [s for s in network.subnets
|
||||
if s.ip_version == 4 and s.enable_dhcp]
|
||||
if not v4_dhcp_subnets:
|
||||
return False
|
||||
|
||||
if conf.enable_metadata_network and conf.enable_isolated_metadata:
|
||||
if conf.force_metadata:
|
||||
return True
|
||||
|
||||
if not conf.enable_isolated_metadata:
|
||||
return False
|
||||
|
||||
if conf.enable_metadata_network:
|
||||
# check if the network has a metadata subnet
|
||||
meta_cidr = netaddr.IPNetwork(METADATA_DEFAULT_CIDR)
|
||||
if any(netaddr.IPNetwork(s.cidr) in meta_cidr
|
||||
for s in network.subnets):
|
||||
return True
|
||||
|
||||
if not conf.enable_isolated_metadata:
|
||||
return False
|
||||
|
||||
isolated_subnets = cls.get_isolated_subnets(network)
|
||||
# Only ipv4 isolated subnet, which has dhcp enabled, will use
|
||||
# metadata proxy.
|
||||
return any(isolated_subnets[s.id] for s in network.subnets
|
||||
if s.ip_version == 4 and s.enable_dhcp)
|
||||
return any(isolated_subnets[s.id] for s in v4_dhcp_subnets)
|
||||
|
||||
|
||||
class DeviceManager(object):
|
||||
|
|
|
@ -68,7 +68,8 @@ fake_subnet2 = dhcp.DictModel(dict(id='dddddddd-dddd-dddd-dddddddddddd',
|
|||
|
||||
fake_subnet3 = dhcp.DictModel(dict(id='bbbbbbbb-1111-2222-bbbbbbbbbbbb',
|
||||
network_id='12345678-1234-5678-1234567890ab',
|
||||
cidr='192.168.1.1/24', enable_dhcp=True))
|
||||
cidr='192.168.1.1/24', enable_dhcp=True,
|
||||
ip_version=4))
|
||||
|
||||
fake_ipv6_subnet = dhcp.DictModel(dict(id='bbbbbbbb-1111-2222-bbbbbbbbbbbb',
|
||||
network_id='12345678-1234-5678-1234567890ab',
|
||||
|
@ -81,7 +82,7 @@ fake_meta_subnet = dhcp.DictModel(dict(id='bbbbbbbb-1111-2222-bbbbbbbbbbbb',
|
|||
network_id='12345678-1234-5678-1234567890ab',
|
||||
cidr='169.254.169.252/30',
|
||||
gateway_ip='169.254.169.253',
|
||||
enable_dhcp=True))
|
||||
enable_dhcp=True, ip_version=4))
|
||||
|
||||
fake_fixed_ip1 = dhcp.DictModel(dict(id='', subnet_id=fake_subnet1.id,
|
||||
ip_address='172.9.9.9'))
|
||||
|
@ -955,7 +956,7 @@ class TestDhcpAgentEventHandler(base.BaseTestCase):
|
|||
def test_subnet_create_restarts_with_dhcp_disabled(self):
|
||||
payload = dict(subnet=dhcp.DictModel(
|
||||
dict(network_id=fake_network.id, enable_dhcp=False,
|
||||
cidr='99.99.99.0/24')))
|
||||
cidr='99.99.99.0/24', ip_version=4)))
|
||||
self.cache.get_network_by_id.return_value = fake_network
|
||||
new_net = copy.deepcopy(fake_network)
|
||||
new_net.subnets.append(payload['subnet'])
|
||||
|
|
|
@ -2138,8 +2138,8 @@ class TestDnsmasq(TestBase):
|
|||
|
||||
def test_should_enable_metadata_isolated_meta_disabled_returns_false(self):
|
||||
self.conf.set_override('enable_isolated_metadata', False)
|
||||
self.assertFalse(dhcp.Dnsmasq.should_enable_metadata(self.conf,
|
||||
mock.ANY))
|
||||
self.assertFalse(dhcp.Dnsmasq.should_enable_metadata(
|
||||
self.conf, FakeV4MetadataNetwork()))
|
||||
|
||||
def test_should_enable_metadata_with_metadata_network_returns_true(self):
|
||||
self.conf.set_override('enable_metadata_network', True)
|
||||
|
|
Loading…
Reference in New Issue