Fix double counting of IP addresses

The range allocator in pkg/controller/nodeipam/ipam/range_allocator.go
may call Occupy() on the same range twice:

1. Just before subscribing to the NodeInformer
2. From a callback given to the NodeInformer soon after registration
This commit is contained in:
Hristo Venev 2020-10-26 01:40:05 -07:00
parent 59405cc2b4
commit 4d28391c24

View File

@ -232,16 +232,20 @@ func (s *CidrSet) Release(cidr *net.IPNet) error {
s.Lock() s.Lock()
defer s.Unlock() defer s.Unlock()
for i := begin; i <= end; i++ { for i := begin; i <= end; i++ {
// Only change the counters if we change the bit to prevent
// double counting.
if s.used.Bit(i) != 0 {
s.used.SetBit(&s.used, i, 0) s.used.SetBit(&s.used, i, 0)
s.allocatedCIDRs-- s.allocatedCIDRs--
cidrSetReleases.WithLabelValues(s.label).Inc() cidrSetReleases.WithLabelValues(s.label).Inc()
} }
}
cidrSetUsage.WithLabelValues(s.label).Set(float64(s.allocatedCIDRs) / float64(s.maxCIDRs)) cidrSetUsage.WithLabelValues(s.label).Set(float64(s.allocatedCIDRs) / float64(s.maxCIDRs))
return nil return nil
} }
// Occupy marks the given CIDR range as used. Occupy does not check if the CIDR // Occupy marks the given CIDR range as used. Occupy succeeds even if the CIDR
// range was previously used. // range was previously used.
func (s *CidrSet) Occupy(cidr *net.IPNet) (err error) { func (s *CidrSet) Occupy(cidr *net.IPNet) (err error) {
begin, end, err := s.getBeginingAndEndIndices(cidr) begin, end, err := s.getBeginingAndEndIndices(cidr)
@ -251,10 +255,14 @@ func (s *CidrSet) Occupy(cidr *net.IPNet) (err error) {
s.Lock() s.Lock()
defer s.Unlock() defer s.Unlock()
for i := begin; i <= end; i++ { for i := begin; i <= end; i++ {
// Only change the counters if we change the bit to prevent
// double counting.
if s.used.Bit(i) == 0 {
s.used.SetBit(&s.used, i, 1) s.used.SetBit(&s.used, i, 1)
s.allocatedCIDRs++ s.allocatedCIDRs++
cidrSetAllocations.WithLabelValues(s.label).Inc() cidrSetAllocations.WithLabelValues(s.label).Inc()
} }
}
cidrSetUsage.WithLabelValues(s.label).Set(float64(s.allocatedCIDRs) / float64(s.maxCIDRs)) cidrSetUsage.WithLabelValues(s.label).Set(float64(s.allocatedCIDRs) / float64(s.maxCIDRs))
return nil return nil