Skip to content

Commit 36c0788

Browse files
committed
fix(allocator): avoid deleting non-existent pod allocations and improve error handling
- Add check to ensure pod exists in PodAllocation before deletion during release - Update test cases to verify pod removal and recycling behavior correctly - Return error immediately after logging failure to handle pod recycle in pool controller
1 parent 2a339c8 commit 36c0788

3 files changed

Lines changed: 13 additions & 4 deletions

File tree

kubernetes/internal/controller/allocator.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -412,9 +412,11 @@ func (allocator *defaultAllocator) doDeallocate(ctx context.Context, status *All
412412
return false, err
413413
}
414414
for _, pod := range toRelease.Pods {
415-
delete(status.PodAllocation, pod)
416-
deallocate = true
417-
status.PodsToRecycle = append(status.PodsToRecycle, pod)
415+
if _, ok := status.PodAllocation[pod]; ok {
416+
delete(status.PodAllocation, pod)
417+
deallocate = true
418+
status.PodsToRecycle = append(status.PodsToRecycle, pod)
419+
}
418420
}
419421
pods := make([]string, 0)
420422
for _, pod := range allocatedPods {

kubernetes/internal/controller/allocator_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,19 +274,25 @@ func TestAllocatorSchedule(t *testing.T) {
274274
},
275275
},
276276
},
277+
// Pod1 is allocated to sbx1 in pool level
277278
poolAlloc: &PoolAllocation{
278-
PodAllocation: map[string]string{},
279+
PodAllocation: map[string]string{
280+
"pod1": "sbx1",
281+
},
279282
},
283+
// Sandbox has pod1 allocated
280284
sandboxAlloc: &SandboxAllocation{
281285
Pods: []string{
282286
"pod1",
283287
},
284288
},
289+
// Sandbox releases pod1
285290
release: &AllocationRelease{
286291
Pods: []string{
287292
"pod1",
288293
},
289294
},
295+
// Pod1 should be removed from allocation and added to recycle
290296
wantStatus: &AllocStatus{
291297
PodAllocation: map[string]string{},
292298
PodSupplement: 0,

kubernetes/internal/controller/pool_controller.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ func (r *PoolReconciler) reconcilePool(ctx context.Context, pool *sandboxv1alpha
193193
delay = defaultRetryTime
194194
if err = r.handlePodRecycle(ctx, latestPool, pod); err != nil {
195195
log.Error(err, "Failed to handle pod recycle", "pod", pod.Name)
196+
return err
196197
}
197198
}
198199
if int32(len(scheRes.idlePods)) >= scheRes.supplySandbox {

0 commit comments

Comments
 (0)