Skip to content

Commit d565a4d

Browse files
committed
Push driver config during Init
Currently the driver configuration is pushed through a separate api. This makes driver configuration possible at any arbitrary time. This unncessarily complicates the driver implementation. More importantly the driver does not get access to it's configuration before it can do the handshake with libnetwork. This make the internal drivers a little bit different to external plugins which can get their configuration before the handshake with libnetwork. This PR attempts to fix that mismatch between internal drivers and external plugins. Signed-off-by: Jana Radhakrishnan <[email protected]>
1 parent cdd2ba4 commit d565a4d

25 files changed

Lines changed: 214 additions & 353 deletions

libnetwork/README.md

Lines changed: 47 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -17,61 +17,55 @@ There are many networking solutions available to suit a broad range of use-cases
1717

1818

1919
```go
20-
// Create a new controller instance
21-
controller, err := libnetwork.New()
22-
if err != nil {
23-
return
24-
}
25-
26-
// Select and configure the network driver
27-
networkType := "bridge"
28-
29-
driverOptions := options.Generic{}
30-
genericOption := make(map[string]interface{})
31-
genericOption[netlabel.GenericData] = driverOptions
32-
err = controller.ConfigureNetworkDriver(networkType, genericOption)
33-
if err != nil {
34-
return
35-
}
36-
37-
// Create a network for containers to join.
38-
// NewNetwork accepts Variadic optional arguments that libnetwork and Drivers can use.
39-
network, err := controller.NewNetwork(networkType, "network1")
40-
if err != nil {
41-
return
42-
}
43-
44-
// For each new container: allocate IP and interfaces. The returned network
45-
// settings will be used for container infos (inspect and such), as well as
46-
// iptables rules for port publishing. This info is contained or accessible
47-
// from the returned endpoint.
48-
ep, err := network.CreateEndpoint("Endpoint1")
49-
if err != nil {
50-
return
51-
}
52-
53-
// Create the sandbox for the containr.
54-
sbx, err := controller.NewSandbox("container1",
55-
libnetwork.OptionHostname("test"),
56-
libnetwork.OptionDomainname("docker.io"))
57-
58-
// A sandbox can join the endpoint via the join api.
59-
// Join accepts Variadic arguments which libnetwork and Drivers can use.
60-
err = ep.Join(sbx)
61-
if err != nil {
62-
return
63-
}
64-
65-
// libnetwork client can check the endpoint's operational data via the Info() API
66-
epInfo, err := ep.DriverInfo()
67-
mapData, ok := epInfo[netlabel.PortMap]
20+
// Select and configure the network driver
21+
networkType := "bridge"
22+
23+
// Create a new controller instance
24+
driverOptions := options.Generic{}
25+
genericOption := make(map[string]interface{})
26+
genericOption[netlabel.GenericData] = driverOptions
27+
controller, err := libnetwork.New(config.OptionDriverConfig(networkType, genericOption))
28+
if err != nil {
29+
return
30+
}
31+
32+
// Create a network for containers to join.
33+
// NewNetwork accepts Variadic optional arguments that libnetwork and Drivers can use.
34+
network, err := controller.NewNetwork(networkType, "network1")
35+
if err != nil {
36+
return
37+
}
38+
39+
// For each new container: allocate IP and interfaces. The returned network
40+
// settings will be used for container infos (inspect and such), as well as
41+
// iptables rules for port publishing. This info is contained or accessible
42+
// from the returned endpoint.
43+
ep, err := network.CreateEndpoint("Endpoint1")
44+
if err != nil {
45+
return
46+
}
47+
48+
// Create the sandbox for the containr.
49+
sbx, err := controller.NewSandbox("container1",
50+
libnetwork.OptionHostname("test"),
51+
libnetwork.OptionDomainname("docker.io"))
52+
53+
// A sandbox can join the endpoint via the join api.
54+
// Join accepts Variadic arguments which libnetwork and Drivers can use.
55+
err = ep.Join(sbx)
56+
if err != nil {
57+
return
58+
}
59+
60+
// libnetwork client can check the endpoint's operational data via the Info() API
61+
epInfo, err := ep.DriverInfo()
62+
mapData, ok := epInfo[netlabel.PortMap]
63+
if ok {
64+
portMapping, ok := mapData.([]types.PortBinding)
6865
if ok {
69-
portMapping, ok := mapData.([]types.PortBinding)
70-
if ok {
71-
fmt.Printf("Current port mapping for endpoint %s: %v", ep.Name(), portMapping)
72-
}
66+
fmt.Printf("Current port mapping for endpoint %s: %v", ep.Name(), portMapping)
7367
}
74-
68+
}
7569
```
7670
#### Current Status
7771
Please watch this space for updates on the progress.
@@ -87,4 +81,3 @@ Want to hack on libnetwork? [Docker's contributions guidelines](https://github.c
8781

8882
## Copyright and license
8983
Code and documentation copyright 2015 Docker, inc. Code released under the Apache 2.0 license. Docs released under Creative commons.
90-

libnetwork/api/api_test.go

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,6 @@ func createTestNetwork(t *testing.T, network string) (libnetwork.NetworkControll
9393
t.Fatal(err)
9494
}
9595

96-
err = c.ConfigureNetworkDriver(bridgeNetType, nil)
97-
if err != nil {
98-
t.Fatal(err)
99-
}
100-
10196
netOption := options.Generic{
10297
netlabel.GenericData: options.Generic{
10398
"BridgeName": network,
@@ -184,10 +179,6 @@ func TestCreateDeleteNetwork(t *testing.T) {
184179
if err != nil {
185180
t.Fatal(err)
186181
}
187-
err = c.ConfigureNetworkDriver(bridgeNetType, nil)
188-
if err != nil {
189-
t.Fatal(err)
190-
}
191182

192183
badBody, err := json.Marshal("bad body")
193184
if err != nil {
@@ -262,10 +253,6 @@ func TestGetNetworksAndEndpoints(t *testing.T) {
262253
if err != nil {
263254
t.Fatal(err)
264255
}
265-
err = c.ConfigureNetworkDriver(bridgeNetType, nil)
266-
if err != nil {
267-
t.Fatal(err)
268-
}
269256

270257
ops := options.Generic{
271258
netlabel.GenericData: map[string]string{
@@ -536,11 +523,6 @@ func TestProcGetServices(t *testing.T) {
536523
t.Fatal(err)
537524
}
538525

539-
err = c.ConfigureNetworkDriver(bridgeNetType, nil)
540-
if err != nil {
541-
t.Fatal(err)
542-
}
543-
544526
// Create 2 networks
545527
netName1 := "production"
546528
netOption := options.Generic{
@@ -1124,10 +1106,6 @@ func TestCreateDeleteEndpoints(t *testing.T) {
11241106
if err != nil {
11251107
t.Fatal(err)
11261108
}
1127-
err = c.ConfigureNetworkDriver(bridgeNetType, nil)
1128-
if err != nil {
1129-
t.Fatal(err)
1130-
}
11311109

11321110
nc := networkCreate{Name: "firstNet", NetworkType: bridgeNetType}
11331111
body, err := json.Marshal(nc)
@@ -1250,10 +1228,6 @@ func TestJoinLeave(t *testing.T) {
12501228
if err != nil {
12511229
t.Fatal(err)
12521230
}
1253-
err = c.ConfigureNetworkDriver(bridgeNetType, nil)
1254-
if err != nil {
1255-
t.Fatal(err)
1256-
}
12571231

12581232
nb, err := json.Marshal(networkCreate{Name: "network", NetworkType: bridgeNetType})
12591233
if err != nil {
@@ -1694,11 +1668,6 @@ func TestHttpHandlerUninit(t *testing.T) {
16941668
t.Fatal(err)
16951669
}
16961670

1697-
err = c.ConfigureNetworkDriver(bridgeNetType, nil)
1698-
if err != nil {
1699-
t.Fatal(err)
1700-
}
1701-
17021671
h := &httpHandler{c: c}
17031672
h.initRouter()
17041673
if h.r == nil {
@@ -1796,10 +1765,6 @@ func TestEndToEnd(t *testing.T) {
17961765
if err != nil {
17971766
t.Fatal(err)
17981767
}
1799-
err = c.ConfigureNetworkDriver(bridgeNetType, nil)
1800-
if err != nil {
1801-
t.Fatal(err)
1802-
}
18031768

18041769
handleRequest := NewHTTPHandler(c)
18051770

libnetwork/cmd/ovrouter/ovrouter.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,6 @@ func main() {
6565
return
6666
}
6767

68-
r := &router{}
69-
if err := overlay.Init(r); err != nil {
70-
fmt.Printf("Failed to initialize overlay driver: %v\n", err)
71-
os.Exit(1)
72-
}
73-
7468
opt := make(map[string]interface{})
7569
if len(os.Args) > 1 {
7670
opt[netlabel.OverlayBindInterface] = os.Args[1]
@@ -85,7 +79,11 @@ func main() {
8579
opt[netlabel.KVProviderURL] = os.Args[4]
8680
}
8781

88-
r.d.Config(opt)
82+
r := &router{}
83+
if err := overlay.Init(r, opt); err != nil {
84+
fmt.Printf("Failed to initialize overlay driver: %v\n", err)
85+
os.Exit(1)
86+
}
8987

9088
if err := r.d.CreateNetwork("testnetwork",
9189
map[string]interface{}{}); err != nil {

libnetwork/cmd/readme_test/readme.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,21 @@ import (
44
"fmt"
55

66
"github.com/docker/libnetwork"
7+
"github.com/docker/libnetwork/config"
78
"github.com/docker/libnetwork/netlabel"
89
"github.com/docker/libnetwork/options"
910
"github.com/docker/libnetwork/types"
1011
)
1112

1213
func main() {
13-
// Create a new controller instance
14-
controller, err := libnetwork.New()
15-
if err != nil {
16-
return
17-
}
18-
1914
// Select and configure the network driver
2015
networkType := "bridge"
2116

17+
// Create a new controller instance
2218
driverOptions := options.Generic{}
2319
genericOption := make(map[string]interface{})
2420
genericOption[netlabel.GenericData] = driverOptions
25-
err = controller.ConfigureNetworkDriver(networkType, genericOption)
21+
controller, err := libnetwork.New(config.OptionDriverConfig(networkType, genericOption))
2622
if err != nil {
2723
return
2824
}

libnetwork/cmd/test/libnetwork.toml

Lines changed: 0 additions & 12 deletions
This file was deleted.

libnetwork/cmd/test/main.go

Lines changed: 0 additions & 49 deletions
This file was deleted.

libnetwork/config/config.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type DaemonCfg struct {
2121
DefaultNetwork string
2222
DefaultDriver string
2323
Labels []string
24+
DriverCfg map[string]interface{}
2425
}
2526

2627
// ClusterCfg represents cluster configuration
@@ -71,6 +72,13 @@ func OptionDefaultDriver(dd string) Option {
7172
}
7273
}
7374

75+
// OptionDriverConfig returns an option setter for driver configuration.
76+
func OptionDriverConfig(networkType string, config map[string]interface{}) Option {
77+
return func(c *Config) {
78+
c.Daemon.DriverCfg[networkType] = config
79+
}
80+
}
81+
7482
// OptionLabels function returns an option setter for labels
7583
func OptionLabels(labels []string) Option {
7684
return func(c *Config) {

0 commit comments

Comments
 (0)