Skip to content

Commit 5409658

Browse files
committed
change reservations in kea to include client classes
1 parent de70326 commit 5409658

1 file changed

Lines changed: 36 additions & 15 deletions

File tree

  • python/ironic-understack/ironic_understack/dhcp

python/ironic-understack/ironic_understack/dhcp/kea.py

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
from urllib.parse import urlparse
2+
13
import requests
24
from ironic import objects
35
from ironic.common import exception
46
from ironic.dhcp import base
57
from oslo_log import log as logging
6-
from urllib.parse import urlparse
78

89
from ironic_understack.conf import CONF
910

@@ -74,7 +75,7 @@ def get_statistics(self, name=None):
7475
return self._make_request("statistic-get", {"name": name})
7576
return self._make_request("statistic-get-all", {})
7677

77-
def _update_host_reservation(self, hw_address, boot_file_name=None, remove=False):
78+
def _update_host_reservation(self, hw_address, kea_options=None, remove=False):
7879
"""Modify a host reservation in the Kea config file or hosts database."""
7980
# TODO(cid) add support/replace with the host database configuration
8081
# option in a central database managed by Ironic; the commands to have
@@ -84,24 +85,37 @@ def _update_host_reservation(self, hw_address, boot_file_name=None, remove=False
8485
config = self.get_config()
8586
config["arguments"].pop("hash", None)
8687
dhcp4_config = config["arguments"]["Dhcp4"]
87-
next_server = urlparse(boot_file_name).hostname
8888

8989
reservations = dhcp4_config.get("reservations", [])
9090
found = False
9191
for reservation in reservations:
9292
if reservation.get("hw-address") == hw_address:
93-
reservation["boot-file-name"] = boot_file_name
94-
reservation["next-server"] = next_server
93+
if "httpclient" in reservation.get("client-classes", []):
94+
boot_file_name = kea_options["http"]["boot_file_name"]
95+
reservation["boot-file-name"] = boot_file_name
96+
next_server = urlparse(boot_file_name).hostname
97+
reservation["next-server"] = next_server
98+
elif "pxeclient" in reservation.get("client-classes", []):
99+
boot_file_name = kea_options["pxe"]["boot_file_name"]
100+
reservation["boot-file-name"] = boot_file_name
101+
next_server = urlparse(boot_file_name).hostname
102+
reservation["next-server"] = next_server
95103
found = True
96-
break
97-
98104
if not found:
105+
boot_file_name = kea_options["http"]["boot_file_name"]
99106
reservations.append(
100107
{
101108
"hw-address": hw_address,
102-
"boot-file-name": boot_file_name,
103-
"next-server": next_server,
104-
}
109+
"boot-file-name": kea_options["http"]["boot_file_name"],
110+
"next-server": urlparse(boot_file_name).hostname,
111+
"client-classes": ["httpclient"],
112+
},
113+
{
114+
"hw-address": hw_address,
115+
"boot-file-name": kea_options["pxe"]["boot_file_name"],
116+
"next-server": urlparse(boot_file_name).hostname,
117+
"client-classes": ["pxeclient"],
118+
},
105119
)
106120
dhcp4_config["reservations"] = reservations
107121

@@ -116,13 +130,20 @@ def update_port_dhcp_opts(self, port_id, dhcp_options, context=None):
116130
"""Update DHCP options for a specific port in Kea."""
117131
port = objects.Port.get(context, port_id)
118132

119-
boot_file_name = ""
133+
kea_options = {}
120134
print(f"DHCP_OPTIONS: {dhcp_options}")
121135
for opt in dhcp_options:
122-
if opt["opt_name"].startswith("!"):
123-
boot_file_name = opt["opt_value"]
124-
break
125-
return self._update_host_reservation(port.address, boot_file_name)
136+
if opt["opt_name"] == "!175,67":
137+
kea_options["http"] = {
138+
"boot_file_name": opt["opt_value"],
139+
"client_class": "httpclient",
140+
}
141+
elif opt["opt_name"] == "67":
142+
kea_options["pxe"] = {
143+
"boot_file_name": opt["opt_value"],
144+
"client_class": "pxeclient",
145+
}
146+
return self._update_host_reservation(port.address, kea_options)
126147

127148
def update_dhcp_opts(self, task, options, vifs=None):
128149
"""Update DHCP options for all ports associated with a node."""

0 commit comments

Comments
 (0)