Skip to content

Commit 52258f5

Browse files
committed
fix(pool): skip recycling pods in idle pod selection
1 parent 453a039 commit 52258f5

3 files changed

Lines changed: 26 additions & 13 deletions

File tree

kubernetes/internal/controller/allocator.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,6 @@ func (allocator *defaultAllocator) Schedule(ctx context.Context, spec *AllocSpec
227227
if spec.RecyclingPods.Has(pod.Name) {
228228
continue
229229
}
230-
// Exclude pods that are restarting (have recycle-meta annotation)
231-
if isRecycling(pod) {
232-
continue
233-
}
234230
if pod.Status.Phase != corev1.PodRunning {
235231
continue
236232
}
@@ -240,11 +236,11 @@ func (allocator *defaultAllocator) Schedule(ctx context.Context, spec *AllocSpec
240236
for podName, sandboxName := range status.PodAllocation {
241237
sandboxToPods[sandboxName] = append(sandboxToPods[sandboxName], podName)
242238
}
243-
sandboxAlloc, dirtySandboxes, poolAllocate, err := allocator.allocate(ctx, status, sandboxToPods, availablePods, spec.Sandboxes, spec.Pods)
239+
sandboxAlloc, dirtySandboxes, poolAllocate, err := allocator.allocate(ctx, status, sandboxToPods, availablePods, spec.Sandboxes)
244240
if err != nil {
245241
log.Error(err, "allocate failed")
246242
}
247-
poolDeallocate, err := allocator.deallocate(ctx, status, sandboxToPods, spec.Sandboxes)
243+
poolDeallocate, err := allocator.deallocate(ctx, status, sandboxToPods, spec.Sandboxes, spec.RecyclingPods)
248244
if err != nil {
249245
log.Error(err, "deallocate failed")
250246
}
@@ -284,7 +280,7 @@ func (allocator *defaultAllocator) initAllocation(ctx context.Context, spec *All
284280
return status, nil
285281
}
286282

287-
func (allocator *defaultAllocator) allocate(ctx context.Context, status *AllocStatus, sandboxToPods map[string][]string, availablePods []string, sandboxes []*sandboxv1alpha1.BatchSandbox, pods []*corev1.Pod) (map[string][]string, []string, bool, error) {
283+
func (allocator *defaultAllocator) allocate(ctx context.Context, status *AllocStatus, sandboxToPods map[string][]string, availablePods []string, sandboxes []*sandboxv1alpha1.BatchSandbox) (map[string][]string, []string, bool, error) {
288284
errs := make([]error, 0)
289285
sandboxAlloc := make(map[string][]string)
290286
dirtySandboxes := make([]string, 0)
@@ -366,7 +362,7 @@ func (allocator *defaultAllocator) doAllocate(ctx context.Context, status *Alloc
366362
return sandboxAlloc, remainAvailablePods, sandboxDirty, poolAllocate, nil
367363
}
368364

369-
func (allocator *defaultAllocator) deallocate(ctx context.Context, status *AllocStatus, sandboxToPods map[string][]string, sandboxes []*sandboxv1alpha1.BatchSandbox) (bool, error) {
365+
func (allocator *defaultAllocator) deallocate(ctx context.Context, status *AllocStatus, sandboxToPods map[string][]string, sandboxes []*sandboxv1alpha1.BatchSandbox, recycling sets.Set[string]) (bool, error) {
370366
log := logf.FromContext(ctx)
371367
poolDeallocate := false
372368
errs := make([]error, 0)
@@ -393,6 +389,9 @@ func (allocator *defaultAllocator) deallocate(ctx context.Context, status *Alloc
393389
pods := sandboxToPods[name]
394390
log.Info("GC deleted sandbox allocation", "sandbox", name, "podCount", len(pods))
395391
for _, pod := range pods {
392+
if recycling.Has(pod) {
393+
continue
394+
}
396395
delete(status.PodAllocation, pod)
397396
poolDeallocate = true
398397
}

kubernetes/internal/controller/allocator_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
corev1 "k8s.io/api/core/v1"
2424
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25+
"k8s.io/apimachinery/pkg/util/sets"
2526

2627
sandboxv1alpha1 "github.com/alibaba/OpenSandbox/sandbox-k8s/apis/sandbox/v1alpha1"
2728
"github.com/golang/mock/gomock"
@@ -324,6 +325,7 @@ func TestAllocatorSchedule(t *testing.T) {
324325
Name: "pool1",
325326
},
326327
},
328+
RecyclingPods: sets.New("pod-deallocated"),
327329
Sandboxes: []*sandboxv1alpha1.BatchSandbox{
328330
{
329331
ObjectMeta: metav1.ObjectMeta{
@@ -610,9 +612,10 @@ func TestScheduleExcludesRestartingPods(t *testing.T) {
610612
},
611613
}
612614
spec := &AllocSpec{
613-
Pods: pods,
614-
Sandboxes: sandboxes,
615-
Pool: &sandboxv1alpha1.Pool{ObjectMeta: metav1.ObjectMeta{Name: "pool1"}},
615+
Pods: pods,
616+
Sandboxes: sandboxes,
617+
Pool: &sandboxv1alpha1.Pool{ObjectMeta: metav1.ObjectMeta{Name: "pool1"}},
618+
RecyclingPods: sets.New("pod-restarting"),
616619
}
617620

618621
store.EXPECT().GetAllocation(gomock.Any(), gomock.Any()).Return(&PoolAllocation{PodAllocation: map[string]string{}}, nil).Times(1)

kubernetes/internal/controller/pool_controller.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ func (r *PoolReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
158158
return r.reconcilePool(ctx, pool, batchSandboxes, pods)
159159
}
160160

161-
func (r *PoolReconciler) collectRecyclingPods(batchSandboxes []*sandboxv1alpha1.BatchSandbox) (sets.Set[string], error) {
161+
func (r *PoolReconciler) collectRecyclingPods(batchSandboxes []*sandboxv1alpha1.BatchSandbox, pods []*corev1.Pod) (sets.Set[string], error) {
162162
recycling := make(sets.Set[string])
163163
for _, batchSandbox := range batchSandboxes {
164164
if batchSandbox.DeletionTimestamp != nil {
@@ -178,6 +178,11 @@ func (r *PoolReconciler) collectRecyclingPods(batchSandboxes []*sandboxv1alpha1.
178178
}
179179
}
180180
}
181+
for _, pod := range pods {
182+
if isRecycling(pod) {
183+
recycling.Insert(pod.Name)
184+
}
185+
}
181186
return recycling, nil
182187
}
183188

@@ -194,7 +199,7 @@ func (r *PoolReconciler) reconcilePool(ctx context.Context, pool *sandboxv1alpha
194199
}
195200

196201
// 2. First, handle Pod Recycle to ensure pods are ready for scheduling
197-
recyclingPods, err := r.collectRecyclingPods(batchSandboxes)
202+
recyclingPods, err := r.collectRecyclingPods(batchSandboxes, pods)
198203
if err != nil {
199204
log.Error(err, "Failed to collect recycling pods")
200205
return err
@@ -555,12 +560,18 @@ func (r *PoolReconciler) pickPodsToDelete(pods []*corev1.Pod, idlePodNames []str
555560
if !ok {
556561
continue
557562
}
563+
if isRecycling(pod) {
564+
continue
565+
}
558566
podsToDelete = append(podsToDelete, pod)
559567
}
560568
for _, pod := range idlePods { // delete pod from pool scale
561569
if scaleIn <= 0 {
562570
break
563571
}
572+
if isRecycling(pod) {
573+
continue
574+
}
564575
if pod.DeletionTimestamp == nil {
565576
podsToDelete = append(podsToDelete, pod)
566577
}

0 commit comments

Comments
 (0)