removed bridge subnet collisions

This commit is contained in:
Jake Hillion 2020-11-14 12:54:50 +00:00
parent e47e058c3f
commit 3e777bde92
2 changed files with 18 additions and 6 deletions

View File

@ -136,7 +136,7 @@ class ProxmoxRunner:
def build(self, bridge: structure.Bridge):
bridges, nodes = bridge_node_search(bridge, lambda x: self._create_bridge(x), lambda x: self._create_node(x))
self._build_bridges()
self._build_bridges(bridges)
with concurrent.futures.ThreadPoolExecutor(max_workers=8) as executor:
build_futures = [executor.submit(self._build_node, node) for node in nodes]
@ -177,10 +177,16 @@ class ProxmoxRunner:
self._created_bridges.append(bridge_name)
return bridge_name
def _build_bridges(self):
def _build_bridges(self, bridges: List[structure.Bridge]):
network_task = self._proxmox.nodes(self._proxmox_node).network.put()
self._await_task(network_task)
existing = []
for bridge in bridges:
while True in [ipaddress.ip_network(bridge.get_network()).overlaps(x) for x in existing]:
bridge.new_network()
existing.append(bridge.get_network())
def _create_node(self, node: structure.Node) -> int:
while True:
try:

View File

@ -69,8 +69,7 @@ class Bridge:
# Generate a random class c private range by default (10.0.0.0)
self.netmask = 24
self._addr: ipaddress.ip_address = ipaddress.ip_address('10.0.0.0') + random.randint(0, 16777216)
self._network_iterator = ipaddress.ip_network('{}/{}'.format(self._addr, self.netmask), False).hosts()
self.new_network()
def get_interfaces(self) -> List[Interface]:
return self._interfaces
@ -88,9 +87,16 @@ class Bridge:
def get_ip_address(self) -> ipaddress.ip_address:
return next(self._network_iterator)
def get_network(self) -> str:
def get_network(self) -> ipaddress.ip_network:
return ipaddress.ip_network('{}/{}'.format(self._addr, self.netmask), False)
def get_network_string(self) -> str:
return str(ipaddress.ip_network('{}/{}'.format(self._addr, self.netmask), False))
def new_network(self):
self._addr: ipaddress.ip_address = ipaddress.ip_address('10.0.0.0') + random.randint(0, 16777216)
self._network_iterator = ipaddress.ip_network('{}/{}'.format(self._addr, self.netmask), False).hosts()
class Node:
def __init__(self, interfaces: List[Interface], setup_params: Dict = None):
@ -291,7 +297,7 @@ class LocalPortal(Node):
policy_routing = '\n\n'.join([policy_routing_string.format(
table_number=i + 10,
device='eth{}'.format(i),
network=iface.get_bridge().get_network(),
network=iface.get_bridge().get_network_string(),
local_address=iface.get_address(),
) for i, iface in enumerate(self.get_interfaces()[:-2])])