From 89b1502d96269c6b2836ccb0a6eb73eb79a9ed3e Mon Sep 17 00:00:00 2001 From: Eirik A Date: Sun, 4 Jun 2023 08:20:28 +0100 Subject: [PATCH] Add `predicates::resource_version` (#1221) * Add `predicates::resource_version` This adds a resource_version predicate helper for things that cannot use `predicates::generation` like pods (which do not set the .medata.generation). We also change the default example to use deployments, as these can make use of the `predicates::generation` fn by default. Signed-off-by: clux * clippy Signed-off-by: clux * fix new "unneeded sub cfg" lint that showed up on nightly Signed-off-by: clux --------- Signed-off-by: clux --- kube-client/src/api/util/mod.rs | 2 +- kube-client/src/lib.rs | 8 ++++---- kube-runtime/src/utils/predicate.rs | 5 +++++ kube-runtime/src/utils/watch_ext.rs | 12 ++++++------ kube/src/lib.rs | 4 ++-- 5 files changed, 18 insertions(+), 13 deletions(-) diff --git a/kube-client/src/api/util/mod.rs b/kube-client/src/api/util/mod.rs index 63212739b..06923a0d6 100644 --- a/kube-client/src/api/util/mod.rs +++ b/kube-client/src/api/util/mod.rs @@ -60,7 +60,7 @@ impl Api { // Tests that require a cluster and the complete feature set // Can be run with `cargo test -p kube-client --lib -- --ignored` #[cfg(test)] -#[cfg(all(feature = "client"))] +#[cfg(feature = "client")] mod test { use crate::{ api::{Api, DeleteParams, ListParams, PostParams}, diff --git a/kube-client/src/lib.rs b/kube-client/src/lib.rs index ed593bd04..c92cf83b0 100644 --- a/kube-client/src/lib.rs +++ b/kube-client/src/lib.rs @@ -149,7 +149,7 @@ mod test { #[cfg(feature = "when_rustls_works_with_k3d")] #[tokio::test] #[ignore = "needs cluster (lists pods)"] - #[cfg(all(feature = "rustls-tls"))] + #[cfg(feature = "rustls-tls")] async fn custom_client_rustls_configuration() -> Result<(), Box> { let config = Config::infer().await?; let https = config.rustls_https_connector()?; @@ -164,7 +164,7 @@ mod test { #[tokio::test] #[ignore = "needs cluster (lists pods)"] - #[cfg(all(feature = "openssl-tls"))] + #[cfg(feature = "openssl-tls")] async fn custom_client_openssl_tls_configuration() -> Result<(), Box> { let config = Config::infer().await?; let https = config.openssl_https_connector()?; @@ -179,7 +179,7 @@ mod test { #[tokio::test] #[ignore = "needs cluster (lists api resources)"] - #[cfg(all(feature = "discovery"))] + #[cfg(feature = "discovery")] async fn group_discovery_oneshot() -> Result<(), Box> { use crate::{core::DynamicObject, discovery}; let client = Client::try_default().await?; @@ -273,7 +273,7 @@ mod test { #[tokio::test] #[ignore = "needs cluster (will create and attach to a pod)"] - #[cfg(all(feature = "ws"))] + #[cfg(feature = "ws")] async fn pod_can_exec_and_write_to_stdin() -> Result<(), Box> { use crate::api::{DeleteParams, ListParams, Patch, PatchParams, WatchEvent}; diff --git a/kube-runtime/src/utils/predicate.rs b/kube-runtime/src/utils/predicate.rs index 4ca66abe6..69cdec13d 100644 --- a/kube-runtime/src/utils/predicate.rs +++ b/kube-runtime/src/utils/predicate.rs @@ -99,6 +99,11 @@ pub mod predicates { obj.meta().generation.map(|g| hash(&g)) } + /// Hash the resource version of a Resource K + pub fn resource_version(obj: &K) -> Option { + obj.meta().resource_version.as_ref().map(hash) + } + /// Hash the labels of a Resource K pub fn labels(obj: &K) -> Option { Some(hash(obj.labels())) diff --git a/kube-runtime/src/utils/watch_ext.rs b/kube-runtime/src/utils/watch_ext.rs index 8246c23ed..0d18fd2fa 100644 --- a/kube-runtime/src/utils/watch_ext.rs +++ b/kube-runtime/src/utils/watch_ext.rs @@ -56,17 +56,17 @@ pub trait WatchStreamExt: Stream { /// # use futures::{pin_mut, Stream, StreamExt, TryStreamExt}; /// use kube::{Api, Client, ResourceExt}; /// use kube_runtime::{watcher, WatchStreamExt, predicates}; - /// use k8s_openapi::api::core::v1::Pod; + /// use k8s_openapi::api::apps::v1::Deployment; /// # async fn wrapper() -> Result<(), Box> { /// # let client: kube::Client = todo!(); - /// let pods: Api = Api::default_namespaced(client); - /// let changed_pods = watcher(pods, watcher::Config::default()) + /// let deploys: Api = Api::default_namespaced(client); + /// let changed_deploys = watcher(deploys, watcher::Config::default()) /// .applied_objects() /// .predicate_filter(predicates::generation); - /// pin_mut!(changed_pods); + /// pin_mut!(changed_deploys); /// - /// while let Some(pod) = changed_pods.try_next().await? { - /// println!("saw Pod '{} with hitherto unseen generation", pod.name_any()); + /// while let Some(d) = changed_deploys.try_next().await? { + /// println!("saw Deployment '{} with hitherto unseen generation", d.name_any()); /// } /// # Ok(()) /// # } diff --git a/kube/src/lib.rs b/kube/src/lib.rs index dd97e2a73..630484ca6 100644 --- a/kube/src/lib.rs +++ b/kube/src/lib.rs @@ -356,7 +356,7 @@ mod test { #[tokio::test] #[ignore = "needs cluster (fetches api resources, and lists all)"] - #[cfg(all(feature = "derive"))] + #[cfg(feature = "derive")] async fn derived_resources_discoverable() -> Result<(), Box> { use crate::{ core::{DynamicObject, GroupVersion, GroupVersionKind}, @@ -432,7 +432,7 @@ mod test { #[tokio::test] #[ignore = "needs cluster (will create await a pod)"] - #[cfg(all(feature = "runtime"))] + #[cfg(feature = "runtime")] async fn pod_can_await_conditions() -> Result<(), Box> { use crate::{ api::{DeleteParams, PostParams},