Skip to content

Commit ebc764c

Browse files
authored
Merge pull request opensandbox-group#586 from fengcone/feature/public-delRecycle
fix(controller): delete dirty pods in pool allocation
2 parents 6295384 + b3e8b5e commit ebc764c

3 files changed

Lines changed: 31 additions & 13 deletions

File tree

kubernetes/internal/controller/allocator.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@ type AllocStatus struct {
183183
PodAllocation map[string]string
184184
// pod request count
185185
PodSupplement int32
186+
// DirtyPods are pods that should be recycled (deleted) after release
187+
DirtyPods []string
186188
}
187189

188190
type SandboxSyncInfo struct {
@@ -387,6 +389,7 @@ func (allocator *defaultAllocator) deallocate(ctx context.Context, status *Alloc
387389
for _, pod := range pods {
388390
delete(status.PodAllocation, pod)
389391
poolDeallocate = true
392+
status.DirtyPods = append(status.DirtyPods, pod)
390393
}
391394
delete(sandboxToPods, name)
392395
}
@@ -408,6 +411,7 @@ func (allocator *defaultAllocator) doDeallocate(ctx context.Context, status *All
408411
for _, pod := range toRelease.Pods {
409412
delete(status.PodAllocation, pod)
410413
deallocate = true
414+
status.DirtyPods = append(status.DirtyPods, pod)
411415
log.V(1).Info("Pod released from sandbox", "pod", pod, "sandbox", name)
412416
}
413417
pods := make([]string, 0)

kubernetes/internal/controller/allocator_test.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -271,21 +271,20 @@ func TestAllocatorSchedule(t *testing.T) {
271271
},
272272
},
273273
poolAlloc: &PoolAllocation{
274-
PodAllocation: map[string]string{},
274+
PodAllocation: map[string]string{
275+
"pod1": "sbx1",
276+
},
275277
},
276278
sandboxAlloc: &SandboxAllocation{
277-
Pods: []string{
278-
"pod1",
279-
},
279+
Pods: []string{"pod1"},
280280
},
281281
release: &AllocationRelease{
282-
Pods: []string{
283-
"pod1", "sbx1",
284-
},
282+
Pods: []string{"pod1"},
285283
},
286284
wantStatus: &AllocStatus{
287285
PodAllocation: map[string]string{},
288286
PodSupplement: 0,
287+
DirtyPods: []string{"pod1"},
289288
},
290289
},
291290
}
@@ -298,6 +297,10 @@ func TestAllocatorSchedule(t *testing.T) {
298297
syncer.EXPECT().GetRelease(gomock.Any(), gomock.Any()).Return(c.release, nil).Times(len(c.spec.Sandboxes))
299298
status, _, _, err := allocator.Schedule(context.Background(), c.spec)
300299
assert.NoError(t, err)
300+
if !reflect.DeepEqual(c.wantStatus, status) {
301+
t.Logf("want: %+v", c.wantStatus)
302+
t.Logf("got: %+v", status)
303+
}
301304
assert.True(t, reflect.DeepEqual(c.wantStatus, status))
302305
})
303306
}

kubernetes/internal/controller/pool_controller.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ func (r *PoolReconciler) reconcilePool(ctx context.Context, pool *sandboxv1alpha
166166
schedulePods := r.filterEvictingPods(ctx, latestPool, pods, allocBeforeSchedule)
167167

168168
// 4. Schedule and allocate
169-
podAllocation, pendingSyncs, idlePods, supplySandbox, poolDirty, err := r.scheduleSandbox(ctx, latestPool, batchSandboxes, schedulePods)
169+
podAllocation, pendingSyncs, idlePods, dirtyPods, supplySandbox, poolDirty, err := r.scheduleSandbox(ctx, latestPool, batchSandboxes, schedulePods)
170170
if err != nil {
171171
return err
172172
}
@@ -207,7 +207,7 @@ func (r *PoolReconciler) reconcilePool(ctx context.Context, pool *sandboxv1alpha
207207
if err != nil {
208208
return err
209209
}
210-
latestIdlePods, deleteOld, supplyNew := r.updatePool(ctx, latestRevision, schedulePods, idlePods)
210+
latestIdlePods, deleteOld, supplyNew := r.updatePool(ctx, latestRevision, schedulePods, idlePods, dirtyPods)
211211

212212
args := &scaleArgs{
213213
latestRevision: latestRevision,
@@ -327,7 +327,7 @@ func (r *PoolReconciler) SetupWithManager(mgr ctrl.Manager) error {
327327
Complete(r)
328328
}
329329

330-
func (r *PoolReconciler) scheduleSandbox(ctx context.Context, pool *sandboxv1alpha1.Pool, batchSandboxes []*sandboxv1alpha1.BatchSandbox, pods []*corev1.Pod) (map[string]string, []SandboxSyncInfo, []string, int32, bool, error) {
330+
func (r *PoolReconciler) scheduleSandbox(ctx context.Context, pool *sandboxv1alpha1.Pool, batchSandboxes []*sandboxv1alpha1.BatchSandbox, pods []*corev1.Pod) (map[string]string, []SandboxSyncInfo, []string, []string, int32, bool, error) {
331331
log := logf.FromContext(ctx)
332332
spec := &AllocSpec{
333333
Sandboxes: batchSandboxes,
@@ -336,7 +336,7 @@ func (r *PoolReconciler) scheduleSandbox(ctx context.Context, pool *sandboxv1alp
336336
}
337337
status, pendingSyncs, poolDirty, err := r.Allocator.Schedule(ctx, spec)
338338
if err != nil {
339-
return nil, nil, nil, 0, false, err
339+
return nil, nil, nil, nil, 0, false, err
340340
}
341341
idlePods := make([]string, 0)
342342
for _, pod := range pods {
@@ -346,10 +346,10 @@ func (r *PoolReconciler) scheduleSandbox(ctx context.Context, pool *sandboxv1alp
346346
}
347347
log.Info("Schedule result", "pool", pool.Name, "allocated", len(status.PodAllocation),
348348
"idlePods", len(idlePods), "supplement", status.PodSupplement, "pendingSyncs", len(pendingSyncs), "poolDirty", poolDirty)
349-
return status.PodAllocation, pendingSyncs, idlePods, status.PodSupplement, poolDirty, nil
349+
return status.PodAllocation, pendingSyncs, idlePods, status.DirtyPods, status.PodSupplement, poolDirty, nil
350350
}
351351

352-
func (r *PoolReconciler) updatePool(ctx context.Context, latestRevision string, pods []*corev1.Pod, idlePods []string) ([]string, []string, int32) {
352+
func (r *PoolReconciler) updatePool(ctx context.Context, latestRevision string, pods []*corev1.Pod, idlePods []string, dirtyPods []string) ([]string, []string, int32) {
353353
podMap := make(map[string]*corev1.Pod)
354354
for _, pod := range pods {
355355
podMap[pod.Name] = pod
@@ -358,7 +358,18 @@ func (r *PoolReconciler) updatePool(ctx context.Context, latestRevision string,
358358
deleteOld := make([]string, 0)
359359
supplyNew := int32(0)
360360

361+
dirtySet := make(map[string]bool)
362+
for _, p := range dirtyPods {
363+
dirtySet[p] = true
364+
}
365+
361366
for _, name := range idlePods {
367+
if dirtySet[name] {
368+
deleteOld = append(deleteOld, name)
369+
// no need to supply, next reconcile will do this job
370+
continue
371+
}
372+
362373
pod, ok := podMap[name]
363374
if !ok {
364375
continue

0 commit comments

Comments
 (0)