Skip to content

Commit

Permalink
Patch implementation for unstable client
Browse files Browse the repository at this point in the history
Signed-off-by: Danil-Grigorev <[email protected]>
  • Loading branch information
Danil-Grigorev committed Sep 9, 2024
1 parent b6e7db6 commit 0288752
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
63 changes: 60 additions & 3 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 k8s_openapi::{
};
use kube_core::{
object::ObjectList,
params::{GetParams, ListParams},
params::{GetParams, ListParams, Patch, PatchParams},
request::Request,
ApiResource, ClusterResourceScope, DynamicResourceScope, NamespaceResourceScope, Resource,
ApiResource, ClusterResourceScope, DynamicResourceScope, NamespaceResourceScope, ObjectMeta, Resource,

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

View workflow job for this annotation

GitHub Actions / msrv

unused import: `ObjectMeta`
ResourceExt,
};
use serde::{de::DeserializeOwned, Serialize};
use std::fmt::Debug;
Expand Down Expand Up @@ -149,7 +150,7 @@ where

/// Scopes for `unstable-client` [`Client#impl-Client`] extension methods
pub mod scope {
pub use super::{Cluster, Namespace, NamespacedRef};
pub use super::{Cluster, Namespace, NamespacedRef, Subresource};
}

// All objects can be listed cluster-wide
Expand Down Expand Up @@ -230,6 +231,15 @@ pub enum NamespaceError {
MissingName,
}

#[derive(Default)]
pub enum Subresource {

Check failure on line 235 in kube-client/src/client/client_ext.rs

View workflow job for this annotation

GitHub Actions / msrv

missing documentation for an enum
#[default]
Core,

Check failure on line 237 in kube-client/src/client/client_ext.rs

View workflow job for this annotation

GitHub Actions / msrv

missing documentation for a variant
Status,

Check failure on line 238 in kube-client/src/client/client_ext.rs

View workflow job for this annotation

GitHub Actions / msrv

missing documentation for a variant
Scale,

Check failure on line 239 in kube-client/src/client/client_ext.rs

View workflow job for this annotation

GitHub Actions / msrv

missing documentation for a variant
Custom(String),

Check failure on line 240 in kube-client/src/client/client_ext.rs

View workflow job for this annotation

GitHub Actions / msrv

missing documentation for a variant
}

/// 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 @@ -385,6 +395,53 @@ impl Client {
req.extensions_mut().insert("list");
self.request::<ObjectList<K>>(req).await
}

/// Patch 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, Subresource};
/// # 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?;
/// // Perform an apply patch on the resource
/// client.patch(pod, &PatchParams::apply("controller").force(), &Patch::Apply(&pod), &Default::default()).await?;
/// // Perform an apply patch on the resource status
/// client.patch(pod, &PatchParams::apply("controller").force(), &Patch::Apply(&pod), &Subresource::Status).await?;
/// # Ok(())
/// # }
/// ```
pub async fn patch<K, P: Serialize + Debug>(
&self,
resource: &K,
pp: &PatchParams,
patch: &Patch<P>,
subresource: &Subresource,
) -> Result<K>
where
K: ResourceExt + Serialize + DeserializeOwned + Clone + Debug,
<K as Resource>::DynamicType: Default,
{
let name = resource.meta().name.as_ref().ok_or(Error::NameResolve)?;
let url = K::url_path(&Default::default(), resource.meta().namespace.as_deref());
let req = Request::new(url);
let req = match subresource {
Subresource::Core => req.patch(name, pp, patch).map_err(Error::BuildRequest)?,
Subresource::Status => req
.patch_subresource("status", name, pp, patch)
.map_err(Error::BuildRequest)?,
Subresource::Scale => req
.patch_subresource("scale", name, pp, patch)
.map_err(Error::BuildRequest)?,
Subresource::Custom(subresource) => req
.patch_subresource(subresource, name, pp, patch)
.map_err(Error::BuildRequest)?,
};
self.request::<K>(req).await
}
}

// 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

0 comments on commit 0288752

Please sign in to comment.