@@ -347,15 +347,21 @@ pub async fn print_cfs_session_logs(
347347 let mut logs_stream =
348348 get_cfs_session_container_git_clone_logs_stream ( client. clone ( ) , cfs_session_name) . await ?;
349349
350- while let Some ( line) = logs_stream. try_next ( ) . await . unwrap ( ) {
350+ while let Some ( line) = logs_stream. try_next ( ) . await ? {
351351 println ! ( "{}" , line) ;
352352 }
353353
354- let mut logs_stream = get_cfs_session_container_ansible_logs_stream ( client, cfs_session_name)
355- . await
356- . unwrap ( ) ;
354+ let mut logs_stream =
355+ get_cfs_session_container_ansible_logs_stream ( client. clone ( ) , cfs_session_name) . await ?;
357356
358- while let Some ( line) = logs_stream. try_next ( ) . await . unwrap ( ) {
357+ while let Some ( line) = logs_stream. try_next ( ) . await ? {
358+ println ! ( "{}" , line) ;
359+ }
360+
361+ let mut logs_stream =
362+ get_cfs_session_container_teardown_logs_stream ( client, cfs_session_name) . await ?;
363+
364+ while let Some ( line) = logs_stream. try_next ( ) . await ? {
359365 println ! ( "{}" , line) ;
360366 }
361367
@@ -637,6 +643,99 @@ pub async fn get_cfs_session_container_ansible_logs_stream(
637643 get_container_logs_stream ( ansible_container, cfs_session_pod, & pods_api) . await
638644}
639645
646+ pub async fn get_cfs_session_container_teardown_logs_stream (
647+ client : kube:: Client ,
648+ cfs_session_name : & str ,
649+ ) -> Result < Lines < impl AsyncBufReadExt > , Box < dyn Error + std:: marker:: Send + Sync > > {
650+ let container_name = "teardown" ;
651+
652+ let pods_api: kube:: Api < Pod > = kube:: Api :: namespaced ( client, "services" ) ;
653+
654+ let params = kube:: api:: ListParams :: default ( )
655+ . limit ( 1 )
656+ . labels ( format ! ( "cfsession={}" , cfs_session_name) . as_str ( ) ) ;
657+
658+ let mut pods = pods_api. list ( & params) . await ?;
659+
660+ let mut i = 0 ;
661+ let max = 30 ;
662+ let delay_secs = 2 ;
663+
664+ // Waiting for pod to start
665+ while pods. items . is_empty ( ) && i <= max {
666+ println ! (
667+ "Waiting k8s to create pod/container for cfs session '{}'. Trying again in {} secs. Attempt {} of {}" ,
668+ cfs_session_name,
669+ delay_secs,
670+ i + 1 ,
671+ max
672+ ) ;
673+ i += 1 ;
674+ tokio:: time:: sleep ( time:: Duration :: from_secs ( delay_secs) ) . await ;
675+ pods = pods_api. list ( & params) . await ?;
676+ }
677+
678+ if pods. items . is_empty ( ) {
679+ return Err ( format ! (
680+ "Pod for cfs session '{}' not created. Aborting operation." ,
681+ cfs_session_name
682+ )
683+ . into ( ) ) ;
684+ }
685+
686+ let cfs_session_pod = & pods. items [ 0 ] . clone ( ) ;
687+
688+ let cfs_session_pod_name = cfs_session_pod. metadata . name . clone ( ) . unwrap ( ) ;
689+ log:: info!( "Pod name: {}" , cfs_session_pod_name) ;
690+
691+ let mut containers = cfs_session_pod. spec . as_ref ( ) . unwrap ( ) . containers . iter ( ) ;
692+
693+ log:: debug!(
694+ "Containers found in pod {}: {:#?}" ,
695+ cfs_session_pod_name,
696+ containers
697+ ) ;
698+
699+ let ansible_container: & Container = containers
700+ . find ( |container| container. name . eq ( container_name) )
701+ . unwrap ( ) ;
702+
703+ let mut container_status = get_container_status ( cfs_session_pod, & ansible_container. name ) ;
704+
705+ let mut i = 0 ;
706+ let max = 300 ;
707+
708+ // Waiting for container ansible-x to start
709+ while container_status. as_ref ( ) . is_none ( )
710+ || container_status. as_ref ( ) . unwrap ( ) . waiting . is_some ( ) && i <= max
711+ {
712+ println ! (
713+ "Container ({}) status missing or 'waiting'. Checking again in 2 secs. Attempt {} of {}" ,
714+ ansible_container. name,
715+ i + 1 ,
716+ max
717+ ) ;
718+ i += 1 ;
719+ tokio:: time:: sleep ( time:: Duration :: from_secs ( 2 ) ) . await ;
720+ let pods = pods_api. list ( & params) . await ?;
721+ container_status = get_container_status ( & pods. items [ 0 ] , & ansible_container. name ) ;
722+ log:: debug!(
723+ "Container status:\n {:#?}" ,
724+ container_status. as_ref( ) . unwrap( )
725+ ) ;
726+ }
727+
728+ if container_status. as_ref ( ) . unwrap ( ) . waiting . is_some ( ) {
729+ return Err ( format ! (
730+ "Container ({}) status is waiting. Aborting operation." ,
731+ ansible_container. name
732+ )
733+ . into ( ) ) ;
734+ }
735+
736+ get_container_logs_stream ( ansible_container, cfs_session_pod, & pods_api) . await
737+ }
738+
640739fn get_container_status (
641740 pod : & k8s_openapi:: api:: core:: v1:: Pod ,
642741 container_name : & String ,
0 commit comments