Skip to content

Commit aa70e33

Browse files
WIP: prototyping apply patch wrapper
Signed-off-by: Danil-Grigorev <[email protected]>
1 parent 8e083a9 commit aa70e33

File tree

2 files changed

+41
-19
lines changed

2 files changed

+41
-19
lines changed

kube-client/src/client/client_ext.rs

+38-16
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
use crate::{Client, Error, Result};
22
use k8s_openapi::{
3-
api::{
4-
core::v1::{LocalObjectReference, Namespace as k8sNs, ObjectReference},
5-
},
3+
api::core::v1::{LocalObjectReference, Namespace as k8sNs, ObjectReference},
64
apimachinery::pkg::apis::meta::v1::OwnerReference,
75
};
86
use kube_core::{
97
object::ObjectList,
108
params::{GetParams, ListParams, Patch, PatchParams},
119
request::Request,
12-
ApiResource, ClusterResourceScope, DynamicResourceScope, NamespaceResourceScope, ObjectMeta, Resource,
13-
ResourceExt,
10+
ApiResource, ClusterResourceScope, DynamicResourceScope, NamespaceResourceScope, Resource, ResourceExt,
11+
TypeMeta,
1412
};
1513
use serde::{de::DeserializeOwned, Serialize};
1614
use std::fmt::Debug;
@@ -233,6 +231,18 @@ pub enum NamespaceError {
233231
MissingName,
234232
}
235233

234+
#[derive(Serialize, Clone, Debug)]
235+
/// ApplyObject allows to wrap an object into Patch::Apply compatible structure,
236+
/// with populated TypeMeta.
237+
pub struct ApplyObject<R: Serialize> {
238+
/// Contains the API version and type of the request.
239+
#[serde(flatten)]
240+
pub types: TypeMeta,
241+
/// Contains the object data.
242+
#[serde(flatten)]
243+
pub data: R,
244+
}
245+
236246
/// Generic client extensions for the `unstable-client` feature
237247
///
238248
/// These methods allow users to query across a wide-array of resources without needing
@@ -390,7 +400,7 @@ impl Client {
390400
/// Perform apply patch on the provided `Resource` implementing type `K`
391401
///
392402
/// ```no_run
393-
/// # use k8s_openapi::api::core::v1::;
403+
/// # use k8s_openapi::api::core::v1::Pod;
394404
/// # use k8s_openapi::api::core::v1::Service;
395405
/// # use kube::client::scope::Namespace;
396406
/// # use kube::prelude::*;
@@ -404,7 +414,7 @@ impl Client {
404414
/// # Ok(())
405415
/// # }
406416
/// ```
407-
pub async fn apply<K, P: Serialize + Debug>(&self, resource: &K, pp: &PatchParams) -> Result<K>
417+
pub async fn apply<K>(&self, resource: &K, pp: &PatchParams) -> Result<K>
408418
where
409419
K: ResourceExt + Serialize + DeserializeOwned + Clone + Debug,
410420
<K as Resource>::DynamicType: Default,
@@ -414,10 +424,17 @@ impl Client {
414424
let url = K::url_path(&Default::default(), meta.namespace.as_deref());
415425
let req = Request::new(url);
416426

417-
let mut resource = resource.clone();
418-
resource.meta_mut().managed_fields = None;
419-
let patch = &Patch::Apply(resource);
420-
let req = req.patch(name, pp, patch).map_err(Error::BuildRequest)?;
427+
let apply = ApplyObject::<K> {
428+
types: TypeMeta::resource::<K>(),
429+
data: {
430+
let mut resource = resource.clone();
431+
resource.meta_mut().managed_fields = None;
432+
resource
433+
},
434+
};
435+
let req = req
436+
.patch(name, pp, &Patch::Apply(apply))
437+
.map_err(Error::BuildRequest)?;
421438
self.request::<K>(req).await
422439
}
423440

@@ -438,7 +455,7 @@ impl Client {
438455
/// # Ok(())
439456
/// # }
440457
/// ```
441-
pub async fn apply_status<K, P: Serialize + Debug>(&self, resource: &K, pp: &PatchParams) -> Result<K>
458+
pub async fn apply_status<K>(&self, resource: &K, pp: &PatchParams) -> Result<K>
442459
where
443460
K: ResourceExt + Serialize + DeserializeOwned + Clone + Debug,
444461
<K as Resource>::DynamicType: Default,
@@ -448,11 +465,16 @@ impl Client {
448465
let url = K::url_path(&Default::default(), meta.namespace.as_deref());
449466
let req = Request::new(url);
450467

451-
let mut resource = resource.clone();
452-
resource.meta_mut().managed_fields = None;
453-
let patch = &Patch::Apply(resource);
468+
let apply = ApplyObject::<K> {
469+
types: TypeMeta::resource::<K>(),
470+
data: {
471+
let mut resource = resource.clone();
472+
resource.meta_mut().managed_fields = None;
473+
resource
474+
},
475+
};
454476
let req = req
455-
.patch_subresource("status", name, pp, patch)
477+
.patch_subresource("status", name, pp, &Patch::Apply(apply))
456478
.map_err(Error::BuildRequest)?;
457479
self.request::<K>(req).await
458480
}

kube-core/src/metadata.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ impl TypeMeta {
4545
/// assert_eq!(type_meta.kind, "Pod");
4646
/// assert_eq!(type_meta.api_version, "v1");
4747
/// ```
48-
pub fn resource<K: Resource<DynamicType = ()>>() -> Self {
48+
pub fn resource<K: Resource<DynamicType = impl Default>>() -> Self {
4949
TypeMeta {
50-
api_version: K::api_version(&()).into(),
51-
kind: K::kind(&()).into(),
50+
api_version: K::api_version(&Default::default()).into(),
51+
kind: K::kind(&Default::default()).into(),
5252
}
5353
}
5454
}

0 commit comments

Comments
 (0)