Skip to content

Commit e08a9d7

Browse files
committed
fix: cfs session logs also shows teardown stage logs
1 parent 4980242 commit e08a9d7

File tree

2 files changed

+118
-17
lines changed

2 files changed

+118
-17
lines changed

src/cfs/session.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,20 +1281,22 @@ pub mod mesa {
12811281
.await
12821282
.unwrap();
12831283

1284-
let _ = print_cfs_session_logs(client, &cfs_session_name).await;
1285-
} else {
1286-
// User does not want the CFS logs but we still need to wayt the CFS session to
1287-
// finis. Wait till the CFS session finishes
1288-
wait_cfs_session_to_finish(
1289-
shasta_token,
1290-
shasta_base_url,
1291-
shasta_root_cert,
1292-
&cfs_session_name,
1293-
)
1294-
.await;
1284+
print_cfs_session_logs(client, &cfs_session_name)
1285+
.await
1286+
.map_err(|e| Error::Message(e.to_string()))?;
12951287
}
12961288

1297-
// Get most recent CFS session status
1289+
// User does not want the CFS (ansible) logs but we still need to wait the CFS session to
1290+
// finish (teardown). Wait till the CFS session finishes
1291+
wait_cfs_session_to_finish(
1292+
shasta_token,
1293+
shasta_base_url,
1294+
shasta_root_cert,
1295+
&cfs_session_name,
1296+
)
1297+
.await;
1298+
1299+
// Get CFS session status
12981300
let cfs_session: CfsSessionGetResponse = get(
12991301
shasta_token,
13001302
shasta_base_url,

src/common/kubernetes.rs

Lines changed: 104 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
640739
fn get_container_status(
641740
pod: &k8s_openapi::api::core::v1::Pod,
642741
container_name: &String,

0 commit comments

Comments
 (0)