github.com/psychoss/docker@v1.9.0/daemon/network.go (about)

     1  package daemon
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"net"
     7  	"strings"
     8  
     9  	"github.com/docker/docker/daemon/network"
    10  	"github.com/docker/libnetwork"
    11  )
    12  
    13  const (
    14  	// NetworkByID represents a constant to find a network by its ID
    15  	NetworkByID = iota + 1
    16  	// NetworkByName represents a constant to find a network by its Name
    17  	NetworkByName
    18  )
    19  
    20  // NetworkControllerEnabled checks if the networking stack is enabled.
    21  // This feature depends on OS primitives and it's dissabled in systems like Windows.
    22  func (daemon *Daemon) NetworkControllerEnabled() bool {
    23  	return daemon.netController != nil
    24  }
    25  
    26  // FindNetwork function finds a network for a given string that can represent network name or id
    27  func (daemon *Daemon) FindNetwork(idName string) (libnetwork.Network, error) {
    28  	// Find by Name
    29  	n, err := daemon.GetNetwork(idName, NetworkByName)
    30  	if _, ok := err.(libnetwork.ErrNoSuchNetwork); err != nil && !ok {
    31  		return nil, err
    32  	}
    33  
    34  	if n != nil {
    35  		return n, nil
    36  	}
    37  
    38  	// Find by id
    39  	n, err = daemon.GetNetwork(idName, NetworkByID)
    40  	if err != nil {
    41  		return nil, err
    42  	}
    43  
    44  	return n, nil
    45  }
    46  
    47  // GetNetwork function returns a network for a given string that represents the network and
    48  // a hint to indicate if the string is an Id or Name of the network
    49  func (daemon *Daemon) GetNetwork(idName string, by int) (libnetwork.Network, error) {
    50  	c := daemon.netController
    51  	switch by {
    52  	case NetworkByID:
    53  		list := daemon.GetNetworksByID(idName)
    54  
    55  		if len(list) == 0 {
    56  			return nil, libnetwork.ErrNoSuchNetwork(idName)
    57  		}
    58  
    59  		if len(list) > 1 {
    60  			return nil, libnetwork.ErrInvalidID(idName)
    61  		}
    62  
    63  		return list[0], nil
    64  	case NetworkByName:
    65  		if idName == "" {
    66  			idName = c.Config().Daemon.DefaultNetwork
    67  		}
    68  		return c.NetworkByName(idName)
    69  	}
    70  	return nil, errors.New("unexpected selector for GetNetwork")
    71  }
    72  
    73  // GetNetworksByID returns a list of networks whose ID partially matches zero or more networks
    74  func (daemon *Daemon) GetNetworksByID(partialID string) []libnetwork.Network {
    75  	c := daemon.netController
    76  	list := []libnetwork.Network{}
    77  	l := func(nw libnetwork.Network) bool {
    78  		if strings.HasPrefix(nw.ID(), partialID) {
    79  			list = append(list, nw)
    80  		}
    81  		return false
    82  	}
    83  	c.WalkNetworks(l)
    84  
    85  	return list
    86  }
    87  
    88  // CreateNetwork creates a network with the given name, driver and other optional parameters
    89  func (daemon *Daemon) CreateNetwork(name, driver string, ipam network.IPAM, options map[string]string) (libnetwork.Network, error) {
    90  	c := daemon.netController
    91  	if driver == "" {
    92  		driver = c.Config().Daemon.DefaultDriver
    93  	}
    94  
    95  	nwOptions := []libnetwork.NetworkOption{}
    96  
    97  	v4Conf, v6Conf, err := getIpamConfig(ipam.Config)
    98  	if err != nil {
    99  		return nil, err
   100  	}
   101  
   102  	nwOptions = append(nwOptions, libnetwork.NetworkOptionIpam(ipam.Driver, "", v4Conf, v6Conf))
   103  	nwOptions = append(nwOptions, libnetwork.NetworkOptionDriverOpts(options))
   104  	return c.NewNetwork(driver, name, nwOptions...)
   105  }
   106  
   107  func getIpamConfig(data []network.IPAMConfig) ([]*libnetwork.IpamConf, []*libnetwork.IpamConf, error) {
   108  	ipamV4Cfg := []*libnetwork.IpamConf{}
   109  	ipamV6Cfg := []*libnetwork.IpamConf{}
   110  	for _, d := range data {
   111  		iCfg := libnetwork.IpamConf{}
   112  		iCfg.PreferredPool = d.Subnet
   113  		iCfg.SubPool = d.IPRange
   114  		iCfg.Gateway = d.Gateway
   115  		iCfg.AuxAddresses = d.AuxAddress
   116  		ip, _, err := net.ParseCIDR(d.Subnet)
   117  		if err != nil {
   118  			return nil, nil, fmt.Errorf("Invalid subnet %s : %v", d.Subnet, err)
   119  		}
   120  		if ip.To4() != nil {
   121  			ipamV4Cfg = append(ipamV4Cfg, &iCfg)
   122  		} else {
   123  			ipamV6Cfg = append(ipamV6Cfg, &iCfg)
   124  		}
   125  	}
   126  	return ipamV4Cfg, ipamV6Cfg, nil
   127  }