Skip to content

Commit 9f3b466

Browse files
add subnet id
1 parent 15c42ee commit 9f3b466

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

kea-subnets.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import yaml
99
import jinja2
1010
import dotenv
11+
import zlib
1112

1213
dotenv.load_dotenv()
1314

@@ -36,7 +37,7 @@ def main(url, token, parent_prefix, ip_range_role, config, template_path):
3637
env.filters['ip'] = filter_ip
3738

3839
nb = pynetbox.api(url, token)
39-
prefixes = nb.ipam.prefixes.filter(status='active', within=parent_prefix)
40+
prefixes = nb.ipam.prefixes.filter(status='active', within_include=parent_prefix)
4041
ip_ranges = list(nb.ipam.ip_ranges.filter(status='active', role=ip_range_role))
4142
ip_addresses = list(nb.ipam.ip_addresses.filter(status='dhcp', parent=parent_prefix))
4243
subnets = []
@@ -51,12 +52,17 @@ def main(url, token, parent_prefix, ip_range_role, config, template_path):
5152
for ip_address in filter(lambda a : netaddr.IPNetwork(a.address).ip in _prefix, ip_addresses):
5253
reservations.append(ip_address)
5354

54-
subnet_template = env.get_template('subnet.yaml.j2')
55-
subnets.append(yaml.safe_load(subnet_template.render(
56-
prefix=prefix,
57-
pools=pools,
58-
reservations=reservations
59-
)))
55+
if pools:
56+
subnet_template = env.get_template('subnet.yaml.j2')
57+
subnets.append(yaml.safe_load(subnet_template.render(
58+
# The Kea subnet ID is a 32 bit unsigned int.
59+
# We assume that CRC32 of the prefix is sufficiently unique.
60+
id=zlib.crc32(bytes(_prefix.ip)) % (1<<32),
61+
62+
prefix=prefix,
63+
pools=pools,
64+
reservations=reservations
65+
)))
6066

6167
if (config):
6268
config_json = yaml.safe_load(open(config, 'r'))

templates/subnet.yaml.j2

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{#
22
The following variables are available:
33
4+
- id (Kea subnet ID, derived from prefix)
45
- prefix
56
- pools (list of IP Range)
67
- reservations (list of IP Address)
@@ -51,6 +52,7 @@
5152
{%- endmacro %}
5253

5354
---
55+
id: {{ id }}
5456
subnet: "{{ prefix.prefix }}"
5557
option-data: {{ option_data(prefix) }}
5658
pools: [

0 commit comments

Comments
 (0)