Skip to content

Commit

Permalink
add watch bookmarks - fixes #54
Browse files Browse the repository at this point in the history
also for #219
  • Loading branch information
clux committed Apr 7, 2020
1 parent 232261f commit 8cccbda
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 3 deletions.
4 changes: 2 additions & 2 deletions kube/examples/configmap_informer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ use kube::{
/// Example way to read secrets
#[tokio::main]
async fn main() -> anyhow::Result<()> {
std::env::set_var("RUST_LOG", "info,kube=debug");
std::env::set_var("RUST_LOG", "info,kube=trace");
env_logger::init();
let client = Client::try_default().await?;
let namespace = std::env::var("NAMESPACE").unwrap_or("default".into());

let cms: Api<ConfigMap> = Api::namespaced(client, &namespace);
let lp = ListParams::default().timeout(10); // short watch timeout in this example
let lp = ListParams::default().allow_bookmarks().timeout(10); // short watch timeout in this example
let inf = Informer::new(cms).params(lp);

loop {
Expand Down
6 changes: 6 additions & 0 deletions kube/src/api/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ where
Modified(K),
/// Resource was deleted
Deleted(K),
/// Resource bookmark
///
/// From [Watch bookmarks](https://kubernetes.io/docs/reference/using-api/api-concepts/#watch-bookmarks)
/// NB: This became Beta first in Kubernetes 1.16
Bookmark(K),
/// There was some kind of error
Error(ErrorResponse),
}
Expand All @@ -33,6 +38,7 @@ where
WatchEvent::Added(_) => write!(f, "Added event"),
WatchEvent::Modified(_) => write!(f, "Modified event"),
WatchEvent::Deleted(_) => write!(f, "Deleted event"),
WatchEvent::Bookmark(_) => write!(f, "Bookmark event"),
WatchEvent::Error(e) => write!(f, "Error event: {:?}", e),
}
}
Expand Down
10 changes: 10 additions & 0 deletions kube/src/api/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ pub struct ListParams {
pub include_uninitialized: bool,
pub label_selector: Option<String>,
pub timeout: Option<u32>,
pub allow_bookmarks: bool,
}

impl ListParams {
Expand Down Expand Up @@ -148,6 +149,12 @@ impl ListParams {
self.include_uninitialized = true;
self
}

/// Enables watch bookmarks from the api server if supported
pub fn allow_bookmarks(mut self) -> Self {
self.allow_bookmarks = true;
self
}
}

// TODO: WatchParams (same as ListParams but with extra resource_version + allow_watch_bookmarks)
Expand Down Expand Up @@ -318,6 +325,9 @@ impl Resource {
if let Some(labels) = &lp.label_selector {
qp.append_pair("labelSelector", &labels);
}
if lp.allow_bookmarks {
qp.append_pair("allowWatchBookmarks", "true");
}

let urlstr = qp.finish();
let req = http::Request::get(urlstr);
Expand Down
5 changes: 4 additions & 1 deletion kube/src/runtime/informer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,10 @@ where
async move {
// Check if we need to update our version based on the incoming events
match &event {
Ok(WatchEvent::Added(o)) | Ok(WatchEvent::Modified(o)) | Ok(WatchEvent::Deleted(o)) => {
Ok(WatchEvent::Added(o))
| Ok(WatchEvent::Modified(o))
| Ok(WatchEvent::Deleted(o))
| Ok(WatchEvent::Bookmark(o)) => {
// always store the last seen resourceVersion
if let Some(nv) = Meta::resource_ver(o) {
*version.lock().await = nv.clone();
Expand Down
3 changes: 3 additions & 0 deletions kube/src/runtime/reflector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ where
debug!("Removing {} from {}", Meta::name(&o), kind);
state.remove(&ObjectId::key_for(&o));
}
WatchEvent::Bookmark(o) => {
debug!("Bookmarking {} from {}", Meta::name(&o), kind);
}
WatchEvent::Error(e) => {
warn!("Failed to watch {}: {:?}", kind, e);
return Err(Error::Api(e));
Expand Down

0 comments on commit 8cccbda

Please sign in to comment.