refactor: split hive-gateway into module dir, DHCP range into hive-network
This commit is contained in:
parent
ecaad48fad
commit
5f9af9fc97
8 changed files with 1143 additions and 1075 deletions
|
|
@ -5,6 +5,28 @@
|
|||
}:
|
||||
let
|
||||
cfg = config.services.hyperhive.network;
|
||||
|
||||
# IPv4 helpers for the DHCP-pool computation below — nix integers
|
||||
# are 64-bit so all /0-/32 values are safe.
|
||||
ipToInt =
|
||||
ip:
|
||||
builtins.foldl' (acc: x: acc * 256 + x) 0 (
|
||||
map lib.strings.toIntBase10 (lib.strings.splitString "." ip)
|
||||
);
|
||||
intToIp =
|
||||
n:
|
||||
let
|
||||
a = n / 16777216;
|
||||
b = (n - a * 16777216) / 65536;
|
||||
c = (n - a * 16777216 - b * 65536) / 256;
|
||||
d = n - a * 16777216 - b * 65536 - c * 256;
|
||||
in
|
||||
"${toString a}.${toString b}.${toString c}.${toString d}";
|
||||
# 2^n via recursion (nix has no pow builtin).
|
||||
pow2 = n: if n == 0 then 1 else 2 * (pow2 (n - 1));
|
||||
hostCount = pow2 (32 - cfg.bridgePrefixLength);
|
||||
# Mask off host bits to get the network base address.
|
||||
networkBase = builtins.bitAnd (ipToInt cfg.bridgeIp) (4294967295 - hostCount + 1);
|
||||
in
|
||||
{
|
||||
# Hive-internal network — host-side bridge + per-agent DNS resolver.
|
||||
|
|
@ -117,6 +139,35 @@ in
|
|||
'';
|
||||
};
|
||||
|
||||
# DHCP pool covering all usable host addresses on the bridge
|
||||
# subnet, computed from bridgeIp/bridgePrefixLength: .2 (first
|
||||
# usable after the .1 gateway) to .(hostCount-2) (last usable
|
||||
# before broadcast). All containers — agents and service
|
||||
# containers alike — receive their IPs dynamically from this pool;
|
||||
# there are no hash-derived static assignments. Consumed by the
|
||||
# dnsmasq that runs in the gateway container (hive-gateway module).
|
||||
dhcpRangeStart = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
default = intToIp (networkBase + 2);
|
||||
defaultText = lib.literalMD "first usable bridge address after the gateway";
|
||||
description = ''
|
||||
Read-only computed first address of the bridge DHCP pool.
|
||||
'';
|
||||
};
|
||||
|
||||
dhcpRangeEnd = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
default = intToIp (networkBase + hostCount - 2);
|
||||
defaultText = lib.literalMD "last usable bridge address before broadcast";
|
||||
description = ''
|
||||
Read-only computed last address of the bridge DHCP pool.
|
||||
'';
|
||||
};
|
||||
|
||||
isolateContainers = lib.mkOption {
|
||||
type = lib.types.bool;
|
||||
default = true;
|
||||
|
|
|
|||
Loading…
Reference in a new issue