Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Native client apply support #1574

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 97 additions & 2 deletions kube-client/src/client/client_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
};
use kube_core::{
object::ObjectList,
params::{GetParams, ListParams},
params::{GetParams, ListParams, Patch, PatchParams},
request::Request,
ApiResource, ClusterResourceScope, DynamicResourceScope, NamespaceResourceScope, Resource,
ApiResource, ClusterResourceScope, DynamicResourceScope, NamespaceResourceScope, Resource, ResourceExt,
TypeMeta,
};
use serde::{de::DeserializeOwned, Serialize};
use std::fmt::Debug;
Expand Down Expand Up @@ -230,6 +231,18 @@
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 @@ -383,6 +396,88 @@
req.extensions_mut().insert("list");
self.request::<ObjectList<K>>(req).await
}

/// Perform apply patch on the provided `Resource` implementing type `K`
///
/// ```no_run
/// # use k8s_openapi::api::core::v1::Pod;
/// # use k8s_openapi::api::core::v1::Service;
/// # use kube::client::scope::Namespace;
/// # use kube::prelude::*;
/// # use kube::api::{PatchParams, Patch};
/// # async fn wrapper() -> Result<(), Box<dyn std::error::Error>> {
/// # let client: kube::Client = todo!();
/// let pod: Pod = client.get("some_pod", &Namespace::from("default")).await?;
/// let pp = &PatchParams::apply("controller").force();
/// // Perform an apply patch on the resource
/// client.apply(&pod, pp).await?;
/// # Ok(())
/// # }
/// ```
pub async fn apply<K>(&self, resource: &K, pp: &PatchParams) -> Result<K>
where
K: ResourceExt + Serialize + DeserializeOwned + Clone + Debug,
<K as Resource>::DynamicType: Default,
{
let meta = resource.meta();
let name = meta.name.as_ref().ok_or(Error::NameResolve)?;
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 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
}

/// Perform apply patch on the provided `Resource` status, implementing type `K`
///
/// ```no_run
/// # use k8s_openapi::api::core::v1::Pod;
/// # use k8s_openapi::api::core::v1::Service;
/// # use kube::client::scope::Namespace;
/// # use kube::prelude::*;
/// # use kube::api::{PatchParams, Patch};
/// # async fn wrapper() -> Result<(), Box<dyn std::error::Error>> {
/// # let client: kube::Client = todo!();
/// let pod: Pod = client.get("some_pod", &Namespace::from("default")).await?;
/// let pp = &PatchParams::apply("controller").force();
/// // Perform an apply patch on the resource status
/// client.apply_status(&pod, pp).await?;
/// # Ok(())
/// # }
/// ```
pub async fn apply_status<K>(&self, resource: &K, pp: &PatchParams) -> Result<K>
where
K: ResourceExt + Serialize + DeserializeOwned + Clone + Debug,
<K as Resource>::DynamicType: Default,
{
let meta = resource.meta();
let name = meta.name.as_ref().ok_or(Error::NameResolve)?;
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 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::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
}
}

// Resource url_path resolver
Expand Down
6 changes: 6 additions & 0 deletions kube-client/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ pub enum Error {
#[cfg_attr(docsrs, doc(cfg(feature = "unstable-client")))]
#[error("Reference resolve error: {0}")]
RefResolve(String),

/// Error resolving resource name
#[cfg(feature = "unstable-client")]
#[cfg_attr(docsrs, doc(cfg(feature = "unstable-client")))]
#[error("Resource has no name")]
NameResolve,
}

#[derive(Error, Debug)]
Expand Down
12 changes: 6 additions & 6 deletions kube-core/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
/// assert_eq!(type_meta.kind, "PodList");
/// assert_eq!(type_meta.api_version, "v1");
/// ```
pub fn list<K: Resource<DynamicType = ()>>() -> Self {
pub fn list<K: Resource<DynamicType = impl Default>>() -> Self {

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

View check run for this annotation

Codecov / codecov/patch

kube-core/src/metadata.rs#L31

Added line #L31 was not covered by tests
TypeMeta {
api_version: K::api_version(&()).into(),
kind: K::kind(&()).to_string() + "List",
api_version: K::api_version(&Default::default()).into(),
kind: K::kind(&Default::default()).to_string() + "List",

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

View check run for this annotation

Codecov / codecov/patch

kube-core/src/metadata.rs#L33-L34

Added lines #L33 - L34 were not covered by tests
}
}

Expand All @@ -45,10 +45,10 @@
/// 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
Loading