Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[17.09] Adding ipam options to ipam driver requests #2455

Merged
merged 2 commits into from
Jan 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions manager/allocator/cnmallocator/networkallocator.go
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,7 @@ func (na *cnmNetworkAllocator) releaseEndpoints(networks []*api.NetworkAttachmen

// allocate virtual IP for a single endpoint attachment of the service.
func (na *cnmNetworkAllocator) 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 @@ -582,9 +583,13 @@ func (na *cnmNetworkAllocator) 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 @@ -636,6 +641,7 @@ func (na *cnmNetworkAllocator) deallocateVIP(vip *api.Endpoint_VirtualIP) error
// allocate the IP addresses for a single network attachment of the task.
func (na *cnmNetworkAllocator) 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 @@ -665,11 +671,16 @@ func (na *cnmNetworkAllocator) allocateNetworkIPs(nAttach *api.NetworkAttachment
}
}
}
// 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 @@ -897,8 +908,16 @@ func (na *cnmNetworkAllocator) allocatePools(n *api.Network) (map[string]string,
}
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 @@ -959,3 +978,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/cnmallocator/networkallocator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -772,8 +772,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 @@ -786,7 +786,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/cnmallocator/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 @@ -24,7 +24,7 @@ github.com/docker/go-connections 3ede32e2033de7505e6500d6c868c2b9ed9f169d
github.com/docker/go-events 9461782956ad83b30282bf90e31fa6a70c255ba9
github.com/docker/go-units 954fed01cc617c55d838fa2230073f2cb17386c8
github.com/docker/libkv 9fd56606e928ff1f309808f5d5a0b7a2ef73f9a8
github.com/docker/libnetwork 19ac3ea7f52bb46e0eb10669756cdae0c441a5b1
github.com/docker/libnetwork 21544598c53fa36a3c771a8725c643dd2340f845
github.com/docker/libtrust 9cbd2a1374f46905c68a4eb3694a130610adc62a
github.com/opencontainers/runc d40db12e72a40109dfcf28539f5ee0930d2f0277
github.com/opencontainers/go-digest 21dfd564fd89c944783d00d069f33e3e7123c448
Expand Down
49 changes: 39 additions & 10 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.

4 changes: 2 additions & 2 deletions vendor/github.com/docker/libnetwork/types/types.go

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

2 changes: 1 addition & 1 deletion vendor/github.com/docker/libnetwork/vendor.conf

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