Merge "Calculate IPv4 DHCP subnets once for metadata"

This commit is contained in:
Jenkins 2016-12-20 10:42:54 +00:00 committed by Gerrit Code Review
commit 96587ce6af
3 changed files with 19 additions and 17 deletions

View File

@ -1073,26 +1073,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):

View File

@ -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'])

View File

@ -2167,8 +2167,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)