1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00
moby--moby/libnetwork/cmd/readme_test
Alessandro Boch 1fae5573d2 Refactor NetworkController interface
- To reflect work flow. NewDriver() => ConfigureDriver()
  and no NetworkDriver returned.
  libnetwork clients would refer to a driver/network type, then
  internally controller will retrieve the correspondent driver
  instance, but this is not a concern of the clients.
- Remove NetworkDriver interface
- Removed stale blank dependency on bridge in libnetwork_test.go

Signed-off-by: Alessandro Boch <aboch@docker.com>
2015-04-23 18:46:01 -07:00
..
readme.go Refactor NetworkController interface 2015-04-23 18:46:01 -07: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()

	// Select and configure the network driver
	networkType := "bridge"
	option := options.Generic{}
	err := controller.ConfigureNetworkDriver(networkType, option)
	if err != nil {
		return
	}

	netOptions := options.Generic{}
	// Create a network for containers to join.
	network, err := controller.NewNetwork(networkType, "network1", netOptions)
	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
	// iptables rules for port publishing. This info is contained or accessible
	// from the returned endpoint.
	ep, err := network.CreateEndpoint("Endpoint1", networkNamespace.Key(), nil)
	if err != nil {
		return
	}

	// Add interfaces to the namespace.
	sinfo := ep.SandboxInfo()
	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
	}
}