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

package builder

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

Package builder provides wraps other controller-runtime libraries and exposes simple patterns for building common Controllers.

Projects built with the builder package can trivially be rebased on top of the underlying packages if the project requires more customized behavior in the future.

Index

Examples

Package Files

controller.go doc.go webhook.go

type Builder Uses

type Builder struct {
    // contains filtered or unexported fields
}

Builder builds a Controller.

This example creates a simple application ControllerManagedBy that is configured for ReplicaSets and Pods.

* Create a new application for ReplicaSets that manages Pods owned by the ReplicaSet and calls into ReplicaSetReconciler.

* Start the application.

Code:

package main

import (
    "context"
    "fmt"
    "os"

    logf "sigs.k8s.io/controller-runtime/pkg/log"

    appsv1 "k8s.io/api/apps/v1"
    corev1 "k8s.io/api/core/v1"
    "sigs.k8s.io/controller-runtime/pkg/builder"
    "sigs.k8s.io/controller-runtime/pkg/client"
    "sigs.k8s.io/controller-runtime/pkg/client/config"
    "sigs.k8s.io/controller-runtime/pkg/manager"
    "sigs.k8s.io/controller-runtime/pkg/manager/signals"
    "sigs.k8s.io/controller-runtime/pkg/reconcile"
)

// This example creates a simple application ControllerManagedBy that is configured for ReplicaSets and Pods.
//
// * Create a new application for ReplicaSets that manages Pods owned by the ReplicaSet and calls into
// ReplicaSetReconciler.
//
// * Start the application.
func main() {
    var log = logf.Log.WithName("builder-examples")

    mgr, err := manager.New(config.GetConfigOrDie(), manager.Options{})
    if err != nil {
        log.Error(err, "could not create manager")
        os.Exit(1)
    }

    err = builder.
        ControllerManagedBy(mgr).  // Create the ControllerManagedBy
        For(&appsv1.ReplicaSet{}). // ReplicaSet is the Application API
        Owns(&corev1.Pod{}).       // ReplicaSet owns Pods created by it
        Complete(&ReplicaSetReconciler{})
    if err != nil {
        log.Error(err, "could not create controller")
        os.Exit(1)
    }

    if err := mgr.Start(signals.SetupSignalHandler()); err != nil {
        log.Error(err, "could not start manager")
        os.Exit(1)
    }
}

// ReplicaSetReconciler is a simple ControllerManagedBy example implementation.
type ReplicaSetReconciler struct {
    client.Client
}

// Implement the business logic:
// This function will be called when there is a change to a ReplicaSet or a Pod with an OwnerReference
// to a ReplicaSet.
//
// * Read the ReplicaSet
// * Read the Pods
// * Set a Label on the ReplicaSet with the Pod count
func (a *ReplicaSetReconciler) Reconcile(req reconcile.Request) (reconcile.Result, error) {
    // Read the ReplicaSet
    rs := &appsv1.ReplicaSet{}
    err := a.Get(context.TODO(), req.NamespacedName, rs)
    if err != nil {
        return reconcile.Result{}, err
    }

    // List the Pods matching the PodTemplate Labels
    pods := &corev1.PodList{}
    err = a.List(context.TODO(), pods, client.InNamespace(req.Namespace),
        client.MatchingLabels(rs.Spec.Template.Labels))
    if err != nil {
        return reconcile.Result{}, err
    }

    // Update the ReplicaSet
    rs.Labels["pod-count"] = fmt.Sprintf("%v", len(pods.Items))
    err = a.Update(context.TODO(), rs)
    if err != nil {
        return reconcile.Result{}, err
    }

    return reconcile.Result{}, nil
}

func (a *ReplicaSetReconciler) InjectClient(c client.Client) error {
    a.Client = c
    return nil
}

func ControllerManagedBy Uses

func ControllerManagedBy(m manager.Manager) *Builder

ControllerManagedBy returns a new controller builder that will be started by the provided Manager

func (*Builder) Build Uses

func (blder *Builder) Build(r reconcile.Reconciler) (controller.Controller, error)

Build builds the Application ControllerManagedBy and returns the Controller it created.

func (*Builder) Complete Uses

func (blder *Builder) Complete(r reconcile.Reconciler) error

Complete builds the Application ControllerManagedBy.

func (*Builder) For Uses

func (blder *Builder) For(apiType runtime.Object) *Builder

For defines the type of Object being *reconciled*, and configures the ControllerManagedBy to respond to create / delete / update events by *reconciling the object*. This is the equivalent of calling Watches(&source.Kind{Type: apiType}, &handler.EnqueueRequestForObject{})

func (*Builder) ForType Uses

func (blder *Builder) ForType(apiType runtime.Object) *Builder

ForType defines the type of Object being *reconciled*, and configures the ControllerManagedBy to respond to create / delete / update events by *reconciling the object*. This is the equivalent of calling Watches(&source.Kind{Type: apiType}, &handler.EnqueueRequestForObject{})

Deprecated: Use For

func (*Builder) Named Uses

func (blder *Builder) Named(name string) *Builder

Named sets the name of the controller to the given name. The name shows up in metrics, among other things, and thus should be a prometheus compatible name (underscores and alphanumeric characters only).

By default, controllers are named using the lowercase version of their kind.

func (*Builder) Owns Uses

func (blder *Builder) Owns(apiType runtime.Object) *Builder

Owns defines types of Objects being *generated* by the ControllerManagedBy, and configures the ControllerManagedBy to respond to create / delete / update events by *reconciling the owner object*. This is the equivalent of calling Watches(&handler.EnqueueRequestForOwner{&source.Kind{Type: <ForType-apiType>}, &handler.EnqueueRequestForOwner{OwnerType: apiType, IsController: true})

func (*Builder) Watches Uses

func (blder *Builder) Watches(src source.Source, eventhandler handler.EventHandler) *Builder

Watches exposes the lower-level ControllerManagedBy Watches functions through the builder. Consider using Owns or For instead of Watches directly.

func (*Builder) WithConfig Uses

func (blder *Builder) WithConfig(config *rest.Config) *Builder

WithConfig sets the Config to use for configuring clients. Defaults to the in-cluster config or to ~/.kube/config.

Deprecated: Use ControllerManagedBy(Manager) and this isn't needed.

func (*Builder) WithEventFilter Uses

func (blder *Builder) WithEventFilter(p predicate.Predicate) *Builder

WithEventFilter sets the event filters, to filter which create/update/delete/generic events eventually trigger reconciliations. For example, filtering on whether the resource version has changed. Defaults to the empty list.

func (*Builder) WithOptions Uses

func (blder *Builder) WithOptions(options controller.Options) *Builder

WithOptions overrides the controller options use in doController. Defaults to empty.

type WebhookBuilder Uses

type WebhookBuilder struct {
    // contains filtered or unexported fields
}

WebhookBuilder builds a Webhook.

This example use webhook builder to create a simple webhook that is managed by a manager for CRD ChaosPod. And then start the manager.

Code:

package main

import (
    "os"

    "sigs.k8s.io/controller-runtime/pkg/builder"
    "sigs.k8s.io/controller-runtime/pkg/client/config"
    logf "sigs.k8s.io/controller-runtime/pkg/log"
    "sigs.k8s.io/controller-runtime/pkg/manager"
    "sigs.k8s.io/controller-runtime/pkg/manager/signals"
    "sigs.k8s.io/controller-runtime/pkg/webhook/admission"

    examplegroup "sigs.k8s.io/controller-runtime/examples/crd/pkg"
)

// examplegroup.ChaosPod has implemented both admission.Defaulter and
// admission.Validator interfaces.
var _ admission.Defaulter = &examplegroup.ChaosPod{}
var _ admission.Validator = &examplegroup.ChaosPod{}

// This example use webhook builder to create a simple webhook that is managed
// by a manager for CRD ChaosPod. And then start the manager.
func main() {
    var log = logf.Log.WithName("webhookbuilder-example")

    mgr, err := manager.New(config.GetConfigOrDie(), manager.Options{})
    if err != nil {
        log.Error(err, "could not create manager")
        os.Exit(1)
    }

    err = builder.
        WebhookManagedBy(mgr).         // Create the WebhookManagedBy
        For(&examplegroup.ChaosPod{}). // ChaosPod is a CRD.
        Complete()
    if err != nil {
        log.Error(err, "could not create webhook")
        os.Exit(1)
    }

    if err := mgr.Start(signals.SetupSignalHandler()); err != nil {
        log.Error(err, "could not start manager")
        os.Exit(1)
    }
}

func WebhookManagedBy Uses

func WebhookManagedBy(m manager.Manager) *WebhookBuilder

func (*WebhookBuilder) Complete Uses

func (blder *WebhookBuilder) Complete() error

Complete builds the webhook.

func (*WebhookBuilder) For Uses

func (blder *WebhookBuilder) For(apiType runtime.Object) *WebhookBuilder

For takes a runtime.Object which should be a CR. If the given object implements the admission.Defaulter interface, a MutatingWebhook will be wired for this type. If the given object implements the admission.Validator interface, a ValidatingWebhook will be wired for this type.

Package builder imports 17 packages (graph) and is imported by 5 packages. Updated 2019-09-18. Refresh now. Tools for package owners.