controller-runtimesigs.k8s.io/controller-runtime/pkg/source Index | Examples | Files

package source

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

Package source provides event streams to hook up to Controllers with Controller.Watch. Events are used with handler.EventHandlers to enqueue reconcile.Requests and trigger Reconciles for Kubernetes objects.

Index

Examples

Types

type ChannelOpt

type ChannelOpt[object any, request comparable] func(*channel[object, request])

ChannelOpt allows to configure a source.Channel.

func WithBufferSize

func WithBufferSize[object any, request comparable](bufferSize int) ChannelOpt[object, request]

WithBufferSize configures the buffer size for a source.Channel. By default, the buffer size is 1024.

func WithPredicates

func WithPredicates[object any, request comparable](p ...predicate.TypedPredicate[object]) ChannelOpt[object, request]

WithPredicates adds the configured predicates to a source.Channel.

type Func

type Func = TypedFunc[reconcile.Request]

Func is a function that implements Source.

type Informer

type Informer struct {
	// Informer is the controller-runtime Informer
	Informer   cache.Informer
	Handler    handler.EventHandler
	Predicates []predicate.Predicate
}

Informer is used to provide a source of events originating inside the cluster from Watches (e.g. Pod Create).

func (*Informer) Start

Start is internal and should be called only by the Controller to register an EventHandler with the Informer to enqueue reconcile.Requests.

func (*Informer) String

func (is *Informer) String() string

type Source

type Source = TypedSource[reconcile.Request]

Source is a source of events (e.g. Create, Update, Delete operations on Kubernetes Objects, Webhook callbacks, etc) which should be processed by event.EventHandlers to enqueue reconcile.Requests.

* Use Kind for events originating in the cluster (e.g. Pod Create, Pod Update, Deployment Update).

* Use Channel for events originating outside the cluster (e.g. GitHub Webhook callback, Polling external urls).

Users may build their own Source implementations.

func Channel

func Channel[object any](
	source <-chan event.TypedGenericEvent[object],
	handler handler.TypedEventHandler[object, reconcile.Request],
	opts ...ChannelOpt[object, reconcile.Request],
) Source

Channel is used to provide a source of events originating outside the cluster (e.g. GitHub Webhook callback). Channel requires the user to wire the external source (e.g. http handler) to write GenericEvents to the underlying channel.

Example

This example reads GenericEvents from a channel and enqueues a reconcile.Request containing the Name and Namespace provided by the event.

Code:play 

package main

import (
	"sigs.k8s.io/controller-runtime/pkg/controller"
	"sigs.k8s.io/controller-runtime/pkg/event"
	"sigs.k8s.io/controller-runtime/pkg/handler"
	"sigs.k8s.io/controller-runtime/pkg/source"
)

var ctrl controller.Controller

func main() {
	events := make(chan event.GenericEvent)

	err := ctrl.Watch(
		source.Channel(
			events,
			&handler.EnqueueRequestForObject{},
		),
	)
	if err != nil {
		// handle it
	}
}

type SyncingSource

type SyncingSource = TypedSyncingSource[reconcile.Request]

SyncingSource is a source that needs syncing prior to being usable. The controller will call its WaitForSync prior to starting workers.

func Kind

func Kind[object client.Object](
	cache cache.Cache,
	obj object,
	handler handler.TypedEventHandler[object, reconcile.Request],
	predicates ...predicate.TypedPredicate[object],
) SyncingSource

Kind creates a KindSource with the given cache provider.

Example

This example Watches for Pod Events (e.g. Create / Update / Delete) and enqueues a reconcile.Request with the Name and Namespace of the Pod.

Code:play 

package main

import (
	corev1 "k8s.io/api/core/v1"
	"sigs.k8s.io/controller-runtime/pkg/controller"
	"sigs.k8s.io/controller-runtime/pkg/handler"
	"sigs.k8s.io/controller-runtime/pkg/manager"
	"sigs.k8s.io/controller-runtime/pkg/source"
)

var mgr manager.Manager
var ctrl controller.Controller

func main() {
	err := ctrl.Watch(source.Kind(mgr.GetCache(), &corev1.Pod{}, &handler.TypedEnqueueRequestForObject[*corev1.Pod]{}))
	if err != nil {
		// handle it
	}
}

type TypedFunc

type TypedFunc[request comparable] func(context.Context, workqueue.TypedRateLimitingInterface[request]) error

TypedFunc is a function that implements Source.

func (TypedFunc[request]) Start

func (f TypedFunc[request]) Start(ctx context.Context, queue workqueue.TypedRateLimitingInterface[request]) error

Start implements Source.

func (TypedFunc[request]) String

func (f TypedFunc[request]) String() string

type TypedSource

type TypedSource[request comparable] interface {
	// Start is internal and should be called only by the Controller to start the source.
	// Start must be non-blocking.
	Start(context.Context, workqueue.TypedRateLimitingInterface[request]) error
}

TypedSource is a generic source of events (e.g. Create, Update, Delete operations on Kubernetes Objects, Webhook callbacks, etc) which should be processed by event.EventHandlers to enqueue a request.

* Use Kind for events originating in the cluster (e.g. Pod Create, Pod Update, Deployment Update).

* Use Channel for events originating outside the cluster (e.g. GitHub Webhook callback, Polling external urls).

Users may build their own Source implementations.

func TypedChannel

func TypedChannel[object any, request comparable](
	source <-chan event.TypedGenericEvent[object],
	handler handler.TypedEventHandler[object, request],
	opts ...ChannelOpt[object, request],
) TypedSource[request]

TypedChannel is used to provide a source of events originating outside the cluster (e.g. GitHub Webhook callback). Channel requires the user to wire the external source (e.g. http handler) to write GenericEvents to the underlying channel.

type TypedSyncingSource

type TypedSyncingSource[request comparable] interface {
	TypedSource[request]
	WaitForSync(ctx context.Context) error
}

TypedSyncingSource is a source that needs syncing prior to being usable. The controller will call its WaitForSync prior to starting workers.

func TypedKind

func TypedKind[object client.Object, request comparable](
	cache cache.Cache,
	obj object,
	handler handler.TypedEventHandler[object, request],
	predicates ...predicate.TypedPredicate[object],
) TypedSyncingSource[request]

TypedKind creates a KindSource with the given cache provider.

Source Files

doc.go source.go

Version
v0.21.0 (latest)
Published
May 20, 2025
Platform
linux/amd64
Imports
15 packages
Last checked
25 minutes ago

Tools for package owners.