[−][src]Function kube_runtime::watcher::watcher
pub fn watcher<K: Meta + Clone + DeserializeOwned + Send + 'static>(
api: Api<K>,
list_params: ListParams
) -> impl Stream<Item = Result<Event<K>>> + Send
Watches a Kubernetes Resource for changes continuously
Creates an indefinite read stream through continual Api::watch
calls, and keeping track
of returned resource versions.
It tries to recover (by reconnecting and resyncing as required) if polled again after an error.
However, keep in mind that most terminal TryStream
combinators (such as TryFutureExt::try_for_each
and TryFutureExt::try_concat
will terminate eagerly if an Error
reaches them.
This is intended to provide a safe and atomic input interface for a state store like a reflector
,
direct users may want to flatten composite events with try_flatten_applied
:
use kube::{api::{Api, ListParams, Meta}, Client}; use kube_runtime::{utils::try_flatten_applied, watcher}; use k8s_openapi::api::core::v1::Pod; use futures::{StreamExt, TryStreamExt}; #[tokio::main] async fn main() -> Result<(), kube_runtime::watcher::Error> { let client = Client::try_default().await.unwrap(); let pods: Api<Pod> = Api::namespaced(client, "apps"); let watcher = watcher(pods, ListParams::default()); try_flatten_applied(watcher) .try_for_each(|p| async move { println!("Applied: {}", Meta::name(&p)); Ok(()) }) .await?; Ok(()) }
Migration from kube::runtime
This is similar to the legacy kube::runtime::Informer
, or the watching half of client-go's Reflector
.
Renamed to avoid confusion with client-go's Informer
(which watches a Reflector
for updates, rather
the Kubernetes API).