Skip to content

Commit

Permalink
Push driver config during Init
Browse files Browse the repository at this point in the history
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 <mrjana@docker.com>
  • Loading branch information
mrjana committed Sep 19, 2015
1 parent 00a92f0 commit 80b6402
Show file tree
Hide file tree
Showing 22 changed files with 193 additions and 326 deletions.
101 changes: 47 additions & 54 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,61 +17,55 @@ There are many networking solutions available to suit a broad range of use-cases


```go
// Create a new controller instance
controller, err := libnetwork.New()
if err != nil {
return
}

// Select and configure the network driver
networkType := "bridge"

driverOptions := options.Generic{}
genericOption := make(map[string]interface{})
genericOption[netlabel.GenericData] = driverOptions
err = controller.ConfigureNetworkDriver(networkType, genericOption)
if err != nil {
return
}

// Create a network for containers to join.
// NewNetwork accepts Variadic optional arguments that libnetwork and Drivers can use.
network, err := controller.NewNetwork(networkType, "network1")
if err != nil {
return
}

// For each new container: allocate IP and interfaces. The returned network
// settings will be used for container infos (inspect and such), as well as
// iptables rules for port publishing. This info is contained or accessible
// from the returned endpoint.
ep, err := network.CreateEndpoint("Endpoint1")
if err != nil {
return
}

// Create the sandbox for the containr.
sbx, err := controller.NewSandbox("container1",
libnetwork.OptionHostname("test"),
libnetwork.OptionDomainname("docker.io"))

// A sandbox can join the endpoint via the join api.
// Join accepts Variadic arguments which libnetwork and Drivers can use.
err = ep.Join(sbx)
if err != nil {
return
}

// libnetwork client can check the endpoint's operational data via the Info() API
epInfo, err := ep.DriverInfo()
mapData, ok := epInfo[netlabel.PortMap]
// Select and configure the network driver
networkType := "bridge"

// Create a new controller instance
driverOptions := options.Generic{}
genericOption := make(map[string]interface{})
genericOption[netlabel.GenericData] = driverOptions
controller, err := libnetwork.New(config.OptionDriverConfig(networkType, genericOption))
if err != nil {
return
}

// Create a network for containers to join.
// NewNetwork accepts Variadic optional arguments that libnetwork and Drivers can use.
network, err := controller.NewNetwork(networkType, "network1")
if err != nil {
return
}

// For each new container: allocate IP and interfaces. The returned network
// settings will be used for container infos (inspect and such), as well as
// iptables rules for port publishing. This info is contained or accessible
// from the returned endpoint.
ep, err := network.CreateEndpoint("Endpoint1")
if err != nil {
return
}

// Create the sandbox for the containr.
sbx, err := controller.NewSandbox("container1",
libnetwork.OptionHostname("test"),
libnetwork.OptionDomainname("docker.io"))

// A sandbox can join the endpoint via the join api.
// Join accepts Variadic arguments which libnetwork and Drivers can use.
err = ep.Join(sbx)
if err != nil {
return
}

// libnetwork client can check the endpoint's operational data via the Info() API
epInfo, err := ep.DriverInfo()
mapData, ok := epInfo[netlabel.PortMap]
if ok {
portMapping, ok := mapData.([]types.PortBinding)
if ok {
portMapping, ok := mapData.([]types.PortBinding)
if ok {
fmt.Printf("Current port mapping for endpoint %s: %v", ep.Name(), portMapping)
}
fmt.Printf("Current port mapping for endpoint %s: %v", ep.Name(), portMapping)
}

}
```
#### Current Status
Please watch this space for updates on the progress.
Expand All @@ -87,4 +81,3 @@ Want to hack on libnetwork? [Docker's contributions guidelines](https://github.c

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

35 changes: 0 additions & 35 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,6 @@ func createTestNetwork(t *testing.T, network string) (libnetwork.NetworkControll
t.Fatal(err)
}

err = c.ConfigureNetworkDriver(bridgeNetType, nil)
if err != nil {
t.Fatal(err)
}

netOption := options.Generic{
netlabel.GenericData: options.Generic{
"BridgeName": network,
Expand Down Expand Up @@ -184,10 +179,6 @@ func TestCreateDeleteNetwork(t *testing.T) {
if err != nil {
t.Fatal(err)
}
err = c.ConfigureNetworkDriver(bridgeNetType, nil)
if err != nil {
t.Fatal(err)
}

badBody, err := json.Marshal("bad body")
if err != nil {
Expand Down Expand Up @@ -262,10 +253,6 @@ func TestGetNetworksAndEndpoints(t *testing.T) {
if err != nil {
t.Fatal(err)
}
err = c.ConfigureNetworkDriver(bridgeNetType, nil)
if err != nil {
t.Fatal(err)
}

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

err = c.ConfigureNetworkDriver(bridgeNetType, nil)
if err != nil {
t.Fatal(err)
}

// Create 2 networks
netName1 := "production"
netOption := options.Generic{
Expand Down Expand Up @@ -1124,10 +1106,6 @@ func TestCreateDeleteEndpoints(t *testing.T) {
if err != nil {
t.Fatal(err)
}
err = c.ConfigureNetworkDriver(bridgeNetType, nil)
if err != nil {
t.Fatal(err)
}

nc := networkCreate{Name: "firstNet", NetworkType: bridgeNetType}
body, err := json.Marshal(nc)
Expand Down Expand Up @@ -1250,10 +1228,6 @@ func TestJoinLeave(t *testing.T) {
if err != nil {
t.Fatal(err)
}
err = c.ConfigureNetworkDriver(bridgeNetType, nil)
if err != nil {
t.Fatal(err)
}

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

err = c.ConfigureNetworkDriver(bridgeNetType, nil)
if err != nil {
t.Fatal(err)
}

h := &httpHandler{c: c}
h.initRouter()
if h.r == nil {
Expand Down Expand Up @@ -1796,10 +1765,6 @@ func TestEndToEnd(t *testing.T) {
if err != nil {
t.Fatal(err)
}
err = c.ConfigureNetworkDriver(bridgeNetType, nil)
if err != nil {
t.Fatal(err)
}

handleRequest := NewHTTPHandler(c)

Expand Down
12 changes: 5 additions & 7 deletions cmd/ovrouter/ovrouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,6 @@ func main() {
return
}

r := &router{}
if err := overlay.Init(r); err != nil {
fmt.Printf("Failed to initialize overlay driver: %v\n", err)
os.Exit(1)
}

opt := make(map[string]interface{})
if len(os.Args) > 1 {
opt[netlabel.OverlayBindInterface] = os.Args[1]
Expand All @@ -85,7 +79,11 @@ func main() {
opt[netlabel.KVProviderURL] = os.Args[4]
}

r.d.Config(opt)
r := &router{}
if err := overlay.Init(r, opt); err != nil {
fmt.Printf("Failed to initialize overlay driver: %v\n", err)
os.Exit(1)
}

if err := r.d.CreateNetwork("testnetwork",
map[string]interface{}{}); err != nil {
Expand Down
10 changes: 3 additions & 7 deletions cmd/readme_test/readme.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,21 @@ import (
"fmt"

"github.com/docker/libnetwork"
"github.com/docker/libnetwork/config"
"github.com/docker/libnetwork/netlabel"
"github.com/docker/libnetwork/options"
"github.com/docker/libnetwork/types"
)

func main() {
// Create a new controller instance
controller, err := libnetwork.New()
if err != nil {
return
}

// Select and configure the network driver
networkType := "bridge"

// Create a new controller instance
driverOptions := options.Generic{}
genericOption := make(map[string]interface{})
genericOption[netlabel.GenericData] = driverOptions
err = controller.ConfigureNetworkDriver(networkType, genericOption)
controller, err := libnetwork.New(config.OptionDriverConfig(networkType, genericOption))
if err != nil {
return
}
Expand Down
12 changes: 0 additions & 12 deletions cmd/test/libnetwork.toml

This file was deleted.

49 changes: 0 additions & 49 deletions cmd/test/main.go

This file was deleted.

8 changes: 8 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type DaemonCfg struct {
DefaultNetwork string
DefaultDriver string
Labels []string
DriverCfg map[string]interface{}
}

// ClusterCfg represents cluster configuration
Expand Down Expand Up @@ -71,6 +72,13 @@ func OptionDefaultDriver(dd string) Option {
}
}

// OptionDriverConfig returns an option setter for driver configuration.
func OptionDriverConfig(networkType string, config map[string]interface{}) Option {
return func(c *Config) {
c.Daemon.DriverCfg[networkType] = config
}
}

// OptionLabels function returns an option setter for labels
func OptionLabels(labels []string) Option {
return func(c *Config) {
Expand Down
Loading

0 comments on commit 80b6402

Please sign in to comment.