|
1 | 1 | use crate::{Client, Error, Result};
|
2 | 2 | use k8s_openapi::{
|
3 |
| - api::core::v1::{LocalObjectReference, Namespace as k8sNs, ObjectReference}, |
| 3 | + api::{ |
| 4 | + core::v1::{LocalObjectReference, Namespace as k8sNs, ObjectReference}, |
| 5 | + }, |
4 | 6 | apimachinery::pkg::apis::meta::v1::OwnerReference,
|
5 | 7 | };
|
6 | 8 | use kube_core::{
|
7 | 9 | object::ObjectList,
|
8 |
| - params::{GetParams, ListParams}, |
| 10 | + params::{GetParams, ListParams, Patch, PatchParams}, |
9 | 11 | request::Request,
|
10 |
| - ApiResource, ClusterResourceScope, DynamicResourceScope, NamespaceResourceScope, Resource, |
| 12 | + ApiResource, ClusterResourceScope, DynamicResourceScope, NamespaceResourceScope, ObjectMeta, Resource, |
| 13 | + ResourceExt, |
11 | 14 | };
|
12 | 15 | use serde::{de::DeserializeOwned, Serialize};
|
13 | 16 | use std::fmt::Debug;
|
@@ -383,6 +386,76 @@ impl Client {
|
383 | 386 | req.extensions_mut().insert("list");
|
384 | 387 | self.request::<ObjectList<K>>(req).await
|
385 | 388 | }
|
| 389 | + |
| 390 | + /// Perform apply patch on the provided `Resource` implementing type `K` |
| 391 | + /// |
| 392 | + /// ```no_run |
| 393 | + /// # use k8s_openapi::api::core::v1::; |
| 394 | + /// # use k8s_openapi::api::core::v1::Service; |
| 395 | + /// # use kube::client::scope::Namespace; |
| 396 | + /// # use kube::prelude::*; |
| 397 | + /// # use kube::api::{PatchParams, Patch}; |
| 398 | + /// # async fn wrapper() -> Result<(), Box<dyn std::error::Error>> { |
| 399 | + /// # let client: kube::Client = todo!(); |
| 400 | + /// let pod: Pod = client.get("some_pod", &Namespace::from("default")).await?; |
| 401 | + /// let pp = &PatchParams::apply("controller").force(); |
| 402 | + /// // Perform an apply patch on the resource |
| 403 | + /// client.apply(pod, pp).await?; |
| 404 | + /// # Ok(()) |
| 405 | + /// # } |
| 406 | + /// ``` |
| 407 | + pub async fn apply<K, P: Serialize + Debug>(&self, resource: &K, pp: &PatchParams) -> Result<K> |
| 408 | + where |
| 409 | + K: ResourceExt + Serialize + DeserializeOwned + Clone + Debug, |
| 410 | + <K as Resource>::DynamicType: Default, |
| 411 | + { |
| 412 | + let meta = resource.meta(); |
| 413 | + let name = meta.name.as_ref().ok_or(Error::NameResolve)?; |
| 414 | + let url = K::url_path(&Default::default(), meta.namespace.as_deref()); |
| 415 | + let req = Request::new(url); |
| 416 | + |
| 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)?; |
| 421 | + self.request::<K>(req).await |
| 422 | + } |
| 423 | + |
| 424 | + /// Perform apply patch on the provided `Resource` status, implementing type `K` |
| 425 | + /// |
| 426 | + /// ```no_run |
| 427 | + /// # use k8s_openapi::api::core::v1::Pod; |
| 428 | + /// # use k8s_openapi::api::core::v1::Service; |
| 429 | + /// # use kube::client::scope::Namespace; |
| 430 | + /// # use kube::prelude::*; |
| 431 | + /// # use kube::api::{PatchParams, Patch}; |
| 432 | + /// # async fn wrapper() -> Result<(), Box<dyn std::error::Error>> { |
| 433 | + /// # let client: kube::Client = todo!(); |
| 434 | + /// let pod: Pod = client.get("some_pod", &Namespace::from("default")).await?; |
| 435 | + /// let pp = &PatchParams::apply("controller").force(); |
| 436 | + /// // Perform an apply patch on the resource status |
| 437 | + /// client.apply(pod, pp).await?; |
| 438 | + /// # Ok(()) |
| 439 | + /// # } |
| 440 | + /// ``` |
| 441 | + pub async fn apply_status<K, P: Serialize + Debug>(&self, resource: &K, pp: &PatchParams) -> Result<K> |
| 442 | + where |
| 443 | + K: ResourceExt + Serialize + DeserializeOwned + Clone + Debug, |
| 444 | + <K as Resource>::DynamicType: Default, |
| 445 | + { |
| 446 | + let meta = resource.meta(); |
| 447 | + let name = meta.name.as_ref().ok_or(Error::NameResolve)?; |
| 448 | + let url = K::url_path(&Default::default(), meta.namespace.as_deref()); |
| 449 | + let req = Request::new(url); |
| 450 | + |
| 451 | + let mut resource = resource.clone(); |
| 452 | + resource.meta_mut().managed_fields = None; |
| 453 | + let patch = &Patch::Apply(resource); |
| 454 | + let req = req |
| 455 | + .patch_subresource("status", name, pp, patch) |
| 456 | + .map_err(Error::BuildRequest)?; |
| 457 | + self.request::<K>(req).await |
| 458 | + } |
386 | 459 | }
|
387 | 460 |
|
388 | 461 | // Resource url_path resolver
|
|
0 commit comments