diff --git a/libnetwork/endpoint.go b/libnetwork/endpoint.go index 4a2c32948e..f15645d648 100644 --- a/libnetwork/endpoint.go +++ b/libnetwork/endpoint.go @@ -25,7 +25,7 @@ type Endpoint struct { name string id string network *Network - iface *endpointInterface + iface *EndpointInterface joinInfo *endpointJoinInfo sandboxID string exposedPorts []types.TransportPort @@ -223,7 +223,7 @@ func (ep *Endpoint) CopyTo(o datastore.KVObject) error { copy(dstEp.ingressPorts, ep.ingressPorts) if ep.iface != nil { - dstEp.iface = &endpointInterface{} + dstEp.iface = &EndpointInterface{} if err := ep.iface.CopyTo(dstEp.iface); err != nil { return err } diff --git a/libnetwork/endpoint_info.go b/libnetwork/endpoint_info.go index 53e6d21495..fc06a6424c 100644 --- a/libnetwork/endpoint_info.go +++ b/libnetwork/endpoint_info.go @@ -11,11 +11,10 @@ import ( // EndpointInfo provides an interface to retrieve network resources bound to the endpoint. type EndpointInfo interface { - // Iface returns InterfaceInfo, go interface that can be used - // to get more information on the interface which was assigned to + // Iface returns information about the interface which was assigned to // the endpoint by the driver. This can be used after the // endpoint has been created. - Iface() InterfaceInfo + Iface() *EndpointInterface // Gateway returns the IPv4 gateway assigned by the driver. // This will only return a valid value if a container has joined the endpoint. @@ -36,25 +35,8 @@ type EndpointInfo interface { LoadBalancer() bool } -// InterfaceInfo provides an interface to retrieve interface addresses bound to the endpoint. -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 - - // LinkLocalAddresses returns the list of link-local (IPv4/IPv6) addresses assigned to the endpoint. - LinkLocalAddresses() []*net.IPNet - - // SrcName returns the name of the interface w/in the container - SrcName() string -} - -type endpointInterface struct { +// EndpointInterface holds interface addresses bound to the endpoint. +type EndpointInterface struct { mac net.HardwareAddr addr *net.IPNet addrv6 *net.IPNet @@ -66,7 +48,7 @@ type endpointInterface struct { v6PoolID string } -func (epi *endpointInterface) MarshalJSON() ([]byte, error) { +func (epi *EndpointInterface) MarshalJSON() ([]byte, error) { epMap := make(map[string]interface{}) if epi.mac != nil { epMap["mac"] = epi.mac.String() @@ -96,7 +78,7 @@ func (epi *endpointInterface) MarshalJSON() ([]byte, error) { return json.Marshal(epMap) } -func (epi *endpointInterface) UnmarshalJSON(b []byte) error { +func (epi *EndpointInterface) UnmarshalJSON(b []byte) error { var ( err error epMap map[string]interface{} @@ -153,7 +135,7 @@ func (epi *endpointInterface) UnmarshalJSON(b []byte) error { return nil } -func (epi *endpointInterface) CopyTo(dstEpi *endpointInterface) error { +func (epi *EndpointInterface) CopyTo(dstEpi *EndpointInterface) error { dstEpi.mac = types.GetMacCopy(epi.mac) dstEpi.addr = types.GetIPNetCopy(epi.addr) dstEpi.addrv6 = types.GetIPNetCopy(epi.addrv6) @@ -212,7 +194,7 @@ func (ep *Endpoint) Info() EndpointInfo { return sb.getEndpoint(ep.ID()) } -func (ep *Endpoint) Iface() InterfaceInfo { +func (ep *Endpoint) Iface() *EndpointInterface { ep.mu.Lock() defer ep.mu.Unlock() @@ -234,7 +216,9 @@ func (ep *Endpoint) Interface() driverapi.InterfaceInfo { return nil } -func (epi *endpointInterface) SetMacAddress(mac net.HardwareAddr) error { +// SetMacAddress allows the driver to set the mac address to the endpoint interface +// during the call to CreateEndpoint, if the mac address is not already set. +func (epi *EndpointInterface) SetMacAddress(mac net.HardwareAddr) error { if epi.mac != nil { return types.ForbiddenErrorf("endpoint interface MAC address present (%s). Cannot be modified with %s.", epi.mac, mac) } @@ -245,7 +229,7 @@ func (epi *endpointInterface) SetMacAddress(mac net.HardwareAddr) error { return nil } -func (epi *endpointInterface) SetIPAddress(address *net.IPNet) error { +func (epi *EndpointInterface) SetIPAddress(address *net.IPNet) error { if address.IP == nil { return types.InvalidParameterErrorf("tried to set nil IP address to endpoint interface") } @@ -263,27 +247,33 @@ func setAddress(ifaceAddr **net.IPNet, address *net.IPNet) error { return nil } -func (epi *endpointInterface) MacAddress() net.HardwareAddr { +// MacAddress returns the MAC address assigned to the endpoint. +func (epi *EndpointInterface) MacAddress() net.HardwareAddr { return types.GetMacCopy(epi.mac) } -func (epi *endpointInterface) Address() *net.IPNet { +// Address returns the IPv4 address assigned to the endpoint. +func (epi *EndpointInterface) Address() *net.IPNet { return types.GetIPNetCopy(epi.addr) } -func (epi *endpointInterface) AddressIPv6() *net.IPNet { +// AddressIPv6 returns the IPv6 address assigned to the endpoint. +func (epi *EndpointInterface) AddressIPv6() *net.IPNet { return types.GetIPNetCopy(epi.addrv6) } -func (epi *endpointInterface) LinkLocalAddresses() []*net.IPNet { +// LinkLocalAddresses returns the list of link-local (IPv4/IPv6) addresses assigned to the endpoint. +func (epi *EndpointInterface) LinkLocalAddresses() []*net.IPNet { return epi.llAddrs } -func (epi *endpointInterface) SrcName() string { +// SrcName returns the name of the interface w/in the container +func (epi *EndpointInterface) SrcName() string { return epi.srcName } -func (epi *endpointInterface) SetNames(srcName string, dstPrefix string) error { +// SetNames method assigns the srcName and dstPrefix for the interface. +func (epi *EndpointInterface) SetNames(srcName string, dstPrefix string) error { epi.srcName = srcName epi.dstPrefix = dstPrefix return nil diff --git a/libnetwork/libnetwork_internal_test.go b/libnetwork/libnetwork_internal_test.go index 562e9aae16..fca86adbf4 100644 --- a/libnetwork/libnetwork_internal_test.go +++ b/libnetwork/libnetwork_internal_test.go @@ -191,7 +191,7 @@ func TestEndpointMarshalling(t *testing.T) { id: "efghijklmno", sandboxID: "ambarabaciccicocco", anonymous: true, - iface: &endpointInterface{ + iface: &EndpointInterface{ mac: []byte{11, 12, 13, 14, 15, 16}, addr: &net.IPNet{ IP: net.IP{10, 0, 1, 23}, @@ -222,7 +222,7 @@ func TestEndpointMarshalling(t *testing.T) { } } -func compareEndpointInterface(a, b *endpointInterface) bool { +func compareEndpointInterface(a, b *EndpointInterface) bool { if a == b { return true } diff --git a/libnetwork/network.go b/libnetwork/network.go index a67fb4ca78..586994bcc1 100644 --- a/libnetwork/network.go +++ b/libnetwork/network.go @@ -1119,7 +1119,7 @@ func (n *Network) CreateEndpoint(name string, options ...EndpointOption) (*Endpo func (n *Network) createEndpoint(name string, options ...EndpointOption) (*Endpoint, error) { var err error - ep := &Endpoint{name: name, generic: make(map[string]interface{}), iface: &endpointInterface{}} + ep := &Endpoint{name: name, generic: make(map[string]interface{}), iface: &EndpointInterface{}} ep.id = stringid.GenerateRandomID() // Initialize ep.network with a possibly stale copy of n. We need this to get network from