builder

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2018 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

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

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Builder

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

Builder builds an Application Controller (e.g. Operator) and returns a manager.Manager to start it.

Example

This example creates a simple application Controller 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.

package main

import (
	"context"
	"fmt"
	"os"

	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/reconcile"
	logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
	"sigs.k8s.io/controller-runtime/pkg/runtime/signals"
)

// NB: don't call SetLogger in init(), or else you'll mess up logging in the main suite.
var log = logf.Log.WithName("builder-examples")

// This example creates a simple application Controller 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() {
	rs, err := builder.SimpleController().
		ForType(&appsv1.ReplicaSet{}). // ReplicaSet is the Application API
		Owns(&corev1.Pod{}).           // ReplicaSet owns Pods created by it
		Build(&ReplicaSetReconciler{}) // Build
	if err != nil {
		log.Error(err, "Unable to build controller")
		os.Exit(1)
	}

	if err := rs.Start(signals.SetupSignalHandler()); err != nil {
		log.Error(err, "Unable to run controller")
		os.Exit(1)
	}
}

// ReplicaSetReconciler is a simple Controller 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(), client.InNamespace(req.Namespace).MatchingLabels(rs.Spec.Template.Labels), pods)
	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
}
Output:

func SimpleController

func SimpleController() *Builder

SimpleController returns a new Builder

func (*Builder) Build

Build builds the Application Controller and returns the Manager used to start it.

func (*Builder) ForType

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

ForType sets the ForType that generates other types

func (*Builder) Owns

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

Owns configures the Application Controller to respond to create / delete / update events for objects it managedObjects - e.g. creates. apiType is an empty instance of an object matching the managed object type.

func (*Builder) WithConfig

func (b *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.

func (*Builder) WithEventFilter

func (b *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) WithManager

func (b *Builder) WithManager(m manager.Manager) *Builder

WithManager sets the Manager to use for registering the Controller. Defaults to a new manager.Manager.

Jump to

Keyboard shortcuts

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