Skip to content

Commit 42a48a2

Browse files
committed
Dnet agent mode support and IT
Signed-off-by: Jana Radhakrishnan <mrjana@docker.com>
1 parent 632017b commit 42a48a2

8 files changed

Lines changed: 224 additions & 36 deletions

File tree

api/api.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,17 @@ func procCreateNetwork(c libnetwork.NetworkController, vars map[string]string, b
307307
if len(create.DriverOpts) > 0 {
308308
options = append(options, libnetwork.NetworkOptionDriverOpts(create.DriverOpts))
309309
}
310-
nw, err := c.NewNetwork(create.NetworkType, create.Name, "", options...)
310+
311+
if len(create.IPv4Conf) > 0 {
312+
ipamV4Conf := &libnetwork.IpamConf{
313+
PreferredPool: create.IPv4Conf[0].PreferredPool,
314+
SubPool: create.IPv4Conf[0].SubPool,
315+
}
316+
317+
options = append(options, libnetwork.NetworkOptionIpam("default", "", []*libnetwork.IpamConf{ipamV4Conf}, nil, nil))
318+
}
319+
320+
nw, err := c.NewNetwork(create.NetworkType, create.Name, create.ID, options...)
311321
if err != nil {
312322
return nil, convertNetworkError(err)
313323
}
@@ -697,6 +707,7 @@ func procAttachBackend(c libnetwork.NetworkController, vars map[string]string, b
697707
if err != nil {
698708
return nil, convertNetworkError(err)
699709
}
710+
700711
return sb.Key(), &successResponse
701712
}
702713

api/types.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,19 @@ type sandboxResource struct {
3232
Body types
3333
************/
3434

35+
type ipamConf struct {
36+
PreferredPool string
37+
SubPool string
38+
Gateway string
39+
AuxAddresses map[string]string
40+
}
41+
3542
// networkCreate is the expected body of the "create network" http request message
3643
type networkCreate struct {
3744
Name string `json:"name"`
45+
ID string `json:"id"`
3846
NetworkType string `json:"network_type"`
47+
IPv4Conf []ipamConf `json:"ipv4_configuration"`
3948
DriverOpts map[string]string `json:"driver_opts"`
4049
NetworkOpts map[string]string `json:"network_opts"`
4150
}

client/network.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"fmt"
77
"net/http"
8+
"strings"
89
"text/tabwriter"
910

1011
flag "github.com/docker/docker/pkg/mflag"
@@ -42,8 +43,13 @@ func (cli *NetworkCli) CmdNetwork(chain string, args ...string) error {
4243
func (cli *NetworkCli) CmdNetworkCreate(chain string, args ...string) error {
4344
cmd := cli.Subcmd(chain, "create", "NETWORK-NAME", "Creates a new network with a name specified by the user", false)
4445
flDriver := cmd.String([]string{"d", "-driver"}, "", "Driver to manage the Network")
46+
flID := cmd.String([]string{"-id"}, "", "Network ID string")
47+
flOpts := cmd.String([]string{"o", "-opt"}, "", "Network options")
4548
flInternal := cmd.Bool([]string{"-internal"}, false, "Config the network to be internal")
4649
flIPv6 := cmd.Bool([]string{"-ipv6"}, false, "Enable IPv6 on the network")
50+
flSubnet := cmd.String([]string{"-subnet"}, "", "Subnet option")
51+
flRange := cmd.String([]string{"-ip-range"}, "", "Range option")
52+
4753
cmd.Require(flag.Exact, 1)
4854
err := cmd.ParseFlags(args, true)
4955
if err != nil {
@@ -56,9 +62,30 @@ func (cli *NetworkCli) CmdNetworkCreate(chain string, args ...string) error {
5662
if *flIPv6 {
5763
networkOpts[netlabel.EnableIPv6] = "true"
5864
}
65+
66+
driverOpts := make(map[string]string)
67+
if *flOpts != "" {
68+
opts := strings.Split(*flOpts, ",")
69+
for _, opt := range opts {
70+
driverOpts[netlabel.Key(opt)] = netlabel.Value(opt)
71+
}
72+
}
73+
74+
var icList []ipamConf
75+
if *flSubnet != "" {
76+
ic := ipamConf{
77+
PreferredPool: *flSubnet,
78+
}
79+
80+
if *flRange != "" {
81+
ic.SubPool = *flRange
82+
}
83+
84+
icList = append(icList, ic)
85+
}
86+
5987
// Construct network create request body
60-
var driverOpts []string
61-
nc := networkCreate{Name: cmd.Arg(0), NetworkType: *flDriver, DriverOpts: driverOpts, NetworkOpts: networkOpts}
88+
nc := networkCreate{Name: cmd.Arg(0), NetworkType: *flDriver, ID: *flID, IPv4Conf: icList, DriverOpts: driverOpts, NetworkOpts: networkOpts}
6289
obj, _, err := readBody(cli.call("POST", "/networks", nc, nil))
6390
if err != nil {
6491
return err

client/types.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,20 @@ type SandboxResource struct {
3131
/***********
3232
Body types
3333
************/
34+
type ipamConf struct {
35+
PreferredPool string
36+
SubPool string
37+
Gateway string
38+
AuxAddresses map[string]string
39+
}
3440

3541
// networkCreate is the expected body of the "create network" http request message
3642
type networkCreate struct {
3743
Name string `json:"name"`
44+
ID string `json:"id"`
3845
NetworkType string `json:"network_type"`
39-
DriverOpts []string `json:"driver_opts"`
46+
IPv4Conf []ipamConf `json:"ipv4_configuration"`
47+
DriverOpts map[string]string `json:"driver_opts"`
4048
NetworkOpts map[string]string `json:"network_opts"`
4149
}
4250

cmd/dnet/dnet.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,15 @@ func processConfig(cfg *config.Config) []config.Option {
9191
dd = cfg.Daemon.DefaultDriver
9292
}
9393
options = append(options, config.OptionDefaultDriver(dd))
94+
if cfg.Daemon.IsAgent {
95+
options = append(options, config.OptionAgent())
96+
}
97+
98+
if cfg.Daemon.Bind != "" {
99+
options = append(options, config.OptionBind(cfg.Daemon.Bind))
100+
}
101+
102+
options = append(options, config.OptionNeighbors(cfg.Daemon.Neighbors))
94103

95104
if cfg.Daemon.Labels != nil {
96105
options = append(options, config.OptionLabels(cfg.Daemon.Labels))

test/integration/dnet/helpers.bash

Lines changed: 76 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ function dnet_container_name() {
1010
echo dnet-$1-$2
1111
}
1212

13+
function dnet_container_ip() {
14+
docker inspect --format '{{.NetworkSettings.IPAddress}}' dnet-$1-$2
15+
}
16+
1317
function get_sbox_id() {
1418
local line
1519

@@ -18,17 +22,17 @@ function get_sbox_id() {
1822
}
1923

2024
function net_connect() {
21-
local al gl
22-
if [ -n "$4" ]; then
23-
if [ "${4}" != ":" ]; then
24-
al="--alias=${4}"
25-
fi
26-
fi
27-
if [ -n "$5" ]; then
28-
gl="--alias=${5}"
29-
fi
30-
dnet_cmd $(inst_id2port ${1}) service publish $gl ${2}.${3}
31-
dnet_cmd $(inst_id2port ${1}) service attach $al ${2} ${2}.${3}
25+
local al gl
26+
if [ -n "$4" ]; then
27+
if [ "${4}" != ":" ]; then
28+
al="--alias=${4}"
29+
fi
30+
fi
31+
if [ -n "$5" ]; then
32+
gl="--alias=${5}"
33+
fi
34+
dnet_cmd $(inst_id2port ${1}) service publish $gl ${2}.${3}
35+
dnet_cmd $(inst_id2port ${1}) service attach $al ${2} ${2}.${3}
3236
}
3337

3438
function net_disconnect() {
@@ -107,21 +111,24 @@ function parse_discovery_str() {
107111
}
108112

109113
function start_dnet() {
110-
local inst suffix name hport cport hopt store bridge_ip labels tomlfile
114+
local inst suffix name hport cport hopt store bridge_ip labels tomlfile nip
111115
local discovery provider address
112116

113117
inst=$1
114118
shift
115119
suffix=$1
116120
shift
117121

118-
stop_dnet ${inst} ${suffix}
119-
name=$(dnet_container_name ${inst} ${suffix})
122+
store=$(echo $suffix | cut -d":" -f1)
123+
nip=$(echo $suffix | cut -s -d":" -f2)
124+
125+
126+
stop_dnet ${inst} ${store}
127+
name=$(dnet_container_name ${inst} ${store})
120128

121129
hport=$((41000+${inst}-1))
122130
cport=2385
123131
hopt=""
124-
store=${suffix}
125132

126133
while [ -n "$1" ]
127134
do
@@ -138,21 +145,32 @@ function start_dnet() {
138145

139146
bridge_ip=$(get_docker_bridge_ip)
140147

141-
echo "start_dnet parsed values: " ${inst} ${suffix} ${name} ${hport} ${cport} ${hopt} ${store} ${labels}
148+
echo "start_dnet parsed values: " ${inst} ${suffix} ${name} ${hport} ${cport} ${hopt} ${store}
142149

143150
mkdir -p /tmp/dnet/${name}
144151
tomlfile="/tmp/dnet/${name}/libnetwork.toml"
145152

146153
# Try discovery URLs with or without path
154+
neigh_ip=""
155+
neighbors=""
147156
if [ "$store" = "zookeeper" ]; then
148157
read discovery provider address < <(parse_discovery_str zk://${bridge_ip}:2182)
149158
elif [ "$store" = "etcd" ]; then
150159
read discovery provider address < <(parse_discovery_str etcd://${bridge_ip}:42000/custom_prefix)
151-
else
160+
elif [ "$store" = "consul" ]; then
152161
read discovery provider address < <(parse_discovery_str consul://${bridge_ip}:8500/custom_prefix)
162+
else
163+
if [ "$nip" != "" ]; then
164+
neighbors="neighbors = [\"${nip}:7946\"]"
165+
fi
166+
167+
discovery=""
168+
provider=""
169+
address=""
153170
fi
154171

155-
cat > ${tomlfile} <<EOF
172+
if [ "$discovery" != "" ]; then
173+
cat > ${tomlfile} <<EOF
156174
title = "LibNetwork Configuration file for ${name}"
157175
158176
[daemon]
@@ -166,9 +184,22 @@ title = "LibNetwork Configuration file for ${name}"
166184
provider = "${provider}"
167185
address = "${address}"
168186
EOF
187+
else
188+
cat > ${tomlfile} <<EOF
189+
title = "LibNetwork Configuration file for ${name}"
190+
191+
[daemon]
192+
debug = false
193+
isagent = true
194+
bind = "eth0"
195+
${neighbors}
196+
EOF
197+
fi
198+
169199
cat ${tomlfile}
170200
docker run \
171201
-d \
202+
--hostname=${name} \
172203
--name=${name} \
173204
--privileged \
174205
-p ${hport}:${cport} \
@@ -183,6 +214,19 @@ EOF
183214
wait_for_dnet $(inst_id2port ${inst}) ${name}
184215
}
185216

217+
function start_ovrouter() {
218+
local name=${1}
219+
local parent=${2}
220+
221+
docker run \
222+
-d \
223+
--name=${name} \
224+
--net=container:${parent} \
225+
--volumes-from ${parent} \
226+
-w /go/src/github.com/docker/libnetwork \
227+
mrjana/golang ./cmd/ovrouter/ovrouter eth0
228+
}
229+
186230
function skip_for_circleci() {
187231
if [ -n "$CIRCLECI" ]; then
188232
skip
@@ -289,7 +333,7 @@ function test_overlay() {
289333
end=3
290334
# Setup overlay network and connect containers ot it
291335
if [ -z "${2}" -o "${2}" != "skip_add" ]; then
292-
if [ -z "${2}" -o "${2}" != "internal" ]; then
336+
if [ -z "${2}" -o "${2}" != "internal" ]; then
293337
dnet_cmd $(inst_id2port 1) network create -d overlay multihost
294338
else
295339
dnet_cmd $(inst_id2port 1) network create -d overlay --internal multihost
@@ -307,7 +351,7 @@ function test_overlay() {
307351
do
308352
if [ -z "${2}" -o "${2}" != "internal" ]; then
309353
runc $(dnet_container_name $i $dnet_suffix) $(get_sbox_id ${i} container_${i}) \
310-
"ping -c 1 www.google.com"
354+
"ping -c 1 www.google.com"
311355
else
312356
default_route=`runc $(dnet_container_name $i $dnet_suffix) $(get_sbox_id ${i} container_${i}) "ip route | grep default"`
313357
[ "$default_route" = "" ]
@@ -324,33 +368,33 @@ function test_overlay() {
324368

325369
# Setup bridge network and connect containers ot it
326370
if [ -z "${2}" -o "${2}" != "skip_add" ]; then
327-
if [ -z "${2}" -o "${2}" != "internal" ]; then
371+
if [ -z "${2}" -o "${2}" != "internal" ]; then
328372
dnet_cmd $(inst_id2port 1) network create -d bridge br1
329373
dnet_cmd $(inst_id2port 1) network create -d bridge br2
330374
net_connect ${start} container_${start} br1
331375
net_connect ${start} container_${start} br2
332376

333-
# Make sure external connectivity works
377+
# Make sure external connectivity works
334378
runc $(dnet_container_name ${start} $dnet_suffix) $(get_sbox_id ${start} container_${start}) \
335-
"ping -c 1 www.google.com"
379+
"ping -c 1 www.google.com"
336380
net_disconnect ${start} container_${start} br1
337381
net_disconnect ${start} container_${start} br2
338382

339-
# Make sure external connectivity works
383+
# Make sure external connectivity works
340384
runc $(dnet_container_name ${start} $dnet_suffix) $(get_sbox_id ${start} container_${start}) \
341-
"ping -c 1 www.google.com"
385+
"ping -c 1 www.google.com"
342386
dnet_cmd $(inst_id2port 1) network rm br1
343387
dnet_cmd $(inst_id2port 1) network rm br2
344388

345-
# Disconnect from overlay network
346-
net_disconnect ${start} container_${start} multihost
389+
# Disconnect from overlay network
390+
net_disconnect ${start} container_${start} multihost
347391

348-
# Connect to overlay network again
349-
net_connect ${start} container_${start} multihost
392+
# Connect to overlay network again
393+
net_connect ${start} container_${start} multihost
350394

351-
# Make sure external connectivity still works
352-
runc $(dnet_container_name ${start} $dnet_suffix) $(get_sbox_id ${start} container_${start}) \
353-
"ping -c 1 www.google.com"
395+
# Make sure external connectivity still works
396+
runc $(dnet_container_name ${start} $dnet_suffix) $(get_sbox_id ${start} container_${start}) \
397+
"ping -c 1 www.google.com"
354398
fi
355399
fi
356400

0 commit comments

Comments
 (0)