Skip to content

Commit

Permalink
Merge pull request #3926 from ty-dc/fix/error-allocatedipcount
Browse files Browse the repository at this point in the history
Added debug log of AllocatedIPCount of ippool
  • Loading branch information
ty-dc authored Sep 19, 2024
2 parents 999c8b4 + 6ef5fc2 commit 51c3e4b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
17 changes: 15 additions & 2 deletions pkg/ippoolmanager/ippool_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,15 @@ func (im *ipPoolManager) genRandomIP(ctx context.Context, ipPool *spiderpoolv2be
ipPool.Status.AllocatedIPCount = new(int64)
}

*ipPool.Status.AllocatedIPCount++
// reference issue: https://github.com/spidernet-io/spiderpool/issues/3771
if int64(len(usedIPs)) != *ipPool.Status.AllocatedIPCount {
logger.Sugar().Errorf("Handling AllocatedIPCount while allocating IP from IPPool %s, but there is a data discrepancy. Expected %d, but got %d.", ipPool.Name, len(usedIPs), *ipPool.Status.AllocatedIPCount)
}

// Adding a newly assigned IP
usedIPs = append(usedIPs, resIP)
*ipPool.Status.AllocatedIPCount = int64(len(usedIPs))

if *ipPool.Status.AllocatedIPCount > int64(*im.config.MaxAllocatedIPs) {
return nil, fmt.Errorf("%w, threshold of IP records(<=%d) for IPPool %s exceeded", constant.ErrIPUsedOut, im.config.MaxAllocatedIPs, ipPool.Name)
}
Expand Down Expand Up @@ -248,12 +256,17 @@ func (im *ipPoolManager) ReleaseIP(ctx context.Context, poolName string, ipAndUI
ipPool.Status.AllocatedIPCount = new(int64)
}

// reference issue: https://github.com/spidernet-io/spiderpool/issues/3771
if int64(len(allocatedRecords)) != *ipPool.Status.AllocatedIPCount {
logger.Sugar().Errorf("Handling AllocatedIPCount while releasing IP from IPPool %s, but there is a data discrepancy. Expected %d, but got %d.", ipPool.Name, len(allocatedRecords), *ipPool.Status.AllocatedIPCount)
}

release := false
for _, iu := range ipAndUIDs {
if record, ok := allocatedRecords[iu.IP]; ok {
if record.PodUID == iu.UID {
delete(allocatedRecords, iu.IP)
*ipPool.Status.AllocatedIPCount--
*ipPool.Status.AllocatedIPCount = int64(len(allocatedRecords))
release = true
}
}
Expand Down
21 changes: 19 additions & 2 deletions test/e2e/reclaim/reclaim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ var _ = Describe("test ip with reclaim ip case", Label("reclaim"), func() {
}
})
})
DescribeTable("dirty IP record in the IPPool should be auto clean by Spiderpool", Pending, func() {
DescribeTable("dirty IP record in the IPPool should be auto clean by Spiderpool", func() {
// Generate IPPool annotation string
podIppoolAnnoStr := common.GeneratePodIPPoolAnnotations(frame, common.NIC1, v4poolNameList, v6poolNameList)

Expand Down Expand Up @@ -586,8 +586,25 @@ var _ = Describe("test ip with reclaim ip case", Label("reclaim"), func() {
// Delete Pod
Expect(frame.DeletePod(podName, namespace)).To(Succeed(), "Failed to delete pod %v/%v\n", namespace, podName)
GinkgoWriter.Printf("succeed to delete pod %v/%v\n", namespace, podName)

// Check whether the dirty IP data is recovered successfully and whether the AllocatedIPCount decreases and meets expectations?
Eventually(func() error {
if frame.Info.IpV4Enabled {
if err := common.CheckIppoolSanity(frame, v4poolName); err != nil {
return err
}
GinkgoWriter.Printf("successfully checked sanity of v4 spiderIPPool %v \n", v4poolName)
}
if frame.Info.IpV6Enabled {
if err := common.CheckIppoolSanity(frame, v6poolName); err != nil {
return err
}
GinkgoWriter.Printf("successfully checked sanity of v6 spiderIPPool %v \n", v6poolName)
}
return nil
}).WithTimeout(time.Minute).WithPolling(time.Second * 10).Should(BeNil())
},
PEntry("a dirty IP record (pod name is wrong or containerID is wrong) in the IPPool should be auto clean by Spiderpool", Serial, Label("G00005", "G00007")),
Entry("a dirty IP record (pod name is wrong or containerID is wrong) in the IPPool should be auto clean by Spiderpool", Serial, Label("G00005", "G00007")),
)
})

Expand Down

0 comments on commit 51c3e4b

Please sign in to comment.