Skip to content

Commit 4187482

Browse files
committed
Use await_condition and reduce unwrap in examples
Signed-off-by: kazk <[email protected]>
1 parent 61d2add commit 4187482

File tree

2 files changed

+13
-43
lines changed

2 files changed

+13
-43
lines changed

examples/pod_portforward.rs

+6-20
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
#[macro_use] extern crate log;
2-
3-
use futures::{StreamExt, TryStreamExt};
1+
use futures::StreamExt;
42
use k8s_openapi::api::core::v1::Pod;
53

64
use kube::{
7-
api::{Api, DeleteParams, ListParams, PostParams, WatchEvent},
5+
api::{Api, DeleteParams, PostParams},
6+
runtime::wait::{await_condition, conditions::is_pod_running},
87
Client, ResourceExt,
98
};
109

@@ -34,22 +33,9 @@ async fn main() -> anyhow::Result<()> {
3433
pods.create(&PostParams::default(), &p).await?;
3534

3635
// Wait until the pod is running, otherwise we get 500 error.
37-
let lp = ListParams::default().fields("metadata.name=example").timeout(10);
38-
let mut stream = pods.watch(&lp, "0").await?.boxed();
39-
while let Some(status) = stream.try_next().await? {
40-
match status {
41-
WatchEvent::Added(o) => {
42-
info!("Added {}", o.name());
43-
}
44-
WatchEvent::Modified(o) => {
45-
let s = o.status.as_ref().expect("status exists on pod");
46-
if s.phase.clone().unwrap_or_default() == "Running" {
47-
break;
48-
}
49-
}
50-
_ => {}
51-
}
52-
}
36+
// Wait until the pod is running, otherwise we get 500 error.
37+
let running = await_condition(pods.clone(), "example", is_pod_running());
38+
let _ = tokio::time::timeout(std::time::Duration::from_secs(15), running).await?;
5339

5440
let mut pf = pods.portforward("example", &[80]).await?;
5541
let mut ports = pf.ports().unwrap();

examples/pod_portforward_hyper_http.rs

+7-23
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
#[macro_use] extern crate log;
2-
3-
use futures::{StreamExt, TryStreamExt};
41
use k8s_openapi::api::core::v1::Pod;
52

63
use kube::{
7-
api::{Api, DeleteParams, ListParams, PostParams, WatchEvent},
4+
api::{Api, DeleteParams, PostParams},
5+
runtime::wait::{await_condition, conditions::is_pod_running},
86
Client, ResourceExt,
97
};
108

@@ -34,22 +32,8 @@ async fn main() -> anyhow::Result<()> {
3432
pods.create(&PostParams::default(), &p).await?;
3533

3634
// Wait until the pod is running, otherwise we get 500 error.
37-
let lp = ListParams::default().fields("metadata.name=example").timeout(10);
38-
let mut stream = pods.watch(&lp, "0").await?.boxed();
39-
while let Some(status) = stream.try_next().await? {
40-
match status {
41-
WatchEvent::Added(o) => {
42-
info!("Added {}", o.name());
43-
}
44-
WatchEvent::Modified(o) => {
45-
let s = o.status.as_ref().expect("status exists on pod");
46-
if s.phase.clone().unwrap_or_default() == "Running" {
47-
break;
48-
}
49-
}
50-
_ => {}
51-
}
52-
}
35+
let running = await_condition(pods.clone(), "example", is_pod_running());
36+
let _ = tokio::time::timeout(std::time::Duration::from_secs(15), running).await?;
5337

5438
let mut pf = pods.portforward("example", &[80]).await?;
5539
let mut ports = pf.ports().unwrap();
@@ -72,11 +56,11 @@ async fn main() -> anyhow::Result<()> {
7256
.body(Body::from(""))
7357
.unwrap();
7458

75-
let (parts, body) = sender.send_request(http_req).await.unwrap().into_parts();
59+
let (parts, body) = sender.send_request(http_req).await?.into_parts();
7660
assert!(parts.status == 200);
7761

78-
let body_bytes = body::to_bytes(body).await.unwrap();
79-
let body_str = std::str::from_utf8(&body_bytes).unwrap();
62+
let body_bytes = body::to_bytes(body).await?;
63+
let body_str = std::str::from_utf8(&body_bytes)?;
8064
assert!(body_str.contains("Welcome to nginx!"));
8165

8266
// Delete it

0 commit comments

Comments
 (0)