Skip to content

Commit 074c28e

Browse files
authored
Merge pull request #2315 from rackerlabs/docs/writing-ironic-drivers-guide
docs: add contributor guide for writing Ironic drivers and interfaces
2 parents a180662 + 6af7c67 commit 074c28e

4 files changed

Lines changed: 317 additions & 0 deletions

File tree

‎docs/contributing/index.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ operating it.
1616
`changelog.d/` fragment, and CI enforces it.
1717
- [Adding and Removing an Application](../design-guide/add-remove-app.md) — how a
1818
component becomes an ArgoCD `Application`.
19+
- [Writing Ironic Drivers and Interfaces](writing-ironic-drivers.md) — how to add
20+
a new hardware type or interface to `ironic-understack`, starting from no
21+
Ironic knowledge.
1922
- [Design and Background](../openstack-helm.md) — why the project is shaped the
2023
way it is, starting with why we diverge from upstream OpenStack Helm.
2124

Lines changed: 309 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,309 @@
1+
# Writing Ironic Drivers and Interfaces
2+
3+
This page is for contributors adding or changing bare metal behavior in
4+
`ironic-understack`, UnderStack's Ironic plugin package. It assumes no prior
5+
Ironic knowledge. If you already know Ironic well, skip to
6+
[How UnderStack plugs in](#how-understack-plugs-in).
7+
8+
## Ironic in a nutshell
9+
10+
Ironic manages physical servers ("nodes") the way Nova manages virtual
11+
machines. A node's behavior is defined by a **driver**, and a driver is built
12+
from two things:
13+
14+
- A **hardware type** — a Python class that says which vendor/family of
15+
hardware this is (generic Redfish, iDRAC, a network switch, ...).
16+
- A set of **hardware interfaces** — one implementation per concern: `power`,
17+
`management`, `boot`, `deploy`, `inspect`, `raid`, `bios`, `network`,
18+
`storage`, `rescue`, `firmware`, `vendor`, `console`. Each interface answers
19+
one question, e.g. "how do I power this on?" or "how do I find out what
20+
hardware this is?"
21+
22+
Both are loaded via Python entry points and enabled by name in `ironic.conf`
23+
(`enabled_hardware_types`, `enabled_<interface>_interfaces`). This is what lets
24+
UnderStack ship its own hardware types and interfaces as a separate package
25+
without forking Ironic itself.
26+
27+
Read these two upstream pages before writing any code — this page does not
28+
repeat their content:
29+
30+
- [Drivers](https://docs.openstack.org/ironic/latest/admin/drivers.html) — the
31+
catalog of hardware types and interfaces Ironic ships, and how they combine.
32+
- [Features](https://docs.openstack.org/ironic/latest/admin/features.html) —
33+
what Ironic can do end to end (deployment, cleaning, RAID, rescue, etc.).
34+
- [Pluggable Drivers](https://docs.openstack.org/ironic/latest/contributor/drivers.html)
35+
— upstream's own guide to subclassing a hardware type or interface. Everything
36+
it says about `AbstractHardwareType`, `PowerInterface`,
37+
`ManagementInterface`, etc. applies unchanged inside `ironic-understack`.
38+
39+
## The node state machine
40+
41+
A node's lifecycle is a state machine — `available`, `deploying`, `active`,
42+
`cleaning`, `manageable`, and so on — documented in full at
43+
[Bare Metal State Machine](https://docs.openstack.org/ironic/latest/user/states.html).
44+
Interfaces and hooks are the code that runs *during* specific state
45+
transitions, so knowing which transition you're targeting tells you which
46+
interface to touch.
47+
48+
### The four operations we care about
49+
50+
Most of the work UnderStack contributors do lands in one of four operations:
51+
52+
1. **Inspecting** — discovering what a node actually is: CPU, memory, disks,
53+
NICs, chassis model. Driven by the `inspect` interface
54+
(`inspect_hardware()`), plus an `ironic.inspection.hooks` pipeline that
55+
post-processes the inventory data the ramdisk reports before it's saved to
56+
the node. This is where UnderStack matches a node to a flavor and resource
57+
class — see [`redfish_inspect_understack.py`](#writing-a-new-hardware-interface)
58+
below.
59+
2. **Cleaning** — wiping disks and resetting BIOS/RAID/firmware settings
60+
between tenants, or before first use. Implemented as **clean steps**:
61+
methods decorated with `@ironic.drivers.base.clean_step(priority=...)` on
62+
whichever interface owns that action (`management`, `deploy`, `raid`, ...).
63+
Runs in the `cleaning` state. If the device has no deploy ramdisk to boot
64+
(true of most network gear), every clean step it defines must pass
65+
`requires_ramdisk=False` — see
66+
[Interfaces for devices with no ramdisk or BMC agent](#interfaces-for-devices-with-no-ramdisk-or-bmc-agent).
67+
3. **Deploying** — writing the tenant's image and configuration to the node.
68+
Implemented as **deploy steps**
69+
(`@ironic.drivers.base.deploy_step(priority=...)`), conventionally at
70+
priority 100 for the main deploy method. Runs in the `deploying` state.
71+
4. **Servicing** — post-deployment maintenance (e.g. a firmware update) on a
72+
node that's already active, without a full teardown/redeploy. Implemented
73+
as **service steps** (`@ironic.drivers.base.service_step(...)`), explicitly
74+
requested by the caller rather than run automatically. Runs in the
75+
`servicing` state.
76+
77+
Clean, deploy, and service steps share the same shape: synchronous steps
78+
return `None` and the conductor moves on; asynchronous steps return the
79+
matching `WAIT` state and later call back (`continue_node_clean`,
80+
`continue_node_deploy`, `continue_node_service`) to resume. `ironic-understack`
81+
doesn't define any custom steps of these three kinds today — if you're adding
82+
one, read the decorator docstrings in `ironic/drivers/base.py` and the
83+
upstream [deploy steps guide](https://docs.openstack.org/ironic/latest/contributor/deploy-steps.html)
84+
first.
85+
86+
## How UnderStack plugs in
87+
88+
`ironic-understack` ([`python/ironic-understack/`](https://github.com/rackerlabs/understack/tree/main/python/ironic-understack))
89+
is an ordinary Python package that Ironic loads via entry points, declared in
90+
its `pyproject.toml`:
91+
92+
| Entry point group | What it's for | UnderStack examples |
93+
| --- | --- | --- |
94+
| `ironic.hardware.types` | New hardware type | `netdev` |
95+
| `ironic.hardware.interfaces.inspect` | New `inspect` interface implementation | `redfish-understack`, `idrac-redfish-understack` |
96+
| `ironic.inspection.hooks` | Post-processing step for inspection data | `resource-class`, `update-baremetal-port`, `port-bios-name`, `node-name-check`, `chassis_model` |
97+
| `ironic.api.middleware` | WSGI middleware on the Ironic API | `portgroup-name-validation` |
98+
| `ironic.console.container` | Alternate console container backend | `kubernetes` |
99+
100+
The package is baked into the Ironic container image at build time
101+
(`COPY python/ironic-understack ...` in
102+
[`containers/ironic/Dockerfile`](https://github.com/rackerlabs/understack/blob/main/containers/ironic/Dockerfile)),
103+
so a new driver or interface needs both a code change here and a container
104+
rebuild before it's live — check [`README.md`](https://github.com/rackerlabs/understack/blob/main/python/ironic-understack/README.md)
105+
and [`DEVELOPMENT.md`](https://github.com/rackerlabs/understack/blob/main/python/ironic-understack/DEVELOPMENT.md)
106+
for the local dev loop.
107+
108+
For UnderStack's existing shipped hardware types, see the
109+
[Ironic design reference](../design-guide/ironic.md).
110+
111+
## Writing a new hardware interface
112+
113+
Use this when an existing interface (Redfish, iDRAC, IPMI, ...) is *almost*
114+
right but needs UnderStack-specific behavior layered on top. The pattern used
115+
throughout `ironic_understack/drivers/` is a mixin over the upstream class,
116+
registered under the same entry point group as the interface you're
117+
extending.
118+
119+
`ironic_understack/drivers/redfish_inspect_understack.py` extends the stock
120+
Redfish and iDRAC-Redfish `inspect` interfaces to match discovered hardware
121+
against a flavor:
122+
123+
```python
124+
class FlavorInspectMixin:
125+
def inspect_hardware(self, task):
126+
upstream_state = super().inspect_hardware(task)
127+
# ... read inspection data, match a flavor, set task.node.resource_class
128+
return upstream_state
129+
130+
131+
class UnderstackRedfishInspect(FlavorInspectMixin, RedfishInspect):
132+
...
133+
134+
135+
class UnderstackDracRedfishInspect(FlavorInspectMixin, DracRedfishInspect):
136+
...
137+
```
138+
139+
The steps to follow this pattern for a different interface (say, a custom
140+
`management` behavior):
141+
142+
1. Pick the upstream class you're extending, e.g.
143+
`ironic.drivers.modules.redfish.management.RedfishManagement`.
144+
2. Subclass it (with a mixin if the same logic should apply to more than one
145+
base class), calling `super().<method>(task)` and only adding what's
146+
different.
147+
3. Register it under the matching entry point group in `pyproject.toml`, e.g.:
148+
149+
```toml
150+
[project.entry-points."ironic.hardware.interfaces.management"]
151+
my-management-understack = "ironic_understack.drivers.my_module:MyManagement"
152+
```
153+
154+
4. Add it to `enabled_management_interfaces` in `ironic.conf` and to the
155+
relevant hardware type's `supported_management_interfaces` list (see
156+
below) so Ironic actually picks it up.
157+
5. Write a unit test alongside the existing ones in
158+
`ironic_understack/tests/` — run with `pytest` (or `uv run pytest`) from
159+
`python/ironic-understack/`.
160+
161+
If you only need to react to inspection data rather than replace the whole
162+
`inspect` interface, prefer an **inspection hook**
163+
(`ironic.inspection.hooks`, see `ironic_understack/hooks/`) — it's a smaller
164+
surface: implement `InspectionHook.__call__(self, task, inventory,
165+
plugin_data)` and register it, no interface subclassing required.
166+
167+
## Interfaces for devices with no ramdisk or BMC agent
168+
169+
The pattern above — mix in over `RedfishInspect`/`RedfishManagement` — only
170+
works when the device speaks Redfish (or IPMI, or has a sushy-compatible BMC).
171+
A lot of the hardware UnderStack needs to manage doesn't: network gear like
172+
switches and firewalls is typically managed out-of-band over SSH or a
173+
vendor API, and never boots an Ironic deploy ramdisk at all. For that class of
174+
device you write the interface from scratch against the abstract base
175+
classes, not as a mixin over an existing implementation.
176+
177+
`netdev_hardware.py`'s `NetdevHardware` (above) is **not** a template for
178+
this — it wires every interface to a no-op precisely because it does no real
179+
inspection or management. A device you actually want to inspect and clean
180+
needs real implementations behind those two interfaces.
181+
182+
**Inspect, from scratch.** Subclass `ironic.drivers.base.InspectInterface`
183+
directly and implement the one abstract method, `inspect_hardware`. Nothing
184+
requires a ramdisk here — everything is driven by however you talk to the
185+
device (SSH, a vendor API, SNMP, ...):
186+
187+
```python
188+
from ironic.common import exception
189+
from ironic.common import states
190+
from ironic.drivers import base
191+
192+
193+
class MyDeviceInspect(base.InspectInterface):
194+
# Override if "memory_mb"/"local_gb"/"cpu_arch" don't apply to this
195+
# device; leave empty rather than reporting properties that don't exist.
196+
ESSENTIAL_PROPERTIES = set()
197+
198+
def get_properties(self):
199+
return {}
200+
201+
def validate(self, task):
202+
pass # check that credentials/connection info are present
203+
204+
def inspect_hardware(self, task):
205+
# Connect to the device, discover its properties/ports however is
206+
# appropriate for it, then persist what you found:
207+
# task.node.properties = {...}
208+
# task.node.save()
209+
# Create ironic Port objects for discovered NICs/interfaces as needed.
210+
if <inspection failed>:
211+
raise exception.HardwareInspectionFailure(...)
212+
return states.MANAGEABLE
213+
```
214+
215+
**Cleaning, out-of-band.** Real cleaning behavior belongs on a real
216+
`management` interface. You don't have to reimplement the boot-device
217+
methods `ManagementInterface` requires (`get_supported_boot_devices`,
218+
`set_boot_device`, `get_boot_device`) if they're meaningless for this
219+
device — subclass `ironic.drivers.modules.noop_mgmt.NoopManagement` to inherit
220+
sensible no-op defaults for those, then add your real clean step(s) on top.
221+
The key detail is `requires_ramdisk=False`, since there's no ramdisk to wait
222+
on:
223+
224+
```python
225+
from ironic.drivers import base
226+
from ironic.drivers.modules import noop_mgmt
227+
228+
229+
class MyDeviceManagement(noop_mgmt.NoopManagement):
230+
def validate(self, task):
231+
pass # check that credentials/connection info are present
232+
233+
@base.clean_step(priority=10, requires_ramdisk=False)
234+
def reset_to_factory_defaults(self, task):
235+
# Talk to the device and reset its configuration. Return None when
236+
# done (synchronous); return states.CLEANWAIT and later call
237+
# continue_node_clean() if the reset is asynchronous.
238+
...
239+
```
240+
241+
Wire both into a new hardware type (a sibling to `NetdevHardware`, not a
242+
modification of it) via `supported_inspect_interfaces` and
243+
`supported_management_interfaces`, register the entry points, and enable them
244+
— same steps as [Writing a new hardware interface](#writing-a-new-hardware-interface)
245+
and [Writing a new hardware type](#writing-a-new-hardware-type) above. Test
246+
the clean step by calling it directly against a mocked connection/task, the
247+
same way `test_netdev_hardware.py` tests interface selection without a
248+
running Ironic service.
249+
250+
## Writing a new hardware type
251+
252+
Use this when a class of node needs a genuinely different combination of
253+
interfaces, not just a tweak to one of them.
254+
`ironic_understack/drivers/netdev_hardware.py` is UnderStack's example — a
255+
hardware type for network devices that Ironic tracks only for Neutron port
256+
binding, with every other interface set to a no-op:
257+
258+
```python
259+
class NetdevHardware(generic.ManualManagementHardware):
260+
@property
261+
def supported_deploy_interfaces(self):
262+
return [noop.NoDeploy]
263+
264+
@property
265+
def supported_network_interfaces(self):
266+
return [neutron.NeutronNetwork]
267+
268+
# ... one supported_<interface>_interfaces property per interface
269+
```
270+
271+
To add your own:
272+
273+
1. Subclass `ironic.drivers.hardware_type.AbstractHardwareType`, or more
274+
usually `ironic.drivers.generic.GenericHardware` (or one of its variants
275+
like `ManualManagementHardware`) to inherit sane defaults for interfaces
276+
you don't need to change.
277+
2. Override `supported_<interface>_interfaces` for each interface where the
278+
default isn't right, listing implementations in priority order (most
279+
preferred first).
280+
3. Register it:
281+
282+
```toml
283+
[project.entry-points."ironic.hardware.types"]
284+
my-hardware = "ironic_understack.drivers.my_hardware:MyHardware"
285+
```
286+
287+
4. Add it to `enabled_hardware_types` in `ironic.conf`, and make sure every
288+
interface it lists is also present in the corresponding
289+
`enabled_<interface>_interfaces` option.
290+
5. Test it the way `ironic_understack/tests/test_netdev_hardware.py` does —
291+
instantiate the class directly and assert on
292+
`supported_<interface>_interfaces`, no running Ironic service required:
293+
294+
```python
295+
def test_netdev_deploy():
296+
hw = NetdevHardware()
297+
assert [c.__name__ for c in hw.supported_deploy_interfaces] == ["NoDeploy"]
298+
```
299+
300+
## Where to go next
301+
302+
- [Ironic design reference](../design-guide/ironic.md) — what UnderStack
303+
ships today and why.
304+
- [Enabling Drivers](https://docs.openstack.org/ironic/latest/install/enabling-drivers.html)
305+
— the operator side of `enabled_hardware_types` / `enabled_<interface>_interfaces`.
306+
- [Bare Metal State Machine](https://docs.openstack.org/ironic/latest/user/states.html)
307+
— the full state diagram behind the "four operations" above.
308+
- [`operator-guide/openstack-ironic-inspection-guide.md`](../operator-guide/openstack-ironic-inspection-guide.md)
309+
— how inspection behaves operationally in UnderStack today.

‎docs/design-guide/ironic.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Ironic
22

3+
See [Writing Ironic Drivers and Interfaces](../contributing/writing-ironic-drivers.md)
4+
for how to add a new hardware type or interface. This page documents what
5+
UnderStack ships today.
6+
37
## Custom Hardware Types
48

59
UnderStack ships additional Ironic hardware types via the `ironic-understack`

‎properdocs.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ nav:
235235
- Contributing:
236236
- contributing/index.md
237237
- design-guide/add-remove-app.md
238+
- contributing/writing-ironic-drivers.md
238239
- 'Design and Background':
239240
- openstack-helm.md
240241
- design-guide/ironic.md

0 commit comments

Comments
 (0)