123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493 |
- /*
- 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
- }
- */
- package libnetwork
- import (
- "container/heap"
- "fmt"
- "net"
- "sync"
- log "github.com/Sirupsen/logrus"
- "github.com/docker/docker/pkg/plugins"
- "github.com/docker/docker/pkg/stringid"
- "github.com/docker/libnetwork/config"
- "github.com/docker/libnetwork/datastore"
- "github.com/docker/libnetwork/driverapi"
- "github.com/docker/libnetwork/hostdiscovery"
- "github.com/docker/libnetwork/osl"
- "github.com/docker/libnetwork/types"
- )
- // NetworkController provides the interface for controller instance which manages
- // networks.
- 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()
- }
- // NetworkWalker is a client provided function which will be used to walk the Networks.
- // When the function returns true, the walk will stop.
- type NetworkWalker func(nw Network) bool
- // SandboxWalker is a client provided function which will be used to walk the Sandboxes.
- // When the function returns true, the walk will stop.
- type SandboxWalker func(sb Sandbox) bool
- type driverData struct {
- driver driverapi.Driver
- capability driverapi.Capability
- }
- type driverTable map[string]*driverData
- type networkTable map[string]*network
- type endpointTable map[string]*endpoint
- type sandboxTable map[string]*sandbox
- type controller struct {
- id string
- networks networkTable
- drivers driverTable
- sandboxes sandboxTable
- cfg *config.Config
- globalStore, localStore datastore.DataStore
- extKeyListener net.Listener
- sync.Mutex
- }
- // New creates a new instance of network controller.
- func New(cfgOptions ...config.Option) (NetworkController, error) {
- var cfg *config.Config
- if len(cfgOptions) > 0 {
- cfg = &config.Config{
- Daemon: config.DaemonCfg{
- DriverCfg: make(map[string]interface{}),
- },
- }
- cfg.ProcessOptions(cfgOptions...)
- }
- c := &controller{
- id: stringid.GenerateRandomID(),
- cfg: cfg,
- networks: networkTable{},
- sandboxes: sandboxTable{},
- drivers: driverTable{}}
- if err := initDrivers(c); err != nil {
- return nil, err
- }
- if cfg != nil {
- if err := c.initGlobalStore(); err != nil {
- // Failing to initalize datastore is a bad situation to be in.
- // But it cannot fail creating the Controller
- log.Debugf("Failed to Initialize Datastore due to %v. Operating in non-clustered mode", err)
- }
- if err := c.initDiscovery(); err != nil {
- // Failing to initalize discovery is a bad situation to be in.
- // But it cannot fail creating the Controller
- log.Debugf("Failed to Initialize Discovery : %v", err)
- }
- if err := c.initLocalStore(); err != nil {
- return nil, fmt.Errorf("Failed to Initialize LocalDatastore due to %v.", err)
- }
- }
- if err := c.startExternalKeyListener(); err != nil {
- return nil, err
- }
- return c, nil
- }
- func (c *controller) ID() string {
- return c.id
- }
- func (c *controller) validateHostDiscoveryConfig() bool {
- if c.cfg == nil || c.cfg.Cluster.Discovery == "" || c.cfg.Cluster.Address == "" {
- return false
- }
- return true
- }
- func (c *controller) initDiscovery() error {
- if c.cfg == nil {
- return fmt.Errorf("discovery initialization requires a valid configuration")
- }
- hostDiscovery := hostdiscovery.NewHostDiscovery()
- return hostDiscovery.StartDiscovery(&c.cfg.Cluster, c.hostJoinCallback, c.hostLeaveCallback)
- }
- func (c *controller) hostJoinCallback(hosts []net.IP) {
- }
- func (c *controller) hostLeaveCallback(hosts []net.IP) {
- }
- func (c *controller) Config() config.Config {
- c.Lock()
- defer c.Unlock()
- if c.cfg == nil {
- return config.Config{}
- }
- return *c.cfg
- }
- func (c *controller) RegisterDriver(networkType string, driver driverapi.Driver, capability driverapi.Capability) error {
- if !config.IsValidName(networkType) {
- return ErrInvalidName(networkType)
- }
- c.Lock()
- if _, ok := c.drivers[networkType]; ok {
- c.Unlock()
- return driverapi.ErrActiveRegistration(networkType)
- }
- c.drivers[networkType] = &driverData{driver, capability}
- c.Unlock()
- return nil
- }
- // NewNetwork creates a new network of the specified network type. The options
- // are network specific and modeled in a generic way.
- func (c *controller) NewNetwork(networkType, name string, options ...NetworkOption) (Network, error) {
- if !config.IsValidName(name) {
- return nil, ErrInvalidName(name)
- }
- // Check if a network already exists with the specified network name
- c.Lock()
- for _, n := range c.networks {
- if n.name == name {
- c.Unlock()
- return nil, NetworkNameError(name)
- }
- }
- c.Unlock()
- // Construct the network object
- network := &network{
- name: name,
- networkType: networkType,
- id: stringid.GenerateRandomID(),
- ctrlr: c,
- endpoints: endpointTable{},
- persist: true,
- }
- network.processOptions(options...)
- if err := c.addNetwork(network); err != nil {
- return nil, err
- }
- if err := c.updateToStore(network); err != nil {
- log.Warnf("couldnt create network %s: %v", network.name, err)
- if e := network.Delete(); e != nil {
- log.Warnf("couldnt cleanup network %s: %v", network.name, err)
- }
- return nil, err
- }
- return network, nil
- }
- func (c *controller) addNetwork(n *network) error {
- c.Lock()
- // Check if a driver for the specified network type is available
- dd, ok := c.drivers[n.networkType]
- c.Unlock()
- if !ok {
- var err error
- dd, err = c.loadDriver(n.networkType)
- if err != nil {
- return err
- }
- }
- n.Lock()
- n.svcRecords = svcMap{}
- n.driver = dd.driver
- n.dataScope = dd.capability.DataScope
- d := n.driver
- n.Unlock()
- // Create the network
- if err := d.CreateNetwork(n.id, n.generic); err != nil {
- return err
- }
- if n.isGlobalScoped() {
- if err := n.watchEndpoints(); err != nil {
- return err
- }
- }
- c.Lock()
- c.networks[n.id] = n
- c.Unlock()
- return nil
- }
- func (c *controller) Networks() []Network {
- c.Lock()
- defer c.Unlock()
- list := make([]Network, 0, len(c.networks))
- for _, n := range c.networks {
- list = append(list, n)
- }
- return list
- }
- func (c *controller) WalkNetworks(walker NetworkWalker) {
- for _, n := range c.Networks() {
- if walker(n) {
- return
- }
- }
- }
- func (c *controller) NetworkByName(name string) (Network, error) {
- if name == "" {
- return nil, ErrInvalidName(name)
- }
- var n Network
- s := func(current Network) bool {
- if current.Name() == name {
- n = current
- return true
- }
- return false
- }
- c.WalkNetworks(s)
- if n == nil {
- return nil, ErrNoSuchNetwork(name)
- }
- return n, nil
- }
- func (c *controller) NetworkByID(id string) (Network, error) {
- if id == "" {
- return nil, ErrInvalidID(id)
- }
- c.Lock()
- defer c.Unlock()
- if n, ok := c.networks[id]; ok {
- return n, nil
- }
- return nil, ErrNoSuchNetwork(id)
- }
- // NewSandbox creates a new sandbox for the passed container id
- func (c *controller) NewSandbox(containerID string, options ...SandboxOption) (Sandbox, error) {
- var err error
- if containerID == "" {
- return nil, types.BadRequestErrorf("invalid container ID")
- }
- var existing Sandbox
- look := SandboxContainerWalker(&existing, containerID)
- c.WalkSandboxes(look)
- if existing != nil {
- return nil, types.BadRequestErrorf("container %s is already present: %v", containerID, existing)
- }
- // Create sandbox and process options first. Key generation depends on an option
- sb := &sandbox{
- id: stringid.GenerateRandomID(),
- containerID: containerID,
- endpoints: epHeap{},
- epPriority: map[string]int{},
- config: containerConfig{},
- controller: c,
- }
- // This sandbox may be using an existing osl sandbox, sharing it with another sandbox
- var peerSb Sandbox
- c.WalkSandboxes(SandboxKeyWalker(&peerSb, sb.Key()))
- if peerSb != nil {
- sb.osSbox = peerSb.(*sandbox).osSbox
- }
- heap.Init(&sb.endpoints)
- sb.processOptions(options...)
- if err = sb.setupResolutionFiles(); err != nil {
- return nil, err
- }
- if sb.osSbox == nil && !sb.config.useExternalKey {
- if sb.osSbox, err = osl.NewSandbox(sb.Key(), !sb.config.useDefaultSandBox); err != nil {
- return nil, fmt.Errorf("failed to create new osl sandbox: %v", err)
- }
- }
- c.Lock()
- c.sandboxes[sb.id] = sb
- c.Unlock()
- return sb, nil
- }
- func (c *controller) Sandboxes() []Sandbox {
- c.Lock()
- defer c.Unlock()
- list := make([]Sandbox, 0, len(c.sandboxes))
- for _, s := range c.sandboxes {
- list = append(list, s)
- }
- return list
- }
- func (c *controller) WalkSandboxes(walker SandboxWalker) {
- for _, sb := range c.Sandboxes() {
- if walker(sb) {
- return
- }
- }
- }
- func (c *controller) SandboxByID(id string) (Sandbox, error) {
- if id == "" {
- return nil, ErrInvalidID(id)
- }
- c.Lock()
- s, ok := c.sandboxes[id]
- c.Unlock()
- if !ok {
- return nil, types.NotFoundErrorf("sandbox %s not found", id)
- }
- return s, nil
- }
- // SandboxContainerWalker returns a Sandbox Walker function which looks for an existing Sandbox with the passed containerID
- func SandboxContainerWalker(out *Sandbox, containerID string) SandboxWalker {
- return func(sb Sandbox) bool {
- if sb.ContainerID() == containerID {
- *out = sb
- return true
- }
- return false
- }
- }
- // SandboxKeyWalker returns a Sandbox Walker function which looks for an existing Sandbox with the passed key
- func SandboxKeyWalker(out *Sandbox, key string) SandboxWalker {
- return func(sb Sandbox) bool {
- if sb.Key() == key {
- *out = sb
- return true
- }
- return false
- }
- }
- func (c *controller) loadDriver(networkType string) (*driverData, error) {
- // Plugins pkg performs lazy loading of plugins that acts as remote drivers.
- // As per the design, this Get call will result in remote driver discovery if there is a corresponding plugin available.
- _, err := plugins.Get(networkType, driverapi.NetworkPluginEndpointType)
- if err != nil {
- if err == plugins.ErrNotFound {
- return nil, types.NotFoundErrorf(err.Error())
- }
- return nil, err
- }
- c.Lock()
- defer c.Unlock()
- dd, ok := c.drivers[networkType]
- if !ok {
- return nil, ErrInvalidNetworkDriver(networkType)
- }
- return dd, nil
- }
- func (c *controller) Stop() {
- c.stopExternalKeyListener()
- osl.GC()
- }
|