-
-
Notifications
You must be signed in to change notification settings - Fork 401
Wait for pod resize #1922
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
Open
hugoponthieu
wants to merge
16
commits into
kube-rs:main
Choose a base branch
from
hugoponthieu:wait-for-pod-resize
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Wait for pod resize #1922
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
1b41e26
feat: utility to wait for pod resizing
hugoponthieu 2149bf6
example wait pod resize
hugoponthieu ba7a08f
fix: fmt examples for wait resizing pods
hugoponthieu 4e5d0ae
fix: typo and rust fmt
hugoponthieu c386575
Merge branch 'main' into wait-for-pod-resize
hugoponthieu 3de14b7
fix: use condition to determine if pod is still resizing
786d848
Merge branch 'main' into wait-for-pod-resize
hugoponthieu 4e98346
Merge branch 'main' into wait-for-pod-resize
hugoponthieu e1a7771
refacto(example): use newer image for wait resize, add utility to log…
4049160
Merge branch 'main' into wait-for-pod-resize
hugoponthieu a92af25
Merge branch 'main' into wait-for-pod-resize
hugoponthieu f50c8d9
feat(resize): compare container spec and statuses
7ad1127
Merge branch 'main' into wait-for-pod-resize
hugoponthieu b0a6c6c
Merge branch 'main' into wait-for-pod-resize
hugoponthieu c2f30fd
Merge branch 'main' into wait-for-pod-resize
hugoponthieu f34eab3
Merge branch 'main' into wait-for-pod-resize
doxxx93 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| use k8s_openapi::api::core::v1::Pod; | ||
| use kube::{ | ||
| Client, ResourceExt, | ||
| api::{Api, DeleteParams, Patch, PatchParams, PostParams}, | ||
| runtime::wait::{await_condition, conditions}, | ||
| }; | ||
| use tracing::*; | ||
|
|
||
| fn inspect_pod_resize(pod: &Pod) { | ||
| if let Some(spec) = &pod.spec { | ||
| if let Some(container) = spec.containers.first() { | ||
| info!("Spec resources (desired): {:?}", container.resources); | ||
| } | ||
| } | ||
| if let Some(status) = &pod.status { | ||
| info!("Resize status: {:?}", status.resize); | ||
| if let Some(container_status) = status.container_statuses.as_ref().and_then(|cs| cs.first()) { | ||
| info!("Status resources (actual): {:?}", container_status.resources); | ||
| info!("Container restart count: {}", container_status.restart_count); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[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 and resize policy | ||
| info!("Creating pod with initial resource requirements"); | ||
| let pod_template = serde_json::json!({ | ||
| "apiVersion": "v1", | ||
| "kind": "Pod", | ||
| "metadata": { "name": "resize-wait-demo" }, | ||
| "spec": { | ||
| "containers": [{ | ||
| "name": "app", | ||
| "image": "alpine:3.23", | ||
| "resizePolicy": [ | ||
| { | ||
| "resourceName": "cpu", | ||
| "restartPolicy": "NotRequired" | ||
| }, | ||
| { | ||
| "resourceName": "memory", | ||
| "restartPolicy": "RestartContainer" | ||
| } | ||
| ], | ||
| "resources": { | ||
| "requests": { | ||
| "cpu": "100m", | ||
| "memory": "128Mi" | ||
| }, | ||
| "limits": { | ||
| "cpu": "200m", | ||
| "memory": "256Mi" | ||
| } | ||
| } | ||
| }] | ||
| } | ||
| }); | ||
| let pod = serde_json::from_value(pod_template)?; | ||
| let pp = PostParams::default(); | ||
| match pods.create(&pp, &pod).await { | ||
| Ok(created) => info!("Created pod: {}", created.name_any()), | ||
| Err(kube::Error::Api(ae)) if ae.code == 409 => { | ||
| info!("Pod already exists, patching it..."); | ||
| let pp = PatchParams::apply("pod-resize-example"); | ||
| let patch = Patch::Apply(pod); | ||
| let _ = pods.patch("resize-wait-demo", &pp, &patch).await?; | ||
| } | ||
| Err(e) => return Err(e.into()), | ||
| } | ||
| // Wait for pod to be running | ||
| info!("Waiting for pod to be running..."); | ||
| let running = await_condition(pods.clone(), "resize-wait-demo", conditions::is_pod_running()); | ||
| tokio::time::timeout(std::time::Duration::from_secs(60), running).await??; | ||
| info!("✓ Pod is running"); | ||
|
|
||
| // Display initial resources | ||
| let current = pods.get("resize-wait-demo").await?; | ||
| info!("Initial pod state:"); | ||
| inspect_pod_resize(¤t); | ||
|
|
||
| // Resize CPU (no restart required) | ||
| info!("\n--- Example 1: Resizing CPU (NotRequired restart policy) ---"); | ||
| let cpu_patch = serde_json::json!({ | ||
| "spec": { | ||
| "containers": [{ | ||
| "name": "app", | ||
| "resources": { | ||
| "requests": { | ||
| "cpu": "150m" | ||
| }, | ||
| "limits": { | ||
| "cpu": "300m" | ||
| } | ||
| } | ||
| }] | ||
| } | ||
| }); | ||
|
|
||
| let patch_params = PatchParams::default(); | ||
| info!("Patching pod with new CPU resources..."); | ||
| pods.patch_resize("resize-wait-demo", &patch_params, &Patch::Strategic(cpu_patch)) | ||
| .await?; | ||
|
|
||
| info!("Waiting for resize to complete..."); | ||
| let resized = await_condition(pods.clone(), "resize-wait-demo", conditions::is_pod_resized()); | ||
| let pod = tokio::time::timeout(std::time::Duration::from_secs(30), resized).await??; | ||
| info!("✓ Pod CPU resize completed successfully!"); | ||
| if let Some(pod) = pod { | ||
| inspect_pod_resize(&pod); | ||
| } | ||
|
|
||
| // Resize Memory (restart required) | ||
| info!("\n--- Example 2: Resizing Memory (RestartContainer policy) ---"); | ||
| let mem_patch = serde_json::json!({ | ||
| "spec": { | ||
| "containers": [{ | ||
| "name": "app", | ||
| "resources": { | ||
| "requests": { | ||
| "memory": "192Mi" | ||
| }, | ||
| "limits": { | ||
| "memory": "384Mi" | ||
| } | ||
| } | ||
| }] | ||
| } | ||
| }); | ||
|
|
||
| info!("Patching pod with new memory resources..."); | ||
| pods.patch_resize("resize-wait-demo", &patch_params, &Patch::Strategic(mem_patch)) | ||
| .await?; | ||
|
|
||
| info!("Waiting for memory resize to complete (container will restart)..."); | ||
| let resized = await_condition(pods.clone(), "resize-wait-demo", conditions::is_pod_resized()); | ||
| let pod = tokio::time::timeout(std::time::Duration::from_secs(60), resized).await??; | ||
| info!("✓ Pod memory resize completed successfully!"); | ||
| if let Some(pod) = pod { | ||
| inspect_pod_resize(&pod); | ||
| } | ||
|
|
||
| // Cleanup | ||
| info!("\nCleaning up..."); | ||
| let dp = DeleteParams::default(); | ||
| pods.delete("resize-wait-demo", &dp).await?; | ||
| info!("Pod deleted"); | ||
|
|
||
| Ok(()) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this can be simplified with server side apply; pseudo code;
let p = pods.patch(pod, serverside).await?;
if the pod exists, no need to delete it, we are overwriting it to have the parameters in the json above