diff --git a/ipamapi/contract.go b/ipamapi/contract.go index ae6ecc8990..0c85a7527b 100644 --- a/ipamapi/contract.go +++ b/ipamapi/contract.go @@ -79,4 +79,6 @@ type Ipam interface { // Capability represents the requirements and capabilities of the IPAM driver type Capability struct { RequiresMACAddress bool + RequiresEndpointID bool + RequiresNetworkID bool } diff --git a/ipams/remote/api/api.go b/ipams/remote/api/api.go index e357630cbb..6e5c818fb2 100644 --- a/ipams/remote/api/api.go +++ b/ipams/remote/api/api.go @@ -23,11 +23,16 @@ func (r *Response) GetError() string { type GetCapabilityResponse struct { Response RequiresMACAddress bool + RequiresEndpointID bool + RequiresNetworkID bool } // ToCapability converts the capability response into the internal ipam driver capaility structure func (capRes GetCapabilityResponse) ToCapability() *ipamapi.Capability { - return &ipamapi.Capability{RequiresMACAddress: capRes.RequiresMACAddress} + return &ipamapi.Capability{ + RequiresMACAddress: capRes.RequiresMACAddress, + RequiresEndpointID: capRes.RequiresEndpointID, + RequiresNetworkID: capRes.RequiresNetworkID} } // GetAddressSpacesResponse is the response to the ``get default address spaces`` request message diff --git a/netlabel/labels.go b/netlabel/labels.go index d44015f159..b0249c3309 100644 --- a/netlabel/labels.go +++ b/netlabel/labels.go @@ -24,6 +24,12 @@ const ( // MacAddress constant represents Mac Address config of a Container MacAddress = Prefix + ".endpoint.macaddress" + // NetworkID constant represents the network ID + NetworkID = Prefix + ".id" + + // EndpointID constant represents the endpoint ID + EndpointID = Prefix + ".endpoint.id" + // ExposedPorts constant represents the container's Exposed Ports ExposedPorts = Prefix + ".endpoint.exposedports" diff --git a/network.go b/network.go index 1ef4e569a0..6f1435fc9a 100644 --- a/network.go +++ b/network.go @@ -736,6 +736,13 @@ func (n *network) CreateEndpoint(name string, options ...EndpointOption) (Endpoi ep.ipamOptions[netlabel.MacAddress] = ep.iface.mac.String() } + if ipam.capability.RequiresEndpointID { + if ep.ipamOptions == nil { + ep.ipamOptions = make(map[string]string) + } + ep.ipamOptions[netlabel.EndpointID] = ep.id + } + if err = ep.assignAddress(ipam.driver, true, !n.postIPv6); err != nil { return nil, err } @@ -1009,6 +1016,18 @@ func (n *network) ipamAllocateVersion(ipVer int, ipam ipamapi.Ipam) error { *infoList = make([]*IpamInfo, len(*cfgList)) + id, err := n.getController().getIPAM(n.ipamType) + if err != nil { + return err + } + + if id.capability.RequiresNetworkID { + if n.ipamOptions == nil { + n.ipamOptions = make(map[string]string) + } + n.ipamOptions[netlabel.NetworkID] = n.ID() + } + log.Debugf("Allocating IPv%d pools for network %s (%s)", ipVer, n.Name(), n.ID()) for i, cfg := range *cfgList {