github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/container/network.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package container
     5  
     6  import (
     7  	"github.com/juju/juju/network"
     8  )
     9  
    10  const (
    11  	// BridgeNetwork will have the container use the network bridge.
    12  	BridgeNetwork = "bridge"
    13  	// PhyscialNetwork will have the container use a specified network device.
    14  	PhysicalNetwork = "physical"
    15  	// DefaultLxdBridge is the default name for the lxd bridge.
    16  	DefaultLxdBridge = "lxdbr0"
    17  	// DefaultLxcBridge is the package created container bridge.
    18  	DefaultLxcBridge = "lxcbr0"
    19  	// DefaultKvmBridge is the default bridge for KVM instances.
    20  	DefaultKvmBridge = "virbr0"
    21  )
    22  
    23  // NetworkConfig defines how the container network will be configured.
    24  type NetworkConfig struct {
    25  	NetworkType string
    26  	Device      string
    27  	MTU         int
    28  
    29  	Interfaces []network.InterfaceInfo
    30  }
    31  
    32  // FallbackInterfaceInfo returns a single "eth0" interface configured with DHCP.
    33  func FallbackInterfaceInfo() []network.InterfaceInfo {
    34  	return []network.InterfaceInfo{{
    35  		InterfaceName: "eth0",
    36  		InterfaceType: network.EthernetInterface,
    37  		ConfigType:    network.ConfigDHCP,
    38  	}}
    39  }
    40  
    41  // BridgeNetworkConfig returns a valid NetworkConfig to use the specified device
    42  // as a network bridge for the container. It also allows passing in specific
    43  // configuration for the container's network interfaces and default MTU to use.
    44  // If interfaces is empty, FallbackInterfaceInfo() is used to get the a sane
    45  // default
    46  func BridgeNetworkConfig(device string, mtu int, interfaces []network.InterfaceInfo) *NetworkConfig {
    47  	if len(interfaces) == 0 {
    48  		interfaces = FallbackInterfaceInfo()
    49  	}
    50  	return &NetworkConfig{BridgeNetwork, device, mtu, interfaces}
    51  }
    52  
    53  // PhysicalNetworkConfig returns a valid NetworkConfig to use the
    54  // specified device as the network device for the container. It also
    55  // allows passing in specific configuration for the container's
    56  // network interfaces and default MTU to use. If interfaces is nil the
    57  // default configuration is used for the respective container type.
    58  func PhysicalNetworkConfig(device string, mtu int, interfaces []network.InterfaceInfo) *NetworkConfig {
    59  	return &NetworkConfig{PhysicalNetwork, device, mtu, interfaces}
    60  }