Skip to content

Commit

Permalink
dynamic_watcher example: show how to use dynamic type information at …
Browse files Browse the repository at this point in the history
…loop level (#1258)

dynamic_watcher example: show how to grab type information

common question

Signed-off-by: clux <[email protected]>
  • Loading branch information
clux authored and jmintb committed Jul 22, 2023
1 parent e6eba5d commit b0c6794
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
15 changes: 9 additions & 6 deletions examples/dynamic_watcher.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use futures::{Stream, StreamExt, TryStreamExt};
use kube::{
api::{Api, DynamicObject, GroupVersionKind, Resource, ResourceExt},
api::{Api, ApiResource, DynamicObject, GroupVersionKind, Resource, ResourceExt},
runtime::{metadata_watcher, watcher, watcher::Event, WatchStreamExt},
};
use serde::de::DeserializeOwned;
Expand Down Expand Up @@ -32,21 +32,24 @@ async fn main() -> anyhow::Result<()> {

// Start a metadata or a full resource watch
if watch_metadata {
handle_events(metadata_watcher(api, wc)).await
handle_events(metadata_watcher(api, wc), &ar).await
} else {
handle_events(watcher(api, wc)).await
handle_events(watcher(api, wc), &ar).await
}
}

async fn handle_events<K: Resource + Clone + Debug + Send + DeserializeOwned + 'static>(
async fn handle_events<
K: Resource<DynamicType = ApiResource> + Clone + Debug + Send + DeserializeOwned + 'static,
>(
stream: impl Stream<Item = watcher::Result<Event<K>>> + Send + 'static,
ar: &ApiResource,
) -> anyhow::Result<()> {
let mut items = stream.applied_objects().boxed();
while let Some(p) = items.try_next().await? {
if let Some(ns) = p.namespace() {
info!("saw {} in {ns}", p.name_any());
info!("saw {} {} in {ns}", K::kind(ar), p.name_any());
} else {
info!("saw {}", p.name_any());
info!("saw {} {}", K::kind(ar), p.name_any());
}
trace!("full obj: {p:?}");
}
Expand Down
40 changes: 40 additions & 0 deletions kube-core/src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,8 @@ impl PostParams {
///
/// See [kubernetes patch docs](https://kubernetes.io/docs/tasks/run-application/update-api-object-kubectl-patch/#use-a-json-merge-patch-to-update-a-deployment) for the older patch types.
///
/// Note that applying an invalid patch will **not** always return an error. See an example of such a case below.
///
/// Note that patches have different effects on different fields depending on their merge strategies.
/// These strategies are configurable when deriving your [`CustomResource`](https://docs.rs/kube-derive/*/kube_derive/derive.CustomResource.html#customizing-schemas).
///
Expand Down Expand Up @@ -473,6 +475,44 @@ impl PostParams {
/// };
/// let patch = Patch::Apply(&r);
/// ```
/// # Invalid Patches
///
/// In this example patch contains a `PodSpec` and **not** a complete or partial `Pod`.
/// This patch is invalid as the full structure of a resource is required.
/// The invalid patch will be accepted by the cluster, but no changes will be made.
///
/// Using serve-side style [`Apply`](Patch::Apply) patches mitigates this issue.
///
/// ```no_run
/// use k8s_openapi::api::core::v1::{Pod, PodSpec};
/// use kube::{Api, api::{PatchParams, Patch}};
///
/// # async fn wrapper() -> Result<(), Box<dyn std::error::Error>> {
/// # let client = kube::Client::try_default().await?;
/// let pods: Api<Pod> = Api::namespaced(client, "apps");
/// let pp = PatchParams::default();
///
/// let invalid_patch: PodSpec = serde_json::from_value(serde_json::json!({
/// "activeDeadlineSeconds": 5
/// }))?;
///
/// // This will have no effect on mypod.
/// pods.patch("mypod", &pp, &Patch::Strategic(invalid_patch)).await?;
///
/// let valid_patch: Pod = serde_json::from_value(serde_json::json!({
/// "spec": {
/// "activeDeadlineSeconds": 5
/// }
/// }))?;
///
/// // This will set activeDeadlineSeconds to 5.
/// pods.patch("mypod", &pp, &Patch::Strategic(valid_patch)).await?;
///
/// # Ok(())
/// # }
/// ```
///
///
#[non_exhaustive]
#[derive(Debug, PartialEq, Clone)]
pub enum Patch<T: Serialize> {
Expand Down

0 comments on commit b0c6794

Please sign in to comment.