Skip to content

angelokurtis/reconciler

Repository files navigation

reconciler

Go Reference Build Status Coverage

A library to avoid overstuffed Reconcile functions of Kubernetes operators as suggested in this blog post

Features

  • Avoid overstuffed functions through handlers in a chain of responsibility
  • Wrap the reconciliation results to improve the readability of your code

Getting Started

Install by running:

go get github.com/angelokurtis/reconciler/v2

Split the Reconcile responsibilities into handlers with one single purpose and chain them in your controller:

// package omitted

import "github.com/angelokurtis/reconciler/v2"
// other imports

func (r *MemcachedReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
	// Fetch the Memcached instance
	m := &cachev1alpha1.Memcached{}
	err := r.Get(ctx, req.NamespacedName, m)
	if err != nil {
		if errors.IsNotFound(err) {
			return r.Finish(ctx) // Ignoring since object must be deleted
		}
		return r.RequeueOnErr(ctx, err) // Failed to get Memcached
	}

	// Chains reconcile handlers
	return reconciler.Chain[*cachev1alpha1.Memcached](
		&memcached.DeploymentCreation{Client: r.Client, Scheme: r.Scheme},
		&memcached.Status{Client: r.Client},
	).Reconcile(ctx, m)
}

Your handler can compose reconciler.Funcs that already implement most of the methods required by the Handler interface, so you just need to put your logic in the Reconcile(context.Context, T) implementation:

// package omitted

import "github.com/angelokurtis/reconciler/v2"
// other imports

type DeploymentCreation struct {
    reconciler.Funcs[*cachev1alpha1.Memcached]
	// other fields
}

func (d *DeploymentCreation) Reconcile(ctx context.Context, obj *cachev1alpha1.Memcached) (ctrl.Result, error) {
	// TODO(user): your logic here
	return d.Next(ctx, obj)
}

About

A library to avoid overstuffed Reconcile functions of Kubernetes operators

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors