1- #[ macro_use] extern crate log;
2-
3- use futures:: { StreamExt , TryStreamExt } ;
41use k8s_openapi:: api:: core:: v1:: Pod ;
52
63use 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