From d605967c975e1ebd90d430061a7530510d0dd5f2 Mon Sep 17 00:00:00 2001 From: Brian Haley Date: Wed, 3 May 2017 12:25:57 -0400 Subject: [PATCH] Fix error getting segment_id in linux DHCP driver A recent change [1] added code to the linux DHCP driver to check the subnet segment_id attribute when adding extra host routes for a subnet. I have seen both KeyError and AttributeError failures in various check jobs lately since there is no guarantee all subnets have this attribute. Be safer by using getattr(). [1] https://review.openstack.org/#/c/438171/ Related-bug: #1668154 Change-Id: I96bea4edab4c96f2ca28ba854f6ca8d4ab4df5f5 --- neutron/agent/linux/dhcp.py | 12 +++++++----- neutron/tests/unit/agent/linux/test_dhcp.py | 2 -- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/neutron/agent/linux/dhcp.py b/neutron/agent/linux/dhcp.py index 61290a47645..12502ca3d9c 100644 --- a/neutron/agent/linux/dhcp.py +++ b/neutron/agent/linux/dhcp.py @@ -869,6 +869,7 @@ class Dnsmasq(DhcpLocalProcess): isolated_subnets = self.get_isolated_subnets(self.network) for i, subnet in enumerate(self.network.subnets): addr_mode = getattr(subnet, 'ipv6_address_mode', None) + segment_id = getattr(subnet, 'segment_id', None) if (not subnet.enable_dhcp or (subnet.ip_version == 6 and addr_mode == constants.IPV6_SLAAC)): @@ -914,11 +915,12 @@ class Dnsmasq(DhcpLocalProcess): ) if subnet.ip_version == 4: - host_routes.extend(["%s,0.0.0.0" % (s.cidr) for s in - self.network.subnets - if (s.ip_version == 4 and - s.cidr != subnet.cidr and - s.segment_id == subnet.segment_id)]) + for s in self.network.subnets: + sub_segment_id = getattr(s, 'segment_id', None) + if (s.ip_version == 4 and + s.cidr != subnet.cidr and + sub_segment_id == segment_id): + host_routes.append("%s,0.0.0.0" % s.cidr) if host_routes: if gateway: diff --git a/neutron/tests/unit/agent/linux/test_dhcp.py b/neutron/tests/unit/agent/linux/test_dhcp.py index b09194fb90c..952f69365d0 100644 --- a/neutron/tests/unit/agent/linux/test_dhcp.py +++ b/neutron/tests/unit/agent/linux/test_dhcp.py @@ -385,7 +385,6 @@ class FakeV4Subnet(Dictable): self.enable_dhcp = True self.host_routes = [FakeV4HostRoute()] self.dns_nameservers = ['8.8.8.8'] - self.segment_id = None class FakeV4Subnet2(FakeV4Subnet): @@ -499,7 +498,6 @@ class FakeV4SubnetNoDHCP(object): self.enable_dhcp = False self.host_routes = [] self.dns_nameservers = [] - self.segment_id = None class FakeV6SubnetDHCPStateful(Dictable):