@@ -157,6 +157,15 @@ func (r *KeystoneEndpointReconciler) Reconcile(ctx context.Context, req ctrl.Req
157157 return ctrl.Result {}, err
158158 }
159159
160+ // If both the endpoint and the KeystoneAPI is deleted then we can skip
161+ // the cleanup of the endpoint in the DB as the DB is going away as well.
162+ // Moreover if KeystoneAPI is being deleted then we cannot talk to the
163+ // keystone REST API any more. This happens for example during namespace
164+ // deletion.
165+ if ! instance .DeletionTimestamp .IsZero () && ! keystoneAPI .DeletionTimestamp .IsZero () {
166+ return r .reconcileDeleteFinalizersOnly (ctx , instance , helper , keystoneAPI )
167+ }
168+
160169 // If this KeystoneEndpoint CR is being deleted and it has not registered any actual
161170 // endpoints on the OpenStack side, just redirect execution to the "reconcileDelete()"
162171 // logic to avoid potentially hanging on waiting for the KeystoneAPI to be ready
@@ -303,6 +312,43 @@ func (r *KeystoneEndpointReconciler) reconcileDelete(
303312 return ctrl.Result {}, nil
304313}
305314
315+ func (r * KeystoneEndpointReconciler ) reconcileDeleteFinalizersOnly (
316+ ctx context.Context ,
317+ instance * keystonev1.KeystoneEndpoint ,
318+ helper * helper.Helper ,
319+ keystoneAPI * keystonev1.KeystoneAPI ,
320+ ) (ctrl.Result , error ) {
321+ l := GetLog (ctx )
322+ l .Info ("Reconciling Endpoint delete while KeystoneAPI is being deleted" )
323+
324+ ksSvc , err := keystonev1 .GetKeystoneServiceWithName (ctx , helper , instance .Spec .ServiceName , instance .Namespace )
325+ if err == nil {
326+ // Remove the finalizer for this endpoint from the Service
327+ if controllerutil .RemoveFinalizer (ksSvc , fmt .Sprintf ("%s-%s" , helper .GetFinalizer (), instance .Name )) {
328+ err := r .Update (ctx , ksSvc )
329+
330+ if err != nil {
331+ return ctrl.Result {}, err
332+ }
333+ }
334+ } else if ! k8s_errors .IsNotFound (err ) {
335+ return ctrl.Result {}, err
336+ }
337+
338+ if controllerutil .RemoveFinalizer (keystoneAPI , fmt .Sprintf ("%s-%s" , helper .GetFinalizer (), instance .Name )) {
339+ err := r .Update (ctx , keystoneAPI )
340+
341+ if err != nil {
342+ return ctrl.Result {}, err
343+ }
344+ }
345+
346+ controllerutil .RemoveFinalizer (instance , helper .GetFinalizer ())
347+ l .Info ("Reconciled Endpoint delete successfully" )
348+
349+ return ctrl.Result {}, nil
350+ }
351+
306352func (r * KeystoneEndpointReconciler ) reconcileNormal (
307353 ctx context.Context ,
308354 instance * keystonev1.KeystoneEndpoint ,
0 commit comments