network

package
v0.0.0-...-a002913 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2015 License: AGPL-3.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Id of the default public juju network
	DefaultPublic = "juju-public"

	// Id of the default private juju network
	DefaultPrivate = "juju-private"

	// Provider Id for the default network
	DefaultProviderId = "juju-unknown"
)

TODO(dimitern): Remove this once we use spaces as per the model.

Variables

View Source
var InterfaceByNameAddrs = func(name string) ([]net.Addr, error) {
	iface, err := net.InterfaceByName(name)
	if err != nil {
		return nil, err
	}
	return iface.Addrs()
}

InterfaceByNameAddrs returns the addresses for the given interface name. It's exported to facilitate cross-package testing.

View Source
var LXCNetDefaultConfig = "/etc/default/lxc-net"

LXCNetDefaultConfig is the location of the default network config of the lxc package. It's exported to allow cross-package testing.

Functions

func DecimalToIPv4

func DecimalToIPv4(addr uint32) net.IP

DecimalToIPv4 converts a decimal to the dotted quad IP address format.

func ExactScopeMatch

func ExactScopeMatch(addr Address, addrScopes ...Scope) bool

ExactScopeMatch checks if an address exactly matches any of the specified scopes. An address will not match if globalPreferIPv6 is set and it isn't an IPv6 address.

func HostPortsToStrings

func HostPortsToStrings(hps []HostPort) []string

HostPortsToStrings converts each HostPort to string calling its NetAddr() method.

func IPv4ToDecimal

func IPv4ToDecimal(ipv4Addr net.IP) (uint32, error)

IPv4ToDecimal converts a dotted quad IP address to its decimal equivalent.

func InitializeFromConfig

func InitializeFromConfig(config PreferIPv6Getter)

InitializeFromConfig needs to be called once after the environment or agent configuration is available to configure networking settings.

func IsNoAddress

func IsNoAddress(err error) bool

IsNoAddress reports whether err was created with NoAddressf().

func NoAddressf

func NoAddressf(format string, args ...interface{}) error

NoAddressf returns an error which satisfies IsNoAddress().

func ResetGlobalPreferIPv6

func ResetGlobalPreferIPv6()

ResetGlobalPreferIPv6 resets the global variable back to the default, and is called only from the isolation test suite to make sure we have a clean environment.

func SelectInternalHostPort

func SelectInternalHostPort(hps []HostPort, machineLocal bool) string

SelectInternalHostPort picks one HostPort from a slice that can be used as an endpoint for juju internal communication and returns it in its NetAddr form. If there are no suitable addresses, the empty string is returned.

func SelectPublicHostPort

func SelectPublicHostPort(hps []HostPort) string

SelectPublicHostPort picks one HostPort from a slice that would be appropriate to display as a publicly accessible endpoint. If there are no suitable candidates, the empty string is returned.

func SortAddresses

func SortAddresses(addrs []Address, preferIPv6 bool)

SortAddresses sorts the given Address slice according to the sortOrder of each address and the preferIpv6 flag. See Address.sortOrder() for more info.

func SortHostPorts

func SortHostPorts(hps []HostPort, preferIPv6 bool)

SortHostPorts sorts the given HostPort slice according to the sortOrder of each HostPort's embedded Address and the preferIpv6 flag. See Address.sortOrder() for more info.

func SortInterfaceInfo

func SortInterfaceInfo(interfaces []InterfaceInfo)

SortInterfaceInfo sorts a slice of InterfaceInfo on DeviceIndex in ascending order.

func SortPortRanges

func SortPortRanges(portRanges []PortRange)

SortPortRanges sorts the given ports, first by protocol, then by number.

Types

type Address

type Address struct {
	Value       string
	Type        AddressType
	NetworkName string
	Scope
}

Address represents the location of a machine, including metadata about what kind of location the address describes.

func FilterLXCAddresses

func FilterLXCAddresses(addresses []Address) []Address

FilterLXCAddresses tries to discover the default lxc bridge name and all of its addresses, then filters those addresses out of the given ones and returns the result. Any errors encountered during this process are logged, but not considered fatal. See LP bug #1416928.

func HostsWithoutPort

func HostsWithoutPort(hps []HostPort) []Address

HostsWithoutPort strips the port from each HostPort, returning just the addresses.

func NewAddress

func NewAddress(value string) Address

NewAddress creates a new Address, deriving its type from the value and using ScopeUnknown as scope. It's a shortcut to calling NewScopedAddress(value, ScopeUnknown).

func NewAddresses

func NewAddresses(inAddresses ...string) (outAddresses []Address)

NewAddresses is a convenience function to create addresses from a string slice.

func NewScopedAddress

func NewScopedAddress(value string, scope Scope) Address

NewScopedAddress creates a new Address, deriving its type from the value.

If the specified scope is ScopeUnknown, then NewScopedAddress will attempt derive the scope based on reserved IP address ranges. Because passing ScopeUnknown is fairly common, NewAddress() above does exactly that.

func SelectInternalAddress

func SelectInternalAddress(addresses []Address, machineLocal bool) (Address, bool)

SelectInternalAddress picks one address from a slice that can be used as an endpoint for juju internal communication. If there are are no suitable addresses, then ok is false (and an empty address is returned). If a suitable address is then ok is true.

func SelectPublicAddress

func SelectPublicAddress(addresses []Address) (Address, bool)

SelectPublicAddress picks one address from a slice that would be appropriate to display as a publicly accessible endpoint. If there are no suitable addresses, then ok is false (and an empty address is returned). If a suitable address is then ok is true.

func (Address) GoString

func (a Address) GoString() string

GoString implements fmt.GoStringer.

func (Address) String

func (a Address) String() string

String returns a string representation of the address, in the form: scope:address(network name); for example:

public:c2-54-226-162-124.compute-1.amazonaws.com(ec2network)

If the scope is NetworkUnknown, the initial scope: prefix will be omitted. If the NetworkName is blank, the (network name) suffix will be omitted.

type AddressType

type AddressType string

AddressType represents the possible ways of specifying a machine location by either a hostname resolvable by dns lookup, or IPv4 or IPv6 address.

const (
	HostName    AddressType = "hostname"
	IPv4Address AddressType = "ipv4"
	IPv6Address AddressType = "ipv6"
)

func DeriveAddressType

func DeriveAddressType(value string) AddressType

DeriveAddressType attempts to detect the type of address given.

type BySpaceName

type BySpaceName []SpaceInfo

func (BySpaceName) Len

func (s BySpaceName) Len() int

func (BySpaceName) Less

func (s BySpaceName) Less(i, j int) bool

func (BySpaceName) Swap

func (s BySpaceName) Swap(i, j int)

type HostPort

type HostPort struct {
	Address
	Port int
}

HostPort associates an address with a port.

func AddressesWithPort

func AddressesWithPort(addrs []Address, port int) []HostPort

AddressesWithPort returns the given addresses all associated with the given port.

func CollapseHostPorts

func CollapseHostPorts(serversHostPorts [][]HostPort) []HostPort

CollapseHostPorts returns a flattened list of HostPorts keeping the same order they appear in serversHostPorts.

func DropDuplicatedHostPorts

func DropDuplicatedHostPorts(hps []HostPort) []HostPort

DropDuplicatedHostPorts removes any HostPorts duplicates from the given slice and returns the result.

func EnsureFirstHostPort

func EnsureFirstHostPort(first HostPort, hps []HostPort) []HostPort

EnsureFirstHostPort scans the given list of HostPorts and if "first" is found, it moved to index 0. Otherwise, if "first" is not in the list, it's inserted at index 0.

func FilterUnusableHostPorts

func FilterUnusableHostPorts(hps []HostPort) []HostPort

FilterUnusableHostPorts returns a copy of the given HostPorts after removing any addresses unlikely to be usable (ScopeMachineLocal or ScopeLinkLocal).

func NewHostPorts

func NewHostPorts(port int, addresses ...string) []HostPort

NewHostPorts creates a list of HostPorts from each given string address and port.

func ParseHostPorts

func ParseHostPorts(hostPorts ...string) ([]HostPort, error)

ParseHostPorts creates a list of HostPorts parsing each given string containing address:port. An error is returned if any string cannot be parsed as HostPort.

func ResolveOrDropHostnames

func ResolveOrDropHostnames(hps []HostPort) []HostPort

ResolveOrDropHostnames tries to resolve each address of type HostName (except for "localhost" - it's kept unchanged) using the local resolver. If successful, each IP address corresponding to the hostname is inserted in the same order. If not successful, a debug log is added and the hostname is removed from the list. Duplicated addresses after the resolving is done are removed.

func (HostPort) GoString

func (hp HostPort) GoString() string

GoString implements fmt.GoStringer.

func (HostPort) NetAddr

func (hp HostPort) NetAddr() string

NetAddr returns the host-port as an address suitable for calling net.Dial.

func (HostPort) String

func (hp HostPort) String() string

String implements Stringer.

type Id

type Id string

Id defines a provider-specific network id.

const AnySubnet Id = ""

AnySubnet when passed as a subnet id should be interpreted by the providers as "the subnet id does not matter". It's up to the provider how to handle this case - it might return an error.

type InterfaceConfigType

type InterfaceConfigType string

InterfaceConfigType defines valid network interface configuration types. See interfaces(5) for details

const (
	ConfigUnknown InterfaceConfigType = ""
	ConfigDHCP    InterfaceConfigType = "dhcp"
	ConfigStatic  InterfaceConfigType = "static"
	ConfigManual  InterfaceConfigType = "manual"
)

type InterfaceInfo

type InterfaceInfo struct {
	// DeviceIndex specifies the order in which the network interface
	// appears on the host. The primary interface has an index of 0.
	DeviceIndex int

	// MACAddress is the network interface's hardware MAC address
	// (e.g. "aa:bb:cc:dd:ee:ff").
	MACAddress string

	// CIDR of the network, in 123.45.67.89/24 format.
	CIDR string

	// NetworkName is juju-internal name of the network.
	NetworkName string

	// ProviderId is a provider-specific NIC id.
	ProviderId Id

	// ProviderSubnetId is the provider-specific id for the associated
	// subnet.
	ProviderSubnetId Id

	// AvailabilityZones describes the availability zones the associated
	// subnet is in.
	AvailabilityZones []string

	// VLANTag needs to be between 1 and 4094 for VLANs and 0 for
	// normal networks. It's defined by IEEE 802.1Q standard.
	VLANTag int

	// InterfaceName is the raw OS-specific network device name (e.g.
	// "eth1", even for a VLAN eth1.42 virtual interface).
	InterfaceName string

	// Disabled is true when the interface needs to be disabled on the
	// machine, e.g. not to configure it.
	Disabled bool

	// NoAutoStart is true when the interface should not be configured
	// to start automatically on boot. By default and for
	// backwards-compatibility, interfaces are configured to
	// auto-start.
	NoAutoStart bool

	// ConfigType determines whether the interface should be
	// configured via DHCP, statically, manually, etc. See
	// interfaces(5) for more information.
	ConfigType InterfaceConfigType

	// Address contains an optional static IP address to configure for
	// this network interface. The subnet mask to set will be inferred
	// from the CIDR value.
	Address Address

	// DNSServers contains an optional list of IP addresses and/or
	// hostnames to configure as DNS servers for this network
	// interface.
	DNSServers []Address

	// DNSSearch contains the default DNS domain to use for
	// non-FQDN lookups.
	DNSSearch string

	// Gateway address, if set, defines the default gateway to
	// configure for this network interface. For containers this
	// usually is (one of) the host address(es).
	GatewayAddress Address

	// ExtraConfig can contain any valid setting and its value allowed
	// inside an "iface" section of a interfaces(5) config file, e.g.
	// "up", "down", "mtu", etc.
	ExtraConfig map[string]string
}

InterfaceInfo describes a single network interface available on an instance. For providers that support networks, this will be available at StartInstance() time. TODO(mue): Rename to InterfaceConfig due to consistency later.

func (*InterfaceInfo) ActualInterfaceName

func (i *InterfaceInfo) ActualInterfaceName() string

ActualInterfaceName returns raw interface name for raw interface (e.g. "eth0") and virtual interface name for virtual interface (e.g. "eth0.42")

func (*InterfaceInfo) IsVLAN

func (i *InterfaceInfo) IsVLAN() bool

IsVLAN returns true when the interface is a VLAN interface.

func (*InterfaceInfo) IsVirtual

func (i *InterfaceInfo) IsVirtual() bool

IsVirtual returns true when the interface is a virtual device, as opposed to a physical device (e.g. a VLAN or a network alias)

type Port

type Port struct {
	Protocol string
	Number   int
}

Port identifies a network port number for a particular protocol.

NOTE(dimitern): This is deprecated and should be removed, use PortRange instead. There are a few places which still use Port, especially in apiserver/params, so it can't be removed yet.

func (Port) GoString

func (p Port) GoString() string

GoString implements fmt.GoStringer.

func (Port) String

func (p Port) String() string

String implements Stringer.

type PortRange

type PortRange struct {
	FromPort int
	ToPort   int
	Protocol string
}

PortRange represents a single range of ports.

func CollapsePorts

func CollapsePorts(ports []Port) (result []PortRange)

CollapsePorts collapses a slice of ports into port ranges.

NOTE(dimitern): This is deprecated and should be removed when possible. It still exists, because in a few places slices of Ports are converted to PortRanges internally.

func MustParsePortRange

func MustParsePortRange(portRange string) PortRange

MustParsePortRange converts a raw port-range string into a PortRange. If the string is invalid, the function panics.

func ParsePortRange

func ParsePortRange(inPortRange string) (PortRange, error)

ParsePortRange builds a PortRange from the provided string. If the string does not include a protocol then "tcp" is used. Validate() gets called on the result before returning. If validation fails the invalid PortRange is still returned. Example strings: "80/tcp", "443", "12345-12349/udp".

func ParsePortRanges

func ParsePortRanges(inPortRanges string) ([]PortRange, error)

ParsePortRanges splits the provided string on commas and extracts a PortRange from each part of the split string. Whitespace is ignored. Example strings: "80/tcp", "80,443,1234/udp", "123-456, 25/tcp".

func (PortRange) ConflictsWith

func (a PortRange) ConflictsWith(b PortRange) bool

ConflictsWith determines if the two port ranges conflict.

func (PortRange) GoString

func (p PortRange) GoString() string

func (PortRange) String

func (p PortRange) String() string

func (PortRange) Validate

func (p PortRange) Validate() error

IsValid determines if the port range is valid.

type PortSet

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

PortSet is a set-like container of Port values.

func NewPortSet

func NewPortSet(portRanges ...PortRange) PortSet

NewPortSet creates a map of protocols to sets of stringified port numbers.

func (*PortSet) Add

func (ps *PortSet) Add(protocol string, port int)

Add adds a port to the PortSet.

func (*PortSet) AddRanges

func (ps *PortSet) AddRanges(portRanges ...PortRange)

AddRanges adds port ranges to the PortSet.

func (*PortSet) Contains

func (ps *PortSet) Contains(protocol string, port int) bool

Contains returns true if the provided port is in the set.

func (*PortSet) ContainsRanges

func (ps *PortSet) ContainsRanges(portRanges ...PortRange) bool

ContainsRanges returns true if the provided port ranges are in the set.

func (PortSet) Difference

func (ps PortSet) Difference(other PortSet) PortSet

Difference returns a new PortSet of the values that are not in the other PortSet.

func (PortSet) Intersection

func (ps PortSet) Intersection(other PortSet) PortSet

Intersection returns a new PortSet of the values that are in both this set and the other, but not in just one of either.

func (PortSet) IsEmpty

func (ps PortSet) IsEmpty() bool

IsEmpty returns true if the PortSet is empty.

func (PortSet) PortNumbers

func (ps PortSet) PortNumbers(protocol string) []int

PortNumbers returns a list of all the port numbers in the set for the given protocol.

func (PortSet) PortRanges

func (ps PortSet) PortRanges(protocols ...string) []PortRange

PortRanges returns a list of all the port ranges in the set for the given protocols. If no protocols are provided all known protocols in the set are used.

func (PortSet) PortStrings

func (ps PortSet) PortStrings(protocol string) []string

PortStrings returns a list of stringified ports in the set for the given protocol. This is strictly a convenience method for situations where another API requires a list of strings.

func (PortSet) Ports

func (ps PortSet) Ports(protocols ...string) []Port

PortNumbers returns a list of all the port numbers in the set for the given protocols. If no protocols are provided then all known protocols in the set are used.

func (PortSet) Protocols

func (ps PortSet) Protocols() []string

Protocols returns a list of protocols known to the PortSet.

func (*PortSet) Remove

func (ps *PortSet) Remove(protocol string, port int)

Remove removes the given port from the set.

func (*PortSet) RemoveRanges

func (ps *PortSet) RemoveRanges(portRanges ...PortRange)

RemoveRanges removes all ports in the given PortRange values from the set.

func (PortSet) Size

func (ps PortSet) Size() int

Size returns the number of ports in the set.

func (PortSet) Union

func (ps PortSet) Union(other PortSet) PortSet

Union returns a new PortSet of the shared values that are common between both PortSets.

func (PortSet) Values

func (ps PortSet) Values() []Port

Values returns a list of all the ports in the set.

type PreferIPv6Getter

type PreferIPv6Getter interface {
	PreferIPv6() bool
}

PreferIPv6Getter will be implemented by both the environment and agent config.

type Scope

type Scope string

Scope denotes the context a location may apply to. If a name or address can be reached from the wider internet, it is considered public. A private network address is either specific to the cloud or cloud subnet a machine belongs to, or to the machine itself for containers.

const (
	ScopeUnknown      Scope = ""
	ScopePublic       Scope = "public"
	ScopeCloudLocal   Scope = "local-cloud"
	ScopeMachineLocal Scope = "local-machine"
	ScopeLinkLocal    Scope = "link-local"
)

type SpaceInfo

type SpaceInfo struct {
	Name  string
	CIDRs []string
}

type SubnetInfo

type SubnetInfo struct {
	// CIDR of the network, in 123.45.67.89/24 format. Can be empty if
	// unknown.
	CIDR string

	// ProviderId is a provider-specific network id. This the only
	// required field.
	ProviderId Id

	// VLANTag needs to be between 1 and 4094 for VLANs and 0 for
	// normal networks. It's defined by IEEE 802.1Q standard, and used
	// to define a VLAN network. For more information, see:
	// http://en.wikipedia.org/wiki/IEEE_802.1Q.
	VLANTag int

	// AllocatableIPLow and AllocatableIPHigh describe the allocatable
	// portion of the subnet. The provider will only permit allocation
	// between these limits. If they are empty then none of the subnet is
	// allocatable.
	AllocatableIPLow  net.IP
	AllocatableIPHigh net.IP

	// AvailabilityZones describes which availability zone(s) this
	// subnet is in. It can be empty if the provider does not support
	// availability zones.
	AvailabilityZones []string

	// SpaceName holds the juju network space associated with this
	// subnet. Can be empty if not supported.
	SpaceName string
}

SubnetInfo describes the bare minimum information for a subnet, which the provider knows about but juju might not yet.

Jump to

Keyboard shortcuts

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