Merge pull request #583 from containernetworking/bugfix/wrong_startrange

host-local: remove redundant startRange in RangeIterator to avoid mismatching with startIP
This commit is contained in:
Casey Callendrello 2021-03-10 17:20:11 +01:00 committed by GitHub
commit 2989aba9dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 6 deletions

View File

@ -22,6 +22,7 @@ import (
"strconv" "strconv"
current "github.com/containernetworking/cni/pkg/types/100" current "github.com/containernetworking/cni/pkg/types/100"
"github.com/containernetworking/plugins/pkg/ip" "github.com/containernetworking/plugins/pkg/ip"
"github.com/containernetworking/plugins/plugins/ipam/host-local/backend" "github.com/containernetworking/plugins/plugins/ipam/host-local/backend"
) )
@ -132,9 +133,8 @@ type RangeIter struct {
// Our current position // Our current position
cur net.IP cur net.IP
// The IP and range index where we started iterating; if we hit this again, we're done. // The IP where we started iterating; if we hit this again, we're done.
startIP net.IP startIP net.IP
startRange int
} }
// GetIter encapsulates the strategy for this allocator. // GetIter encapsulates the strategy for this allocator.
@ -164,7 +164,6 @@ func (a *IPAllocator) GetIter() (*RangeIter, error) {
for i, r := range *a.rangeset { for i, r := range *a.rangeset {
if r.Contains(lastReservedIP) { if r.Contains(lastReservedIP) {
iter.rangeIdx = i iter.rangeIdx = i
iter.startRange = i
// We advance the cursor on every Next(), so the first call // We advance the cursor on every Next(), so the first call
// to next() will return lastReservedIP + 1 // to next() will return lastReservedIP + 1
@ -174,7 +173,6 @@ func (a *IPAllocator) GetIter() (*RangeIter, error) {
} }
} else { } else {
iter.rangeIdx = 0 iter.rangeIdx = 0
iter.startRange = 0
iter.startIP = (*a.rangeset)[0].RangeStart iter.startIP = (*a.rangeset)[0].RangeStart
} }
return &iter, nil return &iter, nil
@ -210,7 +208,7 @@ func (i *RangeIter) Next() (*net.IPNet, net.IP) {
if i.startIP == nil { if i.startIP == nil {
i.startIP = i.cur i.startIP = i.cur
} else if i.rangeIdx == i.startRange && i.cur.Equal(i.startIP) { } else if i.cur.Equal(i.startIP) {
// IF we've looped back to where we started, give up // IF we've looped back to where we started, give up
return nil, nil return nil, nil
} }

View File

@ -49,6 +49,23 @@ func mkalloc() IPAllocator {
return alloc return alloc
} }
func newAllocatorWithMultiRanges() IPAllocator {
p := RangeSet{
Range{RangeStart: net.IP{192, 168, 1, 0}, RangeEnd: net.IP{192, 168, 1, 3}, Subnet: mustSubnet("192.168.1.1/30")},
Range{RangeStart: net.IP{192, 168, 2, 0}, RangeEnd: net.IP{192, 168, 2, 3}, Subnet: mustSubnet("192.168.2.1/30")},
}
_ = p.Canonicalize()
store := fakestore.NewFakeStore(map[string]string{}, map[string]net.IP{})
alloc := IPAllocator{
rangeset: &p,
store: store,
rangeID: "rangeid",
}
return alloc
}
func (t AllocatorTestCase) run(idx int) (*current.IPConfig, error) { func (t AllocatorTestCase) run(idx int) (*current.IPConfig, error) {
fmt.Fprintln(GinkgoWriter, "Index:", idx) fmt.Fprintln(GinkgoWriter, "Index:", idx)
p := RangeSet{} p := RangeSet{}
@ -320,6 +337,39 @@ var _ = Describe("host-local ip allocator", func() {
} }
}) })
}) })
Context("when lastReservedIP is at the end of one of multi ranges", func() {
It("should use the first IP of next range as startIP after Next", func() {
a := newAllocatorWithMultiRanges()
// reserve the last IP of the first range
reserved, err := a.store.Reserve("ID", "eth0", net.IP{192, 168, 1, 3}, a.rangeID)
Expect(reserved).To(BeTrue())
Expect(err).NotTo(HaveOccurred())
// get range iterator and do the first Next
r, err := a.GetIter()
Expect(err).NotTo(HaveOccurred())
ip := r.nextip()
Expect(ip).NotTo(BeNil())
Expect(r.startIP).To(Equal(net.IP{192, 168, 2, 0}))
})
})
Context("when no lastReservedIP", func() {
It("should use the first IP of the first range as startIP after Next", func() {
a := newAllocatorWithMultiRanges()
// get range iterator and do the first Next
r, err := a.GetIter()
Expect(err).NotTo(HaveOccurred())
ip := r.nextip()
Expect(ip).NotTo(BeNil())
Expect(r.startIP).To(Equal(net.IP{192, 168, 1, 0}))
})
})
}) })
// nextip is a convenience function used for testing // nextip is a convenience function used for testing