Skip to content

Commit d4da5c4

Browse files
committed
Draft: do observables as interface
1 parent 45683f4 commit d4da5c4

File tree

3 files changed

+38
-15
lines changed

3 files changed

+38
-15
lines changed

internal/controller/etcdcluster_controller.go

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,13 @@ type EtcdClusterReconciler struct {
7474

7575
// Reconcile checks CR and current cluster state and performs actions to transform current state to desired.
7676
func (r *EtcdClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
77+
var state State
7778
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()
8184
if err != nil {
8285
if errors.IsNotFound(err) {
8386
log.Debug(ctx, "object not found")
@@ -86,9 +89,10 @@ func (r *EtcdClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request)
8689
// Error retrieving object, requeue
8790
return reconcile.Result{}, err
8891
}
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)
9296
}
9397

9498
// create two services and the pdb
@@ -232,7 +236,7 @@ func (r *EtcdClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request)
232236
}
233237

234238
// 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 {
236240
for _, volumeClaimTemplate := range state.statefulSet.Spec.VolumeClaimTemplates {
237241
if volumeClaimTemplate.Name != "data" {
238242
continue
@@ -254,7 +258,7 @@ func (r *EtcdClusterReconciler) checkAndDeleteStatefulSetIfNecessary(ctx context
254258
}
255259

256260
// 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 {
258262

259263
if err := factory.CreateOrUpdateClusterStateConfigMap(ctx, state.instance, r.Client); err != nil {
260264
log.Error(ctx, err, "reconcile cluster state configmap failed")
@@ -277,7 +281,7 @@ func (r *EtcdClusterReconciler) ensureConditionalClusterObjects(ctx context.Cont
277281
}
278282

279283
// 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) {
281285
// The function 'updateStatusOnErr' will always return non-nil error. Hence, the ctrl.Result will always be ignored.
282286
// Therefore, the ctrl.Result returned by 'updateStatus' function can be discarded.
283287
// 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
289293
}
290294

291295
// 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) {
293297
err := r.Status().Update(ctx, state.instance)
294298
if err == nil {
295299
return ctrl.Result{}, nil
@@ -303,7 +307,7 @@ func (r *EtcdClusterReconciler) updateStatus(ctx context.Context, state *observa
303307
}
304308

305309
// 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) {
307311
sts := &appsv1.StatefulSet{}
308312
err := r.Get(ctx, client.ObjectKeyFromObject(state.instance), sts)
309313
if err == nil {
@@ -323,7 +327,7 @@ func (r *EtcdClusterReconciler) SetupWithManager(mgr ctrl.Manager) error {
323327
Complete(r)
324328
}
325329

326-
func (r *EtcdClusterReconciler) configureAuth(ctx context.Context, state *observables) error {
330+
func (r *EtcdClusterReconciler) configureAuth(ctx context.Context, state State) error {
327331

328332
var err error
329333

@@ -603,7 +607,7 @@ func (r *EtcdClusterReconciler) disableAuth(ctx context.Context, authClient clie
603607
// ensureUnconditionalObjects creates the two services and the PDB
604608
// which can be created at the start of the reconciliation loop
605609
// 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 {
607611
const concurrentOperations = 3
608612
c := make(chan error)
609613
defer close(c)
@@ -669,7 +673,7 @@ func (r *EtcdClusterReconciler) patchOrCreateObject(ctx context.Context, obj cli
669673

670674
// TODO!
671675
// 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) {
673677
cm := factory.TemplateClusterStateConfigMap(state.instance, "new", state.desiredReplicas())
674678
err := ctrl.SetControllerReference(state.instance, cm, r.Scheme)
675679
if err != nil {
@@ -728,3 +732,9 @@ func (r *EtcdClusterReconciler) createOrUpdateStatefulSet(ctx context.Context) e
728732
func (r *EtcdClusterReconciler) promoteLearners(ctx context.Context) error {
729733
return fmt.Errorf("not yet implemented")
730734
}
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+
}

internal/controller/etcdcluster_controller_new.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,13 @@ func (r *ClusterReconciler) etcdClusterConfig(ctx context.Context, cluster *etcd
333333

334334
// todo: implement this
335335
func (r *ClusterReconciler) createClusterFromScratch(ctx context.Context, state *observables) error {
336-
panic("not implemented")
336+
if err := r.createOrUpdateClusterStateConfigMap(ctx, state); err != nil {
337+
return err
338+
}
339+
if err := r.createOrUpdateStatefulSet(ctx, state); err != nil {
340+
return err
341+
}
342+
return nil
337343
}
338344

339345
// todo: implement this

internal/controller/state.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package controller
2+
3+
type State interface {
4+
ClusterExists() bool
5+
PendingDeletion() bool
6+
GetEtcdCluster() error
7+
}

0 commit comments

Comments
 (0)