Skip to content

Commit

Permalink
WIP: prototyping apply patch wrapper
Browse files Browse the repository at this point in the history
Signed-off-by: Danil-Grigorev <danil.grigorev@suse.com>
  • Loading branch information
Danil-Grigorev committed Oct 6, 2024
1 parent 8e083a9 commit 0928b11
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 16 deletions.
48 changes: 35 additions & 13 deletions kube-client/src/client/client_ext.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
use crate::{Client, Error, Result};
use k8s_openapi::{
api::{
core::v1::{LocalObjectReference, Namespace as k8sNs, ObjectReference},
},
api::core::v1::{LocalObjectReference, Namespace as k8sNs, ObjectReference},
apimachinery::pkg::apis::meta::v1::OwnerReference,
};
use kube_core::{
object::ObjectList,
params::{GetParams, ListParams, Patch, PatchParams},
request::Request,
ApiResource, ClusterResourceScope, DynamicResourceScope, NamespaceResourceScope, ObjectMeta, Resource,
ResourceExt,
ApiResource, ClusterResourceScope, DynamicResourceScope, NamespaceResourceScope, Resource, ResourceExt,
TypeMeta,
};
use serde::{de::DeserializeOwned, Serialize};
use std::fmt::Debug;
Expand Down Expand Up @@ -233,6 +231,18 @@ pub enum NamespaceError {
MissingName,
}

#[derive(Serialize, Clone, Debug)]
/// ApplyObject allows to wrap an object into Patch::Apply compatible structure,
/// with populated TypeMeta.
pub struct ApplyObject<R: Serialize> {
/// Contains the API version and type of the request.
#[serde(flatten)]
pub types: TypeMeta,
/// Contains the object data.
#[serde(flatten)]
pub data: R,
}

/// Generic client extensions for the `unstable-client` feature
///
/// These methods allow users to query across a wide-array of resources without needing
Expand Down Expand Up @@ -414,10 +424,17 @@ impl Client {
let url = K::url_path(&Default::default(), meta.namespace.as_deref());
let req = Request::new(url);

Check warning on line 425 in kube-client/src/client/client_ext.rs

View check run for this annotation

Codecov / codecov/patch

kube-client/src/client/client_ext.rs#L422-L425

Added lines #L422 - L425 were not covered by tests

let mut resource = resource.clone();
resource.meta_mut().managed_fields = None;
let patch = &Patch::Apply(resource);
let req = req.patch(name, pp, patch).map_err(Error::BuildRequest)?;
let apply = ApplyObject::<K> {
types: TypeMeta::resource::<K>(),
data: {

Check warning on line 429 in kube-client/src/client/client_ext.rs

View check run for this annotation

Codecov / codecov/patch

kube-client/src/client/client_ext.rs#L428-L429

Added lines #L428 - L429 were not covered by tests
let mut resource = resource.clone();
resource.meta_mut().managed_fields = None;
resource
},
};
let req = req
.patch(name, pp, &Patch::Apply(apply))
.map_err(Error::BuildRequest)?;
self.request::<K>(req).await

Check warning on line 438 in kube-client/src/client/client_ext.rs

View check run for this annotation

Codecov / codecov/patch

kube-client/src/client/client_ext.rs#L435-L438

Added lines #L435 - L438 were not covered by tests
}

Expand Down Expand Up @@ -448,11 +465,16 @@ impl Client {
let url = K::url_path(&Default::default(), meta.namespace.as_deref());
let req = Request::new(url);

Check warning on line 466 in kube-client/src/client/client_ext.rs

View check run for this annotation

Codecov / codecov/patch

kube-client/src/client/client_ext.rs#L463-L466

Added lines #L463 - L466 were not covered by tests

let mut resource = resource.clone();
resource.meta_mut().managed_fields = None;
let patch = &Patch::Apply(resource);
let apply = ApplyObject::<K> {
types: TypeMeta::resource::<K>(),
data: {

Check warning on line 470 in kube-client/src/client/client_ext.rs

View check run for this annotation

Codecov / codecov/patch

kube-client/src/client/client_ext.rs#L469-L470

Added lines #L469 - L470 were not covered by tests
let mut resource = resource.clone();
resource.meta_mut().managed_fields = None;
resource
},
};
let req = req
.patch_subresource("status", name, pp, patch)
.patch_subresource("status", name, pp, &Patch::Apply(apply))
.map_err(Error::BuildRequest)?;
self.request::<K>(req).await

Check warning on line 479 in kube-client/src/client/client_ext.rs

View check run for this annotation

Codecov / codecov/patch

kube-client/src/client/client_ext.rs#L476-L479

Added lines #L476 - L479 were not covered by tests
}
Expand Down
6 changes: 3 additions & 3 deletions kube-core/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ impl TypeMeta {
/// assert_eq!(type_meta.kind, "Pod");
/// assert_eq!(type_meta.api_version, "v1");
/// ```
pub fn resource<K: Resource<DynamicType = ()>>() -> Self {
pub fn resource<K: Resource<DynamicType = impl Default>>() -> Self {

Check warning on line 48 in kube-core/src/metadata.rs

View check run for this annotation

Codecov / codecov/patch

kube-core/src/metadata.rs#L48

Added line #L48 was not covered by tests
TypeMeta {
api_version: K::api_version(&()).into(),
kind: K::kind(&()).into(),
api_version: K::api_version(&Default::default()).into(),
kind: K::kind(&Default::default()).into(),

Check warning on line 51 in kube-core/src/metadata.rs

View check run for this annotation

Codecov / codecov/patch

kube-core/src/metadata.rs#L50-L51

Added lines #L50 - L51 were not covered by tests
}
}
}
Expand Down

0 comments on commit 0928b11

Please sign in to comment.