@@ -283,6 +283,154 @@ where
283283 }
284284}
285285
286+ // ----------------------------------------------------------------------------
287+ // Resize subresource
288+ // ----------------------------------------------------------------------------
289+
290+ /// Marker trait for objects that support the resize sub resource.
291+ ///
292+ /// The resize subresource allows updating container resource requests/limits
293+ /// without restarting the pod. This is available in Kubernetes 1.33+.
294+ ///
295+ /// See [`Api::get_resize`], [`Api::patch_resize`], and [`Api::replace_resize`].
296+ ///
297+ /// See the Kubernetes [documentation](https://kubernetes.io/docs/tasks/configure-pod-container/resize-container-resources/)
298+ /// and [limitations](https://kubernetes.io/docs/tasks/configure-pod-container/resize-container-resources/#limitations)
299+ /// for more details.
300+ #[ cfg_attr( docsrs, doc( cfg( feature = "k8s_if_ge_1_33" ) ) ) ]
301+ pub trait Resize { }
302+
303+ k8s_openapi:: k8s_if_ge_1_33! {
304+ impl Resize for k8s_openapi:: api:: core:: v1:: Pod { }
305+ }
306+
307+ k8s_openapi:: k8s_if_ge_1_33! {
308+ impl <K > Api <K >
309+ where
310+ K : Clone + DeserializeOwned + Resize ,
311+ {
312+ /// Get the named resource with the resize subresource.
313+ ///
314+ /// This returns the whole Pod object with current resource allocations.
315+ ///
316+ /// See the Kubernetes [documentation](https://kubernetes.io/docs/tasks/configure-pod-container/resize-container-resources/)
317+ /// and [limitations](https://kubernetes.io/docs/tasks/configure-pod-container/resize-container-resources/#limitations)
318+ /// for more details.
319+ pub async fn get_resize( & self , name: & str ) -> Result <K > {
320+ let mut req = self
321+ . request
322+ . get_subresource( "resize" , name)
323+ . map_err( Error :: BuildRequest ) ?;
324+ req. extensions_mut( ) . insert( "get_resize" ) ;
325+ self . client. request:: <K >( req) . await
326+ }
327+
328+ /// Patch the resize sub resource.
329+ ///
330+ /// This allows you to update specific container resource requirements
331+ /// without fetching the entire Pod object first.
332+ ///
333+ /// Note that only certain container resource fields can be modified. See the
334+ /// [limitations](https://kubernetes.io/docs/tasks/configure-pod-container/resize-container-resources/#limitations)
335+ /// for details on what can be changed.
336+ ///
337+ /// # Example
338+ ///
339+ /// ```no_run
340+ /// use kube::api::{Api, PatchParams, Patch};
341+ /// use k8s_openapi::api::core::v1::Pod;
342+ /// # async fn wrapper() -> Result<(), Box<dyn std::error::Error>> {
343+ /// # let client = kube::Client::try_default().await?;
344+ /// let pods: Api<Pod> = Api::namespaced(client, "default");
345+ /// let pp = PatchParams::default();
346+ ///
347+ /// let patch = serde_json::json!({
348+ /// "spec": {
349+ /// "containers": [{
350+ /// "name": "mycontainer",
351+ /// "resources": {
352+ /// "requests": {
353+ /// "cpu": "200m",
354+ /// "memory": "512Mi"
355+ /// }
356+ /// }
357+ /// }]
358+ /// }
359+ /// });
360+ ///
361+ /// pods.patch_resize("mypod", &pp, &Patch::Strategic(patch)).await?;
362+ /// # Ok(())
363+ /// # }
364+ /// ```
365+ pub async fn patch_resize<P : serde:: Serialize >(
366+ & self ,
367+ name: & str ,
368+ pp: & PatchParams ,
369+ patch: & Patch <P >,
370+ ) -> Result <K > {
371+ let mut req = self
372+ . request
373+ . patch_subresource( "resize" , name, pp, patch)
374+ . map_err( Error :: BuildRequest ) ?;
375+ req. extensions_mut( ) . insert( "patch_resize" ) ;
376+ self . client. request:: <K >( req) . await
377+ }
378+
379+ /// Replace the resize sub resource entirely.
380+ ///
381+ /// This works similarly to [`Api::replace`] but uses the resize subresource.
382+ /// Takes a full Pod object with updated container resource requirements.
383+ ///
384+ /// Note that only certain container resource fields can be modified. See the
385+ /// [limitations](https://kubernetes.io/docs/tasks/configure-pod-container/resize-container-resources/#limitations)
386+ /// for details on what can be changed.
387+ ///
388+ /// # Example
389+ ///
390+ /// ```no_run
391+ /// use k8s_openapi::api::core::v1::Pod;
392+ /// use kube::{Api, api::PostParams};
393+ /// # async fn wrapper() -> Result<(), Box<dyn std::error::Error>> {
394+ /// # let client = kube::Client::try_default().await?;
395+ /// let pods: Api<Pod> = Api::namespaced(client, "default");
396+ /// let pp = PostParams::default();
397+ ///
398+ /// // Get current pod
399+ /// let mut pod = pods.get("mypod").await?;
400+ ///
401+ /// // Modify resource requirements
402+ /// if let Some(spec) = &mut pod.spec {
403+ /// if let Some(container) = spec.containers.get_mut(0) {
404+ /// if let Some(resources) = &mut container.resources {
405+ /// // Update CPU/memory limits or requests
406+ /// // ...
407+ /// }
408+ /// }
409+ /// }
410+ ///
411+ /// pods.replace_resize("mypod", &pp, &pod).await?;
412+ /// # Ok(())
413+ /// # }
414+ /// ```
415+ pub async fn replace_resize( & self , name: & str , pp: & PostParams , data: & K ) -> Result <K >
416+ where
417+ K : Serialize ,
418+ {
419+ let mut req = self
420+ . request
421+ . replace_subresource(
422+ "resize" ,
423+ name,
424+ pp,
425+ serde_json:: to_vec( data) . map_err( Error :: SerdeError ) ?,
426+ )
427+ . map_err( Error :: BuildRequest ) ?;
428+ req. extensions_mut( ) . insert( "replace_resize" ) ;
429+ self . client. request:: <K >( req) . await
430+ }
431+ }
432+ }
433+
286434// ----------------------------------------------------------------------------
287435
288436// TODO: Replace examples with owned custom resources. Bad practice to write to owned objects
@@ -610,3 +758,19 @@ where
610758 Ok ( Portforwarder :: new ( connection. into_stream ( ) , ports) )
611759 }
612760}
761+
762+ // ----------------------------------------------------------------------------
763+ // Resize subresource tests
764+ // ----------------------------------------------------------------------------
765+
766+ #[ test]
767+ fn resize_path ( ) {
768+ use crate :: api:: { Request , Resource } ;
769+ use k8s_openapi:: api:: core:: v1 as corev1;
770+
771+ k8s_openapi:: k8s_if_ge_1_33! {
772+ let url = corev1:: Pod :: url_path( & ( ) , Some ( "ns" ) ) ;
773+ let req = Request :: new( url) . get_subresource( "resize" , "mypod" ) . unwrap( ) ;
774+ assert_eq!( req. uri( ) , "/api/v1/namespaces/ns/pods/mypod/resize" ) ;
775+ }
776+ }
0 commit comments