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

host-local support idempotent allocation #328

Merged
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
host-local: make allocation idempotent to multiple requests with same id
Signed-off-by: Bruce Ma <[email protected]>
  • Loading branch information
mars1024 committed Jul 6, 2019
commit e8771b36a2934a8be89612f09f4adcc2a36ec93e
39 changes: 27 additions & 12 deletions plugins/ipam/host-local/backend/allocator/allocator.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func NewIPAllocator(s *RangeSet, store backend.Store, id int) *IPAllocator {
}
}

// Get alocates an IP
// Get allocates an IP
func (a *IPAllocator) Get(id string, ifname string, requestedIP net.IP) (*current.IPConfig, error) {
a.store.Lock()
defer a.store.Unlock()
Expand Down Expand Up @@ -73,24 +73,39 @@ func (a *IPAllocator) Get(id string, ifname string, requestedIP net.IP) (*curren
gw = r.Gateway

} else {
iter, err := a.GetIter()
if err != nil {
return nil, err
}
for {
reservedIP, gw = iter.Next()
if reservedIP == nil {
// try to get existing IPs which have been allocated to this id
existIPs := a.store.GetByID(id, ifname)
for _, existIP := range existIPs {
// check whether the existing IP belong to this range set
if r, err := a.rangeset.RangeFor(existIP); err == nil {
reservedIP = &net.IPNet{IP: existIP, Mask: r.Subnet.Mask}
gw = r.Gateway
break
}
}

reserved, err := a.store.Reserve(id, ifname, reservedIP.IP, a.rangeID)
// if no existing IP was found, try to reserve a new one
if reservedIP == nil {
iter, err := a.GetIter()
if err != nil {
return nil, err
}

if reserved {
break
for {
reservedIP, gw = iter.Next()
if reservedIP == nil {
break
}

reserved, err := a.store.Reserve(id, ifname, reservedIP.IP, a.rangeID)
if err != nil {
return nil, err
}

if reserved {
break
}
}

}
}

Expand Down