events

package
v0.113.0 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2026 License: Apache-2.0 Imports: 21 Imported by: 43

Documentation

Overview

Package events provides a Recorder and additional helpers to record Kubernetes Events on an external HTTP endpoint.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FakeRecorder added in v0.113.0

type FakeRecorder struct {
	Events        chan eventsv1.Event
	IncludeObject bool
}

FakeRecorder is used as a fake during tests.

It was invented to be used in tests which require more precise control over e.g. assertions of specific event fields like Reason. For which string comparisons on the concentrated event message using record.FakeRecorder is not sufficient.

To empty the Events channel into a slice of the recorded events, use GetEvents(). Not initializing Events will cause the recorder to not record any messages.

func NewFakeRecorder added in v0.113.0

func NewFakeRecorder(bufferSize int, includeObject bool) *FakeRecorder

NewFakeRecorder creates new fake event recorder with an Events channel with the given size. Setting includeObject to true will cause the recorder to include the object reference in the events.

To initialize a recorder which does not record any events, simply use:

recorder := new(FakeRecorder)

func NewNopRecorder added in v0.113.0

func NewNopRecorder() *FakeRecorder

NewNopRecorder creates a new FakeRecorder that doesn't record any events. This is the most lightweight option for tests that don't need event recording functionality. The recorder will implement the Recorder interface but all event calls will be no-ops.

Example:

r := &MyReconciler{
	Client:   k8sClient,
	Recorder: events.NewNopRecorder(),
}

func (*FakeRecorder) AnnotatedEventf added in v0.113.0

func (f *FakeRecorder) AnnotatedEventf(obj runtime.Object, related runtime.Object,
	annotations map[string]string,
	eventType, reason, action,
	message string, args ...interface{})

AnnotatedEventf emits an event with annotations.

func (*FakeRecorder) Eventf added in v0.113.0

func (f *FakeRecorder) Eventf(obj runtime.Object, related runtime.Object, eventType, reason, action, message string, args ...interface{})

Eventf emits an event with the given message.

func (*FakeRecorder) GetEvents added in v0.113.0

func (f *FakeRecorder) GetEvents() (events []eventsv1.Event)

GetEvents empties the Events channel and returns a slice of recorded events. If the Events channel is nil, it returns nil.

type Recorder

type Recorder interface {
	// EventRecorder records events with a formatted message.
	events.EventRecorder

	// AnnotatedEventRecorder records events with annotations and a formatted message.
	events.AnnotatedEventRecorder
}

Recorder posts events to the Kubernetes API and any other event recorder webhook address, like the GitOps Toolkit notification-controller.

Use it by embedding Recorder in reconciler struct:

import (
	...
	"k8s.io/client-go/tools/events"
	...
)

type MyTypeReconciler {
 	client.Client
	// ... etc.
	events.Recorder
}

Use NewRecorder to create a working Recorder.

func NewRecorder

func NewRecorder(log logr.Logger, webhook, reportingController string, opts ...RecorderOption) (Recorder, error)

NewRecorder creates a Recorder with a Kubernetes event recorder and an external event recorder based on the given webhook. The recorder performs automatic retries for connection errors and 500-range response codes from the external recorder.

The scheme and Kubernetes event recorder can be provided either via WithManager (common case) or via WithScheme and WithEventRecorder (for tests or custom setups).

type RecorderOption added in v0.113.0

type RecorderOption func(*recorder)

RecorderOption configures a recorder.

func WithEventRecorder added in v0.113.0

func WithEventRecorder(er events.AnnotatedEventRecorder) RecorderOption

WithEventRecorder configures the recorder with the given Kubernetes event recorder. Use this together with WithScheme for tests or custom setups where a ctrl.Manager is not available.

func WithManager added in v0.113.0

func WithManager(mgr ctrl.Manager) RecorderOption

WithManager configures the recorder with the scheme and event recorder from the given controller-runtime manager. The Kubernetes event recorder reports under the reportingController passed to NewRecorder.

func WithRetryMax added in v0.113.0

func WithRetryMax(n int) RecorderOption

WithRetryMax configures the maximum number of retries for the HTTP client.

func WithScheme added in v0.113.0

func WithScheme(scheme *runtime.Scheme) RecorderOption

WithScheme configures the recorder with the given runtime scheme for resolving object references. Use this together with WithEventRecorder for tests or custom setups where a ctrl.Manager is not available.

Jump to

Keyboard shortcuts

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