From 35f1a1f3a12918526536553a9eabeb31430768cb Mon Sep 17 00:00:00 2001 From: Ryan Belgrave Date: Thu, 3 Dec 2015 22:20:42 -0500 Subject: [PATCH] When assigning an address for an endpoint set preferred address and options. When requesting a gateway address send a gateway label in the options. Signed-off-by: Ryan Belgrave --- endpoint.go | 12 +++++++++++- ipamapi/contract.go | 2 ++ network.go | 5 ++++- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/endpoint.go b/endpoint.go index 4e475b3093..050a3b3bde 100644 --- a/endpoint.go +++ b/endpoint.go @@ -60,6 +60,8 @@ type endpoint struct { anonymous bool generic map[string]interface{} joinLeaveDone chan struct{} + prefAddress net.IP + ipamOptions map[string]string dbIndex uint64 dbExists bool sync.Mutex @@ -685,6 +687,14 @@ func EndpointOptionGeneric(generic map[string]interface{}) EndpointOption { } } +// CreateOptionIpam function returns an option setter for the ipam configuration for this endpoint +func CreateOptionIpam(prefAddress net.IP, ipamOptions map[string]string) EndpointOption { + return func(ep *endpoint) { + ep.prefAddress = prefAddress + ep.ipamOptions = ipamOptions + } +} + // CreateOptionExposedPorts function returns an option setter for the container exposed // ports option to be passed to network.CreateEndpoint() method. func CreateOptionExposedPorts(exposedPorts []types.TransportPort) EndpointOption { @@ -799,7 +809,7 @@ func (ep *endpoint) assignAddressVersion(ipVer int, ipam ipamapi.Ipam) error { if *address != nil { prefIP = (*address).IP } - addr, _, err := ipam.RequestAddress(d.PoolID, prefIP, nil) + addr, _, err := ipam.RequestAddress(d.PoolID, prefIP, ep.ipamOptions) if err == nil { ep.Lock() *address = addr diff --git a/ipamapi/contract.go b/ipamapi/contract.go index a3d98d1e92..5323c4f7df 100644 --- a/ipamapi/contract.go +++ b/ipamapi/contract.go @@ -16,6 +16,8 @@ const ( DefaultIPAM = "default" // PluginEndpointType represents the Endpoint Type used by Plugin system PluginEndpointType = "IpamDriver" + // RequestAddressType represents the Address Type used when requesting an address + RequestAddressType = "RequestAddressType" ) // Callback provides a Callback interface for registering an IPAM instance into LibNetwork diff --git a/network.go b/network.go index 552d5f0d19..be4ab70505 100644 --- a/network.go +++ b/network.go @@ -971,7 +971,10 @@ func (n *network) ipamAllocateVersion(ipVer int, ipam ipamapi.Ipam) error { // irrespective of whether ipam driver returned a gateway already. // If none of the above is true, libnetwork will allocate one. if cfg.Gateway != "" || d.Gateway == nil { - if d.Gateway, _, err = ipam.RequestAddress(d.PoolID, net.ParseIP(cfg.Gateway), nil); err != nil { + var gatewayOpts = map[string]string{ + ipamapi.RequestAddressType: netlabel.Gateway, + } + if d.Gateway, _, err = ipam.RequestAddress(d.PoolID, net.ParseIP(cfg.Gateway), gatewayOpts); err != nil { return types.InternalErrorf("failed to allocate gateway (%v): %v", cfg.Gateway, err) } }