@@ -74,10 +74,13 @@ type EtcdClusterReconciler struct {
74
74
75
75
// Reconcile checks CR and current cluster state and performs actions to transform current state to desired.
76
76
func (r * EtcdClusterReconciler ) Reconcile (ctx context.Context , req ctrl.Request ) (ctrl.Result , error ) {
77
+ var state State
77
78
log .Debug (ctx , "reconciling object" )
78
- state := & observables {}
79
- state .instance = & etcdaenixiov1alpha1.EtcdCluster {}
80
- err := r .Get (ctx , req .NamespacedName , state .instance )
79
+
80
+ // TODO: marry abstract interface with actual object pointer
81
+ // state := &observables{}
82
+ // state.instance = &etcdaenixiov1alpha1.EtcdCluster{}
83
+ err := state .GetEtcdCluster ()
81
84
if err != nil {
82
85
if errors .IsNotFound (err ) {
83
86
log .Debug (ctx , "object not found" )
@@ -86,9 +89,10 @@ func (r *EtcdClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request)
86
89
// Error retrieving object, requeue
87
90
return reconcile.Result {}, err
88
91
}
89
- // If object is being deleted, skipping reconciliation
90
- if ! state .instance .DeletionTimestamp .IsZero () {
91
- return reconcile.Result {}, nil
92
+
93
+ // If object is being deleted, handle finalizers, dependent objects, etc
94
+ if state .PendingDeletion () {
95
+ return ctrl.Result {}, r .handleDeletion (ctx , state )
92
96
}
93
97
94
98
// create two services and the pdb
@@ -232,7 +236,7 @@ func (r *EtcdClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request)
232
236
}
233
237
234
238
// checkAndDeleteStatefulSetIfNecessary deletes the StatefulSet if the specified storage size has changed.
235
- func (r * EtcdClusterReconciler ) checkAndDeleteStatefulSetIfNecessary (ctx context.Context , state * observables ) error {
239
+ func (r * EtcdClusterReconciler ) checkAndDeleteStatefulSetIfNecessary (ctx context.Context , state State ) error {
236
240
for _ , volumeClaimTemplate := range state .statefulSet .Spec .VolumeClaimTemplates {
237
241
if volumeClaimTemplate .Name != "data" {
238
242
continue
@@ -254,7 +258,7 @@ func (r *EtcdClusterReconciler) checkAndDeleteStatefulSetIfNecessary(ctx context
254
258
}
255
259
256
260
// ensureConditionalClusterObjects creates or updates all objects owned by cluster CR
257
- func (r * EtcdClusterReconciler ) ensureConditionalClusterObjects (ctx context.Context , state * observables ) error {
261
+ func (r * EtcdClusterReconciler ) ensureConditionalClusterObjects (ctx context.Context , state State ) error {
258
262
259
263
if err := factory .CreateOrUpdateClusterStateConfigMap (ctx , state .instance , r .Client ); err != nil {
260
264
log .Error (ctx , err , "reconcile cluster state configmap failed" )
@@ -277,7 +281,7 @@ func (r *EtcdClusterReconciler) ensureConditionalClusterObjects(ctx context.Cont
277
281
}
278
282
279
283
// updateStatusOnErr wraps error and updates EtcdCluster status
280
- func (r * EtcdClusterReconciler ) updateStatusOnErr (ctx context.Context , state * observables , err error ) (ctrl.Result , error ) {
284
+ func (r * EtcdClusterReconciler ) updateStatusOnErr (ctx context.Context , state State , err error ) (ctrl.Result , error ) {
281
285
// The function 'updateStatusOnErr' will always return non-nil error. Hence, the ctrl.Result will always be ignored.
282
286
// Therefore, the ctrl.Result returned by 'updateStatus' function can be discarded.
283
287
// REF: https://pkg.go.dev/sigs.k8s.io/controller-runtime/pkg/reconcile@v0.17.3#Reconciler
@@ -289,7 +293,7 @@ func (r *EtcdClusterReconciler) updateStatusOnErr(ctx context.Context, state *ob
289
293
}
290
294
291
295
// updateStatus updates EtcdCluster status and returns error and requeue in case status could not be updated due to conflict
292
- func (r * EtcdClusterReconciler ) updateStatus (ctx context.Context , state * observables ) (ctrl.Result , error ) {
296
+ func (r * EtcdClusterReconciler ) updateStatus (ctx context.Context , state State ) (ctrl.Result , error ) {
293
297
err := r .Status ().Update (ctx , state .instance )
294
298
if err == nil {
295
299
return ctrl.Result {}, nil
@@ -303,7 +307,7 @@ func (r *EtcdClusterReconciler) updateStatus(ctx context.Context, state *observa
303
307
}
304
308
305
309
// isStatefulSetReady gets managed StatefulSet and checks its readiness.
306
- func (r * EtcdClusterReconciler ) isStatefulSetReady (ctx context.Context , state * observables ) (bool , error ) {
310
+ func (r * EtcdClusterReconciler ) isStatefulSetReady (ctx context.Context , state State ) (bool , error ) {
307
311
sts := & appsv1.StatefulSet {}
308
312
err := r .Get (ctx , client .ObjectKeyFromObject (state .instance ), sts )
309
313
if err == nil {
@@ -323,7 +327,7 @@ func (r *EtcdClusterReconciler) SetupWithManager(mgr ctrl.Manager) error {
323
327
Complete (r )
324
328
}
325
329
326
- func (r * EtcdClusterReconciler ) configureAuth (ctx context.Context , state * observables ) error {
330
+ func (r * EtcdClusterReconciler ) configureAuth (ctx context.Context , state State ) error {
327
331
328
332
var err error
329
333
@@ -603,7 +607,7 @@ func (r *EtcdClusterReconciler) disableAuth(ctx context.Context, authClient clie
603
607
// ensureUnconditionalObjects creates the two services and the PDB
604
608
// which can be created at the start of the reconciliation loop
605
609
// without any risk of disrupting the etcd cluster
606
- func (r * EtcdClusterReconciler ) ensureUnconditionalObjects (ctx context.Context , state * observables ) error {
610
+ func (r * EtcdClusterReconciler ) ensureUnconditionalObjects (ctx context.Context , state State ) error {
607
611
const concurrentOperations = 3
608
612
c := make (chan error )
609
613
defer close (c )
@@ -669,7 +673,7 @@ func (r *EtcdClusterReconciler) patchOrCreateObject(ctx context.Context, obj cli
669
673
670
674
// TODO!
671
675
// nolint:unparam,unused
672
- func (r * EtcdClusterReconciler ) createClusterFromScratch (ctx context.Context , state * observables ) (ctrl.Result , error ) {
676
+ func (r * EtcdClusterReconciler ) createClusterFromScratch (ctx context.Context , state State ) (ctrl.Result , error ) {
673
677
cm := factory .TemplateClusterStateConfigMap (state .instance , "new" , state .desiredReplicas ())
674
678
err := ctrl .SetControllerReference (state .instance , cm , r .Scheme )
675
679
if err != nil {
@@ -728,3 +732,9 @@ func (r *EtcdClusterReconciler) createOrUpdateStatefulSet(ctx context.Context) e
728
732
func (r * EtcdClusterReconciler ) promoteLearners (ctx context.Context ) error {
729
733
return fmt .Errorf ("not yet implemented" )
730
734
}
735
+
736
+ // TODO!
737
+ // nolint:unused
738
+ func (r * EtcdClusterReconciler ) handleDeletion (ctx context.Context , state State ) error {
739
+ return fmt .Errorf ("not yet implemented" )
740
+ }
0 commit comments