Skip to content

Commit

Permalink
Backport swarmkit fix moby#2324
Browse files Browse the repository at this point in the history
Signed-off-by: Abhinandan Prativadi <[email protected]>
  • Loading branch information
abhi committed Oct 4, 2017
1 parent ce88f8a commit ecf35f7
Show file tree
Hide file tree
Showing 12 changed files with 122 additions and 40 deletions.
36 changes: 33 additions & 3 deletions manager/allocator/networkallocator/networkallocator.go
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,7 @@ func (na *NetworkAllocator) releaseEndpoints(networks []*api.NetworkAttachment)

// allocate virtual IP for a single endpoint attachment of the service.
func (na *NetworkAllocator) allocateVIP(vip *api.Endpoint_VirtualIP) error {
var opts map[string]string
localNet := na.getNetwork(vip.NetworkID)
if localNet == nil {
return errors.New("networkallocator: could not find local network state")
Expand Down Expand Up @@ -602,9 +603,13 @@ func (na *NetworkAllocator) allocateVIP(vip *api.Endpoint_VirtualIP) error {
return err
}
}
if localNet.nw.IPAM != nil && localNet.nw.IPAM.Driver != nil {
// set ipam allocation method to serial
opts = setIPAMSerialAlloc(localNet.nw.IPAM.Driver.Options)
}

for _, poolID := range localNet.pools {
ip, _, err := ipam.RequestAddress(poolID, addr, nil)
ip, _, err := ipam.RequestAddress(poolID, addr, opts)
if err != nil && err != ipamapi.ErrNoAvailableIPs && err != ipamapi.ErrIPOutOfRange {
return errors.Wrap(err, "could not allocate VIP from IPAM")
}
Expand Down Expand Up @@ -656,6 +661,7 @@ func (na *NetworkAllocator) deallocateVIP(vip *api.Endpoint_VirtualIP) error {
// allocate the IP addresses for a single network attachment of the task.
func (na *NetworkAllocator) allocateNetworkIPs(nAttach *api.NetworkAttachment) error {
var ip *net.IPNet
var opts map[string]string

ipam, _, _, err := na.resolveIPAM(nAttach.Network)
if err != nil {
Expand Down Expand Up @@ -685,11 +691,16 @@ func (na *NetworkAllocator) allocateNetworkIPs(nAttach *api.NetworkAttachment) e
}
}
}
// Set the ipam options if the network has an ipam driver.
if localNet.nw.IPAM != nil && localNet.nw.IPAM.Driver != nil {
// set ipam allocation method to serial
opts = setIPAMSerialAlloc(localNet.nw.IPAM.Driver.Options)
}

for _, poolID := range localNet.pools {
var err error

ip, _, err = ipam.RequestAddress(poolID, addr, nil)
ip, _, err = ipam.RequestAddress(poolID, addr, opts)
if err != nil && err != ipamapi.ErrNoAvailableIPs && err != ipamapi.ErrIPOutOfRange {
return errors.Wrap(err, "could not allocate IP from IPAM")
}
Expand Down Expand Up @@ -917,8 +928,16 @@ func (na *NetworkAllocator) allocatePools(n *api.Network) (map[string]string, er
}
gwIP.IP = ip
}
if dOptions == nil {
dOptions = make(map[string]string)
}
dOptions[ipamapi.RequestAddressType] = netlabel.Gateway
// set ipam allocation method to serial
dOptions = setIPAMSerialAlloc(dOptions)
defer delete(dOptions, ipamapi.RequestAddressType)

if ic.Gateway != "" || gwIP == nil {
gwIP, _, err = ipam.RequestAddress(poolID, net.ParseIP(ic.Gateway), map[string]string{ipamapi.RequestAddressType: netlabel.Gateway})
gwIP, _, err = ipam.RequestAddress(poolID, net.ParseIP(ic.Gateway), dOptions)
if err != nil {
// Rollback by releasing all the resources allocated so far.
releasePools(ipam, ipamConfigs[:i], pools)
Expand Down Expand Up @@ -1012,3 +1031,14 @@ func IsBuiltInDriver(name string) bool {
}
return false
}

// setIPAMSerialAlloc sets the ipam allocation method to serial
func setIPAMSerialAlloc(opts map[string]string) map[string]string {
if opts == nil {
opts = make(map[string]string)
}
if _, ok := opts[ipamapi.AllocSerialPrefix]; !ok {
opts[ipamapi.AllocSerialPrefix] = "true"
}
return opts
}
6 changes: 3 additions & 3 deletions manager/allocator/networkallocator/networkallocator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -771,8 +771,8 @@ func TestServiceAddRemovePortsIngressMode(t *testing.T) {
assert.Len(t, s.Endpoint.Ports, 0)
assert.Len(t, s.Endpoint.VirtualIPs, 0)

// Publish port again and ensure VIP is the same that was deallocated
// and there is no leak.
// Publish port again and ensure VIP is not the same that was deallocated.
// Since IP allocation is serial we should receive the next available IP.
s.Spec.Endpoint.Ports = append(s.Spec.Endpoint.Ports, &api.PortConfig{Name: "some_tcp",
TargetPort: 1234,
PublishedPort: 1234,
Expand All @@ -785,7 +785,7 @@ func TestServiceAddRemovePortsIngressMode(t *testing.T) {
assert.Len(t, s.Endpoint.Ports, 1)
assert.Equal(t, uint32(1234), s.Endpoint.Ports[0].PublishedPort)
assert.Len(t, s.Endpoint.VirtualIPs, 1)
assert.Equal(t, allocatedVIP, s.Endpoint.VirtualIPs[0].Addr)
assert.NotEqual(t, allocatedVIP, s.Endpoint.VirtualIPs[0].Addr)
}

func TestServiceUpdate(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion manager/allocator/networkallocator/portallocator.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ func (ps *portSpace) allocate(p *api.PortConfig) (err error) {
}

// Check out an arbitrary port from dynamic port space.
swarmPort, err := ps.dynamicPortSpace.GetID()
swarmPort, err := ps.dynamicPortSpace.GetID(true)
if err != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion vendor.conf
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ github.com/docker/go-connections 34b5052da6b11e27f5f2e357b38b571ddddd3928
github.com/docker/go-events 37d35add5005832485c0225ec870121b78fcff1c
github.com/docker/go-units 954fed01cc617c55d838fa2230073f2cb17386c8
github.com/docker/libkv 9fd56606e928ff1f309808f5d5a0b7a2ef73f9a8
github.com/docker/libnetwork 37e20af882e13dd01ade3658b7aabdae3412118b
github.com/docker/libnetwork abace684af65145d7dd8f56fb4033770bb3bb2ed
github.com/docker/libtrust 9cbd2a1374f46905c68a4eb3694a130610adc62a
github.com/opencontainers/runc 8e8d01d38d7b4fb0a35bf89b72bc3e18c98882d7
github.com/opencontainers/go-digest a6d0ee40d4207ea02364bd3b9e8e77b9159ba1eb
Expand Down
57 changes: 45 additions & 12 deletions vendor/github.com/docker/libnetwork/bitseq/sequence.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions vendor/github.com/docker/libnetwork/bitseq/store.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions vendor/github.com/docker/libnetwork/idm/idm.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 14 additions & 6 deletions vendor/github.com/docker/libnetwork/ipam/allocator.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions vendor/github.com/docker/libnetwork/ipamapi/labels.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ecf35f7

Please sign in to comment.