1
- #[ macro_use] extern crate log;
2
-
3
- use futures:: { StreamExt , TryStreamExt } ;
4
1
use k8s_openapi:: api:: core:: v1:: Pod ;
5
2
6
3
use kube:: {
7
- api:: { Api , DeleteParams , ListParams , PostParams , WatchEvent } ,
4
+ api:: { Api , DeleteParams , PostParams } ,
5
+ runtime:: wait:: { await_condition, conditions:: is_pod_running} ,
8
6
Client , ResourceExt ,
9
7
} ;
10
8
@@ -34,22 +32,8 @@ async fn main() -> anyhow::Result<()> {
34
32
pods. create ( & PostParams :: default ( ) , & p) . await ?;
35
33
36
34
// 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 ?;
53
37
54
38
let mut pf = pods. portforward ( "example" , & [ 80 ] ) . await ?;
55
39
let mut ports = pf. ports ( ) . unwrap ( ) ;
@@ -72,11 +56,11 @@ async fn main() -> anyhow::Result<()> {
72
56
. body ( Body :: from ( "" ) )
73
57
. unwrap ( ) ;
74
58
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 ( ) ;
76
60
assert ! ( parts. status == 200 ) ;
77
61
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) ? ;
80
64
assert ! ( body_str. contains( "Welcome to nginx!" ) ) ;
81
65
82
66
// Delete it
0 commit comments