@@ -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
@@ -250,7 +255,7 @@ func (r *PoolReconciler) reconcilePool(ctx context.Context, pool *sandboxv1alpha
250255 pool : latestPool ,
251256 pods : pods ,
252257 allocatedCnt : int32 (len (scheRes .podAllocation )),
253- recycling : int32 ( len ( recyclingPods )) ,
258+ recycling : recyclingPods ,
254259 idlePods : latestIdlePods ,
255260 redundantPods : deleteOld ,
256261 supplyCnt : scheRes .supplySandbox + supplyNew ,
@@ -310,6 +315,9 @@ func (r *PoolReconciler) SetupWithManager(mgr ctrl.Manager) error {
310315 if oldObj .Spec .Replicas != newObj .Spec .Replicas {
311316 return true
312317 }
318+ if oldObj .DeletionTimestamp == nil && newObj .DeletionTimestamp != nil {
319+ return true
320+ }
313321 return false
314322 },
315323 DeleteFunc : func (e event.DeleteEvent ) bool {
@@ -425,8 +433,8 @@ type scaleArgs struct {
425433 pool * sandboxv1alpha1.Pool
426434 pods []* corev1.Pod
427435 allocatedCnt int32
428- recycling int32 // pods that are restarting and not available
429- supplyCnt int32 // to create
436+ recycling sets. Set [ string ] // pods that are restarting and not available
437+ supplyCnt int32 // to create
430438 idlePods []string
431439 redundantPods []string
432440}
@@ -446,7 +454,7 @@ func (r *PoolReconciler) scalePool(ctx context.Context, args *scaleArgs) error {
446454 supplyCnt := args .supplyCnt
447455 redundantPods := args .redundantPods
448456 // Buffer count excludes allocated and restarting pods
449- bufferCnt := totalCnt - allocatedCnt - recycling
457+ bufferCnt := totalCnt - allocatedCnt - int32 ( len ( recycling ))
450458
451459 // Calculate desired buffer cnt.
452460 desiredBufferCnt := bufferCnt
@@ -482,7 +490,7 @@ func (r *PoolReconciler) scalePool(ctx context.Context, args *scaleArgs) error {
482490 if desiredTotalCnt < totalCnt {
483491 scaleIn = totalCnt - desiredTotalCnt
484492 }
485- podsToDelete := r .pickPodsToDelete (pods , args .idlePods , args .redundantPods , scaleIn )
493+ podsToDelete := r .pickPodsToDelete (pods , args .idlePods , args .redundantPods , scaleIn , args . recycling )
486494 log .Info ("Scaling down pool" , "pool" , pool .Name , "scaleIn" , scaleIn , "redundantPods" , len (redundantPods ), "podsToDelete" , len (podsToDelete ))
487495 for _ , pod := range podsToDelete {
488496 log .Info ("Deleting pool pod" , "pool" , pool .Name , "pod" , pod .Name )
@@ -532,7 +540,7 @@ func (r *PoolReconciler) updatePoolStatus(ctx context.Context, latestRevision st
532540 return nil
533541}
534542
535- func (r * PoolReconciler ) pickPodsToDelete (pods []* corev1.Pod , idlePodNames []string , redundantPodNames []string , scaleIn int32 ) []* corev1.Pod {
543+ func (r * PoolReconciler ) pickPodsToDelete (pods []* corev1.Pod , idlePodNames []string , redundantPodNames []string , scaleIn int32 , recycling sets. Set [ string ] ) []* corev1.Pod {
536544 var idlePods []* corev1.Pod
537545 podMap := make (map [string ]* corev1.Pod )
538546 for _ , pod := range pods {
@@ -555,12 +563,18 @@ func (r *PoolReconciler) pickPodsToDelete(pods []*corev1.Pod, idlePodNames []str
555563 if ! ok {
556564 continue
557565 }
566+ if recycling .Has (pod .Name ) {
567+ continue
568+ }
558569 podsToDelete = append (podsToDelete , pod )
559570 }
560571 for _ , pod := range idlePods { // delete pod from pool scale
561572 if scaleIn <= 0 {
562573 break
563574 }
575+ if recycling .Has (pod .Name ) {
576+ continue
577+ }
564578 if pod .DeletionTimestamp == nil {
565579 podsToDelete = append (podsToDelete , pod )
566580 }
0 commit comments