Skip to content

Commit e58f8e2

Browse files
leon-apeleon-inf
authored andcommitted
chore: keep the finalizers added externally on the object (#9403)
(cherry picked from commit c975491)
1 parent a989b03 commit e58f8e2

3 files changed

Lines changed: 36 additions & 35 deletions

File tree

controllers/workloads/instanceset_controller_test.go

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,9 @@ var _ = Describe("InstanceSet Controller", func() {
197197

198198
By("delete the ITS object")
199199
Expect(k8sClient.Delete(ctx, itsObj)).Should(Succeed())
200-
Eventually(testapps.CheckObjExists(&testCtx, itsKey, &workloads.InstanceSet{}, false)).Should(Succeed())
200+
201+
By("check its object NOT deleted")
202+
Consistently(testapps.CheckObjExists(&testCtx, itsKey, &workloads.InstanceSet{}, true)).Should(Succeed())
201203

202204
By("check pods deleted")
203205
podKey := types.NamespacedName{
@@ -206,12 +208,16 @@ var _ = Describe("InstanceSet Controller", func() {
206208
}
207209
Eventually(testapps.CheckObjExists(&testCtx, podKey, &corev1.Pod{}, false)).Should(Succeed())
208210

209-
By("check PVCs deleted")
211+
By("check PVCs deleted, but the pvc-protection finalizer prevent the pvc to be deleted physically")
210212
pvcKey := types.NamespacedName{
211213
Namespace: itsObj.Namespace,
212214
Name: fmt.Sprintf("%s-%s-0", pvc.Name, itsObj.Name),
213215
}
214-
Eventually(testapps.CheckObjExists(&testCtx, pvcKey, &corev1.PersistentVolumeClaim{}, false)).Should(Succeed())
216+
Eventually(testapps.CheckObj(&testCtx, pvcKey, func(g Gomega, pvc *corev1.PersistentVolumeClaim) {
217+
g.Expect(pvc.DeletionTimestamp).ShouldNot(BeNil())
218+
g.Expect(pvc.Finalizers).To(HaveLen(1))
219+
g.Expect(pvc.Finalizers[0]).To(Equal("kubernetes.io/pvc-protection"))
220+
})).Should(Succeed())
215221
})
216222

217223
It("when deleted - retain", func() {
@@ -233,12 +239,14 @@ var _ = Describe("InstanceSet Controller", func() {
233239
}
234240
Eventually(testapps.CheckObjExists(&testCtx, podKey, &corev1.Pod{}, false)).Should(Succeed())
235241

236-
By("check PVCs retained")
242+
By("check PVCs retained and not deleted")
237243
pvcKey := types.NamespacedName{
238244
Namespace: itsObj.Namespace,
239245
Name: fmt.Sprintf("%s-%s-0", pvc.Name, itsObj.Name),
240246
}
241-
Eventually(testapps.CheckObjExists(&testCtx, pvcKey, &corev1.PersistentVolumeClaim{}, true)).Should(Succeed())
247+
Consistently(testapps.CheckObj(&testCtx, pvcKey, func(g Gomega, pvc *corev1.PersistentVolumeClaim) {
248+
g.Expect(pvc.DeletionTimestamp).Should(BeNil())
249+
})).Should(Succeed())
242250
})
243251

244252
It("when scaled - delete", func() {
@@ -261,12 +269,16 @@ var _ = Describe("InstanceSet Controller", func() {
261269
}
262270
Eventually(testapps.CheckObjExists(&testCtx, podKey, &corev1.Pod{}, false)).Should(Succeed())
263271

264-
By("check PVCs deleted")
272+
By("check PVCs deleted, but the pvc-protection finalizer prevent the pvc to be deleted physically")
265273
pvcKey := types.NamespacedName{
266274
Namespace: itsObj.Namespace,
267275
Name: fmt.Sprintf("%s-%s-0", pvc.Name, itsObj.Name),
268276
}
269-
Eventually(testapps.CheckObjExists(&testCtx, pvcKey, &corev1.PersistentVolumeClaim{}, false)).Should(Succeed())
277+
Eventually(testapps.CheckObj(&testCtx, pvcKey, func(g Gomega, pvc *corev1.PersistentVolumeClaim) {
278+
g.Expect(pvc.DeletionTimestamp).ShouldNot(BeNil())
279+
g.Expect(pvc.Finalizers).To(HaveLen(1))
280+
g.Expect(pvc.Finalizers[0]).To(Equal("kubernetes.io/pvc-protection"))
281+
})).Should(Succeed())
270282
})
271283

272284
It("when scaled - retain", func() {
@@ -289,12 +301,14 @@ var _ = Describe("InstanceSet Controller", func() {
289301
}
290302
Eventually(testapps.CheckObjExists(&testCtx, podKey, &corev1.Pod{}, false)).Should(Succeed())
291303

292-
By("check PVCs retained")
304+
By("check PVCs retained and not deleted")
293305
pvcKey := types.NamespacedName{
294306
Namespace: itsObj.Namespace,
295307
Name: fmt.Sprintf("%s-%s-0", pvc.Name, itsObj.Name),
296308
}
297-
Eventually(testapps.CheckObjExists(&testCtx, pvcKey, &corev1.PersistentVolumeClaim{}, true)).Should(Succeed())
309+
Consistently(testapps.CheckObj(&testCtx, pvcKey, func(g Gomega, pvc *corev1.PersistentVolumeClaim) {
310+
g.Expect(pvc.DeletionTimestamp).Should(BeNil())
311+
})).Should(Succeed())
298312
})
299313
})
300314

pkg/controller/kubebuilderx/plan_builder.go

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,9 @@ func buildOrderedVertices(ctx context.Context, currentTree *ObjectTree, desiredT
178178
}
179179
}
180180
}
181-
finalizer := currentTree.GetFinalizer()
182181
deleteOrphanObjects := func() {
183182
for name := range deleteSet {
184183
object := oldSnapshot[name]
185-
keepFinalizer(object, finalizer)
186184
v := model.NewObjectVertex(nil, object, model.ActionDeletePtr(), inDataContext4G())
187185
findAndAppend(v)
188186
}
@@ -203,21 +201,6 @@ func buildOrderedVertices(ctx context.Context, currentTree *ObjectTree, desiredT
203201
return vertices
204202
}
205203

206-
func keepFinalizer(object client.Object, finalizer string) {
207-
var finalizers []string
208-
if len(finalizer) > 0 {
209-
finalizers = append(finalizers, finalizer)
210-
}
211-
object.SetFinalizers(finalizers)
212-
}
213-
214-
func getRemainingFinalizer(obj client.Object) string {
215-
if len(obj.GetFinalizers()) > 0 {
216-
return obj.GetFinalizers()[0]
217-
}
218-
return ""
219-
}
220-
221204
// Plan implementation
222205

223206
func (p *Plan) Execute() error {
@@ -285,7 +268,10 @@ func (b *PlanBuilder) patchObject(ctx context.Context, vertex *model.ObjectVerte
285268
}
286269

287270
func (b *PlanBuilder) deleteObject(ctx context.Context, vertex *model.ObjectVertex) error {
288-
finalizer := getRemainingFinalizer(vertex.Obj)
271+
var finalizer string
272+
if b.currentTree != nil {
273+
finalizer = b.currentTree.GetFinalizer()
274+
}
289275
if len(finalizer) > 0 && controllerutil.RemoveFinalizer(vertex.Obj, finalizer) {
290276
err := b.cli.Update(ctx, vertex.Obj, clientOption(vertex))
291277
if err != nil && !apierrors.IsNotFound(err) {

pkg/controller/kubebuilderx/plan_builder_test.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -128,20 +128,21 @@ var _ = Describe("plan builder test", func() {
128128
Obj: its,
129129
Action: model.ActionDeletePtr(),
130130
}
131-
k8sMock.EXPECT().
132-
Update(gomock.Any(), gomock.Any(), gomock.Any()).
133-
DoAndReturn(func(_ context.Context, obj *workloads.InstanceSet, _ ...client.UpdateOption) error {
134-
Expect(obj).ShouldNot(BeNil())
135-
Expect(obj.Finalizers).Should(HaveLen(0))
136-
return nil
137-
}).Times(1)
131+
// k8sMock.EXPECT().
132+
// Update(gomock.Any(), gomock.Any(), gomock.Any()).
133+
// DoAndReturn(func(_ context.Context, obj *workloads.InstanceSet, _ ...client.UpdateOption) error {
134+
// Expect(obj).ShouldNot(BeNil())
135+
// Expect(obj.Finalizers).Should(HaveLen(0))
136+
// return nil
137+
// }).Times(1)
138138
k8sMock.EXPECT().
139139
Delete(gomock.Any(), gomock.Any(), gomock.Any()).
140140
DoAndReturn(func(_ context.Context, obj *workloads.InstanceSet, _ ...client.DeleteOption) error {
141141
Expect(obj).ShouldNot(BeNil())
142142
Expect(obj.Namespace).Should(Equal(its.Namespace))
143143
Expect(obj.Name).Should(Equal(its.Name))
144-
Expect(obj.Finalizers).Should(HaveLen(0))
144+
Expect(obj.Finalizers).Should(HaveLen(1))
145+
Expect(obj.Finalizers[0]).Should(Equal(finalizer))
145146
return nil
146147
}).Times(1)
147148
Expect(planBuilder.defaultWalkFunc(v)).Should(Succeed())

0 commit comments

Comments
 (0)