Skip to content

Commit 5ec5320

Browse files
authored
Merge pull request #2619 from rackerlabs/enroll-fw-without-ports
feat(enroll-fw): enroll a firewall without ports and let inspection label them PUC-2128
2 parents e8a6fae + 5f3ba4e commit 5ec5320

5 files changed

Lines changed: 261 additions & 19 deletions

File tree

‎python/ironic-understack/ironic_understack/drivers/panos_inspect.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,8 @@ def _interface_sort_key(iface_name: str) -> tuple:
402402
existing_port.address = mac_address
403403
existing_port.local_link_connection = inspected_port.local_link_connection
404404
existing_port.extra = {"bios_name": local_interface}
405-
existing_port.physical_network = physical_network
405+
if physical_network and not existing_port.physical_network:
406+
existing_port.physical_network = physical_network
406407
existing_port.category = category
407408
existing_port.name = port_name
408409
existing_port.save()
@@ -441,7 +442,7 @@ def _validate_firewall_readiness(task) -> None:
441442
:raises HardwareInspectionFailure: if requirements not met
442443
"""
443444
node = task.node
444-
ports = list(task.ports())
445+
ports = list(task.ports)
445446

446447
# Check port categories (should have been assigned during port creation)
447448
categories = {getattr(p, "category", None) for p in ports}

‎python/ironic-understack/ironic_understack/tests/test_panos_inspect.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,38 @@ def test_apply_system_info_ignores_empty_fields(task):
8585
panos_inspect._apply_system_info(task.node, {"model": "", "serial": ""})
8686
assert task.node.properties == {}
8787
assert task.node.extra == {}
88+
89+
90+
@pytest.mark.parametrize(
91+
("current", "derived", "expected"),
92+
[
93+
("f20-3-network", None, "f20-3-network"),
94+
("f20-3-network", "f20-3/f20-4-network", "f20-3-network"),
95+
(None, "f20-3-network", "f20-3-network"),
96+
],
97+
ids=["not-wiped", "not-replaced", "filled-in"],
98+
)
99+
def test_existing_port_keeps_its_physical_network(
100+
task, mocker, current, derived, expected
101+
):
102+
task.node.name = "fw1"
103+
macs = {"ethernet1/1": "60:15:2b:54:1a:10", "ethernet1/2": "60:15:2b:54:1a:11"}
104+
task.ports = [Mock(address=mac, physical_network=current) for mac in macs.values()]
105+
neighbors = [
106+
{
107+
"local_interface": interface,
108+
"system_name": "f20-3-1.iad3",
109+
"port_id": f"Ethernet1/{n}",
110+
"chassis_id": "c4:7e:e0:e4:55:3f",
111+
}
112+
for n, interface in enumerate(macs, start=43)
113+
]
114+
mocker.patch.object(
115+
panos_inspect.vlan_group_name_convention,
116+
"vlan_group_names",
117+
return_value={"f20-3-1.iad3": derived},
118+
)
119+
120+
panos_inspect._update_ports_from_neighbors(task, neighbors, macs)
121+
122+
assert [p.physical_network for p in task.ports] == [expected, expected]

‎python/understack-workflows/tests/test_enroll_fw.py‎

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,3 +450,112 @@ def test_active_node_with_port_roles_still_gets_metadata_updates(mocker):
450450
engine.assert_not_called()
451451
client.update_port.assert_not_called()
452452
client.update_node.assert_called_once() # metadata only
453+
454+
455+
# --- enrollment without ports (inspection labels them) ----------------------
456+
457+
458+
def _enroll_without_ports(mocker, categories, physnet=None):
459+
"""Mock a new node whose inspection leaves ports with these categories."""
460+
client = _mock_client(
461+
mocker,
462+
node_ports=[
463+
SimpleNamespace(
464+
uuid=f"port-{i}",
465+
name=f"fw:ethernet1/{i}",
466+
category=c,
467+
physical_network=physnet,
468+
)
469+
for i, c in enumerate(categories, start=1)
470+
],
471+
)
472+
mocker.patch.object(
473+
enroll_fw.netdev_reconciler,
474+
"find_or_create_netdev_node",
475+
return_value=(_node(provision_state="enroll"), True),
476+
)
477+
engine = mocker.patch.object(enroll_fw.netdev_reconciler, "enroll")
478+
transition = mocker.patch.object(enroll_fw.ironic_node, "transition")
479+
return client, engine, transition
480+
481+
482+
def _steps(transition):
483+
return [call.kwargs["target_state"] for call in transition.call_args_list]
484+
485+
486+
def _physnet_updates(client):
487+
return {call.args[0]: call.args[1] for call in client.update_port.call_args_list}
488+
489+
490+
def test_enroll_without_ports_inspects_before_providing(mocker):
491+
client, engine, transition = _enroll_without_ports(
492+
mocker, ["outside", "inside", None]
493+
)
494+
495+
node_uuid = enroll_fw.enroll_fw(**{**BASE_ARGS, "ports": []}, **FW_FIELDS)
496+
497+
assert node_uuid == "node-uuid"
498+
engine.assert_not_called()
499+
assert _steps(transition) == ["manage", "inspect", "provide"]
500+
# Only the role ports get the physical network.
501+
patch = [{"op": "add", "path": "/physical_network", "value": "f20-1-network"}]
502+
assert _physnet_updates(client) == {"port-1": patch, "port-2": patch}
503+
504+
505+
def test_enroll_without_ports_keeps_a_matching_physical_network(mocker):
506+
client, _engine, transition = _enroll_without_ports(
507+
mocker, ["outside", "inside"], physnet="f20-1-network"
508+
)
509+
510+
enroll_fw.enroll_fw(**{**BASE_ARGS, "ports": []}, **FW_FIELDS)
511+
512+
client.update_port.assert_not_called()
513+
assert _steps(transition) == ["manage", "inspect", "provide"]
514+
515+
516+
def test_enroll_without_ports_is_not_provided_without_both_roles(mocker):
517+
client, _engine, transition = _enroll_without_ports(mocker, ["outside", None])
518+
519+
with pytest.raises(RuntimeError, match="an outside and an inside port"):
520+
enroll_fw.enroll_fw(**{**BASE_ARGS, "ports": []}, **FW_FIELDS)
521+
522+
assert _steps(transition) == ["manage", "inspect"]
523+
client.update_port.assert_not_called()
524+
525+
526+
def test_active_node_without_ports_updates_metadata_only(mocker):
527+
client = _mock_client(mocker, node=_node())
528+
529+
enroll_fw.enroll_fw(**{**BASE_ARGS, "ports": []}, **FW_FIELDS)
530+
531+
client.list_ports.assert_not_called() # nothing to compare
532+
client.update_node.assert_called_once()
533+
534+
535+
@pytest.mark.parametrize("ports", [BASE_ARGS["ports"], []], ids=["ports", "no-ports"])
536+
def test_enroll_fw_requires_a_physical_network(mocker, ports):
537+
client = _mock_client(mocker)
538+
539+
with pytest.raises(ValueError, match="--physical-network"):
540+
enroll_fw.enroll_fw(
541+
**{**BASE_ARGS, "physical_network": " ", "ports": ports}, **FW_FIELDS
542+
)
543+
544+
client.get_node.assert_not_called()
545+
546+
547+
def test_argument_parser_requires_physical_network():
548+
parser = enroll_fw.argument_parser()
549+
with pytest.raises(SystemExit):
550+
parser.parse_args(
551+
[
552+
"--name",
553+
"fw1",
554+
"--resource-class",
555+
"pa1410",
556+
"--management-switch",
557+
"switch1",
558+
"--management-switch-port",
559+
"eth1",
560+
]
561+
)

‎python/understack-workflows/understack_workflows/main/enroll_fw.py‎

Lines changed: 108 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from understack_workflows import firewall
99
from understack_workflows import helpers
10+
from understack_workflows import ironic_node
1011
from understack_workflows import netdev_reconciler
1112
from understack_workflows.ironic.client import IronicClient
1213

@@ -98,6 +99,8 @@ def enroll_fw(
9899
driver = (driver or "").strip()
99100
if not driver:
100101
raise ValueError("enroll-fw requires a non-empty --driver")
102+
if not (physical_network or "").strip():
103+
raise ValueError("enroll-fw requires --physical-network")
101104
resource_class = _require_specific_resource_class(resource_class)
102105
management_switch, management_switch_port = _require_management_location(
103106
management_switch, management_switch_port
@@ -135,6 +138,19 @@ def enroll_fw(
135138
)
136139
return node.uuid
137140

141+
if not ports:
142+
return _enroll_by_inspection(
143+
client=client,
144+
name=name,
145+
physical_network=physical_network,
146+
resource_class=resource_class,
147+
driver=driver,
148+
external_cmdb_id=external_cmdb_id,
149+
driver_info=driver_info,
150+
extra=extra,
151+
properties=properties,
152+
)
153+
138154
# New or re-enrollable node: the engine writes the metadata inside the
139155
# enrollment lifecycle (at create, or patched before the node becomes
140156
# available), so the node is never allocatable in an under-described state.
@@ -160,6 +176,85 @@ def _get_node_or_none(client: IronicClient, name: str):
160176
return None
161177

162178

179+
def _enroll_by_inspection(
180+
*,
181+
client: IronicClient,
182+
name: str,
183+
physical_network: str,
184+
resource_class: str,
185+
driver: str,
186+
external_cmdb_id: int | str | None,
187+
driver_info: dict,
188+
extra: dict,
189+
properties: dict,
190+
) -> str:
191+
"""Enroll a firewall without ports and let inspection find and label them.
192+
193+
Provided only once it has an outside and an inside port, which get the
194+
given physical_network.
195+
"""
196+
if external_cmdb_id not in (None, ""):
197+
extra = {**extra, "external_cmdb_id": external_cmdb_id}
198+
node, created = netdev_reconciler.find_or_create_netdev_node(
199+
client=client,
200+
name=name,
201+
resource_class=resource_class,
202+
driver=driver,
203+
driver_info=driver_info,
204+
extra=extra,
205+
properties=properties,
206+
)
207+
if node.provision_state != "manageable":
208+
ironic_node.transition(node, target_state="manage", expected_state="manageable")
209+
if not created:
210+
netdev_reconciler.update_node_metadata(
211+
client=client,
212+
node=node,
213+
resource_class=resource_class,
214+
driver_info=driver_info,
215+
extra=extra,
216+
properties=properties,
217+
)
218+
219+
logger.info("[node:%s] Inspecting to discover and label the ports", node.uuid)
220+
ironic_node.transition(node, target_state="inspect", expected_state="manageable")
221+
role_ports = [
222+
port
223+
for port in client.list_ports(node.uuid)
224+
if getattr(port, "category", None) in netdev_reconciler.PORT_ROLES
225+
]
226+
if not {port.category for port in role_ports}.issuperset(
227+
netdev_reconciler.PORT_ROLES
228+
):
229+
raise RuntimeError(
230+
f"[node:{node.uuid}] Inspection did not give the node an outside and "
231+
"an inside port; leaving it manageable"
232+
)
233+
234+
for port in role_ports:
235+
if port.physical_network != physical_network:
236+
logger.info(
237+
"[node:%s] Setting physical_network=%s on port %s (was %s)",
238+
node.uuid,
239+
physical_network,
240+
port.name,
241+
port.physical_network,
242+
)
243+
client.update_port(
244+
port.uuid,
245+
[
246+
{
247+
"op": "add",
248+
"path": "/physical_network",
249+
"value": physical_network,
250+
}
251+
],
252+
)
253+
254+
ironic_node.transition(node, target_state="provide", expected_state="available")
255+
return node.uuid
256+
257+
163258
def _update_active_firewall(
164259
*,
165260
client: IronicClient,
@@ -196,14 +291,15 @@ def _update_active_firewall(
196291
f"{resource_class!r} on an in-service firewall"
197292
)
198293

199-
netdev_ports = netdev_reconciler.build_netdev_ports(ports)
200-
_reject_structural_drift(
201-
client=client,
202-
node=node,
203-
name=name,
204-
physical_network=physical_network,
205-
netdev_ports=netdev_ports,
206-
)
294+
# Without ports there is nothing to compare: inspection owns them.
295+
if ports:
296+
_reject_structural_drift(
297+
client=client,
298+
node=node,
299+
name=name,
300+
physical_network=physical_network,
301+
netdev_ports=netdev_reconciler.build_netdev_ports(ports),
302+
)
207303

208304
active_extra = dict(extra)
209305
if external_cmdb_id not in (None, ""):
@@ -271,21 +367,18 @@ def argument_parser():
271367
parser.add_argument(
272368
"--physical-network",
273369
required=True,
274-
help="Port physical_network",
370+
help="Port physical_network, the VLAN group in Nautobot (e.g. f20-3-network)",
275371
)
276372
parser.add_argument(
277373
"--ports",
278-
required=False,
279374
default="[]",
280-
help="Optional JSON array of ports to override LLDP discovery (same format "
281-
"as enroll-netdev). By default, ports are discovered automatically via LLDP "
282-
"during inspection. Use this option to manually specify ports that may not "
283-
"be discovered properly or to override auto-discovered values. 'switch' must "
375+
help="JSON array of ports (same format as enroll-netdev). 'switch' must "
284376
"be the switch FQDN (e.g. n11-22-1.dfw3.rackspace.net) so the Nautobot "
285377
"sync can resolve the cable. Optional per-port 'bios_name' (the device "
286378
"interface name); defaults to the label. At least two ports: the "
287379
"lowest-numbered interface becomes the router's outside NIC (gateway) "
288-
"and the next one its inside NIC (subnets).",
380+
"and the next one its inside NIC (subnets). Leave it out to have "
381+
"inspection discover the ports over LLDP and label them.",
289382
)
290383
parser.add_argument(
291384
"--resource-class",

‎workflows/argo-events/workflowtemplates/enroll-fw.yaml‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ metadata:
88
Defined in `workflows/argo-events/workflowtemplates/enroll-fw.yaml`.
99
Reuses enroll-netdev for the node/port lifecycle and additionally records
1010
firewall management details in driver_info and the HA mate serial in extra.
11-
Defaults to the panos hardware type.
11+
Defaults to the panos hardware type. Without ports, it inspects the
12+
firewall to discover and label them, and only then makes it available.
1213
1314
The workflow reads site-specific configuration from the
1415
`enroll-fw-config` ConfigMap. After enrollment, it runs the templates
@@ -35,9 +36,12 @@ spec:
3536
arguments:
3637
parameters:
3738
- name: name
39+
# VLAN group in Nautobot, e.g. f20-3-network.
3840
- name: physical_network
39-
# JSON array of ports (same format as enroll-netdev).
41+
# JSON array of ports (same format as enroll-netdev). Leave it as [] to
42+
# let inspection discover and label the ports.
4043
- name: ports
44+
value: "[]"
4145
- name: external_cmdb_id
4246
value: ""
4347
- name: rack

0 commit comments

Comments
 (0)