@@ -63,13 +63,14 @@ type CloudStackBaseContext struct {
63
63
type ReconciliationRunner struct {
64
64
ReconcilerBase
65
65
CloudStackBaseContext
66
- ReconciliationSubject client.Object // Underlying crd interface.
67
- ConditionalResult bool // Stores a conidtinal result for stringing if else type methods.
68
- returnEarly bool // A signal that the reconcile should return early.
69
- ReconcileDelete CloudStackReconcilerMethod
70
- Reconcile CloudStackReconcilerMethod
71
- CSUser cloud.Client
72
- ControllerKind string
66
+ ReconciliationSubject client.Object // Underlying crd interface.
67
+ ConditionalResult bool // Stores a conidtinal result for stringing if else type methods.
68
+ returnEarly bool // A signal that the reconcile should return early.
69
+ additionalCommonStages []CloudStackReconcilerMethod
70
+ ReconcileDelete CloudStackReconcilerMethod
71
+ Reconcile CloudStackReconcilerMethod
72
+ CSUser cloud.Client
73
+ ControllerKind string
73
74
}
74
75
75
76
type ConcreteRunner interface {
@@ -110,6 +111,12 @@ func (r *ReconciliationRunner) WithRequestCtx(ctx context.Context) *Reconciliati
110
111
return r
111
112
}
112
113
114
+ // WithRequestCtx sets the request context.
115
+ func (r * ReconciliationRunner ) WithAdditionalCommonStages (fns ... CloudStackReconcilerMethod ) * ReconciliationRunner {
116
+ r .additionalCommonStages = fns
117
+ return r
118
+ }
119
+
113
120
// SetupLogger sets up the reconciler's logger to log with name and namespace values.
114
121
func (r * ReconciliationRunner ) SetupLogger () (res ctrl.Result , retErr error ) {
115
122
r .Log = r .BaseLogger .WithName (r .ControllerKind ).WithValues ("name" , r .Request .Name , "namespace" , r .Request .Namespace )
@@ -237,30 +244,6 @@ func (r *ReconciliationRunner) CheckOwnedCRDsForReadiness(gvks ...schema.GroupVe
237
244
}
238
245
}
239
246
240
- // SetCSUser sets the CSUser client to any user that can operate in specified domain and account if specified.
241
- func (r * ReconciliationRunner ) SetCSUser () (ctrl.Result , error ) {
242
- // TODO: Remove no-op.
243
- // r.CSUser = r.CSClient
244
- // if r.CSCluster.Spec.Account != "" {
245
- // user := &cloud.User{}
246
- // user.Account.Domain.Path = r.CSCluster.Spec.Domain
247
- // user.Account.Name = r.CSCluster.Spec.Account
248
- // if found, err := r.CSClient.GetUserWithKeys(user); err != nil {
249
- // return ctrl.Result{}, err
250
- // } else if !found {
251
- // return ctrl.Result{}, errors.Errorf("could not find sufficient user (with API keys) in domain/account %s/%s",
252
- // r.CSCluster.Spec.Domain, r.CSCluster.Spec.Account)
253
- // }
254
- // cfg := cloud.Config{APIKey: user.APIKey, SecretKey: user.SecretKey}
255
- // client, err := r.CSClient.NewClientFromSpec(cfg)
256
- // if err != nil {
257
- // return ctrl.Result{}, err
258
- // }
259
- // r.CSUser = client
260
- // }
261
- return ctrl.Result {}, nil
262
- }
263
-
264
247
// RequeueIfCloudStackClusterNotReady requeues the reconciliation request if the CloudStackCluster is not ready.
265
248
func (r * ReconciliationRunner ) RequeueIfCloudStackClusterNotReady () (ctrl.Result , error ) {
266
249
if ! r .CSCluster .Status .Ready {
@@ -357,16 +340,21 @@ func (r *ReconciliationRunner) RunBaseReconciliationStages() (res ctrl.Result, r
357
340
}
358
341
}
359
342
}()
360
- return r .RunReconciliationStages (
343
+
344
+ // Inject common stages prior to calling Reconcile or ReconcileDelete.
345
+ baseStages := []CloudStackReconcilerMethod {
361
346
r .SetupLogger ,
362
347
r .GetReconciliationSubject ,
363
348
r .SetupPatcher ,
364
349
r .GetCAPICluster ,
365
350
r .GetCSCluster ,
366
- r .RequeueIfMissingBaseCRs ,
351
+ r .RequeueIfMissingBaseCRs }
352
+ baseStages = append (
353
+ append (baseStages , r .additionalCommonStages ... ),
367
354
r .IfDeletionTimestampIsZero (r .Reconcile ),
368
- r .Else (r .ReconcileDelete ),
369
- )
355
+ r .Else (r .ReconcileDelete ))
356
+
357
+ return r .RunReconciliationStages (baseStages ... )
370
358
}
371
359
372
360
// CheckIfPaused returns with requeue later set if paused.
@@ -378,13 +366,19 @@ func (r *ReconciliationRunner) CheckIfPaused() (ctrl.Result, error) {
378
366
return reconcile.Result {}, nil
379
367
}
380
368
369
+ // SetReturnEarly sets the runner to return early. This causes the runner to break from running further
370
+ // reconciliation stages and return whatever result the current method returns.
371
+ func (r * ReconciliationRunner ) SetReturnEarly () {
372
+ r .returnEarly = true
373
+ }
374
+
381
375
// GetReconcilationSubject gets the reconciliation subject of type defined by the concrete reconciler. It also sets up
382
376
// a patch helper at this point.
383
377
func (r * ReconciliationRunner ) GetReconciliationSubject () (res ctrl.Result , reterr error ) {
384
378
r .Log .V (1 ).Info ("Getting reconciliation subject." )
385
379
err := client .IgnoreNotFound (r .K8sClient .Get (r .RequestCtx , r .Request .NamespacedName , r .ReconciliationSubject ))
386
- if r .ReconciliationSubject .GetName () == "" {
387
- return ctrl. Result {}, errors . Errorf ( "Could not fetch object by namespaced name: %s" , r . Request . NamespacedName )
380
+ if r .ReconciliationSubject .GetName () == "" { // Resource does not exist. No need to reconcile.
381
+ r . SetReturnEarly ( )
388
382
}
389
383
if err != nil {
390
384
return ctrl.Result {}, errors .Wrap (err , "fetching reconciliation subject" )
@@ -403,7 +397,7 @@ func (r *ReconciliationRunner) SetReconciliationSubjectToConcreteSubject(subject
403
397
// InitFromMgr just initiates a ReconcilerBase using given manager's fields/methods.
404
398
func (r * ReconcilerBase ) InitFromMgr (mgr ctrl.Manager , client cloud.Client ) {
405
399
r .K8sClient = mgr .GetClient ()
406
- r .BaseLogger = ctrl .Log .WithName ("controllers" ). WithName ( "name" )
400
+ r .BaseLogger = ctrl .Log .WithName ("controllers" )
407
401
r .Scheme = mgr .GetScheme ()
408
402
r .CSClient = client
409
403
}
0 commit comments