@@ -232,11 +232,11 @@ func (r *ReconciliationRunner) CheckOwnedCRDsForReadiness(gvks ...schema.GroupVe
232
232
} else if ! found {
233
233
return r .RequeueWithMessage (
234
234
fmt .Sprintf (
235
- "Owned object of kind %s with name %s not found, requeueing ." ,
235
+ "Owned object of kind %s with name %s not found, requeuing ." ,
236
236
gvk .Kind ,
237
237
owned .GetName ()))
238
238
} else {
239
- r .Log .Info (fmt .Sprintf ("Owned object %s of kind %s not ready, requeueing " , name , gvk .Kind ))
239
+ r .Log .Info (fmt .Sprintf ("Owned object %s of kind %s not ready, requeuing " , name , gvk .Kind ))
240
240
return ctrl.Result {RequeueAfter : RequeueTimeout }, nil
241
241
}
242
242
}
@@ -246,6 +246,65 @@ func (r *ReconciliationRunner) CheckOwnedCRDsForReadiness(gvks ...schema.GroupVe
246
246
}
247
247
}
248
248
249
+ // CheckOwnedObjectsDeleted queries for the presence of owned objects and requeues if any are still present. Primarily
250
+ // used to prevent deletions of owners before dependents.
251
+ func (r * ReconciliationRunner ) DeleteOwnedObjects (gvks ... schema.GroupVersionKind ) CloudStackReconcilerMethod {
252
+ return func () (ctrl.Result , error ) {
253
+ // For each GroupVersionKind...
254
+ for _ , gvk := range gvks {
255
+ // Query to find objects of this kind.
256
+ potentiallyOnwedObjs := & unstructured.UnstructuredList {}
257
+ potentiallyOnwedObjs .SetGroupVersionKind (gvk )
258
+ err := r .K8sClient .List (r .RequestCtx , potentiallyOnwedObjs )
259
+ if err != nil {
260
+ return ctrl.Result {}, errors .Wrapf (err , "requesting owned objects with gvk %s" , gvk )
261
+ }
262
+
263
+ // Filter objects not actually owned by reconciliation subject via owner reference UID.
264
+ for _ , pOwned := range potentiallyOnwedObjs .Items {
265
+ refs := pOwned .GetOwnerReferences ()
266
+ for _ , ref := range refs {
267
+ if ref .UID == r .ReconciliationSubject .GetUID () {
268
+ if err := r .K8sClient .Delete (r .RequestCtx , & pOwned ); err != nil {
269
+ return ctrl.Result {}, err
270
+ }
271
+ }
272
+ }
273
+ }
274
+ }
275
+ return ctrl.Result {}, nil
276
+ }
277
+ }
278
+
279
+ // CheckOwnedObjectsDeleted queries for the presence of owned objects and requeues if any are still present. Primarily
280
+ // used to prevent deletions of owners before dependents.
281
+ func (r * ReconciliationRunner ) CheckOwnedObjectsDeleted (gvks ... schema.GroupVersionKind ) CloudStackReconcilerMethod {
282
+ return func () (ctrl.Result , error ) {
283
+ // For each GroupVersionKind...
284
+ for _ , gvk := range gvks {
285
+ // Query to find objects of this kind.
286
+ potentiallyOnwedObjs := & unstructured.UnstructuredList {}
287
+ potentiallyOnwedObjs .SetGroupVersionKind (gvk )
288
+ err := r .K8sClient .List (r .RequestCtx , potentiallyOnwedObjs )
289
+ if err != nil {
290
+ return ctrl.Result {}, errors .Wrapf (err , "requesting owned objects with gvk %s" , gvk )
291
+ }
292
+
293
+ // Filter objects not actually owned by reconciliation subject via owner reference UID.
294
+ for _ , pOwned := range potentiallyOnwedObjs .Items {
295
+ refs := pOwned .GetOwnerReferences ()
296
+ for _ , ref := range refs {
297
+ if ref .UID == r .ReconciliationSubject .GetUID () {
298
+ return r .RequeueWithMessage (
299
+ fmt .Sprintf ("Owned object %s of kind %s not yet deleted" , pOwned .GetKind (), pOwned .GetName ()))
300
+ }
301
+ }
302
+ }
303
+ }
304
+ return ctrl.Result {}, nil
305
+ }
306
+ }
307
+
249
308
// RequeueIfCloudStackClusterNotReady requeues the reconciliation request if the CloudStackCluster is not ready.
250
309
func (r * ReconciliationRunner ) RequeueIfCloudStackClusterNotReady () (ctrl.Result , error ) {
251
310
if ! r .CSCluster .Status .Ready {
@@ -272,9 +331,9 @@ func (r *ReconciliationRunner) PatchChangesBackToAPI() (res ctrl.Result, retErr
272
331
273
332
// RequeueWithMessage is a convenience method to log requeue message and then return a result with RequeueAfter set.
274
333
func (r * ReconciliationRunner ) RequeueWithMessage (msg string , keysAndValues ... interface {}) (ctrl.Result , error ) {
275
- // Add requeueing to message if not present. Might turn this into a lint check later.
276
- if ! strings .Contains (strings .ToLower (msg ), "requeue " ) {
277
- msg = msg + " Requeueing ."
334
+ // Add requeuing to message if not present. Might turn this into a lint check later.
335
+ if ! strings .Contains (strings .ToLower (msg ), "requeu " ) {
336
+ msg = msg + " Requeuing ."
278
337
}
279
338
r .Log .Info (msg , keysAndValues ... )
280
339
return ctrl.Result {RequeueAfter : RequeueTimeout }, nil
@@ -284,7 +343,7 @@ func (r *ReconciliationRunner) RequeueWithMessage(msg string, keysAndValues ...i
284
343
func (r * ReconciliationRunner ) RequeueWithMessageStage (msg string , keysAndValues ... interface {}) CloudStackReconcilerMethod {
285
344
return func () (ctrl.Result , error ) {
286
345
if ! strings .Contains (strings .ToLower (msg ), "requeue" ) {
287
- msg = msg + " Requeueing ."
346
+ msg = msg + " Requeuing ."
288
347
}
289
348
r .Log .Info (msg , keysAndValues ... )
290
349
return ctrl.Result {RequeueAfter : RequeueTimeout }, nil
@@ -381,6 +440,7 @@ func (r *ReconciliationRunner) GetReconciliationSubject() (res ctrl.Result, rete
381
440
r .Log .V (1 ).Info ("Getting reconciliation subject." )
382
441
err := client .IgnoreNotFound (r .K8sClient .Get (r .RequestCtx , r .Request .NamespacedName , r .ReconciliationSubject ))
383
442
if r .ReconciliationSubject .GetName () == "" { // Resource does not exist. No need to reconcile.
443
+ r .Log .V (1 ).Info ("Resource not found. Exiting reconciliation." )
384
444
r .SetReturnEarly ()
385
445
}
386
446
if err != nil {
@@ -438,11 +498,11 @@ func (r *ReconciliationRunner) NewChildObjectMeta(name string) metav1.ObjectMeta
438
498
// RequeueIfMissingBaseCRs checks that the ReconciliationSubject, CAPI Cluster, and CloudStackCluster objects were
439
499
// actually fetched and reques if not. The base reconciliation stages will continue even if not so as to allow deletion.
440
500
func (r * ReconciliationRunner ) RequeueIfMissingBaseCRs () (ctrl.Result , error ) {
441
- r .Log .V (1 ).Info ("Requeueing if missing ReconciliationSubject, CloudStack cluster, or CAPI cluster." )
501
+ r .Log .V (1 ).Info ("Requeuing if missing ReconciliationSubject, CloudStack cluster, or CAPI cluster." )
442
502
if r .CSCluster .GetName () == "" {
443
- return r .RequeueWithMessage ("CloudStackCluster wasn't found. Requeueing ." )
503
+ return r .RequeueWithMessage ("CloudStackCluster wasn't found. Requeuing ." )
444
504
} else if r .CAPICluster .GetName () == "" {
445
- return r .RequeueWithMessage ("CAPI Cluster wasn't found. Requeueing ." )
505
+ return r .RequeueWithMessage ("CAPI Cluster wasn't found. Requeuing ." )
446
506
}
447
507
return ctrl.Result {}, nil
448
508
}
0 commit comments