Skip to content

Commit 5a1cbab

Browse files
authored
Merge pull request #809 from fengcone/feature/public-fix-allocation-finalizer
fix(kubernetes): remove pool allocation finalizer on detach
2 parents 58fb08c + 28bb43f commit 5a1cbab

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

kubernetes/internal/controller/batchsandbox_pause_resume.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,7 @@ func (r *BatchSandboxReconciler) completePause(ctx context.Context, bs *sandboxv
446446
patch := client.MergeFrom(latest.DeepCopy())
447447
latest.Spec.Template = pooledTemplate.DeepCopy()
448448
latest.Spec.PoolRef = ""
449+
controllerutil.RemoveFinalizer(latest, FinalizerPoolAllocation)
449450
if latest.Annotations != nil {
450451
delete(latest.Annotations, AnnoAllocReleaseKey)
451452
}
@@ -455,6 +456,7 @@ func (r *BatchSandboxReconciler) completePause(ctx context.Context, bs *sandboxv
455456
}
456457
bs.Spec.Template = pooledTemplate.DeepCopy()
457458
bs.Spec.PoolRef = ""
459+
controllerutil.RemoveFinalizer(bs, FinalizerPoolAllocation)
458460
if bs.Annotations != nil {
459461
delete(bs.Annotations, AnnoAllocReleaseKey)
460462
}
@@ -561,6 +563,7 @@ func (r *BatchSandboxReconciler) continueResume(ctx context.Context, bs *sandbox
561563

562564
if latest.Spec.PoolRef != "" {
563565
latest.Spec.PoolRef = ""
566+
controllerutil.RemoveFinalizer(latest, FinalizerPoolAllocation)
564567
}
565568

566569
if err := r.Patch(ctx, latest, patch); err != nil {

kubernetes/internal/controller/batchsandbox_pause_resume_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
sandboxv1alpha1 "github.com/alibaba/OpenSandbox/sandbox-k8s/apis/sandbox/v1alpha1"
3838
taskscheduler "github.com/alibaba/OpenSandbox/sandbox-k8s/internal/scheduler"
3939
"github.com/alibaba/OpenSandbox/sandbox-k8s/internal/utils/fieldindex"
40+
taskexecutor "github.com/alibaba/OpenSandbox/sandbox-k8s/pkg/task-executor"
4041
)
4142

4243
// newTestReconciler creates a BatchSandboxReconciler with a fake client for testing.
@@ -80,6 +81,11 @@ func (f *forbiddenTaskScheduler) StopTask() []taskscheduler.Task {
8081
return nil
8182
}
8283

84+
func (f *forbiddenTaskScheduler) AddTasks(_ []*taskexecutor.Task) error {
85+
f.t.Fatalf("task scheduler should not add tasks while sandbox is paused")
86+
return nil
87+
}
88+
8389
type fakeSchedulerTask struct {
8490
name string
8591
state taskscheduler.TaskState
@@ -128,6 +134,10 @@ func (r *recordingTaskScheduler) StopTask() []taskscheduler.Task {
128134
return r.tasks
129135
}
130136

137+
func (r *recordingTaskScheduler) AddTasks(_ []*taskexecutor.Task) error {
138+
return nil
139+
}
140+
131141
// ---------- dispatchPauseResume 5-case tests ----------
132142

133143
func TestDispatchPauseResume_Case1_PauseTrue(t *testing.T) {
@@ -932,6 +942,7 @@ func TestContinueResume_PoolMode(t *testing.T) {
932942
Namespace: "default",
933943
Generation: 2,
934944
UID: "test-uid",
945+
Finalizers: []string{FinalizerPoolAllocation},
935946
},
936947
Spec: sandboxv1alpha1.BatchSandboxSpec{
937948
Pause: ptr.To(false),
@@ -958,6 +969,7 @@ func TestContinueResume_PoolMode(t *testing.T) {
958969
updated := &sandboxv1alpha1.BatchSandbox{}
959970
require.NoError(t, r.Get(context.Background(), types.NamespacedName{Namespace: "default", Name: "test-bs"}, updated))
960971
assert.Equal(t, "", updated.Spec.PoolRef)
972+
assert.NotContains(t, updated.Finalizers, FinalizerPoolAllocation)
961973
}
962974

963975
func TestContinueResume_UsesPatchedTemplateWhenCacheReturnsStaleObject(t *testing.T) {
@@ -2054,6 +2066,7 @@ func TestCompletePause_PooledSandboxDetachesForPoolGC(t *testing.T) {
20542066
Namespace: "default",
20552067
Generation: 2,
20562068
UID: "test-bs-uid",
2069+
Finalizers: []string{FinalizerPoolAllocation},
20572070
},
20582071
Spec: sandboxv1alpha1.BatchSandboxSpec{
20592072
Pause: ptr.To(true),
@@ -2107,6 +2120,7 @@ func TestCompletePause_PooledSandboxDetachesForPoolGC(t *testing.T) {
21072120
assert.NotContains(t, updated.Spec.Template.Labels, LabelPoolRevision)
21082121
assert.Equal(t, "", updated.Spec.Template.Spec.NodeName)
21092122
assert.NotContains(t, updated.Annotations, AnnoAllocReleaseKey)
2123+
assert.NotContains(t, updated.Finalizers, FinalizerPoolAllocation)
21102124
}
21112125

21122126
func TestCompletePause_PooledSandboxDoesNotDeleteSourcePod(t *testing.T) {

0 commit comments

Comments
 (0)