Skip to content

Commit b0c6794

Browse files
cluxjmintb
authored andcommitted
dynamic_watcher example: show how to use dynamic type information at loop level (#1258)
dynamic_watcher example: show how to grab type information common question Signed-off-by: clux <[email protected]>
1 parent e6eba5d commit b0c6794

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

examples/dynamic_watcher.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use futures::{Stream, StreamExt, TryStreamExt};
22
use kube::{
3-
api::{Api, DynamicObject, GroupVersionKind, Resource, ResourceExt},
3+
api::{Api, ApiResource, DynamicObject, GroupVersionKind, Resource, ResourceExt},
44
runtime::{metadata_watcher, watcher, watcher::Event, WatchStreamExt},
55
};
66
use serde::de::DeserializeOwned;
@@ -32,21 +32,24 @@ async fn main() -> anyhow::Result<()> {
3232

3333
// Start a metadata or a full resource watch
3434
if watch_metadata {
35-
handle_events(metadata_watcher(api, wc)).await
35+
handle_events(metadata_watcher(api, wc), &ar).await
3636
} else {
37-
handle_events(watcher(api, wc)).await
37+
handle_events(watcher(api, wc), &ar).await
3838
}
3939
}
4040

41-
async fn handle_events<K: Resource + Clone + Debug + Send + DeserializeOwned + 'static>(
41+
async fn handle_events<
42+
K: Resource<DynamicType = ApiResource> + Clone + Debug + Send + DeserializeOwned + 'static,
43+
>(
4244
stream: impl Stream<Item = watcher::Result<Event<K>>> + Send + 'static,
45+
ar: &ApiResource,
4346
) -> anyhow::Result<()> {
4447
let mut items = stream.applied_objects().boxed();
4548
while let Some(p) = items.try_next().await? {
4649
if let Some(ns) = p.namespace() {
47-
info!("saw {} in {ns}", p.name_any());
50+
info!("saw {} {} in {ns}", K::kind(ar), p.name_any());
4851
} else {
49-
info!("saw {}", p.name_any());
52+
info!("saw {} {}", K::kind(ar), p.name_any());
5053
}
5154
trace!("full obj: {p:?}");
5255
}

kube-core/src/params.rs

+40
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,8 @@ impl PostParams {
444444
///
445445
/// 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.
446446
///
447+
/// Note that applying an invalid patch will **not** always return an error. See an example of such a case below.
448+
///
447449
/// Note that patches have different effects on different fields depending on their merge strategies.
448450
/// These strategies are configurable when deriving your [`CustomResource`](https://docs.rs/kube-derive/*/kube_derive/derive.CustomResource.html#customizing-schemas).
449451
///
@@ -473,6 +475,44 @@ impl PostParams {
473475
/// };
474476
/// let patch = Patch::Apply(&r);
475477
/// ```
478+
/// # Invalid Patches
479+
///
480+
/// In this example patch contains a `PodSpec` and **not** a complete or partial `Pod`.
481+
/// This patch is invalid as the full structure of a resource is required.
482+
/// The invalid patch will be accepted by the cluster, but no changes will be made.
483+
///
484+
/// Using serve-side style [`Apply`](Patch::Apply) patches mitigates this issue.
485+
///
486+
/// ```no_run
487+
/// use k8s_openapi::api::core::v1::{Pod, PodSpec};
488+
/// use kube::{Api, api::{PatchParams, Patch}};
489+
///
490+
/// # async fn wrapper() -> Result<(), Box<dyn std::error::Error>> {
491+
/// # let client = kube::Client::try_default().await?;
492+
/// let pods: Api<Pod> = Api::namespaced(client, "apps");
493+
/// let pp = PatchParams::default();
494+
///
495+
/// let invalid_patch: PodSpec = serde_json::from_value(serde_json::json!({
496+
/// "activeDeadlineSeconds": 5
497+
/// }))?;
498+
///
499+
/// // This will have no effect on mypod.
500+
/// pods.patch("mypod", &pp, &Patch::Strategic(invalid_patch)).await?;
501+
///
502+
/// let valid_patch: Pod = serde_json::from_value(serde_json::json!({
503+
/// "spec": {
504+
/// "activeDeadlineSeconds": 5
505+
/// }
506+
/// }))?;
507+
///
508+
/// // This will set activeDeadlineSeconds to 5.
509+
/// pods.patch("mypod", &pp, &Patch::Strategic(valid_patch)).await?;
510+
///
511+
/// # Ok(())
512+
/// # }
513+
/// ```
514+
///
515+
///
476516
#[non_exhaustive]
477517
#[derive(Debug, PartialEq, Clone)]
478518
pub enum Patch<T: Serialize> {

0 commit comments

Comments
 (0)