Skip to content

Commit

Permalink
fix: do not reuse released ip after subnet updated
Browse files Browse the repository at this point in the history
  • Loading branch information
liqd1 committed Dec 15, 2021
1 parent 1624cc5 commit 6c8fa97
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/ipam/ipam.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ func (ipam *IPAM) AddOrUpdateSubnet(name, cidrStr string, excludeIps []string) e
lastIP, _ := util.LastIP(v4cidrStr)
subnet.V4FreeIPList = IPRangeList{&IPRange{Start: IP(firstIP), End: IP(lastIP)}}
subnet.joinFreeWithReserve()
subnet.V4ReleasedIPList = IPRangeList{}
for nicName, ip := range subnet.V4NicToIP {
mac := subnet.NicToMac[nicName]
podName := subnet.V4IPToPod[ip]
Expand All @@ -173,6 +174,7 @@ func (ipam *IPAM) AddOrUpdateSubnet(name, cidrStr string, excludeIps []string) e
lastIP, _ := util.LastIP(v6cidrStr)
subnet.V6FreeIPList = IPRangeList{&IPRange{Start: IP(firstIP), End: IP(lastIP)}}
subnet.joinFreeWithReserve()
subnet.V6ReleasedIPList = IPRangeList{}
for nicName, ip := range subnet.V6NicToIP {
mac := subnet.NicToMac[nicName]
podName := subnet.V6IPToPod[ip]
Expand Down
52 changes: 52 additions & 0 deletions test/unittest/ipam/ipam.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,23 @@ var _ = Describe("[IPAM]", func() {
Expect(err).ShouldNot(HaveOccurred())
Expect(ip).To(Equal("10.16.0.1"))
})

It("donot reuse released address after update subnet's excludedIps", func() {
im := ipam.NewIPAM()
err := im.AddOrUpdateSubnet(subnetName, "10.16.0.0/30", nil)
Expect(err).ShouldNot(HaveOccurred())

ip, _, _, err := im.GetRandomAddress("pod1.ns", "pod1.ns", subnetName, nil)
Expect(err).ShouldNot(HaveOccurred())
Expect(ip).To(Equal("10.16.0.1"))

im.ReleaseAddressByPod("pod1.ns")
err = im.AddOrUpdateSubnet(subnetName, "10.16.0.0/30", []string{"10.16.0.1..10.16.0.2"})
Expect(err).ShouldNot(HaveOccurred())

_, _, _, err = im.GetRandomAddress("pod1.ns", "pod1.ns", subnetName, nil)
Expect(err).Should(MatchError(ipam.ErrNoAvailable))
})
})

Context("[IPv6]", func() {
Expand Down Expand Up @@ -189,6 +206,23 @@ var _ = Describe("[IPAM]", func() {
Expect(err).ShouldNot(HaveOccurred())
Expect(ip).To(Equal("fd00::1"))
})

It("donot reuse released address after update subnet's excludedIps", func() {
im := ipam.NewIPAM()
err := im.AddOrUpdateSubnet(subnetName, "fd00::/126", nil)
Expect(err).ShouldNot(HaveOccurred())

_, ip, _, err := im.GetRandomAddress("pod1.ns", "pod1.ns", subnetName, nil)
Expect(err).ShouldNot(HaveOccurred())
Expect(ip).To(Equal("fd00::1"))

im.ReleaseAddressByPod("pod1.ns")
err = im.AddOrUpdateSubnet(subnetName, "fd00::/126", []string{"fd00::1..fd00::2"})
Expect(err).ShouldNot(HaveOccurred())

_, _, _, err = im.GetRandomAddress("pod1.ns", "pod1.ns", subnetName, nil)
Expect(err).Should(MatchError(ipam.ErrNoAvailable))
})
})

Context("[DualStack]", func() {
Expand Down Expand Up @@ -281,6 +315,24 @@ var _ = Describe("[IPAM]", func() {
Expect(ipv4).To(Equal("10.16.0.1"))
Expect(ipv6).To(Equal("fd00::1"))
})

It("donot reuse released address after update subnet's excludedIps", func() {
im := ipam.NewIPAM()
err := im.AddOrUpdateSubnet(subnetName, "10.16.0.2/30,fd00::/126", nil)
Expect(err).ShouldNot(HaveOccurred())

ipv4, ipv6, _, err := im.GetRandomAddress("pod1.ns", "pod1.ns", subnetName, nil)
Expect(err).ShouldNot(HaveOccurred())
Expect(ipv4).To(Equal("10.16.0.1"))
Expect(ipv6).To(Equal("fd00::1"))

im.ReleaseAddressByPod("pod1.ns")
err = im.AddOrUpdateSubnet(subnetName, "10.16.0.2/30,fd00::/126", []string{"10.16.0.1..10.16.0.2", "fd00::1..fd00::2"})
Expect(err).ShouldNot(HaveOccurred())

_, _, _, err = im.GetRandomAddress("pod1.ns", "pod1.ns", subnetName, nil)
Expect(err).Should(MatchError(ipam.ErrNoAvailable))
})
})
})

Expand Down

0 comments on commit 6c8fa97

Please sign in to comment.