reconciler

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 10, 2022 License: Apache-2.0 Imports: 7 Imported by: 6

README

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

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

// package omitted

import "github.com/angelokurtis/reconciler"
// 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(
		&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 interface functions so you just need to put your logic on Reconcile(context.Context, client.Object) implementation:

// package omitted

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

type DeploymentCreation struct {
	reconciler.Funcs
	// other fields
}

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Funcs

type Funcs struct {
	Result
	// contains filtered or unexported fields
}

func (*Funcs) Next

func (f *Funcs) Next(ctx context.Context, obj client.Object) (ctrl.Result, error)

type Handler

type Handler interface {
	Reconcile(ctx context.Context, obj client.Object) (ctrl.Result, error)
	Next(ctx context.Context, obj client.Object) (ctrl.Result, error)
	// contains filtered or unexported methods
}

func Chain

func Chain(handlers ...Handler) Handler

type Result

type Result struct{}

func (*Result) Finish

func (r *Result) Finish(ctx context.Context) (ctrl.Result, error)

func (*Result) Requeue

func (r *Result) Requeue(ctx context.Context) (ctrl.Result, error)

func (*Result) RequeueAfter

func (r *Result) RequeueAfter(ctx context.Context, duration time.Duration) (ctrl.Result, error)

func (*Result) RequeueOnErr

func (r *Result) RequeueOnErr(ctx context.Context, err error) (ctrl.Result, error)

func (*Result) RequeueOnErrAfter

func (r *Result) RequeueOnErrAfter(ctx context.Context, err error, duration time.Duration) (ctrl.Result, error)

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL