watch

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2024 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsRetryable

func IsRetryable(err error) (time.Duration, error)

IsRetryable checks if the given error might make sense to be retried in the context of watching Kubernetes resources. Returns the retry delay and no error if it's retryable, or the passed in error otherwise.

Types

type Condition

type Condition[T any] func(item *T) (done bool, err error)

Condition is a func that gets called by Watcher for each updated item. The watch will terminate successfully if it returns true, continue if it returns false or terminate with the returned error.

type ErrorCallback

type ErrorCallback = func(error) (retryDelay time.Duration, err error)

ErrorCallback is a func that, if specified, will be called by the Watcher whenever it encounters some error. Whenever the returned error is nil, the Watcher will wait for the specified duration and retry the last call. Otherwise the Watcher will return the returned error.

type Provider

type Provider[L any] interface {
	List(ctx context.Context, opts metav1.ListOptions) (L, error)
	Watch(ctx context.Context, opts metav1.ListOptions) (watch.Interface, error)
}

Provider represents the backend for Watcher. It is compatible with client-go's typed interfaces.

type VersionedResource

type VersionedResource interface {
	GetResourceVersion() string
}

type Watcher

type Watcher[T any] struct {
	List  func(ctx context.Context, opts metav1.ListOptions) (resourceVersion string, items []T, err error)
	Watch func(ctx context.Context, opts metav1.ListOptions) (watch.Interface, error)
	// contains filtered or unexported fields
}

Watcher offers a convenient way of watching Kubernetes resources. An ephemeral alternative to Reflectors and Indexers, useful for one-shot tasks when no caching is required. It performs an initial list of all the resources and then starts watching them. In case the watch needs to be restarted (a.k.a. resource version too old), Watcher will re-list all the resources. The Watcher will restart the watch API call from time to time at the last seen resource version, so that stale HTTP connections won't make the watch go stale, too.

func DaemonSets

func DaemonSets(client Provider[*appsv1.DaemonSetList]) *Watcher[appsv1.DaemonSet]

func Deployments

func Deployments(client Provider[*appsv1.DeploymentList]) *Watcher[appsv1.Deployment]

func FromClient

func FromClient[L metav1.ListInterface, I any](client Provider[L]) *Watcher[I]

FromClient creates a Watcher from the given client-go client. Note that the types L and I need to be connected in a way that L is a pointer type to a struct that has an `Items` field of type []I. This function will panic if this is not the case. Refer to FromProvider in order to provide a custom way of obtaining items from the list type.

func FromProvider

func FromProvider[L VersionedResource, I any](provider Provider[L], itemsFromList func(L) []I) *Watcher[I]

FromProvider creates a Watcher from the given Provider and the corresponding itemsFromList function.

func Nodes

func Nodes(client Provider[*corev1.NodeList]) *Watcher[corev1.Node]

func (*Watcher[T]) Until

func (w *Watcher[T]) Until(ctx context.Context, condition Condition[T]) error

Until runs a watch until condition returns true. It will error out in case the context gets canceled or the condition returns an error.

func (*Watcher[T]) WithErrorCallback

func (w *Watcher[T]) WithErrorCallback(callback ErrorCallback) *Watcher[T]

WithErrorCallback sets this Watcher's error callback. It's invoked every time an error occurs and determines if the watch should continue or terminate:

  • The returned error is nil: continue watching
  • The returned error is not nil: terminate watch with that error

If the error callback is not set or nil, the default behavior is to terminate.

func (*Watcher[T]) WithFieldSelector

func (w *Watcher[T]) WithFieldSelector(selector fields.Selector) *Watcher[T]

WithFieldSelector sets the given field selector for this Watcher. The default is to match everything:

watcher.FromClient(...).WithFieldSelector(fields.Everything())

Refer to the concept for a general introduction to field selectors. To gain an overview of the supported values, have a look at the usages of k8s.io/apimachinery/pkg/runtime.Scheme.AddFieldLabelConversionFunc in the Kubernetes codebase.

func (*Watcher[T]) WithObjectName

func (w *Watcher[T]) WithObjectName(name string) *Watcher[T]

WithObjectName sets this Watcher's field selector in a way to only match objects with the given name.

Jump to

Keyboard shortcuts

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