2015-04-16 00:58:15 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/docker/libnetwork"
|
|
|
|
"github.com/docker/libnetwork/pkg/options"
|
|
|
|
"github.com/docker/libnetwork/sandbox"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
// Create a new controller instance
|
|
|
|
controller := libnetwork.New()
|
|
|
|
|
2015-04-22 19:47:07 -04:00
|
|
|
// Select and configure the network driver
|
|
|
|
networkType := "bridge"
|
2015-04-16 00:58:15 -04:00
|
|
|
option := options.Generic{}
|
2015-04-22 19:47:07 -04:00
|
|
|
err := controller.ConfigureNetworkDriver(networkType, option)
|
2015-04-16 00:58:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
netOptions := options.Generic{}
|
|
|
|
// Create a network for containers to join.
|
2015-04-22 19:47:07 -04:00
|
|
|
network, err := controller.NewNetwork(networkType, "network1", netOptions)
|
2015-04-16 00:58:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// For a new container: create a sandbox instance (providing a unique key).
|
|
|
|
// For linux it is a filesystem path
|
|
|
|
networkPath := "/var/lib/docker/.../4d23e"
|
|
|
|
networkNamespace, err := sandbox.NewSandbox(networkPath)
|
|
|
|
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
|
2015-04-17 18:42:23 -04:00
|
|
|
// iptables rules for port publishing. This info is contained or accessible
|
|
|
|
// from the returned endpoint.
|
2015-04-23 14:15:15 -04:00
|
|
|
ep, err := network.CreateEndpoint("Endpoint1", networkNamespace.Key(), nil)
|
2015-04-16 00:58:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add interfaces to the namespace.
|
2015-04-17 18:42:23 -04:00
|
|
|
sinfo := ep.SandboxInfo()
|
2015-04-16 00:58:15 -04:00
|
|
|
for _, iface := range sinfo.Interfaces {
|
|
|
|
if err := networkNamespace.AddInterface(iface); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the gateway IP
|
|
|
|
if err := networkNamespace.SetGateway(sinfo.Gateway); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|