Merge ~ack/maas:1992185-vmhost-find-addr-interface-children into maas:master

Proposed by Alberto Donato
Status: Merged
Approved by: Alberto Donato
Approved revision: 83619edd2db79380df3082ab5839cd099be5691c
Merge reported by: MAAS Lander
Merged at revision: not available
Proposed branch: ~ack/maas:1992185-vmhost-find-addr-interface-children
Merge into: maas:master
Diff against target: 119 lines (+69/-7)
2 files modified
src/metadataserver/api_twisted.py (+27/-6)
src/metadataserver/tests/test_api_twisted.py (+42/-1)
Reviewer Review Type Date Requested Status
Adam Collard (community) Approve
MAAS Lander Approve
Review via email: mp+431806@code.launchpad.net

Commit message

LP:1992185 look at all children to find the VM host address

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

UNIT TESTS
-b 1992185-vmhost-find-addr-interface-children lp:~ack/maas/+git/maas into -b master lp:~maas-committers/maas

STATUS: FAILED
LOG: http://maas-ci.internal:8080/job/maas-tester/1113/consoleText
COMMIT: 83619edd2db79380df3082ab5839cd099be5691c

review: Needs Fixing
Revision history for this message
Alberto Donato (ack) wrote :

jenkins: !test

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

UNIT TESTS
-b 1992185-vmhost-find-addr-interface-children lp:~ack/maas/+git/maas into -b master lp:~maas-committers/maas

STATUS: SUCCESS
COMMIT: 83619edd2db79380df3082ab5839cd099be5691c

review: Approve
Revision history for this message
Adam Collard (adam-collard) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/src/metadataserver/api_twisted.py b/src/metadataserver/api_twisted.py
2index c5f3789..d71c03c 100644
3--- a/src/metadataserver/api_twisted.py
4+++ b/src/metadataserver/api_twisted.py
5@@ -18,7 +18,7 @@ from twisted.web.server import NOT_DONE_YET
6 from maasserver.api.utils import extract_oauth_key_from_auth_header
7 from maasserver.enum import NODE_STATUS, NODE_TYPE
8 from maasserver.forms.pods import PodForm
9-from maasserver.models import Node
10+from maasserver.models import Interface, Node, StaticIPAddress
11 from maasserver.preseed import CURTIN_INSTALL_LOG
12 from maasserver.secrets import SecretManager
13 from maasserver.utils.orm import (
14@@ -209,11 +209,7 @@ def _create_vmhost_for_deployment(node):
15
16 # the IP is associated to the bridge the boot interface is in, not the
17 # interface itself
18- boot_if = node.get_boot_interface()
19- ifaces = list(boot_if.children.all())
20- ip = node.ip_addresses(ifaces=ifaces + [boot_if])[0]
21- if ":" in ip:
22- ip = f"[{ip}]"
23+ ip = _get_ip_address_for_vmhost(node)
24
25 for secret_key, secret_value in deploy_secrets.items():
26 is_lxd = secret_key == DEPLOY_SECRETS_LXD_KEY
27@@ -275,6 +271,31 @@ def _create_vmhost_for_deployment(node):
28 node.update_status(NODE_STATUS.DEPLOYED)
29
30
31+def _get_ip_address_for_vmhost(node):
32+ boot_interface = node.get_boot_interface()
33+ interface_ids = {boot_interface.id}
34+
35+ # recursively find all children interface IDs
36+ new_ids = {boot_interface.id}
37+ while new_ids:
38+ new_ids = set(
39+ Interface.objects.filter(parents__in=new_ids).values_list(
40+ "id", flat=True
41+ )
42+ )
43+ interface_ids |= new_ids
44+
45+ ip = (
46+ StaticIPAddress.objects.exclude(ip__isnull=True)
47+ .filter(interface__in=interface_ids)
48+ .values_list("ip", flat=True)
49+ .first()
50+ )
51+ if ":" in ip:
52+ ip = f"[{ip}]"
53+ return ip
54+
55+
56 class StatusWorkerService(TimerService):
57 """Service to update nodes from received status messages."""
58
59diff --git a/src/metadataserver/tests/test_api_twisted.py b/src/metadataserver/tests/test_api_twisted.py
60index 1689ed9..7b36e79 100644
61--- a/src/metadataserver/tests/test_api_twisted.py
62+++ b/src/metadataserver/tests/test_api_twisted.py
63@@ -1210,7 +1210,7 @@ class TestCreateVMHostForDeployment(MAASServerTestCase):
64 )
65 )
66
67- def test_creates_vmhost_pick_right_interface(self):
68+ def test_creates_vmhost_pick_right_interface_address(self):
69 user = factory.make_User()
70 node = factory.make_Node_with_Interface_on_Subnet(
71 owner=user,
72@@ -1244,6 +1244,47 @@ class TestCreateVMHostForDeployment(MAASServerTestCase):
73 f"qemu+ssh://virsh@{addr}/system",
74 )
75
76+ def test_creates_vmhost_pick_right_interface_with_bond(self):
77+ user = factory.make_User()
78+ node = factory.make_Node_with_Interface_on_Subnet(
79+ owner=user,
80+ status=NODE_STATUS.DEPLOYING,
81+ agent_name="maas-kvm-pod",
82+ install_kvm=True,
83+ )
84+ iface = factory.make_Interface(node=node)
85+ bond = factory.make_Interface(
86+ iftype=INTERFACE_TYPE.BOND,
87+ node=node,
88+ parents=[node.get_boot_interface(), iface],
89+ )
90+ # deploying a machine as a VM host bridges the boot interface, which is
91+ # now the bond
92+ bridge = factory.make_Interface(
93+ iftype=INTERFACE_TYPE.BRIDGE,
94+ node=node,
95+ parents=[bond],
96+ )
97+ ip = factory.make_StaticIPAddress(interface=bridge)
98+ another_if = factory.make_Interface(INTERFACE_TYPE.PHYSICAL, node=node)
99+ factory.make_StaticIPAddress(interface=another_if)
100+ SecretManager().set_composite_secret(
101+ "deploy-metadata",
102+ {"virsh-password": "xyz123"},
103+ obj=node,
104+ )
105+ _create_vmhost_for_deployment(node)
106+ self.assertEqual(node.status, NODE_STATUS.DEPLOYED)
107+ virsh_vmhost = Pod.objects.get(power_type="virsh")
108+
109+ addr = ip.ip
110+ if IPAddress(addr).version == 6:
111+ addr = f"[{addr}]"
112+ self.assertEqual(
113+ virsh_vmhost.power_parameters.get("power_address"),
114+ f"qemu+ssh://virsh@{addr}/system",
115+ )
116+
117 def test_marks_failed_if_form_invalid(self):
118 mock_form = self.patch(api_twisted_module, "PodForm").return_value
119 mock_form.is_valid.return_value = False

Subscribers

People subscribed via source and target branches