fix(dhcp): one refused reservation no longer stops the apply
driver / lint (pull_request) Successful in 18s
driver / standard (pull_request) Failing after 22s
driver / types (pull_request) Successful in 1m22s
driver / test (3.12) (pull_request) Successful in 1m28s
driver / test (3.13) (pull_request) Successful in 1m26s
driver / lowest (pull_request) Successful in 38s
driver / build (pull_request) Successful in 38s
driver / security (pull_request) Successful in 1m11s
CI / driver (pull_request) Failing after 2m57s
driver / publish (pull_request) Skipped

apply_dhcp_reservationset wrote the diff one reservation at a time and
committed only after the last. A write the device refused -- on
OPNsense, an address in a subnet Kea does not manage -- ended the run
before the commit: every later reservation went unwritten, and the
earlier ones stayed saved but inactive. The next diff read those back as
configured, reported no changes and never committed them.

A refused change now gets a "[failed] <mac> -> <ip>: <reason>" line and
the others are still written. Any non-empty diff commits, also one whose
every write was refused, so writes an interrupted earlier run saved take
effect. DhcpApplyError (a RuntimeError, exported) is raised after the
commit with how many of how many changes were refused.

Refs NetOrk/netork#2381
This commit is contained in:
christianmanivong committed 2026-11-11 22:03:52 +01:00
1 parent 6c7f164004
commit c74173523a
5 files changed
+160 -19

No files matched your search

+2 -1
View File
@@ -127,7 +127,7 @@ from napalm_device_types.container_engine import (
ContainerEngineMixin,
ContainerEngineUnavailable,
)
from napalm_device_types.dhcp import DhcpServerMixin, normalize_cidr, normalize_mac
from napalm_device_types.dhcp import DhcpApplyError, DhcpServerMixin, normalize_cidr, normalize_mac
from napalm_device_types.dns_forward import DnsForwardMixin
from napalm_device_types.firewall import FirewallDriver
from napalm_device_types.firewall_rules import FirewallRuleMixin
@@ -404,6 +404,7 @@ __all__ = [
"DhcpHaDict",
"DhcpHaPeerDict",
"DeviceTypeDriver",
"DhcpApplyError",
"DhcpServerMixin",
"FingerprintRule",
"rules_max_weight",
+56 -16
View File
@@ -58,6 +58,19 @@ _DHCP_SUBNET_COMPARE_FIELDS = (
_HEX_ONLY = re.compile(r"[^0-9a-f]")
class DhcpApplyError(RuntimeError):
"""Some reservations of an applied set were refused by the device.
Raised after the rest were written and committed: ``failed`` of ``attempted``
changes did not go through, and each was named in a ``[failed]`` progress line.
"""
def __init__(self, failed: int, attempted: int) -> None:
self.failed = failed
self.attempted = attempted
super().__init__(f"{failed} of {attempted} DHCP reservation change(s) were refused")
def normalize_mac(mac: str | None) -> str:
"""Reduces a MAC address to lowercase colon-separated form.
@@ -298,29 +311,56 @@ class DhcpServerMixin:
committing reloads the DHCP daemon and drops in-flight requests, which
is too high a price for a no-op run.
A change the device refuses (say, an address in a subnet its DHCP server
does not manage) does not stop the others: it gets a ``[failed]`` line,
the rest are written and committed, and ``DhcpApplyError`` is raised at
the end. Any non-empty diff commits, even one whose every write was
refused, so that writes an earlier, interrupted run saved take effect.
:param desired: The complete desired reservation set.
:yields: Progress lines, one per applied add/update, plus a final
commit line.
:yields: Progress lines, one per applied, updated or refused change,
plus a final commit line.
:raises DhcpApplyError: after the commit, when a change was refused.
"""
diff = self.diff_dhcp_reservations(desired)
for reservation in diff["add"]:
self.apply_dhcp_reservation(reservation)
yield f"[add] {reservation['mac']} -> {reservation['ip']}"
for entry in diff["update"]:
self.apply_dhcp_reservation(entry["reservation"], uuid=entry["uuid"])
fields = ", ".join(entry["changed_fields"])
yield (
f"[update] {entry['reservation']['mac']} -> {entry['reservation']['ip']} ({fields})"
)
if not diff["add"] and not diff["update"]:
attempted = len(diff["add"]) + len(diff["update"])
if not attempted:
yield "[commit] no changes"
return
added = updated = 0
failed: list[str] = []
for reservation in diff["add"]:
try:
self.apply_dhcp_reservation(reservation)
except Exception as exc:
failed.append(f"[failed] {reservation['mac']} -> {reservation['ip']}: {exc}")
yield failed[-1]
continue
added += 1
yield f"[add] {reservation['mac']} -> {reservation['ip']}"
for entry in diff["update"]:
target = entry["reservation"]
try:
self.apply_dhcp_reservation(target, uuid=entry["uuid"])
except Exception as exc:
failed.append(f"[failed] {target['mac']} -> {target['ip']}: {exc}")
yield failed[-1]
continue
updated += 1
fields = ", ".join(entry["changed_fields"])
yield f"[update] {target['mac']} -> {target['ip']} ({fields})"
# Committed even when every write was refused: a run that stopped before its
# commit left saved writes behind, which the device reports as configured, so
# no later diff would ever commit them.
self.commit_dhcp_reservations()
yield (f"[commit] applied {len(diff['add'])} add(s), {len(diff['update'])} update(s)")
yield f"[commit] applied {added} add(s), {updated} update(s)"
if failed:
raise DhcpApplyError(len(failed), attempted)
def diff_dhcp_subnets(self, desired: list[DhcpSubnetDict]) -> DhcpSubnetDiffDict:
"""