Skip to content

Commit f31eecb

Browse files
author
Joshua Reed
committed
Works on single host. Should work on two. We'll see...
1 parent 6860b32 commit f31eecb

File tree

4 files changed

+52
-72
lines changed

4 files changed

+52
-72
lines changed

controllers/cloudstackfailuredomain_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func (r *CloudStackFailureDomainReconciliationRunner) Reconcile() (retRes ctrl.R
106106

107107
// ReconcileDelete on the ReconciliationRunner actually attempts to delete the reconciliation subject.
108108
func (r *CloudStackFailureDomainReconciliationRunner) ReconcileDelete() (retRes ctrl.Result, retErr error) {
109-
r.Log.Info("Deleting CloudStackZone")
109+
r.Log.Info("Deleting CloudStackFailureDomain")
110110
// Address Isolated Networks.
111111
if r.ReconciliationSubject.Spec.Zone.Network.Type == infrav1.NetworkTypeIsolated {
112112
netName := r.ReconciliationSubject.Spec.Zone.Network.Name

controllers/cloudstackisolatednetwork_controller.go

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
"sigs.k8s.io/cluster-api/util/patch"
2424
ctrl "sigs.k8s.io/controller-runtime"
25+
"sigs.k8s.io/controller-runtime/pkg/client"
2526
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
2627

2728
"github.com/pkg/errors"
@@ -57,27 +58,17 @@ func NewCSIsoNetReconciliationRunner() *CloudStackIsoNetReconciliationRunner {
5758
}
5859

5960
func (reconciler *CloudStackIsoNetReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res ctrl.Result, retErr error) {
60-
return NewCSIsoNetReconciliationRunner().
61-
UsingBaseReconciler(reconciler.ReconcilerBase).
62-
ForRequest(req).
63-
WithRequestCtx(ctx).
64-
RunBaseReconciliationStages()
61+
r := NewCSIsoNetReconciliationRunner()
62+
r.UsingBaseReconciler(reconciler.ReconcilerBase).ForRequest(req).WithRequestCtx(ctx)
63+
r.WithAdditionalCommonStages(
64+
r.GetParent(r.ReconciliationSubject, r.FailureDomain),
65+
r.CheckPresent(map[string]client.Object{"CloudStackFailureDomain": r.FailureDomain}),
66+
r.AsFailureDomainUser(&r.FailureDomain.Spec),
67+
)
68+
return r.RunBaseReconciliationStages()
6569
}
6670

6771
func (r *CloudStackIsoNetReconciliationRunner) Reconcile() (retRes ctrl.Result, retErr error) {
68-
if res, err := r.GetParent(r.ReconciliationSubject, r.FailureDomain)(); r.ShouldReturn(res, err) {
69-
return res, err
70-
}
71-
if r.FailureDomain.Spec.Name == "" {
72-
return ctrl.Result{}, errors.New("couldn't get parent Failure Domain for placement")
73-
}
74-
res, err := r.AsFailureDomainUser(&r.FailureDomain.Spec)()
75-
if r.ShouldReturn(res, err) {
76-
return res, err
77-
}
78-
if res, err := r.RequeueIfMissingBaseCRs(); r.ShouldReturn(res, err) {
79-
return res, err
80-
}
8172
controllerutil.AddFinalizer(r.ReconciliationSubject, infrav1.IsolatedNetworkFinalizer)
8273

8374
// Setup isolated network, endpoint, egress, and load balancing.

controllers/cloudstackmachine_controller.go

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -82,22 +82,21 @@ func NewCSMachineReconciliationRunner() *CloudStackMachineReconciliationRunner {
8282
// Reconcile is part of the main kubernetes reconciliation loop which aims to
8383
// move the current state of the cluster closer to the desired state.
8484
func (reconciler *CloudStackMachineReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res ctrl.Result, retErr error) {
85-
return NewCSMachineReconciliationRunner().
86-
UsingBaseReconciler(reconciler.ReconcilerBase).
87-
ForRequest(req).
88-
WithRequestCtx(ctx).
89-
RunBaseReconciliationStages()
90-
}
91-
92-
func (r *CloudStackMachineReconciliationRunner) Reconcile() (retRes ctrl.Result, reterr error) {
93-
return r.RunReconciliationStages(
85+
r := NewCSMachineReconciliationRunner()
86+
r.UsingBaseReconciler(reconciler.ReconcilerBase).ForRequest(req).WithRequestCtx(ctx)
87+
r.WithAdditionalCommonStages(
9488
r.GetParent(r.ReconciliationSubject, r.CAPIMachine),
9589
r.RequeueIfCloudStackClusterNotReady,
9690
r.SetFailureDomainOnCSMachine,
9791
r.GetObjectByName("placeholder", r.FailureDomain,
9892
func() string { return r.ReconciliationSubject.Spec.FailureDomain.Name }),
9993
r.CheckPresent(map[string]client.Object{"CloudStackFailureDomain": r.FailureDomain}),
100-
r.AsFailureDomainUser(&r.FailureDomain.Spec),
94+
r.AsFailureDomainUser(&r.FailureDomain.Spec))
95+
return r.RunBaseReconciliationStages()
96+
}
97+
98+
func (r *CloudStackMachineReconciliationRunner) Reconcile() (retRes ctrl.Result, reterr error) {
99+
return r.RunReconciliationStages(
101100
r.GetObjectByName("placeholder", r.IsoNet,
102101
func() string { return r.IsoNetMetaName(r.FailureDomain.Spec.Zone.Network.Name) }),
103102
r.RunIf(func() bool { return r.FailureDomain.Spec.Zone.Network.Type == cloud.NetworkTypeIsolated },
@@ -145,7 +144,7 @@ func (r *CloudStackMachineReconciliationRunner) SetFailureDomainOnCSMachine() (r
145144
return ctrl.Result{}, nil
146145
}
147146

148-
// GetOrCreateVMInstance creates or gets a VM instance.
147+
// GetOrCreateVMInstance gets or creates a VM instance.
149148
// Implicitly it also fetches its bootstrap secret in order to create said instance.
150149
func (r *CloudStackMachineReconciliationRunner) GetOrCreateVMInstance() (retRes ctrl.Result, reterr error) {
151150
if r.CAPIMachine.Spec.Bootstrap.DataSecretName == nil {
@@ -224,9 +223,6 @@ func (r *CloudStackMachineReconciliationRunner) GetOrCreateMachineStateChecker()
224223
}
225224

226225
func (r *CloudStackMachineReconciliationRunner) ReconcileDelete() (retRes ctrl.Result, reterr error) {
227-
if res, err := r.AsFailureDomainUser(&r.ReconciliationSubject.Spec.FailureDomain)(); r.ShouldReturn(res, err) {
228-
return res, err
229-
}
230226
if r.ReconciliationSubject.Spec.InstanceID != nil {
231227
r.Log.Info("Deleting instance", "instance-id", r.ReconciliationSubject.Spec.InstanceID)
232228
// Use CSClient instead of CSUser here to expunge as admin.
@@ -247,7 +243,6 @@ func (r *CloudStackMachineReconciliationRunner) ReconcileDelete() (retRes ctrl.R
247243

248244
// SetupWithManager registers the machine reconciler to the CAPI controller manager.
249245
func (reconciler *CloudStackMachineReconciler) SetupWithManager(mgr ctrl.Manager) error {
250-
251246
controller, err := ctrl.NewControllerManagedBy(mgr).
252247
For(&infrav1.CloudStackMachine{}).
253248
WithEventFilter(

controllers/utils/base_reconciler.go

Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,14 @@ type CloudStackBaseContext struct {
6363
type ReconciliationRunner struct {
6464
ReconcilerBase
6565
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
7374
}
7475

7576
type ConcreteRunner interface {
@@ -110,6 +111,12 @@ func (r *ReconciliationRunner) WithRequestCtx(ctx context.Context) *Reconciliati
110111
return r
111112
}
112113

114+
// WithRequestCtx sets the request context.
115+
func (r *ReconciliationRunner) WithAdditionalCommonStages(fns ...CloudStackReconcilerMethod) *ReconciliationRunner {
116+
r.additionalCommonStages = fns
117+
return r
118+
}
119+
113120
// SetupLogger sets up the reconciler's logger to log with name and namespace values.
114121
func (r *ReconciliationRunner) SetupLogger() (res ctrl.Result, retErr error) {
115122
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
237244
}
238245
}
239246

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-
264247
// RequeueIfCloudStackClusterNotReady requeues the reconciliation request if the CloudStackCluster is not ready.
265248
func (r *ReconciliationRunner) RequeueIfCloudStackClusterNotReady() (ctrl.Result, error) {
266249
if !r.CSCluster.Status.Ready {
@@ -357,16 +340,21 @@ func (r *ReconciliationRunner) RunBaseReconciliationStages() (res ctrl.Result, r
357340
}
358341
}
359342
}()
360-
return r.RunReconciliationStages(
343+
344+
// Inject common stages prior to calling Reconcile or ReconcileDelete.
345+
baseStages := []CloudStackReconcilerMethod{
361346
r.SetupLogger,
362347
r.GetReconciliationSubject,
363348
r.SetupPatcher,
364349
r.GetCAPICluster,
365350
r.GetCSCluster,
366-
r.RequeueIfMissingBaseCRs,
351+
r.RequeueIfMissingBaseCRs}
352+
baseStages = append(
353+
append(baseStages, r.additionalCommonStages...),
367354
r.IfDeletionTimestampIsZero(r.Reconcile),
368-
r.Else(r.ReconcileDelete),
369-
)
355+
r.Else(r.ReconcileDelete))
356+
357+
return r.RunReconciliationStages(baseStages...)
370358
}
371359

372360
// CheckIfPaused returns with requeue later set if paused.
@@ -378,13 +366,19 @@ func (r *ReconciliationRunner) CheckIfPaused() (ctrl.Result, error) {
378366
return reconcile.Result{}, nil
379367
}
380368

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+
381375
// GetReconcilationSubject gets the reconciliation subject of type defined by the concrete reconciler. It also sets up
382376
// a patch helper at this point.
383377
func (r *ReconciliationRunner) GetReconciliationSubject() (res ctrl.Result, reterr error) {
384378
r.Log.V(1).Info("Getting reconciliation subject.")
385379
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()
388382
}
389383
if err != nil {
390384
return ctrl.Result{}, errors.Wrap(err, "fetching reconciliation subject")
@@ -403,7 +397,7 @@ func (r *ReconciliationRunner) SetReconciliationSubjectToConcreteSubject(subject
403397
// InitFromMgr just initiates a ReconcilerBase using given manager's fields/methods.
404398
func (r *ReconcilerBase) InitFromMgr(mgr ctrl.Manager, client cloud.Client) {
405399
r.K8sClient = mgr.GetClient()
406-
r.BaseLogger = ctrl.Log.WithName("controllers").WithName("name")
400+
r.BaseLogger = ctrl.Log.WithName("controllers")
407401
r.Scheme = mgr.GetScheme()
408402
r.CSClient = client
409403
}

0 commit comments

Comments
 (0)