Skip to content

Commit

Permalink
Add predicates::resource_version (#1221)
Browse files Browse the repository at this point in the history
* 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 <sszynrae@gmail.com>

* clippy

Signed-off-by: clux <sszynrae@gmail.com>

* fix new "unneeded sub cfg" lint that showed up on nightly

Signed-off-by: clux <sszynrae@gmail.com>

---------

Signed-off-by: clux <sszynrae@gmail.com>
  • Loading branch information
clux authored Jun 4, 2023
1 parent ff5b320 commit 89b1502
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 13 deletions.
2 changes: 1 addition & 1 deletion kube-client/src/api/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl Api<ServiceAccount> {
// 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},
Expand Down
8 changes: 4 additions & 4 deletions kube-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn std::error::Error>> {
let config = Config::infer().await?;
let https = config.rustls_https_connector()?;
Expand All @@ -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<dyn std::error::Error>> {
let config = Config::infer().await?;
let https = config.openssl_https_connector()?;
Expand All @@ -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<dyn std::error::Error>> {
use crate::{core::DynamicObject, discovery};
let client = Client::try_default().await?;
Expand Down Expand Up @@ -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<dyn std::error::Error>> {
use crate::api::{DeleteParams, ListParams, Patch, PatchParams, WatchEvent};

Expand Down
5 changes: 5 additions & 0 deletions kube-runtime/src/utils/predicate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<K: Resource>(obj: &K) -> Option<u64> {
obj.meta().resource_version.as_ref().map(hash)
}

/// Hash the labels of a Resource K
pub fn labels<K: Resource>(obj: &K) -> Option<u64> {
Some(hash(obj.labels()))
Expand Down
12 changes: 6 additions & 6 deletions kube-runtime/src/utils/watch_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn std::error::Error>> {
/// # let client: kube::Client = todo!();
/// let pods: Api<Pod> = Api::default_namespaced(client);
/// let changed_pods = watcher(pods, watcher::Config::default())
/// let deploys: Api<Deployment> = 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(())
/// # }
Expand Down
4 changes: 2 additions & 2 deletions kube/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn std::error::Error>> {
use crate::{
core::{DynamicObject, GroupVersion, GroupVersionKind},
Expand Down Expand Up @@ -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<dyn std::error::Error>> {
use crate::{
api::{DeleteParams, PostParams},
Expand Down

0 comments on commit 89b1502

Please sign in to comment.