Fix DECONFIG, BOUND and RENEW events

This commit is contained in:
2021-03-01 10:01:47 +01:00
parent b6ff5e4650
commit 9ad24be9ca
+26 -23
View File
@@ -331,32 +331,35 @@ class ContainerDHCPManager:
self._thread.start() self._thread.start()
def _on_event(self, dhcp, event_type, _event): def _on_event(self, dhcp, event_type, _event):
if event_type != udhcpc.EventType.RENEW or not dhcp.gateway: if event_type == udhcpc.EventType.DECONFIG:
return logger.info('[dhcp container] DECONFIG Event %s', _event)
logger.info('[dhcp container] Flushing IP addresses')
subprocess.check_call(['nsenter', f'-n{dhcp.netns}', '--', '/sbin/ip', 'address', 'flush', 'dev',
str(dhcp.iface['ifname'])], timeout=1, stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL)
elif event_type == udhcpc.EventType.RENEW or event_type == udhcpc.EventType.BOUND:
if event_type == udhcpc.EventType.RENEW:
logger.info('[dhcp container] RENEW Event %s', _event)
elif event_type == udhcpc.EventType.BOUND:
logger.info('[dhcp container] BOUND Event %s', _event)
logger.info('[dhcp container] Replacing gateway with %s', dhcp.gateway) logger.info('[dhcp container] Flushing IP addresses')
subprocess.check_call(['nsenter', f'-n{dhcp.netns}', '--', '/sbin/ip', 'route', 'replace', 'default', 'via', subprocess.check_call(['nsenter', f'-n{dhcp.netns}', '--', '/sbin/ip', 'address', 'flush', 'dev',
str(dhcp.gateway)], timeout=1, stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, str(dhcp.iface['ifname'])], timeout=1, stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL) stderr=subprocess.DEVNULL)
# TODO: Adding default route with NDB seems to be broken (because of the dst syntax?) logger.info('[dhcp container] Adding IP addresses %s', dhcp.ip)
#for route in ndb.routes: subprocess.check_call(['nsenter', f'-n{dhcp.netns}', '--', '/sbin/ip', 'address', 'add', str(dhcp.ip), 'dev',
# if route['type'] != rtypes['RTN_UNICAST'] or \ str(dhcp.iface['ifname'])], timeout=1, stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL,
# route['oif'] != dhcp.iface['index'] or \ stderr=subprocess.DEVNULL)
# (route['family'] == socket.AF_INET6 and not self.ipv6) or \
# route['dst'] not in ('', '/0'):
# continue
# # Needed because Route.remove() doesn't like a blank destination if dhcp.gateway:
# logger.info('Removing default route via %s', route['gateway']) logger.info('[dhcp container] Replacing gateway with %s', dhcp.gateway)
# route['dst'] = '::' if route['family'] == socket.AF_INET6 else '0.0.0.0' subprocess.check_call(['nsenter', f'-n{dhcp.netns}', '--', '/sbin/ip', 'route', 'replace', 'default', 'via',
# (route str(dhcp.gateway)], timeout=1, stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL,
# .remove() stderr=subprocess.DEVNULL)
# .commit()) else:
logger.info('[dhcp container] Unhandled Event %s: %s', event_type,_event)
#logger.info('Adding default route via %s', dhcp.gateway)
#(ndb.routes.add({'oif': dhcp.iface['index'], 'gateway': dhcp.gateway})
# .commit())
def run(self): def run(self):
try: try: