Stream API
The client provides a mechanism to interact with streaming API endpoints, such as the exec
and attach
endpoints.
https://github.com/kubernetes-client/python/
This is achieved through the kubernetes.watch.Watch
class.
- The
stream
method of thekubernetes.watch.Watch
class provides a mechanism to interact with streaming API endpoints. - The method uses the
watch
argument to enable watch mode. - The method iterates through each event in the stream and yields an object with the following keys:
type
: The type of event such as “ADDED”, “DELETED”, etc.raw_object
: A dict representing the watched object.object
: A model representation of the raw object.object
is determined based on the function’s doc string. If it cannot be determined, the value will be the same asraw_object
.
- The
stream
method can be used to watch for changes to an API resource. - The
stream
method handles automatically resuming from the last result. - If the last result is too old, an
ApiException
will be thrown with code 410.
Example:
from kubernetes import client, watch
v1 = client.CoreV1Api()
watch = watch.Watch()
for e in watch.stream(v1.list_namespace, resource_version=1127):
type = e['type']
object = e['object'] # object is one of type return_type
raw_object = e['raw_object'] # raw_object is a dict
...
if should_stop:
watch.stop()
Streaming API Endpoints:
Many API endpoints support the watch feature. The following endpoints provide watch functionality:
exec
attach
logs
Documentation:
- kubernetes/docs/EventsV1Api.md
- kubernetes/docs/FlowcontrolApiserverV1Api.md
- kubernetes/docs/FlowcontrolApiserverV1beta3Api.md
- kubernetes/docs/ApiregistrationV1Api.md
- kubernetes/docs/CoreV1Api.md
- kubernetes/docs/EventsV1Api.md
- kubernetes/docs/ResourceV1alpha3Api.md
- kubernetes/swagger.json.unprocessed
- kubernetes/docs/FlowcontrolApiserverV1Api.md
- kubernetes/swagger.json.unprocessed
- kubernetes/swagger.json.unprocessed
- kubernetes/docs/ApirextensionsV1Api.md
Code Snippets:
- kubernetes/base/watch/watch.py
- kubernetes/base/stream/ws_client.py
- kubernetes/base/stream/stream.py
- kubernetes/client/api/flowcontrol_apiserver_v1beta3_api.py
Note:
The watch
feature is deprecated and it is recommended to use the watch
parameter with a list operation instead, filtered to a single item with the fieldSelector
parameter.
Top-Level Directory Explanations
doc/ - This directory contains documentation files for the project.
doc/source/ - This directory contains the source files for the documentation.
examples/ - This directory contains example usage of the Kubernetes client library.
examples/dynamic-client/ - This directory contains examples of using the dynamic client to interact with Kubernetes.
examples/watch/ - This directory contains examples of using the watch function to monitor Kubernetes resources.
kubernetes/ - This directory contains the main Kubernetes client library.
kubernetes/base/ - This directory contains the base Kubernetes client library.
kubernetes/base/dynamic/ - This directory contains the dynamic client implementation for the base library.
kubernetes/base/stream/ - This directory contains the stream implementation for the base library.
kubernetes/base/watch/ - This directory contains the watch implementation for the base library.
kubernetes/client/ - This directory contains the top-level client for the Kubernetes client library.
kubernetes/client/api/ - This directory contains the API definitions for the client library.
kubernetes/client/models/ - This directory contains the data models used by the client library.
scripts/ - This directory contains scripts used in the development and build process.