Merge lp:~lamont/maas/bug-1602482 into lp:~maas-committers/maas/trunk

Proposed by LaMont Jones
Status: Merged
Approved by: LaMont Jones
Approved revision: no longer in the source branch.
Merged at revision: 5483
Proposed branch: lp:~lamont/maas/bug-1602482
Merge into: lp:~maas-committers/maas/trunk
Diff against target: 102 lines (+21/-18)
2 files modified
src/maasserver/models/staticipaddress.py (+18/-9)
src/maasserver/models/tests/test_staticipaddress.py (+3/-9)
To merge this branch: bzr merge lp:~lamont/maas/bug-1602482
Reviewer Review Type Date Requested Status
Blake Rouse (community) Approve
Review via email: mp+308517@code.launchpad.net

Commit message

If there are any assigned (not DISCOVERED) IP addresses on a host, then do not add any DISCOVERED addresses to the DNS.

Description of the change

If there are any assigned (not DISCOVERED) IP addresses on a host, then do not add any DISCOVERED addresses to the DNS.

To post a comment you must log in.
Revision history for this message
Blake Rouse (blake-rouse) wrote :

Looks good. Makes since, surprised we where not doing this already.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/maasserver/models/staticipaddress.py'
2--- src/maasserver/models/staticipaddress.py 2016-10-12 16:19:16 +0000
3+++ src/maasserver/models/staticipaddress.py 2016-10-14 14:24:16 +0000
4@@ -420,7 +420,8 @@
5 node.node_type,
6 """ + ttl_clause + """ AS ttl,
7 staticip.ip,
8- interface.name
9+ interface.name,
10+ alloc_type != 6 /* DISCOVERED */ AS assigned
11 FROM
12 maasserver_interface AS interface
13 JOIN maasserver_node AS node ON
14@@ -457,6 +458,7 @@
15 host(staticip.ip) != ''
16 ORDER BY
17 node.hostname,
18+ assigned DESC, /* Return all assigned IPs for a node first. */
19 interface.id
20 """
21 # We get user reserved et al mappings first, so that we can overwrite
22@@ -467,6 +469,7 @@
23 iface_is_boot = defaultdict(bool, {
24 hostname: True for hostname in mapping.keys()
25 })
26+ assigned_ips = defaultdict(bool)
27 cursor.execute(sql_query, query_parms)
28 # The records from the query provide, for each hostname (after
29 # stripping domain), the boot and non-boot interface ip address in ipv4
30@@ -485,16 +488,22 @@
31 if is_boot == iface_is_boot[fqdn]:
32 mapping[fqdn].ips.add(ip)
33 # Next, get all the addresses, on all the interfaces, and add the ones
34- # that are not already present on the FQDN as $IFACE.$FQDN.
35+ # that are not already present on the FQDN as $IFACE.$FQDN. Exclude
36+ # any discovered addresses once there are any non-discovered addresses.
37 cursor.execute(iface_sql_query, (domain_or_subnet.id,))
38 for (fqdn, system_id, node_type, ttl,
39- ip, iface_name) in cursor.fetchall():
40- if ip not in mapping[fqdn].ips:
41- name = "%s.%s" % (iface_name, fqdn)
42- mapping[name].node_type = node_type
43- mapping[name].system_id = system_id
44- mapping[name].ttl = ttl
45- mapping[name].ips.add(ip)
46+ ip, iface_name, assigned) in cursor.fetchall():
47+ if assigned:
48+ assigned_ips[fqdn] = True
49+ # If this is an assigned IP, or there are NO assigned IPs on the
50+ # node, then consider adding the IP.
51+ if assigned or not assigned_ips[fqdn]:
52+ if ip not in mapping[fqdn].ips:
53+ name = "%s.%s" % (iface_name, fqdn)
54+ mapping[name].node_type = node_type
55+ mapping[name].system_id = system_id
56+ mapping[name].ttl = ttl
57+ mapping[name].ips.add(ip)
58 return mapping
59
60 def filter_by_ip_family(self, family):
61
62=== modified file 'src/maasserver/models/tests/test_staticipaddress.py'
63--- src/maasserver/models/tests/test_staticipaddress.py 2016-10-12 17:34:30 +0000
64+++ src/maasserver/models/tests/test_staticipaddress.py 2016-10-14 14:24:16 +0000
65@@ -600,16 +600,14 @@
66 staticip = factory.make_StaticIPAddress(
67 alloc_type=IPADDRESS_TYPE.AUTO, interface=iface,
68 subnet=subnet)
69- discovered = factory.make_StaticIPAddress(
70+ factory.make_StaticIPAddress(
71 alloc_type=IPADDRESS_TYPE.DISCOVERED, interface=iface,
72 subnet=subnet)
73 mapping = StaticIPAddress.objects.get_hostname_ip_mapping(
74 node.domain)
75 expected_mapping = {
76 node.fqdn: HostnameIPMapping(
77- node.system_id, 30, {staticip.ip}, node.node_type),
78- "%s.%s" % (iface.name, node.fqdn): HostnameIPMapping(
79- node.system_id, 30, {discovered.ip}, node.node_type)}
80+ node.system_id, 30, {staticip.ip}, node.node_type)}
81 self.assertEqual(expected_mapping, mapping)
82
83 def test_get_hostname_ip_mapping_prefers_bond_with_no_boot_interface(self):
84@@ -828,9 +826,7 @@
85 mapping = StaticIPAddress.objects.get_hostname_ip_mapping(domain)
86 expected_mapping = {
87 node.fqdn: HostnameIPMapping(
88- node.system_id, 30, {sip0.ip}, node.node_type),
89- "%s.%s" % (iface1.name, node.fqdn): HostnameIPMapping(
90- node.system_id, 30, {sip1.ip}, node.node_type)}
91+ node.system_id, 30, {sip0.ip}, node.node_type)}
92 self.assertEqual(expected_mapping, mapping)
93
94 def test_get_hostname_ip_mapping_returns_correct_bond_ip(self):
95@@ -877,8 +873,6 @@
96 expected_mapping = {
97 node.fqdn: HostnameIPMapping(
98 node.system_id, 30, {bond_sip.ip}, node.node_type),
99- "%s.%s" % (iface1.name, node.fqdn): HostnameIPMapping(
100- node.system_id, 30, {sip1.ip}, node.node_type),
101 }
102 self.assertEqual(expected_mapping, mapping)
103