Skip to content

Commit bdf7cff

Browse files
authored
fix: drop only objects the deleted CR actually reconciled (#10870)
A conflicting duplicate Database or Subscription acquires the finalizer before the exclusivity check blocks it, so deleting it with a delete reclaim policy dropped the object owned by the surviving CR. Gate the drop on a recorded reconciliation, the same signal the exclusivity check uses. The Publication controller checks conflicts before adding the finalizer, but its drop hook gets the same guard for consistency and defense in depth. Closes #10866 Assisted-by: Claude Signed-off-by: Marco Nenciarini <[email protected]>
1 parent 8edf116 commit bdf7cff

6 files changed

Lines changed: 75 additions & 0 deletions

File tree

‎internal/management/controller/database_controller.go‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ func (r *DatabaseReconciler) evaluateDropDatabase(ctx context.Context, db *apiv1
188188
if db.Spec.ReclaimPolicy != apiv1.DatabaseReclaimDelete {
189189
return nil
190190
}
191+
191192
// On a replica we cannot drop the database: return without touching
192193
// PostgreSQL so the finalizer is released. Dropping it is left to the
193194
// primary cluster's own Database object, if any.
@@ -198,6 +199,14 @@ func (r *DatabaseReconciler) evaluateDropDatabase(ctx context.Context, db *apiv1
198199
if cluster.IsReplica() {
199200
return nil
200201
}
202+
203+
// An object that never reconciled does not own the database: a
204+
// conflicting duplicate is blocked before applying anything, and its
205+
// deletion must not drop the database owned by the surviving object.
206+
if !db.HasReconciliations() {
207+
return nil
208+
}
209+
201210
sqlDB, err := r.getSuperUserDB()
202211
if err != nil {
203212
return fmt.Errorf("while getting DB connection: %w", err)

‎internal/management/controller/database_controller_test.go‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,22 @@ var _ = Describe("Managed Database status", func() {
204204
})
205205
})
206206

207+
When("reclaim policy is delete but the object never reconciled", func() {
208+
It("on deletion it removes finalizers and does NOT drop the DB", func(ctx SpecContext) {
209+
// Model the state of a conflicting duplicate: it carries the
210+
// finalizer, but the exclusivity check blocked it before it could
211+
// apply anything, so it has no recorded reconciliation.
212+
database.Finalizers = []string{utils.DatabaseFinalizerName}
213+
Expect(fakeClient.Update(ctx, database)).To(Succeed())
214+
Expect(fakeClient.Delete(ctx, database)).To(Succeed())
215+
216+
// No SQL is expected: dbMock would fail on any statement.
217+
err := reconcileDatabase(ctx, fakeClient, r, database)
218+
Expect(err).To(HaveOccurred())
219+
Expect(apierrors.IsNotFound(err)).To(BeTrue())
220+
})
221+
})
222+
207223
When("reclaim policy is retain", func() {
208224
It("on deletion it removes finalizers and does NOT drop the DB", func(ctx SpecContext) {
209225
database.Spec.ReclaimPolicy = apiv1.DatabaseReclaimRetain

‎internal/management/controller/publication_controller.go‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ func (r *PublicationReconciler) evaluateDropPublication(ctx context.Context, pub
167167
if pub.Spec.ReclaimPolicy != apiv1.PublicationReclaimDelete {
168168
return nil
169169
}
170+
170171
// On a replica we cannot drop the publication: return without touching
171172
// PostgreSQL so the finalizer is released. Dropping it is left to the
172173
// primary cluster's own Publication object, if any.
@@ -177,6 +178,14 @@ func (r *PublicationReconciler) evaluateDropPublication(ctx context.Context, pub
177178
if cluster.IsReplica() {
178179
return nil
179180
}
181+
182+
// An object that never reconciled does not own the publication: a
183+
// conflicting duplicate is blocked before applying anything, and its
184+
// deletion must not drop the publication owned by the surviving object.
185+
if !pub.HasReconciliations() {
186+
return nil
187+
}
188+
180189
db, err := r.getDB(pub.Spec.DBName)
181190
if err != nil {
182191
return fmt.Errorf("while getting DB connection: %w", err)

‎internal/management/controller/publication_controller_test.go‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,22 @@ var _ = Describe("Managed publication controller tests", func() {
206206
})
207207
})
208208

209+
When("reclaim policy is delete but the object never reconciled", func() {
210+
It("on deletion it removes finalizers and does NOT drop the Publication", func(ctx SpecContext) {
211+
// Model the state of a conflicting duplicate: it carries the
212+
// finalizer, but the exclusivity check blocked it before it could
213+
// apply anything, so it has no recorded reconciliation.
214+
publication.Finalizers = []string{utils.PublicationFinalizerName}
215+
Expect(fakeClient.Update(ctx, publication)).To(Succeed())
216+
Expect(fakeClient.Delete(ctx, publication)).To(Succeed())
217+
218+
// No SQL is expected: dbMock would fail on any statement.
219+
err := reconcilePublication(ctx, fakeClient, r, publication)
220+
Expect(err).To(HaveOccurred())
221+
Expect(apierrors.IsNotFound(err)).To(BeTrue())
222+
})
223+
})
224+
209225
When("reclaim policy is retain", func() {
210226
It("on deletion it removes finalizers and does NOT drop the Publication", func(ctx SpecContext) {
211227
publication.Spec.ReclaimPolicy = apiv1.PublicationReclaimRetain

‎internal/management/controller/subscription_controller.go‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ func (r *SubscriptionReconciler) evaluateDropSubscription(ctx context.Context, s
175175
if sub.Spec.ReclaimPolicy != apiv1.SubscriptionReclaimDelete {
176176
return nil
177177
}
178+
178179
// On a replica we cannot drop the subscription: return without touching
179180
// PostgreSQL so the finalizer is released. Dropping it is left to the
180181
// primary cluster's own Subscription object, if any.
@@ -185,6 +186,14 @@ func (r *SubscriptionReconciler) evaluateDropSubscription(ctx context.Context, s
185186
if cluster.IsReplica() {
186187
return nil
187188
}
189+
190+
// An object that never reconciled does not own the subscription: a
191+
// conflicting duplicate is blocked before applying anything, and its
192+
// deletion must not drop the subscription owned by the surviving object.
193+
if !sub.HasReconciliations() {
194+
return nil
195+
}
196+
188197
db, err := r.getDB(sub.Spec.DBName)
189198
if err != nil {
190199
return fmt.Errorf("while getting DB connection: %w", err)

‎internal/management/controller/subscription_controller_test.go‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,22 @@ var _ = Describe("Managed subscription controller tests", func() {
236236
})
237237
})
238238

239+
When("reclaim policy is delete but the object never reconciled", func() {
240+
It("on deletion it removes finalizers and does NOT drop the subscription", func(ctx SpecContext) {
241+
// Model the state of a conflicting duplicate: it carries the
242+
// finalizer, but the exclusivity check blocked it before it could
243+
// apply anything, so it has no recorded reconciliation.
244+
subscription.Finalizers = []string{utils.SubscriptionFinalizerName}
245+
Expect(fakeClient.Update(ctx, subscription)).To(Succeed())
246+
Expect(fakeClient.Delete(ctx, subscription)).To(Succeed())
247+
248+
// No SQL is expected: dbMock would fail on any statement.
249+
err := reconcileSubscription(ctx, fakeClient, r, subscription)
250+
Expect(err).To(HaveOccurred())
251+
Expect(apierrors.IsNotFound(err)).To(BeTrue())
252+
})
253+
})
254+
239255
When("reclaim policy is retain", func() {
240256
It("on deletion it removes finalizers and does NOT drop the subscription", func(ctx SpecContext) {
241257
subscription.Spec.ReclaimPolicy = apiv1.SubscriptionReclaimRetain

0 commit comments

Comments
 (0)