kubernetes.informer package

Submodules

Module contents

class kubernetes.informer.ObjectCache(key_func=None)

Bases: object

Thread-safe in-memory mapping of Kubernetes objects.

The SharedInformer keeps this store synchronised with the API server. Consumers can call list() and get_by_key() from any thread safely.

get(obj)

Look up the cached copy of obj. Returns None when absent.

get_by_key(key)

Look up an object by key. Returns None when absent.

list()

Return a snapshot list of all cached objects.

list_keys()

Return a snapshot list of all cache keys.

class kubernetes.informer.SharedInformer(list_func, namespace=None, resync_period=0, label_selector=None, field_selector=None, key_func=None)

Bases: object

Watch a Kubernetes resource and maintain a local cache.

The informer starts a daemon thread that continuously watches the given resource via list_func. On each event the local ObjectCache is updated and registered event-handler callbacks are invoked.

Parameters

list_func:

Bound API method used for the initial list and as the watch source. It must accept a watch keyword argument (e.g. CoreV1Api().list_namespaced_pod).

namespace:

Kubernetes namespace to watch. Pass None for cluster-scoped or all-namespace list functions.

resync_period:

How often (seconds) to perform a full re-list from the API server. Defaults to 0 which disables periodic resyncs.

label_selector:

Optional label selector string forwarded to the API server.

field_selector:

Optional field selector string forwarded to the API server.

key_func:

Optional callable (obj) -> str used to key objects in the cache. Defaults to namespace/name.

add_event_handler(event_type, handler)

Register a callback for a specific event type.

Parameters

event_type:

One of ADDED, MODIFIED, DELETED, BOOKMARK or ERROR.

handler:

Callable invoked with the event object (or the raw exception for ERROR events).

property cache

The ObjectCache maintained by this informer.

remove_event_handler(event_type, handler)

Deregister a previously registered handler.

No-op if handler is not registered.

start()

Start the background watch loop in a daemon thread.

Calling start() more than once without an intervening stop() is a no-op.

stop()

Ask the background watch loop to stop and join the thread.