-
-
Notifications
You must be signed in to change notification settings - Fork 371
Add Resize subresource for Pod
#1851
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| use k8s_openapi::api::core::v1::Pod; | ||
| use kube::{ | ||
| api::{Api, DeleteParams, Patch, PatchParams, PostParams, ResourceExt}, | ||
| runtime::wait::{await_condition, conditions::is_pod_running}, | ||
| Client, | ||
| }; | ||
| use tracing::*; | ||
|
|
||
| #[tokio::main] | ||
| async fn main() -> anyhow::Result<()> { | ||
| tracing_subscriber::fmt::init(); | ||
| let client = Client::try_default().await?; | ||
|
|
||
| let pods: Api<Pod> = Api::default_namespaced(client); | ||
|
|
||
| // Create a sample pod with resource limits | ||
| info!("Creating pod with initial resource requirements"); | ||
| let pod: Pod = serde_json::from_value(serde_json::json!({ | ||
| "apiVersion": "v1", | ||
| "kind": "Pod", | ||
| "metadata": { "name": "resize-demo" }, | ||
| "spec": { | ||
| "containers": [{ | ||
| "name": "app", | ||
| "image": "nginx:1.14.2", | ||
| "resources": { | ||
| "requests": { | ||
| "cpu": "100m", | ||
| "memory": "128Mi" | ||
| }, | ||
| "limits": { | ||
| "cpu": "200m", | ||
| "memory": "256Mi" | ||
| } | ||
| } | ||
| }] | ||
| } | ||
| }))?; | ||
|
|
||
| let pp = PostParams::default(); | ||
| pods.create(&pp, &pod).await?; | ||
|
|
||
| // Wait for pod to be running | ||
| info!("Waiting for pod to be running..."); | ||
| let running = await_condition(pods.clone(), "resize-demo", is_pod_running()); | ||
| let _ = tokio::time::timeout(std::time::Duration::from_secs(30), running).await?; | ||
|
|
||
| // Example 1: Using get_resize to view current state | ||
| info!("Example 1: Getting pod resize subresource"); | ||
| let current = pods.get_resize("resize-demo").await?; | ||
| if let Some(spec) = ¤t.spec { | ||
| if let Some(container) = spec.containers.first() { | ||
| info!("Current resources: {:?}", container.resources); | ||
| } | ||
| } | ||
|
|
||
| // Example 2: Using patch_resize to update resources | ||
| info!("Example 2: Patching pod resources using resize subresource"); | ||
| let patch = serde_json::json!({ | ||
| "spec": { | ||
| "containers": [{ | ||
| "name": "app", | ||
| "resources": { | ||
| "requests": { | ||
| "cpu": "150m", | ||
| "memory": "256Mi" | ||
| }, | ||
| "limits": { | ||
| "cpu": "300m", | ||
| "memory": "512Mi" | ||
| } | ||
| } | ||
| }] | ||
| } | ||
| }); | ||
|
|
||
| let patch_params = PatchParams::default(); | ||
| match pods | ||
| .patch_resize("resize-demo", &patch_params, &Patch::Strategic(patch)) | ||
| .await | ||
| { | ||
| Ok(resized) => { | ||
| info!("Successfully patched pod: {}", resized.name_any()); | ||
| if let Some(spec) = resized.spec { | ||
| if let Some(container) = spec.containers.first() { | ||
| info!("Updated resources via patch: {:?}", container.resources); | ||
| } | ||
| } | ||
| } | ||
| Err(e) => { | ||
| error!("Failed to patch resize pod: {}", e); | ||
| } | ||
| } | ||
|
|
||
| // Example 3: Using replace_resize | ||
| info!("Example 3: Using replace_resize method"); | ||
| let mut current_pod = pods.get_resize("resize-demo").await?; | ||
|
Comment on lines
+96
to
+97
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do you think it's possible and/or worth extending the i feel like if you are patching things like this you might want to "wait for it to complete", and if an
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would like to implement this. Is it possible to create an issue from this comment ?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sure thing, feel free to copy paste it before or after merge (or i can write one after merge). just leave this comment unresolved until we write one. 😄
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. opened #1854 so we don't forget |
||
|
|
||
| if let Some(spec) = &mut current_pod.spec { | ||
| if let Some(container) = spec.containers.get_mut(0) { | ||
| if let Some(resources) = &mut container.resources { | ||
| // Update memory request | ||
| if let Some(requests) = &mut resources.requests { | ||
| requests.insert( | ||
| "memory".to_string(), | ||
| k8s_openapi::apimachinery::pkg::api::resource::Quantity("384Mi".to_string()), | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| match pods.replace_resize("resize-demo", &pp, ¤t_pod).await { | ||
| Ok(resized) => { | ||
| info!("Pod resized via replace: {}", resized.name_any()); | ||
| if let Some(spec) = resized.spec { | ||
| if let Some(container) = spec.containers.first() { | ||
| info!("Final resources via replace: {:?}", container.resources); | ||
| } | ||
| } | ||
| } | ||
| Err(e) => error!("Failed to replace_resize: {}", e), | ||
| } | ||
clux marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Cleanup | ||
| info!("Cleaning up"); | ||
| let dp = DeleteParams::default(); | ||
| pods.delete("resize-demo", &dp).await?; | ||
|
|
||
| Ok(()) | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.