Skip to content

Commit 7646eb0

Browse files
committed
PCI device inspection hook change from capabilites to traits
We oringinally tried to use Capabilities because they are a more powerful matching tool for flavor matching as the scalar quantity allows for flavor conditions like "at least 2" or "exactly 4". However in practise, capability support is not universal whereas traits are more easily consumable in the current deployment. Therefore we drop the capabilities in favour of baremetal node traits. We also change the configuration to make it easier to pass through Helm chat values and to make it compatible with the upstream built-in ironic hooks.
1 parent bb1dd8d commit 7646eb0

8 files changed

Lines changed: 445 additions & 331 deletions

File tree

‎components/ironic/README.md‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,30 @@ helm --namespace openstack \
5555

5656
At this point Ironic will go through some initialization and start up.
5757

58+
## PCI device traits
59+
60+
The `pci-device-trait` inspection hook counts PCI devices that match aliases
61+
configured in the `[ironic_understack]` section of `ironic.conf`. Each alias is
62+
a JSON object with `vendor_id`, `product_id`, and `name` fields. In the
63+
OpenStack-Helm values this is configured as:
64+
65+
```yaml
66+
conf:
67+
ironic:
68+
ironic_understack:
69+
pci_device_alias:
70+
- '{"vendor_id": "1002", "product_id": "75a8", "name": "gpu_amd_mi350p"}'
71+
```
72+
73+
The option uses the same JSON `MultiStrOpt` format as Ironic's upstream
74+
`pci_device_alias` option, but remains in the `[ironic_understack]` section.
75+
The hook normalizes the alias name and adds a custom trait containing the
76+
number of matching devices. For example, a node with two matching devices from
77+
the configuration above receives the `CUSTOM_PCI_2_GPU_AMD_MI350P` trait.
78+
79+
On subsequent inspections, the hook replaces traits in the `CUSTOM_PCI_`
80+
namespace with the current counts while preserving all other node traits.
81+
5882
## Validating Ironic
5983

6084
You can run an OpenStack client in the cluster to validate it is running correctly.

‎components/ironic/values.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ conf:
155155
keep_ports: "present"
156156
redfish:
157157
# Redfish inspection hooks run after inspecting out-of-band using the BMC:
158-
inspection_hooks: "validate-interfaces,ports,port-bios-name,architecture,pci-devices,resource-class,chassis_model,pci-device-capabilities"
158+
inspection_hooks: "validate-interfaces,ports,port-bios-name,architecture,pci-devices,resource-class,chassis_model,pci-device-trait"
159159
# enable sensors and metrics for redfish metrics - https://docs.openstack.org/ironic/latest/admin/drivers/redfish/metrics.html
160160
post_boot_retry_attempts: 6
161161
post_boot_retry_delay: 30

‎python/ironic-understack/ironic_understack/conf/__init__.py‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,16 @@ def setup_conf():
3131
"1d": "bmc",
3232
},
3333
),
34-
cfg.DictOpt(
35-
"pci_device_capabilities",
36-
help="Dictionary of pci device IDs (xxxx:xxxx) to capability name",
37-
default={},
34+
cfg.MultiStrOpt(
35+
"pci_device_alias",
36+
default=[],
37+
help=(
38+
"An alias for a PCI device identified by "
39+
"'vendor_id' and 'product_id' fields. Format: "
40+
'{"vendor_id": "1234", "product_id": "5678", '
41+
'"name": "pci_dev1"}. Use double quotes for the '
42+
"keys and values."
43+
),
3844
),
3945
]
4046
cfg.CONF.register_group(grp)

‎python/ironic-understack/ironic_understack/hooks/inspect_hook_pci_device_capabilities.py‎

Lines changed: 0 additions & 105 deletions
This file was deleted.
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import collections
2+
import json
3+
import re
4+
5+
from ironic import objects
6+
from ironic.drivers.modules.inspector.hooks import base
7+
from oslo_log import log as logging
8+
9+
from ironic_understack.conf import CONF
10+
11+
LOG = logging.getLogger(__name__)
12+
13+
TRAIT_PREFIX = "CUSTOM_PCI_"
14+
MAX_TRAIT_LENGTH = 255
15+
16+
17+
class PciDeviceTraitHook(base.InspectionHook):
18+
"""Control a node's PCI device traits based on PCI device IDs."""
19+
20+
def __call__(self, task, inventory, plugin_data):
21+
_aliases = _parse_pci_alias_entries()
22+
if not _aliases:
23+
LOG.warning("No pci_device_alias configured under [ironic_understack]")
24+
return
25+
26+
pci_devices = inventory.get("pci_devices")
27+
if not pci_devices:
28+
LOG.warning("No PCI device data is present in inventory")
29+
return
30+
31+
pci_device_keys = [(dev["vendor_id"], dev["product_id"]) for dev in pci_devices]
32+
known_pci_device_names = [
33+
_aliases[key] for key in pci_device_keys if key in _aliases
34+
]
35+
required_traits = {
36+
f"{count}_{name}"
37+
for name, count in collections.Counter(known_pci_device_names).items()
38+
}
39+
40+
_set_node_traits(task, required_traits)
41+
42+
43+
def _parse_pci_alias_entries():
44+
parsed_pci_devices = []
45+
46+
for pci_alias_entry in CONF.ironic_understack.pci_device_alias:
47+
try:
48+
parsed_entry = json.loads(pci_alias_entry)
49+
required_keys = {"vendor_id", "product_id", "name"}
50+
if not isinstance(parsed_entry, dict) or set(parsed_entry) != required_keys:
51+
raise ValueError(
52+
'The "pci_device_alias" entry should contain '
53+
'exactly the "vendor_id", "product_id" and '
54+
'"name" keys.'
55+
)
56+
if not all(isinstance(parsed_entry[key], str) for key in required_keys):
57+
raise ValueError(
58+
'The "pci_device_alias" entry should contain string values.'
59+
)
60+
parsed_entry["name"] = _normalize_pci_alias_name(parsed_entry["name"])
61+
parsed_pci_devices.append(parsed_entry)
62+
except (TypeError, ValueError) as exc:
63+
LOG.error("Error parsing 'pci_device_alias' option: %s", exc)
64+
65+
return {
66+
(dev["vendor_id"], dev["product_id"]): dev["name"] for dev in parsed_pci_devices
67+
}
68+
69+
70+
def _normalize_pci_alias_name(name: str) -> str:
71+
"""Return a predictable fragment suitable for an OpenStack custom trait."""
72+
normalized = re.sub(r"[^0-9A-Za-z]+", "_", name).strip("_").upper()
73+
if not normalized:
74+
raise ValueError(
75+
'The "name" in a "pci_device_alias" entry must contain at least '
76+
"one ASCII letter or digit."
77+
)
78+
79+
# The smallest generated prefix includes the count ``1`` and its separator.
80+
if len(f"{TRAIT_PREFIX}1_{normalized}") > MAX_TRAIT_LENGTH:
81+
raise ValueError(
82+
'The normalized "name" in a "pci_device_alias" entry is too long '
83+
f"to fit in a {MAX_TRAIT_LENGTH}-character trait."
84+
)
85+
return normalized
86+
87+
88+
def _set_node_traits(task, required_traits: set[str]):
89+
"""Set a trait on the node for each recognised pci device.
90+
91+
Manages only the ``{TRAIT_PREFIX}`` namespace: the required traits are
92+
added and any other ``{TRAIT_PREFIX}`` trait is removed. All other traits
93+
are left untouched.
94+
"""
95+
required = set()
96+
for trait in required_traits:
97+
trait_name = TRAIT_PREFIX + trait
98+
if len(trait_name) > MAX_TRAIT_LENGTH:
99+
LOG.error(
100+
"Ignoring overlong PCI device trait %r; traits are limited to "
101+
"%d characters",
102+
trait_name,
103+
MAX_TRAIT_LENGTH,
104+
)
105+
continue
106+
required.add(trait_name)
107+
current = set(task.node.traits.get_trait_names())
108+
unmanaged = {t for t in current if not t.startswith(TRAIT_PREFIX)}
109+
required = required | unmanaged
110+
111+
LOG.debug(
112+
"Checking traits for node %s: existing=%s required=%s",
113+
task.node.uuid,
114+
current,
115+
required,
116+
)
117+
if current != required:
118+
objects.TraitList.create(task.context, task.node.id, required)
119+
task.node.save()

0 commit comments

Comments
 (0)