Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix background deletion blocked #51

Merged
merged 1 commit into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/controllers/milvus.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (r *MilvusReconciler) batchDeletePVC(ctx context.Context, namespace, labelK
return nil
}

func (r *MilvusReconciler) Finalize(ctx context.Context, mc v1beta1.Milvus) error {
var Finalize = func(ctx context.Context, r *MilvusReconciler, mc v1beta1.Milvus) error {
deletingReleases := map[string]bool{}
if !mc.Spec.Dep.Etcd.External && mc.Spec.Dep.Etcd.InCluster.DeletionPolicy == v1beta1.DeletionPolicyDelete {
deletingReleases[mc.Name+"-etcd"] = mc.Spec.Dep.Etcd.InCluster.PVCDeletion
Expand Down
17 changes: 10 additions & 7 deletions pkg/controllers/milvus_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ import (
)

const (
MilvusFinalizerName = "milvus.milvus.io/finalizer"
PauseReconcileAnnotation = "milvus.io/pause-reconcile"
MaintainingAnnotation = "milvus.io/maintaining"
MilvusFinalizerName = "milvus.milvus.io/finalizer"
ForegroundDeletionFinalizer = "foregroundDeletion"
PauseReconcileAnnotation = "milvus.io/pause-reconcile"
MaintainingAnnotation = "milvus.io/maintaining"
)

// MilvusReconciler reconciles a Milvus object
Expand Down Expand Up @@ -117,13 +118,15 @@ func (r *MilvusReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
}
}

stopped, err := CheckMilvusStopped(ctx, r.Client, *milvus)
if !stopped || err != nil {
return ctrl.Result{RequeueAfter: unhealthySyncInterval}, err
if controllerutil.ContainsFinalizer(milvus, ForegroundDeletionFinalizer) {
stopped, err := CheckMilvusStopped(ctx, r.Client, *milvus)
if !stopped || err != nil {
return ctrl.Result{RequeueAfter: unhealthySyncInterval}, err
}
}

if controllerutil.ContainsFinalizer(milvus, MilvusFinalizerName) {
if err := r.Finalize(ctx, *milvus); err != nil {
if err := Finalize(ctx, r, *milvus); err != nil {
return ctrl.Result{}, err
}
// metrics
Expand Down
45 changes: 43 additions & 2 deletions pkg/controllers/milvus_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,24 @@ var mockCheckMilvusStop = func(ctx context.Context, cli client.Client, mc v1beta
return mockCheckMilvusStopRet, mockCheckMilvusStopErr
}

var mockFinalizeRet error
var mockFinalize = func(ctx context.Context, r *MilvusReconciler, mc v1beta1.Milvus) error {
return mockFinalizeRet
}

func TestClusterReconciler(t *testing.T) {
bak := CheckMilvusStopped
defer func() {
CheckMilvusStopped = bak
}()
CheckMilvusStopped = mockCheckMilvusStop

bakFinalize := Finalize
defer func() {
Finalize = bakFinalize
}()
Finalize = mockFinalize

config.Init(util.GetGitRepoRootDir())

ctrl := gomock.NewController(t)
Expand Down Expand Up @@ -76,9 +87,38 @@ func TestClusterReconciler(t *testing.T) {
assert.Error(t, err)
})

t.Run("case delete remove finalizer", func(t *testing.T) {
t.Run("case delete background", func(t *testing.T) {
defer ctrl.Finish()
m.Finalizers = []string{MilvusFinalizerName}
mockCheckMilvusStopRet = false
m.ObjectMeta.DeletionTimestamp = &metav1.Time{Time: time.Now()}

mockClient.EXPECT().Get(gomock.Any(), gomock.Any(), gomock.Any()).
Do(func(ctx, key, obj interface{}) {
o := obj.(*v1beta1.Milvus)
*o = m
}).
Return(nil)

mockClient.EXPECT().Status().Return(mockClient)
mockClient.EXPECT().Update(gomock.Any(), gomock.Any()).Times(1)

mockClient.EXPECT().Update(gomock.Any(), gomock.Any()).Do(
func(ctx, obj interface{}, opts ...interface{}) {
// finalizer should be removed
u := obj.(*v1beta1.Milvus)
assert.Equal(t, []string{}, u.Finalizers)
},
).Return(nil)

_, err := r.Reconcile(ctx, reconcile.Request{})
assert.NoError(t, err)
})

t.Run("delete foreground deletion", func(t *testing.T) {
defer ctrl.Finish()
mockCheckMilvusStopRet = true
m.Finalizers = []string{ForegroundDeletionFinalizer, MilvusFinalizerName}
m.ObjectMeta.DeletionTimestamp = &metav1.Time{Time: time.Now()}
mockClient.EXPECT().Get(gomock.Any(), gomock.Any(), gomock.Any()).
Do(func(ctx, key, obj interface{}) {
Expand All @@ -94,7 +134,7 @@ func TestClusterReconciler(t *testing.T) {
func(ctx, obj interface{}, opts ...interface{}) {
// finalizer should be removed
u := obj.(*v1beta1.Milvus)
assert.Equal(t, u.Finalizers, []string{})
assert.Equal(t, []string{ForegroundDeletionFinalizer}, u.Finalizers)
},
).Return(nil)

Expand All @@ -104,6 +144,7 @@ func TestClusterReconciler(t *testing.T) {

t.Run("milvus not stopped or check failed", func(t *testing.T) {
defer ctrl.Finish()
m.Finalizers = []string{ForegroundDeletionFinalizer, MilvusFinalizerName}
mockClient.EXPECT().Get(gomock.Any(), gomock.Any(), gomock.Any()).
Do(func(ctx, key, obj interface{}) {
o := obj.(*v1beta1.Milvus)
Expand Down
14 changes: 7 additions & 7 deletions pkg/controllers/milvus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestCluster_Finalize(t *testing.T) {
m.Default()

t.Run("no delete", func(t *testing.T) {
err := r.Finalize(ctx, m)
err := Finalize(ctx, r, m)
assert.NoError(t, err)
})

Expand All @@ -47,7 +47,7 @@ func TestCluster_Finalize(t *testing.T) {
}
})
mockClient.EXPECT().Delete(gomock.Any(), gomock.Any())
err := r.Finalize(ctx, m)
err := Finalize(ctx, r, m)
assert.NoError(t, err)
})

Expand All @@ -66,7 +66,7 @@ func TestCluster_Finalize(t *testing.T) {
}
})
mockClient.EXPECT().Delete(gomock.Any(), gomock.Any())
err := r.Finalize(ctx, m)
err := Finalize(ctx, r, m)
assert.NoError(t, err)
})

Expand All @@ -76,7 +76,7 @@ func TestCluster_Finalize(t *testing.T) {
m.Spec.Dep.Storage.InCluster.DeletionPolicy = v1beta1.DeletionPolicyDelete
m.Spec.Dep.Storage.InCluster.PVCDeletion = true
mockHelm.EXPECT().Uninstall(gomock.Any(), gomock.Any()).Return(errTest)
err := r.Finalize(ctx, m)
err := Finalize(ctx, r, m)
assert.Error(t, err)
})

Expand All @@ -90,7 +90,7 @@ func TestCluster_Finalize(t *testing.T) {
{},
}
}).Return(errTest)
err := r.Finalize(ctx, m)
err := Finalize(ctx, r, m)
assert.Error(t, err)
})

Expand All @@ -106,7 +106,7 @@ func TestCluster_Finalize(t *testing.T) {
}
})
mockClient.EXPECT().Delete(gomock.Any(), gomock.Any()).Return(errTest)
err := r.Finalize(ctx, m)
err := Finalize(ctx, r, m)
assert.Error(t, err)
})

Expand All @@ -117,7 +117,7 @@ func TestCluster_Finalize(t *testing.T) {
m.Spec.Dep.Etcd.InCluster = nil
m.Spec.Dep.Pulsar.InCluster = nil
m.Spec.Dep.Storage.InCluster = nil
err := r.Finalize(ctx, m)
err := Finalize(ctx, r, m)
assert.NoError(t, err)
})

Expand Down
Loading