libnetwork

package
v0.0.0-10-16-2015.09-0... Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 16, 2015 License: Apache-2.0, Apache-2.0 Imports: 34 Imported by: 0

README

libnetwork - networking for containers

Circle CI Coverage Status GoDoc

Libnetwork provides a native Go implementation for connecting containers

The goal of libnetwork is to deliver a robust Container Network Model that provides a consistent programming interface and the required network abstractions for applications.

NOTE: libnetwork project is under heavy development and is not ready for general use.

Design

Please refer to the design for more information.

Using libnetwork

There are many networking solutions available to suit a broad range of use-cases. libnetwork uses a driver / plugin model to support all of these solutions while abstracting the complexity of the driver implementations by exposing a simple and consistent Network Model to users.

	// 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 container.
	// NewSandbox accepts Variadic optional arguments which libnetwork can use.
	sbx, err := controller.NewSandbox("container1",
		libnetwork.OptionHostname("test"),
		libnetwork.OptionDomainname("docker.io"))

	// A sandbox can join the endpoint via the join api.
	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 {
			fmt.Printf("Current port mapping for endpoint %s: %v", ep.Name(), portMapping)
		}
	}
Current Status

Please watch this space for updates on the progress.

Currently libnetwork is nothing more than an attempt to modularize the Docker platform's networking subsystem by moving it into libnetwork as a library.

Future

Please refer to roadmap for more information.

Contributing

Want to hack on libnetwork? Docker's contributions guidelines apply.

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

Documentation

Overview

Package libnetwork provides the basic functionality and extension points to create network namespaces and allocate interfaces for containers to use.

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 make use of
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 container.
// NewSandbox accepts Variadic optional arguments which libnetwork can use.
sbx, err := controller.NewSandbox("container1",
	libnetwork.OptionHostname("test"),
	libnetwork.OptionDomainname("docker.io"))

// A sandbox can join the endpoint via the join api.
err = ep.Join(sbx)
if err != nil {
	return
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetExternalKey

func SetExternalKey(controllerID string, containerID string, key string) error

SetExternalKey provides a convenient way to set an External key to a sandbox

Types

type ActiveContainerError

type ActiveContainerError struct {
	// contains filtered or unexported fields
}

ActiveContainerError is returned when an endpoint is deleted which has active containers attached to it.

func (*ActiveContainerError) Error

func (ace *ActiveContainerError) Error() string

func (*ActiveContainerError) Forbidden

func (ace *ActiveContainerError) Forbidden()

Forbidden denotes the type of this error

type ActiveEndpointsError

type ActiveEndpointsError struct {
	// contains filtered or unexported fields
}

ActiveEndpointsError is returned when a network is deleted which has active endpoints in it.

func (*ActiveEndpointsError) Error

func (aee *ActiveEndpointsError) Error() string

func (*ActiveEndpointsError) Forbidden

func (aee *ActiveEndpointsError) Forbidden()

Forbidden denotes the type of this error

type Endpoint

type Endpoint interface {
	// A system generated id for this endpoint.
	ID() string

	// Name returns the name of this endpoint.
	Name() string

	// Network returns the name of the network to which this endpoint is attached.
	Network() string

	// Join joins the sandbox to the endpoint and populates into the sandbox
	// the network resources allocated for the endpoint.
	Join(sandbox Sandbox, options ...EndpointOption) error

	// Leave detaches the network resources populated in the sandbox.
	Leave(sandbox Sandbox, options ...EndpointOption) error

	// Return certain operational data belonging to this endpoint
	Info() EndpointInfo

	// DriverInfo returns a collection of driver operational data related to this endpoint retrieved from the driver
	DriverInfo() (map[string]interface{}, error)

	// Delete and detaches this endpoint from the network.
	Delete() error
}

Endpoint represents a logical connection between a network and a sandbox.

type EndpointInfo

type EndpointInfo interface {
	// Iface returns InterfaceInfo, go interface that can be used
	// to get more information on the interface which was assigned to
	// the endpoint by the driver. This can be used after the
	// endpoint has been created.
	Iface() InterfaceInfo

	// Gateway returns the IPv4 gateway assigned by the driver.
	// This will only return a valid value if a container has joined the endpoint.
	Gateway() net.IP

	// GatewayIPv6 returns the IPv6 gateway assigned by the driver.
	// This will only return a valid value if a container has joined the endpoint.
	GatewayIPv6() net.IP

	// Sandbox returns the attached sandbox if there, nil otherwise.
	Sandbox() Sandbox
}

EndpointInfo provides an interface to retrieve network resources bound to the endpoint.

type EndpointOption

type EndpointOption func(ep *endpoint)

EndpointOption is a option setter function type used to pass varios options to Network and Endpoint interfaces methods. The various setter functions of type EndpointOption are provided by libnetwork, they look like <Create|Join|Leave>Option[...](...)

func CreateOptionExposedPorts

func CreateOptionExposedPorts(exposedPorts []types.TransportPort) EndpointOption

CreateOptionExposedPorts function returns an option setter for the container exposed ports option to be passed to network.CreateEndpoint() method.

func CreateOptionPortMapping

func CreateOptionPortMapping(portBindings []types.PortBinding) EndpointOption

CreateOptionPortMapping function returns an option setter for the mapping ports option to be passed to network.CreateEndpoint() method.

func EndpointOptionGeneric

func EndpointOptionGeneric(generic map[string]interface{}) EndpointOption

EndpointOptionGeneric function returns an option setter for a Generic option defined in a Dictionary of Key-Value pair

func JoinOptionPriority

func JoinOptionPriority(ep Endpoint, prio int) EndpointOption

JoinOptionPriority function returns an option setter for priority option to be passed to the endpoint.Join() method.

type EndpointWalker

type EndpointWalker func(ep Endpoint) bool

EndpointWalker is a client provided function which will be used to walk the Endpoints. When the function returns true, the walk will stop.

type ErrInvalidConfigFile

type ErrInvalidConfigFile string

ErrInvalidConfigFile type is returned when an invalid LibNetwork config file is detected

func (ErrInvalidConfigFile) Error

func (cf ErrInvalidConfigFile) Error() string

type ErrInvalidID

type ErrInvalidID string

ErrInvalidID is returned when a query-by-id method is being invoked with an empty id parameter

func (ErrInvalidID) BadRequest

func (ii ErrInvalidID) BadRequest()

BadRequest denotes the type of this error

func (ErrInvalidID) Error

func (ii ErrInvalidID) Error() string

type ErrInvalidJoin

type ErrInvalidJoin struct{}

ErrInvalidJoin is returned if a join is attempted on an endpoint which already has a container joined.

func (ErrInvalidJoin) BadRequest

func (ij ErrInvalidJoin) BadRequest()

BadRequest denotes the type of this error

func (ErrInvalidJoin) Error

func (ij ErrInvalidJoin) Error() string

type ErrInvalidName

type ErrInvalidName string

ErrInvalidName is returned when a query-by-name or resource create method is invoked with an empty name parameter

func (ErrInvalidName) BadRequest

func (in ErrInvalidName) BadRequest()

BadRequest denotes the type of this error

func (ErrInvalidName) Error

func (in ErrInvalidName) Error() string

type ErrInvalidNetworkDriver

type ErrInvalidNetworkDriver string

ErrInvalidNetworkDriver is returned if an invalid driver name is passed.

func (ErrInvalidNetworkDriver) BadRequest

func (ind ErrInvalidNetworkDriver) BadRequest()

BadRequest denotes the type of this error

func (ErrInvalidNetworkDriver) Error

func (ind ErrInvalidNetworkDriver) Error() string

type ErrNoContainer

type ErrNoContainer struct{}

ErrNoContainer is returned when the endpoint has no container attached to it.

func (ErrNoContainer) Error

func (nc ErrNoContainer) Error() string

func (ErrNoContainer) Maskable

func (nc ErrNoContainer) Maskable()

Maskable denotes the type of this error

type ErrNoSuchEndpoint

type ErrNoSuchEndpoint string

ErrNoSuchEndpoint is returned when a endpoint query finds no result

func (ErrNoSuchEndpoint) Error

func (nse ErrNoSuchEndpoint) Error() string

func (ErrNoSuchEndpoint) NotFound

func (nse ErrNoSuchEndpoint) NotFound()

NotFound denotes the type of this error

type ErrNoSuchNetwork

type ErrNoSuchNetwork string

ErrNoSuchNetwork is returned when a network query finds no result

func (ErrNoSuchNetwork) Error

func (nsn ErrNoSuchNetwork) Error() string

func (ErrNoSuchNetwork) NotFound

func (nsn ErrNoSuchNetwork) NotFound()

NotFound denotes the type of this error

type InterfaceInfo

type InterfaceInfo interface {
	// MacAddress returns the MAC address assigned to the endpoint.
	MacAddress() net.HardwareAddr

	// Address returns the IPv4 address assigned to the endpoint.
	Address() net.IPNet

	// AddressIPv6 returns the IPv6 address assigned to the endpoint.
	AddressIPv6() net.IPNet
}

InterfaceInfo provides an interface to retrieve interface addresses bound to the endpoint.

type InvalidContainerIDError

type InvalidContainerIDError string

InvalidContainerIDError is returned when an invalid container id is passed in Join/Leave

func (InvalidContainerIDError) BadRequest

func (id InvalidContainerIDError) BadRequest()

BadRequest denotes the type of this error

func (InvalidContainerIDError) Error

func (id InvalidContainerIDError) Error() string

type Network

type Network interface {
	// A user chosen name for this network.
	Name() string

	// A system generated id for this network.
	ID() string

	// The type of network, which corresponds to its managing driver.
	Type() string

	// Create a new endpoint to this network symbolically identified by the
	// specified unique name. The options parameter carry driver specific options.
	// Labels support will be added in the near future.
	CreateEndpoint(name string, options ...EndpointOption) (Endpoint, error)

	// Delete the network.
	Delete() error

	// Endpoints returns the list of Endpoint(s) in this network.
	Endpoints() []Endpoint

	// WalkEndpoints uses the provided function to walk the Endpoints
	WalkEndpoints(walker EndpointWalker)

	// EndpointByName returns the Endpoint which has the passed name. If not found, the error ErrNoSuchEndpoint is returned.
	EndpointByName(name string) (Endpoint, error)

	// EndpointByID returns the Endpoint which has the passed id. If not found, the error ErrNoSuchEndpoint is returned.
	EndpointByID(id string) (Endpoint, error)
}

A Network represents a logical connectivity zone that containers may join using the Link method. A Network is managed by a specific driver.

type NetworkController

type NetworkController interface {
	// ID provides an unique identity for the controller
	ID() string

	// Config method returns the bootup configuration for the controller
	Config() config.Config

	// Create a new network. The options parameter carries network specific options.
	// Labels support will be added in the near future.
	NewNetwork(networkType, name string, options ...NetworkOption) (Network, error)

	// Networks returns the list of Network(s) managed by this controller.
	Networks() []Network

	// WalkNetworks uses the provided function to walk the Network(s) managed by this controller.
	WalkNetworks(walker NetworkWalker)

	// NetworkByName returns the Network which has the passed name. If not found, the error ErrNoSuchNetwork is returned.
	NetworkByName(name string) (Network, error)

	// NetworkByID returns the Network which has the passed id. If not found, the error ErrNoSuchNetwork is returned.
	NetworkByID(id string) (Network, error)

	// NewSandbox cretes a new network sandbox for the passed container id
	NewSandbox(containerID string, options ...SandboxOption) (Sandbox, error)

	// Sandboxes returns the list of Sandbox(s) managed by this controller.
	Sandboxes() []Sandbox

	// WlakSandboxes uses the provided function to walk the Sandbox(s) managed by this controller.
	WalkSandboxes(walker SandboxWalker)

	// SandboxByID returns the Sandbox which has the passed id. If not found, a types.NotFoundError is returned.
	SandboxByID(id string) (Sandbox, error)

	// Stop network controller
	Stop()
}

NetworkController provides the interface for controller instance which manages networks.

func New

func New(cfgOptions ...config.Option) (NetworkController, error)

New creates a new instance of network controller.

type NetworkNameError

type NetworkNameError string

NetworkNameError is returned when a network with the same name already exists.

func (NetworkNameError) Error

func (nnr NetworkNameError) Error() string

func (NetworkNameError) Forbidden

func (nnr NetworkNameError) Forbidden()

Forbidden denotes the type of this error

type NetworkOption

type NetworkOption func(n *network)

NetworkOption is a option setter function type used to pass varios options to NewNetwork method. The various setter functions of type NetworkOption are provided by libnetwork, they look like NetworkOptionXXXX(...)

func NetworkOptionGeneric

func NetworkOptionGeneric(generic map[string]interface{}) NetworkOption

NetworkOptionGeneric function returns an option setter for a Generic option defined in a Dictionary of Key-Value pair

func NetworkOptionPersist

func NetworkOptionPersist(persist bool) NetworkOption

NetworkOptionPersist returns an option setter to set persistence policy for a network

type NetworkTypeError

type NetworkTypeError string

NetworkTypeError type is returned when the network type string is not known to libnetwork.

func (NetworkTypeError) Error

func (nt NetworkTypeError) Error() string

func (NetworkTypeError) NotFound

func (nt NetworkTypeError) NotFound()

NotFound denotes the type of this error

type NetworkWalker

type NetworkWalker func(nw Network) bool

NetworkWalker is a client provided function which will be used to walk the Networks. When the function returns true, the walk will stop.

type Sandbox

type Sandbox interface {
	// ID returns the ID of the sandbox
	ID() string
	// Key returns the sandbox's key
	Key() string
	// ContainerID returns the container id associated to this sandbox
	ContainerID() string
	// Labels returns the sandbox's labels
	Labels() map[string]interface{}
	// Statistics retrieves the interfaces' statistics for the sandbox
	Statistics() (map[string]*osl.InterfaceStatistics, error)
	// Refresh leaves all the endpoints, resets and re-apply the options,
	// re-joins all the endpoints without destroying the osl sandbox
	Refresh(options ...SandboxOption) error
	// SetKey updates the Sandbox Key
	SetKey(key string) error
	// Delete destroys this container after detaching it from all connected endpoints.
	Delete() error
}

Sandbox provides the control over the network container entity. It is a one to one mapping with the container.

type SandboxOption

type SandboxOption func(sb *sandbox)

SandboxOption is a option setter function type used to pass varios options to NewNetContainer method. The various setter functions of type SandboxOption are provided by libnetwork, they look like ContainerOptionXXXX(...)

func OptionDNS

func OptionDNS(dns string) SandboxOption

OptionDNS function returns an option setter for dns entry option to be passed to container Create method.

func OptionDNSOptions

func OptionDNSOptions(options string) SandboxOption

OptionDNSOptions function returns an option setter for dns options entry option to be passed to container Create method.

func OptionDNSSearch

func OptionDNSSearch(search string) SandboxOption

OptionDNSSearch function returns an option setter for dns search entry option to be passed to container Create method.

func OptionDomainname

func OptionDomainname(name string) SandboxOption

OptionDomainname function returns an option setter for domainname option to be passed to NewSandbox method.

func OptionExtraHost

func OptionExtraHost(name string, IP string) SandboxOption

OptionExtraHost function returns an option setter for extra /etc/hosts options which is a name and IP as strings.

func OptionGeneric

func OptionGeneric(generic map[string]interface{}) SandboxOption

OptionGeneric function returns an option setter for Generic configuration that is not managed by libNetwork but can be used by the Drivers during the call to net container creation method. Container Labels are a good example.

func OptionHostname

func OptionHostname(name string) SandboxOption

OptionHostname function returns an option setter for hostname option to be passed to NewSandbox method.

func OptionHostsPath

func OptionHostsPath(path string) SandboxOption

OptionHostsPath function returns an option setter for hostspath option to be passed to NewSandbox method.

func OptionOriginHostsPath

func OptionOriginHostsPath(path string) SandboxOption

OptionOriginHostsPath function returns an option setter for origin hosts file path tbeo passed to NewSandbox method.

func OptionOriginResolvConfPath

func OptionOriginResolvConfPath(path string) SandboxOption

OptionOriginResolvConfPath function returns an option setter to set the path to the origin resolv.conf file to be passed to net container methods.

func OptionParentUpdate

func OptionParentUpdate(cid string, name, ip string) SandboxOption

OptionParentUpdate function returns an option setter for parent container which needs to update the IP address for the linked container.

func OptionResolvConfPath

func OptionResolvConfPath(path string) SandboxOption

OptionResolvConfPath function returns an option setter for resolvconfpath option to be passed to net container methods.

func OptionUseDefaultSandbox

func OptionUseDefaultSandbox() SandboxOption

OptionUseDefaultSandbox function returns an option setter for using default sandbox to be passed to container Create method.

func OptionUseExternalKey

func OptionUseExternalKey() SandboxOption

OptionUseExternalKey function returns an option setter for using provided namespace instead of creating one.

type SandboxWalker

type SandboxWalker func(sb Sandbox) bool

SandboxWalker is a client provided function which will be used to walk the Sandboxes. When the function returns true, the walk will stop.

func SandboxContainerWalker

func SandboxContainerWalker(out *Sandbox, containerID string) SandboxWalker

SandboxContainerWalker returns a Sandbox Walker function which looks for an existing Sandbox with the passed containerID

func SandboxKeyWalker

func SandboxKeyWalker(out *Sandbox, key string) SandboxWalker

SandboxKeyWalker returns a Sandbox Walker function which looks for an existing Sandbox with the passed key

type UnknownEndpointError

type UnknownEndpointError struct {
	// contains filtered or unexported fields
}

UnknownEndpointError is returned when libnetwork could not find in it's database an endpoint with the same name and id.

func (*UnknownEndpointError) Error

func (uee *UnknownEndpointError) Error() string

func (*UnknownEndpointError) NotFound

func (uee *UnknownEndpointError) NotFound()

NotFound denotes the type of this error

type UnknownNetworkError

type UnknownNetworkError struct {
	// contains filtered or unexported fields
}

UnknownNetworkError is returned when libnetwork could not find in it's database a network with the same name and id.

func (*UnknownNetworkError) Error

func (une *UnknownNetworkError) Error() string

func (*UnknownNetworkError) NotFound

func (une *UnknownNetworkError) NotFound()

NotFound denotes the type of this error

Directories

Path Synopsis
Package bitseq provides a structure and utilities for representing long bitmask as sequence of run-lenght encoded blocks.
Package bitseq provides a structure and utilities for representing long bitmask as sequence of run-lenght encoded blocks.
cmd
drivers
remote/api
Package api represents all requests and responses suitable for conversation with a remote driver.
Package api represents all requests and responses suitable for conversation with a remote driver.
Package idm manages reservation/release of numerical ids from a configured set of contiguous ids
Package idm manages reservation/release of numerical ids from a configured set of contiguous ids
Package ipallocator defines the default IP allocator.
Package ipallocator defines the default IP allocator.
Package ipam that specifies the contract the IPAM plugin need to satisfy, decoupling IPAM interface and implementation.
Package ipam that specifies the contract the IPAM plugin need to satisfy, decoupling IPAM interface and implementation.
Package options provides a way to pass unstructured sets of options to a component expecting a strongly-typed configuration structure.
Package options provides a way to pass unstructured sets of options to a component expecting a strongly-typed configuration structure.
Package osl describes structures and interfaces which abstract os entities
Package osl describes structures and interfaces which abstract os entities
Package resolvconf provides utility code to query and update DNS configuration in /etc/resolv.conf
Package resolvconf provides utility code to query and update DNS configuration in /etc/resolv.conf
dns
Package types contains types that are common across libnetwork project
Package types contains types that are common across libnetwork project

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL