Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions components/ironic/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,30 @@ helm --namespace openstack \

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

## PCI device traits

The `pci-device-trait` inspection hook counts PCI devices that match aliases
configured in the `[ironic_understack]` section of `ironic.conf`. Each alias is
a JSON object with `vendor_id`, `product_id`, and `name` fields. In the
OpenStack-Helm values this is configured as:

```yaml
conf:
ironic:
ironic_understack:
pci_device_alias:
- '{"vendor_id": "0x1002", "product_id": "0x75a8", "name": "gpu_amd_mi350p"}'
```

The option uses the same JSON `MultiStrOpt` format as Ironic's upstream
`pci_device_alias` option, but remains in the `[ironic_understack]` section.
The hook normalizes the alias name and adds a custom trait containing the
number of matching devices. For example, a node with two matching devices from
the configuration above receives the `CUSTOM_PCI_2_GPU_AMD_MI350P` trait.

On subsequent inspections, the hook replaces traits in the `CUSTOM_PCI_`
namespace with the current counts while preserving all other node traits.

## Validating Ironic

You can run an OpenStack client in the cluster to validate it is running correctly.
Expand Down
2 changes: 1 addition & 1 deletion components/ironic/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ conf:
keep_ports: "present"
redfish:
# Redfish inspection hooks run after inspecting out-of-band using the BMC:
inspection_hooks: "validate-interfaces,ports,port-bios-name,architecture,pci-devices,resource-class,chassis_model,pci-device-capabilities"
inspection_hooks: "validate-interfaces,ports,port-bios-name,architecture,pci-devices,resource-class,chassis_model,pci-device-trait"
# enable sensors and metrics for redfish metrics - https://docs.openstack.org/ironic/latest/admin/drivers/redfish/metrics.html
post_boot_retry_attempts: 6
post_boot_retry_delay: 30
Expand Down
14 changes: 10 additions & 4 deletions python/ironic-understack/ironic_understack/conf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,16 @@ def setup_conf():
"1d": "bmc",
},
),
cfg.DictOpt(
"pci_device_capabilities",
help="Dictionary of pci device IDs (xxxx:xxxx) to capability name",
default={},
cfg.MultiStrOpt(
"pci_device_alias",
default=[],
help=(
"An alias for a PCI device identified by "
"'vendor_id' and 'product_id' fields. Format: "
'{"vendor_id": "1234", "product_id": "5678", '
'"name": "pci_dev1"}. Use double quotes for the '
"keys and values."
),
),
]
cfg.CONF.register_group(grp)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import collections
import json
import re

from ironic import objects
from ironic.drivers.modules.inspector.hooks import base
from oslo_log import log as logging

from ironic_understack.conf import CONF

LOG = logging.getLogger(__name__)

TRAIT_PREFIX = "CUSTOM_PCI_"
MAX_TRAIT_LENGTH = 255


class PciDeviceTraitHook(base.InspectionHook):
"""Control a node's PCI device traits based on PCI device IDs."""

def __call__(self, task, inventory, plugin_data):
_aliases = _parse_pci_alias_entries()
if not _aliases:
LOG.warning("No pci_device_alias configured under [ironic_understack]")
return

pci_devices = inventory.get("pci_devices")
if not pci_devices:
LOG.warning("No PCI device data is present in inventory")
return

pci_device_keys = [(dev["vendor_id"], dev["product_id"]) for dev in pci_devices]
known_pci_device_names = [
_aliases[key] for key in pci_device_keys if key in _aliases
]
required_traits = {
f"{count}_{name}"
for name, count in collections.Counter(known_pci_device_names).items()
}

_set_node_traits(task, required_traits)


def _parse_pci_alias_entries():
parsed_pci_devices = []

for pci_alias_entry in CONF.ironic_understack.pci_device_alias:
try:
parsed_value = json.loads(pci_alias_entry)
parsed_entries = (
[parsed_value] if isinstance(parsed_value, dict) else parsed_value
)
if not isinstance(parsed_entries, list):
raise ValueError(
'The "pci_device_alias" entry should be a JSON object or array.'
)

for parsed_entry in parsed_entries:
required_keys = {"vendor_id", "product_id", "name"}
if (
not isinstance(parsed_entry, dict)
or set(parsed_entry) != required_keys
):
raise ValueError(
'The "pci_device_alias" entry should contain '
'exactly the "vendor_id", "product_id" and '
'"name" keys.'
)
if not all(isinstance(parsed_entry[key], str) for key in required_keys):
raise ValueError(
'The "pci_device_alias" entry should contain string values.'
)
parsed_entry["name"] = _normalize_pci_alias_name(parsed_entry["name"])
parsed_pci_devices.append(parsed_entry)
except (TypeError, ValueError) as exc:
LOG.error("Error parsing 'pci_device_alias' option: %s", exc)

return {
(dev["vendor_id"], dev["product_id"]): dev["name"] for dev in parsed_pci_devices
}


def _normalize_pci_alias_name(name: str) -> str:
"""Return a predictable fragment suitable for an OpenStack custom trait."""
normalized = re.sub(r"[^0-9A-Za-z]+", "_", name).strip("_").upper()
if not normalized:
raise ValueError(
'The "name" in a "pci_device_alias" entry must contain at least '
"one ASCII letter or digit."
)

# The smallest generated prefix includes the count ``1`` and its separator.
if len(f"{TRAIT_PREFIX}1_{normalized}") > MAX_TRAIT_LENGTH:
raise ValueError(
'The normalized "name" in a "pci_device_alias" entry is too long '
f"to fit in a {MAX_TRAIT_LENGTH}-character trait."
)
return normalized


def _set_node_traits(task, required_traits: set[str]):
"""Set a trait on the node for each recognised pci device.

Manages only the ``{TRAIT_PREFIX}`` namespace: the required traits are
added and any other ``{TRAIT_PREFIX}`` trait is removed. All other traits
are left untouched.
"""
required = set()
for trait in required_traits:
trait_name = TRAIT_PREFIX + trait
if len(trait_name) > MAX_TRAIT_LENGTH:
LOG.error(
"Ignoring overlong PCI device trait %r; traits are limited to "
"%d characters",
trait_name,
MAX_TRAIT_LENGTH,
)
continue
required.add(trait_name)
current = set(task.node.traits.get_trait_names())
unmanaged = {t for t in current if not t.startswith(TRAIT_PREFIX)}
required = required | unmanaged

LOG.debug(
"Checking traits for node %s: existing=%s required=%s",
task.node.uuid,
current,
required,
)
if current != required:
objects.TraitList.create(task.context, task.node.id, required)
task.node.save()
Loading
Loading