Skip to content

Commit 320b90b

Browse files
lmicciniclaude
andcommitted
Add finalizer and reconcileDelete to InstanceHA controller
The controller calls EnsureServiceTopology which places a finalizer on the referenced Topology CR, but had no mechanism to remove it. Deleting an InstanceHa CR would permanently orphan the Topology CR finalizer. Add finalizer lifecycle following the DNSMasq controller pattern: - Add finalizer on first reconcile (controllerutil.AddFinalizer) - Check DeletionTimestamp before proceeding with reconciliation - reconcileDelete removes the Topology CR finalizer via EnsureDeletedTopologyRef, then removes the InstanceHa finalizer Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 9eab60f commit 320b90b

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

internal/controller/instanceha/instanceha_controller.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,11 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ct
176176
instance.Status.Conditions.Init(&cl)
177177
instance.Status.ObservedGeneration = instance.Generation
178178

179+
// If we're not deleting this and the service object doesn't have our finalizer, add it.
180+
if instance.DeletionTimestamp.IsZero() && controllerutil.AddFinalizer(instance, helper.GetFinalizer()) || isNewInstance {
181+
return ctrl.Result{}, nil
182+
}
183+
179184
//// mark
180185
if instance.Status.NetworkAttachments == nil {
181186
instance.Status.NetworkAttachments = map[string][]string{}
@@ -187,6 +192,11 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ct
187192
cl.Set(c)
188193
}
189194

195+
// Handle service delete
196+
if !instance.DeletionTimestamp.IsZero() {
197+
return r.reconcileDelete(ctx, instance, helper)
198+
}
199+
190200
// Service account, role, binding
191201
rbacRules := []rbacv1.PolicyRule{
192202
{
@@ -897,6 +907,27 @@ func (r *Reconciler) findObjectsForSrc(ctx context.Context, src client.Object) [
897907
return requests
898908
}
899909

910+
func (r *Reconciler) reconcileDelete(ctx context.Context, instance *instancehav1.InstanceHa, helper *helper.Helper) (ctrl.Result, error) {
911+
Log := r.GetLogger(ctx)
912+
Log.Info("Reconciling Service delete")
913+
914+
// Remove finalizer on the Topology CR
915+
if ctrlResult, err := topologyv1.EnsureDeletedTopologyRef(
916+
ctx,
917+
helper,
918+
instance.Status.LastAppliedTopology,
919+
instance.Name,
920+
); err != nil {
921+
return ctrlResult, err
922+
}
923+
924+
// Service is deleted so remove the finalizer.
925+
controllerutil.RemoveFinalizer(instance, helper.GetFinalizer())
926+
Log.Info("Reconciled Service delete successfully")
927+
928+
return ctrl.Result{}, nil
929+
}
930+
900931
// GetContainerImage returns the container image to use for the instance, either from
901932
// the provided containerImage parameter or from the infra-instanceha-config ConfigMap
902933
func (r *Reconciler) GetContainerImage(

test/functional/instanceha_controller_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ var _ = Describe("InstanceHa Controller", func() {
5454
corev1.ConditionFalse,
5555
)
5656
})
57+
58+
It("should have the finalizer set on the CR", func() {
59+
Eventually(func(g Gomega) {
60+
instance := GetInstanceHa(instanceHaName)
61+
g.Expect(instance.Finalizers).To(ContainElement("openstack.org/instanceha"))
62+
}, timeout, interval).Should(Succeed())
63+
})
5764
})
5865

5966
When("prerequisite resources exist", func() {

0 commit comments

Comments
 (0)