Skip to content

Commit

Permalink
ipam: fix duplicate allocation after cidr expansion (#3455)
Browse files Browse the repository at this point in the history
Signed-off-by: zhangzujian <zhangzujian.7@gmail.com>
  • Loading branch information
zhangzujian committed Nov 24, 2023
1 parent 23a4b73 commit 7dc00aa
Showing 1 changed file with 6 additions and 20 deletions.
26 changes: 6 additions & 20 deletions pkg/ipam/ipam.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,9 @@ func (ipam *IPAM) AddOrUpdateSubnet(name, cidrStr, gw string, excludeIps []strin
firstIP, _ := util.FirstIP(v4cidrStr)
lastIP, _ := util.LastIP(v4cidrStr)
ips, _ := NewIPRangeListFrom(fmt.Sprintf("%s..%s", firstIP, lastIP))
subnet.V4Free = ips.Separate(subnet.V4Reserved)
subnet.V4Available = subnet.V4Free.Clone()
subnet.V4Using = subnet.V4Using.Intersect(ips)
subnet.V4Free = ips.Separate(subnet.V4Reserved).Separate(subnet.V4Using)
subnet.V4Available = subnet.V4Free.Clone()
subnet.V4Gw = v4Gw

pool := subnet.IPPools[""]
Expand All @@ -222,7 +222,7 @@ func (ipam *IPAM) AddOrUpdateSubnet(name, cidrStr, gw string, excludeIps []strin
p.V4Available = p.V4Free.Clone()
p.V4Released = NewEmptyIPRangeList()
pool.V4Free = pool.V4Free.Separate(p.V4IPs)
pool.V4Reserved = p.V4Reserved.Separate(p.V4Reserved)
pool.V4Reserved = pool.V4Reserved.Separate(p.V4Reserved)
}
pool.V4Available = pool.V4Free.Clone()

Expand All @@ -232,13 +232,6 @@ func (ipam *IPAM) AddOrUpdateSubnet(name, cidrStr, gw string, excludeIps []strin
klog.Errorf("%s address %s not in subnet %s new cidr %s", podName, ip.String(), name, cidrStr)
delete(subnet.V4NicToIP, nicName)
delete(subnet.V4IPToPod, ip.String())
} else {
// The already assigned addresses should be added in ipam again when subnet cidr changed
pool.V4Available.Remove(ip)
pool.V4Using.Add(ip)
subnet.V4Free.Remove(ip)
subnet.V4Available.Remove(ip)
subnet.V4Using.Add(ip)
}
}

Expand All @@ -254,9 +247,9 @@ func (ipam *IPAM) AddOrUpdateSubnet(name, cidrStr, gw string, excludeIps []strin
firstIP, _ := util.FirstIP(v6cidrStr)
lastIP, _ := util.LastIP(v6cidrStr)
ips, _ := NewIPRangeListFrom(fmt.Sprintf("%s..%s", firstIP, lastIP))
subnet.V6Free = ips.Separate(subnet.V6Reserved)
subnet.V6Available = subnet.V6Free.Clone()
subnet.V6Using = subnet.V6Using.Intersect(ips)
subnet.V6Free = ips.Separate(subnet.V6Reserved).Separate(subnet.V6Using)
subnet.V6Available = subnet.V6Free.Clone()
subnet.V6Gw = v6Gw

pool := subnet.IPPools[""]
Expand All @@ -275,7 +268,7 @@ func (ipam *IPAM) AddOrUpdateSubnet(name, cidrStr, gw string, excludeIps []strin
p.V6Available = p.V6Free.Clone()
p.V6Released = NewEmptyIPRangeList()
pool.V6Free = pool.V6Free.Separate(p.V6IPs)
pool.V6Reserved = p.V6Reserved.Separate(p.V6Reserved)
pool.V6Reserved = pool.V6Reserved.Separate(p.V6Reserved)
}
pool.V6Available = pool.V6Free.Clone()

Expand All @@ -285,13 +278,6 @@ func (ipam *IPAM) AddOrUpdateSubnet(name, cidrStr, gw string, excludeIps []strin
klog.Errorf("%s address %s not in subnet %s new cidr %s", podName, ip.String(), name, cidrStr)
delete(subnet.V6NicToIP, nicName)
delete(subnet.V6IPToPod, ip.String())
} else {
// The already assigned addresses should be added in ipam again when subnet cidr changed
pool.V6Available.Remove(ip)
pool.V6Using.Add(ip)
subnet.V6Free.Remove(ip)
subnet.V6Available.Remove(ip)
subnet.V6Using.Add(ip)
}
}

Expand Down

0 comments on commit 7dc00aa

Please sign in to comment.