Fix IP address and route assignment

pull/8/head
Jack O'Sullivan 5 years ago
parent bdb71ac8ca
commit 1174d4e0da

@ -97,16 +97,13 @@ def create_endpoint():
} }
try: try:
if 'MacAddress' in req_iface and req_iface['MacAddress']: if 'MacAddress' not in req_iface or not req_iface['MacAddress']:
(if_container
.set('address', req_iface['MacAddress'])
.commit())
else:
res_iface['MacAddress'] = if_container['address'] res_iface['MacAddress'] = if_container['address']
def try_addr(type_): def try_addr(type_):
k = 'AddressIPv6' if type_ == 'v6' else 'Address' k = 'AddressIPv6' if type_ == 'v6' else 'Address'
if k in req_iface and req_iface[k]: if k in req_iface and req_iface[k]:
# Just validate the address, Docker will add it to the interface for us
a = ipaddress.ip_address(req_iface[k]) a = ipaddress.ip_address(req_iface[k])
net = None net = None
for addr in bridge_addrs: for addr in bridge_addrs:
@ -119,9 +116,6 @@ def create_endpoint():
to_add = f'{a}/{net.prefixlen}' to_add = f'{a}/{net.prefixlen}'
logger.info('Adding address %s to %s', to_add, if_container['ifname']) logger.info('Adding address %s to %s', to_add, if_container['ifname'])
(if_container
.add_ip(to_add)
.commit())
elif type_ == 'v4': elif type_ == 'v4':
raise NetDhcpError(400, f'DHCP{type_} is currently unsupported') raise NetDhcpError(400, f'DHCP{type_} is currently unsupported')
try_addr('v4') try_addr('v4')
@ -193,32 +187,20 @@ def join():
}, },
'StaticRoutes': [] 'StaticRoutes': []
} }
nonlink = []
for route in bridge.routes: for route in bridge.routes:
# TODO: IPv6 routes # TODO: IPv6 routes
if route['type'] != rtypes['RTN_UNICAST'] or route['family'] != socket.AF_INET: if route['type'] != rtypes['RTN_UNICAST'] or route['family'] != socket.AF_INET:
continue continue
logging.info(route)
if route['dst'] == '' and 'Gateway' not in res: if route['dst'] == '' and 'Gateway' not in res:
res['Gateway'] = route['gateway'] res['Gateway'] = route['gateway']
continue continue
elif route['gateway']:
dst = f'{route["dst"]}/{route["dst_len"]}' res['StaticRoutes'].append({
if route['gateway']: 'Destination': f'{route["dst"]}/{route["dst_len"]}',
nonlink.append({
'Destination': dst,
'RouteType': 0, 'RouteType': 0,
'NextHop': route['gateway'] 'NextHop': route['gateway']
}) })
else:
# on-link route
res['StaticRoutes'].append({
'Destination': dst,
'RouteType': 1
})
# we need to add the on-link routes first
res['StaticRoutes'] += nonlink
logger.info(res) logger.info(res)
return jsonify(res) return jsonify(res)

Loading…
Cancel
Save