Skip to content

Commit

Permalink
Remove debug msgs, link healthcheck to k8s service
Browse files Browse the repository at this point in the history
  • Loading branch information
evanjt committed Oct 23, 2024
1 parent 3122e35 commit 480fd94
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 27 deletions.
5 changes: 5 additions & 0 deletions src/common/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,8 @@ impl UIConfiguration {
}
}
}

#[derive(ToSchema, Deserialize, Serialize)]
pub struct HealthCheck {
pub status: String,
}
25 changes: 22 additions & 3 deletions src/common/views.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use super::models::HealthCheck;
use crate::common::models::UIConfiguration;
use axum::Json;
use crate::external::k8s::services::get_pods;
use axum::{http::StatusCode, Json};

#[utoipa::path(
get,
Expand All @@ -13,9 +15,26 @@ use axum::Json;
)
)
)]
pub async fn healthz() -> &'static str {
pub async fn healthz() -> (StatusCode, Json<HealthCheck>) {
// Get health of the API.
"ok"
match get_pods().await {
Ok(_) => {
return (
StatusCode::OK,
Json(HealthCheck {
status: "ok".to_string(),
}),
)
}
Err(_) => {
return (
StatusCode::INTERNAL_SERVER_ERROR,
Json(HealthCheck {
status: "error".to_string(),
}),
)
}
};
}

#[utoipa::path(
Expand Down
16 changes: 2 additions & 14 deletions src/external/k8s/services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,15 @@ async fn refresh_oidc_token(refresh_token: &str, idp_issuer_url: &str) -> Result
("refresh_token", refresh_token),
("client_id", "runai-cli"),
];
println!("Refreshing OIDC token, params: {:?}", params);
let url = format!("{}/protocol/openid-connect/token", idp_issuer_url);
println!("Refreshing OIDC token, url: {:?}", url);
let res = match client.post(&url).form(&params).send().await {
Ok(res) => res,
Err(e) => {
println!("Failed to refresh token: {:?}", e);
return Err(anyhow!("Failed to refresh token: {}", e));
}
};

println!("Response: {:?}", res);

if res.status().is_success() {
println!("Refreshed OIDC token");
let token_response: TokenResponse = res.json().await?;
Ok(token_response.id_token)
} else {
Expand Down Expand Up @@ -68,13 +62,9 @@ pub async fn get_pods() -> Result<Option<Vec<PodName>>> {
// Get app config and kube client
let app_config = Config::from_env();
let client = match refresh_token_and_get_client().await {
Ok(client) => {
println!("Successfully got Kubernetes client");
client
}
Ok(client) => client,
Err(e) => {
println!("Failed to get Kubernetes client: {}", e);
return Ok(None);
return Err(e);
}
};

Expand Down Expand Up @@ -116,8 +106,6 @@ async fn refresh_token_and_get_client() -> Result<Client> {
.config
.get("idp-issuer-url")
.unwrap();
println!("Kubectl config: {:?}", idp_issuer_url);
println!("Kubectl config kubeconfig: {:?}", kubeconfig);

// Refresh the OIDC token
let new_id_token = refresh_oidc_token(&refresh_token, &idp_issuer_url).await?;
Expand Down
15 changes: 5 additions & 10 deletions src/external/services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ pub async fn check_external_services() {

let service: ActiveModel = match pods_result {
Ok(Some(pods)) => {
println!("Found {} pods", pods.len());
let pods_json = serde_json::to_value(pods).unwrap();
ServiceCreate {
service_name: ServiceName::RCP,
Expand All @@ -24,17 +23,13 @@ pub async fn check_external_services() {
}
.into()
}
Ok(_) => {
println!("No pods found.");
ServiceCreate {
service_name: ServiceName::RCP,
is_online: true,
details: None,
}
.into()
Ok(_) => ServiceCreate {
service_name: ServiceName::RCP,
is_online: true,
details: None,
}
.into(),
Err(err) => {
println!("Error with RCP: {}", err);
let error_json = serde_json::to_value(err.to_string()).unwrap();
ServiceCreate {
service_name: ServiceName::RCP,
Expand Down

0 comments on commit 480fd94

Please sign in to comment.