Skip to content

Commit

Permalink
Fix status messages
Browse files Browse the repository at this point in the history
  • Loading branch information
evanjt committed Oct 31, 2024
1 parent 82a3b51 commit 5b205de
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 55 deletions.
25 changes: 5 additions & 20 deletions src/external/k8s/models.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,26 @@
use crate::{config::Config, submissions};
use anyhow::Result;
use crate::config::Config;
use chrono::{DateTime, Utc};
use serde::Serialize;
use thiserror::Error;
use tokio::io::split;
use uuid::Uuid;

#[derive(Debug)]
pub struct PodInfo {
pub name: String,
pub start_time: DateTime<Utc>,
pub start_time: Option<DateTime<Utc>>,
pub latest_status: String,
pub latest_status_time: DateTime<Utc>,
pub latest_status_time: Option<DateTime<Utc>>,
}

#[derive(Serialize, Debug, Clone)]
pub struct PodName {
pub prefix: String,
pub submission_id: Uuid,
pub start_time: DateTime<Utc>,
pub start_time: Option<DateTime<Utc>>,
pub latest_status: String,
pub latest_status_time: DateTime<Utc>,
pub latest_status_time: Option<DateTime<Utc>>,
pub run_id: u64,
}

#[derive(Error, Debug)]
pub enum PodNameError {
#[error("Pod name does not have the expected structure")]
InvalidStructure,
#[error("Pod name does not have the expected prefix")]
InvalidPrefix,
#[error("Invalid UUID format")]
InvalidUuid(#[from] uuid::Error),
#[error("Invalid run ID")]
InvalidRunId(#[from] std::num::ParseIntError),
}

impl From<PodInfo> for PodName {
fn from(pod_info: PodInfo) -> Self {
let config = Config::from_env();
Expand Down
52 changes: 20 additions & 32 deletions src/external/k8s/services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,6 @@ pub async fn get_pods() -> Result<Vec<crate::external::k8s::models::PodName>, ku
Ok(pod_list) => pod_list,
Err(e) => return Err(e),
};
println!("Found {} pods", pod_list.items.len());
if let Some(first_pod) = pod_list.items.first() {
println!("First pod name: {:?}", first_pod.metadata.name);
println!("First pod status: {:?}", first_pod.status);
} else {
println!("No pods found.");
}

let pod_infos: Vec<crate::external::k8s::models::PodName> = pod_list
.clone()
.items
Expand All @@ -93,37 +85,33 @@ pub async fn get_pods() -> Result<Vec<crate::external::k8s::models::PodName>, ku
return None;
}

let start_time: DateTime<Utc> = pod.status.clone().unwrap().start_time.unwrap().0;
// Get the latest status by the latest container status.conditions ordered by last_transition_time
let latest_status_info = pod.status.as_ref().and_then(|status| {
status.conditions.as_ref().and_then(|conditions| {
conditions
.iter()
.max_by_key(|condition| condition.last_transition_time.clone())
.map(|condition| {
(
condition.reason.clone(),
condition.last_transition_time.clone(),
)
})
})
});

let (latest_status, latest_status_time) = match latest_status_info {
Some((status, time)) => (status.unwrap_or("Unknown".to_string()), time.unwrap().0),
None => ("Unknown".to_string(), Utc::now()),
let start_time: Option<DateTime<Utc>> = match pod.status.clone().unwrap().start_time {
Some(time) => Some(time.0),
None => None,
};

println!("Pod name: {}", name);
println!("Start time: {:?}", start_time);
println!("Latest status message: {:?}", latest_status);
println!("Latest status time: {:?}", latest_status_time);
// println!("\nPod status: {:#?}\n", pod);
let phase = pod.status.as_ref().and_then(|status| status.phase.clone());

// Get the latest status time by the latest container status.conditions ordered by last_transition_time
let latest_status_time: Option<DateTime<Utc>> =
match pod.status.as_ref().and_then(|status| {
status.conditions.as_ref().and_then(|conditions| {
conditions
.iter()
.max_by_key(|condition| condition.last_transition_time.clone())
.map(|condition| condition.last_transition_time.clone())
})
}) {
Some(time) => Some(time.unwrap().0),
None => None,
};

Some(
crate::external::k8s::models::PodInfo {
name,
start_time,
latest_status,
latest_status: phase.unwrap_or_else(|| "Unknown".to_string()),
latest_status_time,
}
.into(),
Expand Down
1 change: 0 additions & 1 deletion src/external/services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use sea_orm::{Database, DatabaseConnection, EntityTrait};
async fn check_kubernetes() -> Result<serde_json::Value> {
match crate::external::k8s::services::get_pods().await {
Ok(pods) => Ok(serde_json::to_value(pods).unwrap()),
Ok(_) => Ok(serde_json::to_value("No pods found").unwrap()),
Err(err) => Err(anyhow!(serde_json::to_value(err.to_string()).unwrap())),
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/submissions/models.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use crate::uploads::models::UploadRead;

use super::db::ActiveModel;
use chrono::NaiveDateTime;
use sea_orm::{DeriveIntoActiveModel, NotSet, Set};
Expand Down

0 comments on commit 5b205de

Please sign in to comment.