Merge ~r00ta/maas:lp-2049508-3.4 into maas:3.4

Proposed by Jacopo Rota
Status: Merged
Approved by: Jacopo Rota
Approved revision: 4ccc7dceeb5ad7f135d707d0210b9b4d9ae320f6
Merge reported by: MAAS Lander
Merged at revision: not available
Proposed branch: ~r00ta/maas:lp-2049508-3.4
Merge into: maas:3.4
Diff against target: 109 lines (+55/-1)
2 files modified
src/maasserver/start_up.py (+21/-1)
src/maasserver/tests/test_start_up.py (+34/-0)
Reviewer Review Type Date Requested Status
MAAS Lander Approve
Jacopo Rota Approve
Review via email: mp+461367@code.launchpad.net

Commit message

fix: lp-2049508 cleanup leftover expired dynamic ips
(cherry picked from commit f8c31120b4a3d386418105d20daf90264f414fd3)

To post a comment you must log in.
Revision history for this message
MAAS Lander (maas-lander) wrote :

UNIT TESTS
-b lp-2049508-3.4 lp:~r00ta/maas/+git/maas into -b 3.4 lp:~maas-committers/maas

STATUS: FAILED
LOG: http://maas-ci.internal:8080/job/maas-tester/4745/console
COMMIT: 4ccc7dceeb5ad7f135d707d0210b9b4d9ae320f6

review: Needs Fixing
Revision history for this message
Jacopo Rota (r00ta) wrote :

jenkins: !test

Revision history for this message
Jacopo Rota (r00ta) wrote :

self approving backport

review: Approve
Revision history for this message
MAAS Lander (maas-lander) wrote :

UNIT TESTS
-b lp-2049508-3.4 lp:~r00ta/maas/+git/maas into -b 3.4 lp:~maas-committers/maas

STATUS: SUCCESS
COMMIT: 4ccc7dceeb5ad7f135d707d0210b9b4d9ae320f6

review: Approve

Update scan failed

At least one of the branches involved have failed to scan. You can manually schedule a rescan if required.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/src/maasserver/start_up.py b/src/maasserver/start_up.py
2index 824eca2..f748089 100644
3--- a/src/maasserver/start_up.py
4+++ b/src/maasserver/start_up.py
5@@ -3,7 +3,6 @@
6
7 """Start-up utilities for the MAAS server."""
8
9-
10 import logging
11
12 from django.db.utils import DatabaseError
13@@ -15,9 +14,11 @@ from maasserver.deprecations import (
14 log_deprecations,
15 sync_deprecation_notifications,
16 )
17+from maasserver.enum import INTERFACE_TYPE, IPADDRESS_TYPE
18 from maasserver.models import (
19 Config,
20 ControllerInfo,
21+ Interface,
22 Notification,
23 RegionController,
24 )
25@@ -103,6 +104,22 @@ def migrate_db_credentials_if_necessary(client: VaultClient) -> None:
26 logger.info("Deleted DB credentials from vault")
27
28
29+def _cleanup_expired_discovered_ip_addresses() -> None:
30+ """
31+ This startup cleanup is needed for the following reasons:
32+ - Major cleanup for https://bugs.launchpad.net/maas/+bug/2049508
33+ - In case we missed some DHCP notifications related to discovered IP addresses, we clean up all the resources here.
34+ """
35+
36+ # Delete all the dummy interfaces and IP addresses that have expired.
37+ # The related DNS records will be deleted when the django post_delete signal is handled.
38+ Interface.objects.filter(
39+ type=INTERFACE_TYPE.UNKNOWN,
40+ ip_addresses__ip__isnull=True,
41+ ip_addresses__alloc_type=IPADDRESS_TYPE.DISCOVERED,
42+ ).delete()
43+
44+
45 @asynchronous(timeout=FOREVER)
46 @inlineCallbacks
47 def start_up(master=False):
48@@ -195,6 +212,9 @@ def inner_start_up(master=False):
49 # Only perform the following if the master process for the
50 # region controller.
51 if master:
52+ # Cleanup in case we missed some DHCP notifications related to discovered ip addresses
53+ _cleanup_expired_discovered_ip_addresses()
54+
55 # Migrate DB credentials to Vault and set the flag if Vault client is configured
56 client = get_region_vault_client()
57 if client is not None:
58diff --git a/src/maasserver/tests/test_start_up.py b/src/maasserver/tests/test_start_up.py
59index 8035787..e1f604f 100644
60--- a/src/maasserver/tests/test_start_up.py
61+++ b/src/maasserver/tests/test_start_up.py
62@@ -9,6 +9,8 @@ from testtools.matchers import HasLength
63
64 from maasserver import deprecations, eventloop, locks, start_up, vault
65 from maasserver.config import RegionConfiguration
66+from maasserver.enum import INTERFACE_TYPE, IPADDRESS_TYPE
67+from maasserver.models import Interface, StaticIPAddress
68 from maasserver.models.config import Config
69 from maasserver.models.controllerinfo import ControllerInfo
70 from maasserver.models.node import RegionController
71@@ -322,6 +324,38 @@ class TestInnerStartUp(MAASServerTestCase):
72
73 migrate_mock.assert_not_called()
74
75+ def test_start_up_cleanup_expired_ip_addresses(self):
76+ # When a host not managed by MAAS requests an IP to the MAAS DHCP server, the rack will notify the region about the
77+ # event.
78+ # If the mac_address if the DHCP lease is unknown, the region
79+ # - creates an interface with type 'unknown'
80+ # - creates a StaticIPAddress(alloc_type=IPADDRESS_TYPE.DISCOVERED)
81+ # - attaches the ip address to the interface
82+ unknown_interface = factory.make_Interface(
83+ iftype=INTERFACE_TYPE.UNKNOWN,
84+ name="eth0",
85+ )
86+
87+ # We already set the IP to None for testing purposes.
88+ ip = factory.make_StaticIPAddress(
89+ alloc_type=IPADDRESS_TYPE.DISCOVERED,
90+ ip=None,
91+ interface=unknown_interface,
92+ )
93+
94+ # Create also some other interfaces with IP addresses so to check that they are not deleted by the cleanup procedure.
95+ for _ in range(5):
96+ interface = factory.make_Interface()
97+ factory.make_StaticIPAddress(interface=interface)
98+
99+ with post_commit_hooks:
100+ start_up.inner_start_up(master=True)
101+
102+ assert not Interface.objects.filter(id=unknown_interface.id).exists()
103+ assert not StaticIPAddress.objects.filter(id=ip.id).exists()
104+ assert StaticIPAddress.objects.all().count() == 5
105+ assert Interface.objects.all().count() == 5
106+
107
108 class TestVaultMigrateDbCredentials(MAASServerTestCase):
109 def setUp(self):

Subscribers

People subscribed via source and target branches