2015-04-16 00:58:15 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-05-04 14:49:53 -04:00
|
|
|
"fmt"
|
2015-12-23 14:47:14 -05:00
|
|
|
"log"
|
2015-05-04 14:49:53 -04:00
|
|
|
|
2015-12-23 14:47:14 -05:00
|
|
|
"github.com/docker/docker/pkg/reexec"
|
2015-04-16 00:58:15 -04:00
|
|
|
"github.com/docker/libnetwork"
|
2015-09-18 17:00:36 -04:00
|
|
|
"github.com/docker/libnetwork/config"
|
2015-05-16 19:02:51 -04:00
|
|
|
"github.com/docker/libnetwork/netlabel"
|
|
|
|
"github.com/docker/libnetwork/options"
|
2015-04-16 00:58:15 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2015-12-23 14:47:14 -05:00
|
|
|
if reexec.Init() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-04-22 19:47:07 -04:00
|
|
|
// Select and configure the network driver
|
|
|
|
networkType := "bridge"
|
2015-04-30 20:57:06 -04:00
|
|
|
|
2015-09-18 17:00:36 -04:00
|
|
|
// Create a new controller instance
|
2015-04-30 20:57:06 -04:00
|
|
|
driverOptions := options.Generic{}
|
|
|
|
genericOption := make(map[string]interface{})
|
2015-05-06 00:19:57 -04:00
|
|
|
genericOption[netlabel.GenericData] = driverOptions
|
2015-09-18 17:00:36 -04:00
|
|
|
controller, err := libnetwork.New(config.OptionDriverConfig(networkType, genericOption))
|
2015-04-16 00:58:15 -04:00
|
|
|
if err != nil {
|
2015-12-23 14:47:14 -05:00
|
|
|
log.Fatalf("libnetwork.New: %s", err)
|
2015-04-16 00:58:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a network for containers to join.
|
2015-07-02 01:00:48 -04:00
|
|
|
// NewNetwork accepts Variadic optional arguments that libnetwork and Drivers can use.
|
2016-02-29 14:49:04 -05:00
|
|
|
network, err := controller.NewNetwork(networkType, "network1", "")
|
2015-04-16 00:58:15 -04:00
|
|
|
if err != nil {
|
2015-12-23 14:47:14 -05:00
|
|
|
log.Fatalf("controller.NewNetwork: %s", err)
|
2015-04-16 00:58:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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-30 20:57:06 -04:00
|
|
|
ep, err := network.CreateEndpoint("Endpoint1")
|
2015-04-16 00:58:15 -04:00
|
|
|
if err != nil {
|
2015-12-23 14:47:14 -05:00
|
|
|
log.Fatalf("network.CreateEndpoint: %s", err)
|
2015-04-16 00:58:15 -04:00
|
|
|
}
|
|
|
|
|
2015-12-23 14:47:14 -05:00
|
|
|
// Create the sandbox for the container.
|
|
|
|
// NewSandbox accepts Variadic optional arguments which libnetwork can use.
|
2015-07-02 01:00:48 -04:00
|
|
|
sbx, err := controller.NewSandbox("container1",
|
|
|
|
libnetwork.OptionHostname("test"),
|
|
|
|
libnetwork.OptionDomainname("docker.io"))
|
2015-12-23 14:47:14 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("controller.NewSandbox: %s", err)
|
|
|
|
}
|
2015-07-02 01:00:48 -04:00
|
|
|
|
|
|
|
// A sandbox can join the endpoint via the join api.
|
|
|
|
err = ep.Join(sbx)
|
2015-04-28 01:57:36 -04:00
|
|
|
if err != nil {
|
2015-12-23 14:47:14 -05:00
|
|
|
log.Fatalf("ep.Join: %s", err)
|
2015-04-16 00:58:15 -04:00
|
|
|
}
|
2015-05-04 14:49:53 -04:00
|
|
|
|
2015-06-26 06:01:23 -04:00
|
|
|
// libnetwork client can check the endpoint's operational data via the Info() API
|
2015-05-14 02:23:45 -04:00
|
|
|
epInfo, err := ep.DriverInfo()
|
2015-12-23 14:47:14 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("ep.DriverInfo: %s", err)
|
2015-05-04 14:49:53 -04:00
|
|
|
}
|
2015-12-23 14:47:14 -05:00
|
|
|
|
|
|
|
macAddress, ok := epInfo[netlabel.MacAddress]
|
|
|
|
if !ok {
|
2016-11-21 20:29:53 -05:00
|
|
|
log.Fatal("failed to get mac address from endpoint info")
|
2015-12-23 14:47:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("Joined endpoint %s (%s) to sandbox %s (%s)\n", ep.Name(), macAddress, sbx.ContainerID(), sbx.Key())
|
2015-04-16 00:58:15 -04:00
|
|
|
}
|