2015-02-20 01:21:42 +00:00
|
|
|
// Package libnetwork provides basic fonctionalities and extension points to
|
|
|
|
// create network namespaces and allocate interfaces for containers to use.
|
|
|
|
//
|
|
|
|
// // Create a network for containers to join.
|
|
|
|
// network, err := libnetwork.NewNetwork("simplebridge", &Options{})
|
|
|
|
// if err != nil {
|
|
|
|
// return err
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// // For a new container: create network namespace (providing the path).
|
|
|
|
// networkPath := "/var/lib/docker/.../4d23e"
|
2015-02-27 17:34:30 +00:00
|
|
|
// networkNamespace, err := libnetwork.NewNetworkNamespace(networkPath)
|
2015-02-20 01:21:42 +00:00
|
|
|
// if err != nil {
|
|
|
|
// return err
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// // 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.
|
2015-02-25 02:41:17 +00:00
|
|
|
// interfaces, err := network.Link(containerID)
|
2015-02-20 01:21:42 +00:00
|
|
|
// if err != nil {
|
|
|
|
// return err
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// // Add interfaces to the namespace.
|
|
|
|
// for _, interface := range interfaces {
|
|
|
|
// if err := networkNamespace.AddInterface(interface); err != nil {
|
|
|
|
// return err
|
|
|
|
// }
|
|
|
|
// }
|
2015-02-20 06:21:05 +00:00
|
|
|
//
|
2015-02-20 01:21:42 +00:00
|
|
|
package libnetwork
|
|
|
|
|
2015-02-27 17:34:30 +00:00
|
|
|
// Interface represents the settings and identity of a network device. It is
|
|
|
|
// used as a return type for Network.Link, and it is common practice for the
|
|
|
|
// caller to use this information when moving interface SrcName from host
|
|
|
|
// namespace to DstName in a different net namespace with the appropriate
|
|
|
|
// network settings.
|
2015-02-20 01:21:42 +00:00
|
|
|
type Interface struct {
|
|
|
|
// The name of the interface in the origin network namespace.
|
|
|
|
SrcName string
|
|
|
|
|
|
|
|
// The name that will be assigned to the interface once moves inside a
|
|
|
|
// network namespace.
|
|
|
|
DstName string
|
|
|
|
|
2015-02-27 17:34:30 +00:00
|
|
|
// IPv4 address for the interface.
|
|
|
|
Address string
|
|
|
|
|
|
|
|
// IPv6 address for the interface.
|
2015-02-20 01:21:42 +00:00
|
|
|
AddressIPv6 string
|
2015-02-27 17:34:30 +00:00
|
|
|
|
|
|
|
// IPv4 gateway for the interface.
|
|
|
|
Gateway string
|
|
|
|
|
|
|
|
// IPv6 gateway for the interface.
|
2015-02-20 01:21:42 +00:00
|
|
|
GatewayIPv6 string
|
|
|
|
}
|
|
|
|
|
2015-03-02 18:17:12 +00:00
|
|
|
// A Network represents a logical connectivity zone that containers may
|
|
|
|
// ulteriorly join using the Link method. A Network is managed by a specific
|
|
|
|
// driver.
|
|
|
|
type Network interface {
|
|
|
|
// A user chosen name for this network.
|
|
|
|
Name() string
|
|
|
|
|
|
|
|
// The type of network, which corresponds to its managing driver.
|
|
|
|
Type() string
|
|
|
|
|
|
|
|
// Create a new link to this network symbolically identified by the
|
|
|
|
// specified unique name.
|
|
|
|
Link(name string) ([]*Interface, error)
|
2015-03-20 20:54:49 +00:00
|
|
|
|
|
|
|
Delete() error
|
2015-03-02 18:17:12 +00:00
|
|
|
}
|
|
|
|
|
2015-02-27 17:34:30 +00:00
|
|
|
// Namespace represents a network namespace, mounted on a specific Path. It
|
|
|
|
// holds a list of Interface, and more can be added dynamically.
|
2015-02-20 01:21:42 +00:00
|
|
|
type Namespace interface {
|
2015-02-27 17:34:30 +00:00
|
|
|
// The path where the network namespace is mounted.
|
2015-02-20 01:21:42 +00:00
|
|
|
Path() string
|
2015-02-27 17:34:30 +00:00
|
|
|
|
|
|
|
// The collection of Interface previously added with the AddInterface
|
|
|
|
// method. Note that this doesn't incude network interfaces added in any
|
|
|
|
// other way (such as the default loopback interface existing in any newly
|
|
|
|
// created network namespace).
|
2015-02-20 01:21:42 +00:00
|
|
|
Interfaces() []*Interface
|
2015-02-27 17:34:30 +00:00
|
|
|
|
|
|
|
// Add an existing Interface to this namespace. The operation will rename
|
|
|
|
// from the Interface SrcName to DstName as it moves, and reconfigure the
|
|
|
|
// interface according to the specified settings.
|
2015-02-20 01:21:42 +00:00
|
|
|
AddInterface(*Interface) error
|
|
|
|
}
|
|
|
|
|
2015-03-04 21:25:43 +00:00
|
|
|
// NewNetwork creates a new network of the specified networkType. The options
|
|
|
|
// are driver specific and modeled in a generic way.
|
2015-03-02 18:17:12 +00:00
|
|
|
func NewNetwork(networkType, name string, options DriverParams) (Network, error) {
|
|
|
|
return createNetwork(networkType, name, options)
|
2015-02-20 01:21:42 +00:00
|
|
|
}
|
2015-02-27 17:34:30 +00:00
|
|
|
|
2015-03-04 21:25:43 +00:00
|
|
|
// NewNetworkNamespace creates a new network namespace mounted on the specified
|
|
|
|
// path.
|
2015-02-27 17:34:30 +00:00
|
|
|
func NewNetworkNamespace(path string) (Namespace, error) {
|
|
|
|
return createNetworkNamespace(path)
|
|
|
|
}
|