Controller logic is implemented in terms of Reconcilers (pkg/reconcile). A Reconciler implements a function which takes a reconcile Request containing the name and namespace of the object to reconcile, reconciles the object, and returns a Response or an error indicating whether to requeue for a second round of processing.
As you can see in the diagram above, a Reconciler is part of a Controller. A Controller has a function to watch changes of the target resources, put change events into the queue, and call Reconcile function with an queue item, and requeue the item if necessary.
type Reconciler interface {
// Reconcile performs a full reconciliation for the object referred to by the Request.
// The Controller will requeue the Request to be processed again if an error is non-nil or
// Result.Requeue is true, otherwise upon completion it will remove the work from the queue.
Reconcile(context.Context, Request) (Result, error)
}
type Request struct {
// NamespacedName is the name and namespace of the object to reconcile.
types.NamespacedName
}
type Result struct {
// Requeue tells the Controller to requeue the reconcile key. Defaults to false.
Requeue bool
// RequeueAfter if greater than 0, tells the Controller to requeue the reconcile key after the Duration.
// Implies that Requeue is true, there is no need to set Requeue to true at the same time as RequeueAfter.
RequeueAfter time.Duration
}
You can use either implementation of the Reconciler
interface:
- a reconciler struct with
Reconcile
function. - a
reconcile.Func
, which implements Reconciler interface:type Func func(context.Context, Request) (Result, error)
(Controller also implements Reconciler interface. The reconciler passed to builder
is used inside the controller's Reconcile
function.)
Reconciler is passed to Controller builder when initializing controller (you can also check it in Manager):
ctrl.NewControllerManagedBy(mgr). // returns controller Builder
For(&corev1.Pod{}). // defines the type of Object being reconciled
Complete(podReconciler) // Complete builds the Application controller, and return error