Skip to content

Commit 624b8aa

Browse files
authored
Merge pull request #1172 from liqd1/bugfix/reset-subnet-releasedIPList
fix: do not reuse released ip after subnet updated
2 parents 7919901 + 6c8fa97 commit 624b8aa

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

pkg/ipam/ipam.go

+2
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ func (ipam *IPAM) AddOrUpdateSubnet(name, cidrStr string, excludeIps []string) e
157157
lastIP, _ := util.LastIP(v4cidrStr)
158158
subnet.V4FreeIPList = IPRangeList{&IPRange{Start: IP(firstIP), End: IP(lastIP)}}
159159
subnet.joinFreeWithReserve()
160+
subnet.V4ReleasedIPList = IPRangeList{}
160161
for nicName, ip := range subnet.V4NicToIP {
161162
mac := subnet.NicToMac[nicName]
162163
podName := subnet.V4IPToPod[ip]
@@ -173,6 +174,7 @@ func (ipam *IPAM) AddOrUpdateSubnet(name, cidrStr string, excludeIps []string) e
173174
lastIP, _ := util.LastIP(v6cidrStr)
174175
subnet.V6FreeIPList = IPRangeList{&IPRange{Start: IP(firstIP), End: IP(lastIP)}}
175176
subnet.joinFreeWithReserve()
177+
subnet.V6ReleasedIPList = IPRangeList{}
176178
for nicName, ip := range subnet.V6NicToIP {
177179
mac := subnet.NicToMac[nicName]
178180
podName := subnet.V6IPToPod[ip]

test/unittest/ipam/ipam.go

+52
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,23 @@ var _ = Describe("[IPAM]", func() {
109109
Expect(err).ShouldNot(HaveOccurred())
110110
Expect(ip).To(Equal("10.16.0.1"))
111111
})
112+
113+
It("donot reuse released address after update subnet's excludedIps", func() {
114+
im := ipam.NewIPAM()
115+
err := im.AddOrUpdateSubnet(subnetName, "10.16.0.0/30", nil)
116+
Expect(err).ShouldNot(HaveOccurred())
117+
118+
ip, _, _, err := im.GetRandomAddress("pod1.ns", "pod1.ns", subnetName, nil)
119+
Expect(err).ShouldNot(HaveOccurred())
120+
Expect(ip).To(Equal("10.16.0.1"))
121+
122+
im.ReleaseAddressByPod("pod1.ns")
123+
err = im.AddOrUpdateSubnet(subnetName, "10.16.0.0/30", []string{"10.16.0.1..10.16.0.2"})
124+
Expect(err).ShouldNot(HaveOccurred())
125+
126+
_, _, _, err = im.GetRandomAddress("pod1.ns", "pod1.ns", subnetName, nil)
127+
Expect(err).Should(MatchError(ipam.ErrNoAvailable))
128+
})
112129
})
113130

114131
Context("[IPv6]", func() {
@@ -189,6 +206,23 @@ var _ = Describe("[IPAM]", func() {
189206
Expect(err).ShouldNot(HaveOccurred())
190207
Expect(ip).To(Equal("fd00::1"))
191208
})
209+
210+
It("donot reuse released address after update subnet's excludedIps", func() {
211+
im := ipam.NewIPAM()
212+
err := im.AddOrUpdateSubnet(subnetName, "fd00::/126", nil)
213+
Expect(err).ShouldNot(HaveOccurred())
214+
215+
_, ip, _, err := im.GetRandomAddress("pod1.ns", "pod1.ns", subnetName, nil)
216+
Expect(err).ShouldNot(HaveOccurred())
217+
Expect(ip).To(Equal("fd00::1"))
218+
219+
im.ReleaseAddressByPod("pod1.ns")
220+
err = im.AddOrUpdateSubnet(subnetName, "fd00::/126", []string{"fd00::1..fd00::2"})
221+
Expect(err).ShouldNot(HaveOccurred())
222+
223+
_, _, _, err = im.GetRandomAddress("pod1.ns", "pod1.ns", subnetName, nil)
224+
Expect(err).Should(MatchError(ipam.ErrNoAvailable))
225+
})
192226
})
193227

194228
Context("[DualStack]", func() {
@@ -281,6 +315,24 @@ var _ = Describe("[IPAM]", func() {
281315
Expect(ipv4).To(Equal("10.16.0.1"))
282316
Expect(ipv6).To(Equal("fd00::1"))
283317
})
318+
319+
It("donot reuse released address after update subnet's excludedIps", func() {
320+
im := ipam.NewIPAM()
321+
err := im.AddOrUpdateSubnet(subnetName, "10.16.0.2/30,fd00::/126", nil)
322+
Expect(err).ShouldNot(HaveOccurred())
323+
324+
ipv4, ipv6, _, err := im.GetRandomAddress("pod1.ns", "pod1.ns", subnetName, nil)
325+
Expect(err).ShouldNot(HaveOccurred())
326+
Expect(ipv4).To(Equal("10.16.0.1"))
327+
Expect(ipv6).To(Equal("fd00::1"))
328+
329+
im.ReleaseAddressByPod("pod1.ns")
330+
err = im.AddOrUpdateSubnet(subnetName, "10.16.0.2/30,fd00::/126", []string{"10.16.0.1..10.16.0.2", "fd00::1..fd00::2"})
331+
Expect(err).ShouldNot(HaveOccurred())
332+
333+
_, _, _, err = im.GetRandomAddress("pod1.ns", "pod1.ns", subnetName, nil)
334+
Expect(err).Should(MatchError(ipam.ErrNoAvailable))
335+
})
284336
})
285337
})
286338

0 commit comments

Comments
 (0)