watch

package
v0.30.8 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2024 License: Apache-2.0 Imports: 16 Imported by: 1,185

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrWatchClosed = errors.New("watch closed before UntilWithoutRetry timeout")

ErrWatchClosed is returned when the watch channel is closed before timeout in UntilWithoutRetry.

Functions

func ContextWithOptionalTimeout

func ContextWithOptionalTimeout(parent context.Context, timeout time.Duration) (context.Context, context.CancelFunc)

ContextWithOptionalTimeout wraps context.WithTimeout and handles infinite timeouts expressed as 0 duration.

func NewIndexerInformerWatcher

func NewIndexerInformerWatcher(lw cache.ListerWatcher, objType runtime.Object) (cache.Indexer, cache.Controller, watch.Interface, <-chan struct{})

NewIndexerInformerWatcher will create an IndexerInformer and wrap it into watch.Interface so you can use it anywhere where you'd have used a regular Watcher returned from Watch method. it also returns a channel you can use to wait for the informers to fully shutdown.

func Until

func Until(ctx context.Context, initialResourceVersion string, watcherClient cache.Watcher, conditions ...ConditionFunc) (*watch.Event, error)

Until wraps the watcherClient's watch function with RetryWatcher making sure that watcher gets restarted in case of errors. The initialResourceVersion will be given to watch method when first called. It shall not be "" or "0" given the underlying WATCH call issues (#74022). Remaining behaviour is identical to function UntilWithoutRetry. (See above.) Until can deal with API timeouts and lost connections. It guarantees you to see all events and in the order they happened. Due to this guarantee there is no way it can deal with 'Resource version too old error'. It will fail in this case. (See `UntilWithSync` if you'd prefer to recover from all the errors including RV too old by re-listing those items. In normal code you should care about being level driven so you'd not care about not seeing all the edges.)

The most frequent usage for Until would be a test where you want to verify exact order of events ("edges").

func UntilWithSync

func UntilWithSync(ctx context.Context, lw cache.ListerWatcher, objType runtime.Object, precondition PreconditionFunc, conditions ...ConditionFunc) (*watch.Event, error)

UntilWithSync creates an informer from lw, optionally checks precondition when the store is synced, and watches the output until each provided condition succeeds, in a way that is identical to function UntilWithoutRetry. (See above.) UntilWithSync can deal with all errors like API timeout, lost connections and 'Resource version too old'. It is the only function that can recover from 'Resource version too old', Until and UntilWithoutRetry will just fail in that case. On the other hand it can't provide you with guarantees as strong as using simple Watch method with Until. It can skip some intermediate events in case of watch function failing but it will re-list to recover and you always get an event, if there has been a change, after recovery. Also with the current implementation based on DeltaFIFO, order of the events you receive is guaranteed only for particular object, not between more of them even it's the same resource. The most frequent usage would be a command that needs to watch the "state of the world" and should't fail, like: waiting for object reaching a state, "small" controllers, ...

func UntilWithoutRetry

func UntilWithoutRetry(ctx context.Context, watcher watch.Interface, conditions ...ConditionFunc) (*watch.Event, error)

UntilWithoutRetry reads items from the watch until each provided condition succeeds, and then returns the last watch encountered. The first condition that returns an error terminates the watch (and the event is also returned). If no event has been received, the returned event will be nil. Conditions are satisfied sequentially so as to provide a useful primitive for higher level composition. Waits until context deadline or until context is canceled.

Warning: Unless you have a very specific use case (probably a special Watcher) don't use this function!!! Warning: This will fail e.g. on API timeouts and/or 'too old resource version' error. Warning: You are most probably looking for a function *Until* or *UntilWithSync* below, Warning: solving such issues. TODO: Consider making this function private to prevent misuse when the other occurrences in our codebase are gone.

Types

type ConditionFunc

type ConditionFunc func(event watch.Event) (bool, error)

ConditionFunc returns true if the condition has been reached, false if it has not been reached yet, or an error if the condition cannot be checked and should terminate. In general, it is better to define level driven conditions over edge driven conditions (pod has ready=true, vs pod modified and ready changed from false to true).

type PreconditionFunc

type PreconditionFunc func(store cache.Store) (bool, error)

PreconditionFunc returns true if the condition has been reached, false if it has not been reached yet, or an error if the condition failed or detected an error state.

type RetryWatcher

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

RetryWatcher will make sure that in case the underlying watcher is closed (e.g. due to API timeout or etcd timeout) it will get restarted from the last point without the consumer even knowing about it. RetryWatcher does that by inspecting events and keeping track of resourceVersion. Especially useful when using watch.UntilWithoutRetry where premature termination is causing issues and flakes. Please note that this is not resilient to etcd cache not having the resource version anymore - you would need to use Informers for that.

func NewRetryWatcher

func NewRetryWatcher(initialResourceVersion string, watcherClient cache.Watcher) (*RetryWatcher, error)

NewRetryWatcher creates a new RetryWatcher. It will make sure that watches gets restarted in case of recoverable errors. The initialResourceVersion will be given to watch method when first called.

func (*RetryWatcher) Done

func (rw *RetryWatcher) Done() <-chan struct{}

Done allows the caller to be notified when Retry watcher stops.

func (*RetryWatcher) ResultChan

func (rw *RetryWatcher) ResultChan() <-chan watch.Event

ResultChan implements Interface.

func (*RetryWatcher) Stop

func (rw *RetryWatcher) Stop()

Stop implements Interface.

Jump to

Keyboard shortcuts

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