Implement IpamDriver.GetDefaultAddressSpace

pull/8/head
Jack O'Sullivan 5 years ago
parent 22f8e24549
commit a616342e2e

@ -1,15 +1,17 @@
PLUGIN_NAME = devplayer0/net-dhcp
PLUGIN_TAG ?= latest
all: clean rootfs create
all: clean rootfs build create
clean:
@echo "### rm ./plugin"
@rm -rf ./plugin
rootfs:
build:
@echo "### docker build: rootfs image with net-dhcp"
@docker build -t ${PLUGIN_NAME}:rootfs .
rootfs:
@echo "### create rootfs directory in ./plugin/rootfs"
@mkdir -p ./plugin/rootfs
@docker create --name tmp ${PLUGIN_NAME}:rootfs
@ -24,6 +26,10 @@ create:
@echo "### create new plugin ${PLUGIN_NAME}:${PLUGIN_TAG} from ./plugin"
@docker plugin create ${PLUGIN_NAME}:${PLUGIN_TAG} ./plugin
debug:
@docker run --rm -ti --cap-add CAP_SYS_ADMIN --network host --volume /run/docker/plugins:/run/docker/plugins \
--volume /run/docker.sock:/run/docker.sock ${PLUGIN_NAME}:rootfs
enable:
@echo "### enable plugin ${PLUGIN_NAME}:${PLUGIN_TAG}"
@docker plugin enable ${PLUGIN_NAME}:${PLUGIN_TAG}

@ -12,6 +12,13 @@
"network": {
"type": "host"
},
"mounts": [
{
"source": "/var/run/docker.sock",
"destination": "/run/docker.sock",
"type": "bind"
}
],
"linux": {
"capabilities": [
"CAP_SYS_ADMIN"

@ -1,10 +1,58 @@
import itertools
import ipaddress
from os import path
import atexit
import pyroute2
import docker
from flask import jsonify
from . import app
ipdb = pyroute2.IPDB()
@atexit.register
def close_ipdb():
ipdb.release()
client = docker.from_env()
@atexit.register
def close_docker():
client.close()
def iface_addrs(iface):
return list(map(ipaddress.ip_interface, iface.ipaddr.ipv4))
def iface_nets(iface):
return list(map(lambda n: n.network, map(ipaddress.ip_interface, iface.ipaddr.ipv4)))
def get_bridges():
reserved_nets = set(map(ipaddress.ip_network, map(lambda c: c['Subnet'], \
itertools.chain.from_iterable(map(lambda i: i['Config'], filter(lambda i: i['Driver'] != 'net-dhcp', \
map(lambda n: n.attrs['IPAM'], client.networks.list())))))))
return list(filter(lambda i: i.kind == 'bridge' and not set(iface_nets(i)).intersection(reserved_nets), \
map(lambda i: ipdb.interfaces[i], filter(lambda k: isinstance(k, str), ipdb.interfaces.keys()))))
@app.route('/IpamDriver.GetCapabilities')
def ipam_get_capabilities():
return jsonify({
'RequiresMACAddress': True,
'RequiresRequestReplay': False
})
@app.route('/IpamDriver.GetDefaultAddressSpace')
def get_default_addr_space():
bridges = get_bridges()
if not bridges:
return jsonify({'Err': 'No bridges available'}), 404
first = None
for b in bridges:
if b.ipaddr.ipv4:
first = b
if not first:
return jsonify({'Err': 'No bridges with addresses available'}), 404
return jsonify({
'LocalDefaultAddressSpace': f'{first.ifname}-{iface_addrs(first)[0].network}',
'GlobalDefaultAddressSpace': None
})

@ -1,2 +1,4 @@
flask==1.1.1
gunicorn==19.9.0
pyroute2==0.5.6
docker==4.0.2
Loading…
Cancel
Save