From 337da7e2218f6b67cd014b20f38ec885fa9c58d1 Mon Sep 17 00:00:00 2001 From: John Belamaric Date: Fri, 26 Feb 2016 10:58:42 -0500 Subject: [PATCH] Allow publishing of additional information to IPAM Adds an IPAM driver capability that allows libnetwork to send additional information during subnet and address allocation. Signed-off-by: John Belamaric --- ipamapi/contract.go | 2 ++ ipams/remote/api/api.go | 9 +++++++-- netlabel/labels.go | 15 +++++++++++++++ network.go | 23 +++++++++++++++++++++++ 4 files changed, 47 insertions(+), 2 deletions(-) diff --git a/ipamapi/contract.go b/ipamapi/contract.go index ae6ecc8990..f7d4cd0fb0 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 + RequiresEndpointInfo bool + RequiresNetworkInfo bool } diff --git a/ipams/remote/api/api.go b/ipams/remote/api/api.go index e357630cbb..acf7bd0c62 100644 --- a/ipams/remote/api/api.go +++ b/ipams/remote/api/api.go @@ -23,12 +23,17 @@ func (r *Response) GetError() string { type GetCapabilityResponse struct { Response RequiresMACAddress bool + RequiresEndpointInfo bool + RequiresNetworkInfo 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, + RequiresEndpointInfo: capRes.RequiresEndpointInfo, + RequiresNetworkInfo: capRes.RequiresNetworkInfo} + } // GetAddressSpacesResponse is the response to the ``get default address spaces`` request message type GetAddressSpacesResponse struct { diff --git a/netlabel/labels.go b/netlabel/labels.go index d44015f159..0ad89c7a45 100644 --- a/netlabel/labels.go +++ b/netlabel/labels.go @@ -24,6 +24,21 @@ const ( // MacAddress constant represents Mac Address config of a Container MacAddress = Prefix + ".endpoint.macaddress" + // Network name + NetworkName = Prefix + ".name" + + // Network ID + NetworkID = Prefix + ".id" + + // Network type + NetworkType = Prefix + ".type" + + // Endpoint name + EndpointName = Prefix + ".endpoint.name" + + // Endpoint cluster host ID + EndpointHost = Prefix + ".endpoint.cluster_host_id" + // ExposedPorts constant represents the container's Exposed Ports ExposedPorts = Prefix + ".endpoint.exposedports" diff --git a/network.go b/network.go index 1ef4e569a0..fe37587ed8 100644 --- a/network.go +++ b/network.go @@ -736,6 +736,15 @@ func (n *network) CreateEndpoint(name string, options ...EndpointOption) (Endpoi ep.ipamOptions[netlabel.MacAddress] = ep.iface.mac.String() } + if ipam.capability.RequiresEndpointInfo { + if ep.ipamOptions == nil { + ep.ipamOptions = make(map[string]string) + } + log.Debugf("Endpoint: %s", ep) + ep.ipamOptions[netlabel.EndpointName] = name + ep.ipamOptions[netlabel.EndpointHost] = ep.locator + } + if err = ep.assignAddress(ipam.driver, true, !n.postIPv6); err != nil { return nil, err } @@ -1009,6 +1018,20 @@ 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.RequiresNetworkInfo { + if n.ipamOptions == nil { + n.ipamOptions = make(map[string]string) + } + n.ipamOptions[netlabel.NetworkName] = n.Name() + n.ipamOptions[netlabel.NetworkID] = n.ID() + n.ipamOptions[netlabel.NetworkType] = n.Type() + } + log.Debugf("Allocating IPv%d pools for network %s (%s)", ipVer, n.Name(), n.ID()) for i, cfg := range *cfgList {