controller-runtime: sigs.k8s.io/controller-runtime/pkg/reconcile Index | Examples | Files | Directories

package reconcile

import "sigs.k8s.io/controller-runtime/pkg/reconcile"

Package reconcile defines the Reconciler interface to implement Kubernetes APIs. Reconciler is provided to Controllers at creation time as the API implementation.

Index

Examples

Package Files

doc.go reconcile.go

type Func Uses

type Func func(Request) (Result, error)

Func is a function that implements the reconcile interface.

This example implements a simple no-op reconcile function that prints the object to be Reconciled.

Code:

type Reconciler struct{}

r := reconcile.Func(func(o reconcile.Request) (reconcile.Result, error) {
    // Create your business logic to create, update, delete objects here.
    fmt.Printf("Name: %s, Namespace: %s", o.Name, o.Namespace)
    return reconcile.Result{}, nil
})

res, err := r.Reconcile(reconcile.Request{NamespacedName: types.NamespacedName{Namespace: "default", Name: "test"}})
if err != nil || res.Requeue || res.RequeueAfter != time.Duration(0) {
    fmt.Printf("got requeue request: %v, %v\n", err, res)
}

Output:

Name: test, Namespace: default

func (Func) Reconcile Uses

func (r Func) Reconcile(o Request) (Result, error)

Reconcile implements Reconciler.

type Reconciler Uses

type Reconciler interface {
    // Reconciler 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(Request) (Result, error)
}

Reconciler implements a Kubernetes API for a specific Resource by Creating, Updating or Deleting Kubernetes objects, or by making changes to systems external to the cluster (e.g. cloudproviders, github, etc).

reconcile implementations compare the state specified in an object by a user against the actual cluster state, and then perform operations to make the actual cluster state reflect the state specified by the user.

Typically, reconcile is triggered by a Controller in response to cluster Events (e.g. Creating, Updating, Deleting Kubernetes objects) or external Events (GitHub Webhooks, polling external sources, etc).

Example reconcile Logic:

* Reader an object and all the Pods it owns.
* Observe that the object spec specifies 5 replicas but actual cluster contains only 1 Pod replica.
* Create 4 Pods and set their OwnerReferences to the object.

reconcile may be implemented as either a type:

type reconcile struct {}

func (reconcile) reconcile(controller.Request) (controller.Result, error) {
	// Implement business logic of reading and writing objects here
	return controller.Result{}, nil
}

Or as a function:

controller.Func(func(o controller.Request) (controller.Result, error) {
	// Implement business logic of reading and writing objects here
	return controller.Result{}, nil
})

Reconciliation is level-based, meaning action isn't driven off changes in individual Events, but instead is driven by actual cluster state read from the apiserver or a local cache. For example if responding to a Pod Delete Event, the Request won't contain that a Pod was deleted, instead the reconcile function observes this when reading the cluster state and seeing the Pod as missing.

type Request Uses

type Request struct {
    // NamespacedName is the name and namespace of the object to reconcile.
    types.NamespacedName
}

Request contains the information necessary to reconcile a Kubernetes object. This includes the information to uniquely identify the object - its Name and Namespace. It does NOT contain information about any specific Event or the object contents itself.

type Result Uses

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
}

Result contains the result of a Reconciler invocation.

Directories

PathSynopsis
reconciletest

Package reconcile imports 2 packages (graph) and is imported by 210 packages. Updated 2019-05-14. Refresh now. Tools for package owners.