2024-07-14 16:38:07 -04:00
|
|
|
#![forbid(unsafe_code)]
|
2024-09-28 21:50:50 -04:00
|
|
|
use std::borrow::Borrow;
|
2024-07-25 20:04:30 -04:00
|
|
|
use std::sync::Arc;
|
2024-07-14 19:01:10 -04:00
|
|
|
use std::time::Duration;
|
|
|
|
|
2024-07-14 15:19:41 -04:00
|
|
|
use axum::http::StatusCode;
|
2024-07-15 21:02:30 -04:00
|
|
|
use axum::middleware;
|
2024-07-14 15:14:52 -04:00
|
|
|
use axum::routing::get;
|
2024-07-14 16:13:06 -04:00
|
|
|
use axum::routing::post;
|
2024-07-14 15:19:41 -04:00
|
|
|
use axum::Json;
|
2024-07-14 15:14:52 -04:00
|
|
|
use axum::Router;
|
2024-07-21 15:54:12 -04:00
|
|
|
use kube::api::PostParams;
|
|
|
|
use kube::Api;
|
2024-07-14 23:08:10 -04:00
|
|
|
use kube::Client;
|
2024-07-14 15:19:41 -04:00
|
|
|
use serde::Serialize;
|
2024-07-14 19:01:10 -04:00
|
|
|
use tokio::signal;
|
|
|
|
use tower_http::timeout::TimeoutLayer;
|
2024-07-14 15:50:13 -04:00
|
|
|
use tower_http::trace::TraceLayer;
|
2024-07-14 15:34:14 -04:00
|
|
|
use tracing_subscriber::layer::SubscriberExt;
|
|
|
|
use tracing_subscriber::util::SubscriberInitExt;
|
2024-07-14 15:14:52 -04:00
|
|
|
|
2024-07-21 15:54:12 -04:00
|
|
|
use self::crd_pipeline_run::PipelineRun;
|
2024-09-28 18:34:16 -04:00
|
|
|
use self::discovery::discover_matching_push_triggers;
|
2024-09-28 16:51:27 -04:00
|
|
|
use self::discovery::discover_webhook_bridge_config;
|
2024-07-25 21:01:44 -04:00
|
|
|
use self::gitea_client::GiteaClient;
|
2024-09-28 19:43:26 -04:00
|
|
|
use self::hook_push::HookPush;
|
2024-09-28 21:50:50 -04:00
|
|
|
use self::hook_push::PipelineParamters;
|
2024-09-28 19:43:26 -04:00
|
|
|
use self::kubernetes::run_pipelines;
|
2024-09-28 22:32:20 -04:00
|
|
|
use self::remote_config::RemoteConfig;
|
2024-07-14 16:13:06 -04:00
|
|
|
use self::webhook::hook;
|
2024-07-15 21:02:30 -04:00
|
|
|
use self::webhook::verify_signature;
|
2024-07-21 15:54:12 -04:00
|
|
|
use kube::CustomResourceExt;
|
2024-07-14 16:13:06 -04:00
|
|
|
|
2024-07-21 15:54:12 -04:00
|
|
|
mod crd_pipeline_run;
|
2024-09-28 16:51:27 -04:00
|
|
|
mod discovery;
|
2024-07-25 21:01:44 -04:00
|
|
|
mod gitea_client;
|
2024-07-14 18:33:24 -04:00
|
|
|
mod hook_push;
|
2024-09-28 19:43:26 -04:00
|
|
|
mod kubernetes;
|
2024-09-28 22:32:20 -04:00
|
|
|
mod remote_config;
|
2024-07-14 16:13:06 -04:00
|
|
|
mod webhook;
|
|
|
|
|
2024-09-29 00:19:22 -04:00
|
|
|
const EXAMPLE_WEBHOOK_PAYLOAD: &'static str = include_str!("../example_tag_webhook_payload.json");
|
2024-07-21 15:54:12 -04:00
|
|
|
|
2024-07-14 15:14:52 -04:00
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
2024-07-14 15:34:14 -04:00
|
|
|
tracing_subscriber::registry()
|
|
|
|
.with(
|
|
|
|
tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| {
|
|
|
|
"webhook_bridge=info,tower_http=debug,axum::rejection=trace".into()
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
.with(tracing_subscriber::fmt::layer())
|
|
|
|
.init();
|
2024-07-14 23:08:10 -04:00
|
|
|
|
|
|
|
let kubernetes_client: Client = Client::try_default()
|
|
|
|
.await
|
|
|
|
.expect("Set KUBECONFIG to a valid kubernetes config.");
|
|
|
|
|
2024-07-25 21:01:44 -04:00
|
|
|
let gitea_api_root = std::env::var("WEBHOOK_BRIDGE_API_ROOT")?;
|
|
|
|
let gitea_api_token = std::env::var("WEBHOOK_BRIDGE_OAUTH_TOKEN")?;
|
|
|
|
let gitea = GiteaClient::new(gitea_api_root, gitea_api_token);
|
|
|
|
|
2024-09-28 21:50:50 -04:00
|
|
|
let webhook_payload: HookPush = serde_json::from_str(EXAMPLE_WEBHOOK_PAYLOAD)?;
|
2024-09-28 18:34:16 -04:00
|
|
|
let repo_tree = gitea
|
|
|
|
.get_tree(
|
|
|
|
"talexander",
|
|
|
|
"webhook_bridge",
|
2024-09-28 21:50:50 -04:00
|
|
|
webhook_payload.get_pull_base_sha()?,
|
2024-09-28 18:34:16 -04:00
|
|
|
)
|
|
|
|
.await?;
|
2024-09-28 22:32:20 -04:00
|
|
|
let remote_config = discover_webhook_bridge_config(&gitea, &repo_tree).await?;
|
2024-09-29 00:19:22 -04:00
|
|
|
let pipelines = discover_matching_push_triggers(
|
|
|
|
&gitea,
|
|
|
|
&repo_tree,
|
|
|
|
&webhook_payload.ref_field,
|
|
|
|
&remote_config,
|
|
|
|
)
|
|
|
|
.await?;
|
2024-07-25 21:01:44 -04:00
|
|
|
|
2024-09-28 19:43:26 -04:00
|
|
|
run_pipelines(webhook_payload, pipelines, kubernetes_client).await?;
|
2024-07-21 15:54:12 -04:00
|
|
|
|
2024-07-25 21:01:44 -04:00
|
|
|
// let app = Router::new()
|
|
|
|
// .route("/hook", post(hook))
|
|
|
|
// .layer(middleware::from_fn(verify_signature))
|
|
|
|
// .route("/health", get(health))
|
|
|
|
// .layer((
|
|
|
|
// TraceLayer::new_for_http(),
|
|
|
|
// // Add a timeout layer so graceful shutdown can't wait forever.
|
|
|
|
// TimeoutLayer::new(Duration::from_secs(600)),
|
|
|
|
// ))
|
|
|
|
// .with_state(AppState {
|
|
|
|
// kubernetes_client,
|
|
|
|
// gitea,
|
|
|
|
// });
|
|
|
|
|
|
|
|
// let listener = tokio::net::TcpListener::bind("0.0.0.0:9988").await?;
|
|
|
|
// tracing::info!("listening on {}", listener.local_addr().unwrap());
|
|
|
|
// axum::serve(listener, app)
|
|
|
|
// .with_graceful_shutdown(shutdown_signal())
|
|
|
|
// .await?;
|
2024-07-14 15:14:52 -04:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-07-25 20:04:30 -04:00
|
|
|
#[derive(Clone)]
|
|
|
|
struct AppState {
|
|
|
|
kubernetes_client: Client,
|
2024-07-25 21:01:44 -04:00
|
|
|
gitea: GiteaClient,
|
2024-07-25 20:04:30 -04:00
|
|
|
}
|
|
|
|
|
2024-07-14 19:01:10 -04:00
|
|
|
async fn shutdown_signal() {
|
|
|
|
let ctrl_c = async {
|
|
|
|
signal::ctrl_c()
|
|
|
|
.await
|
|
|
|
.expect("failed to install Ctrl+C handler");
|
|
|
|
};
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
let terminate = async {
|
|
|
|
signal::unix::signal(signal::unix::SignalKind::terminate())
|
|
|
|
.expect("failed to install signal handler")
|
|
|
|
.recv()
|
|
|
|
.await;
|
|
|
|
};
|
|
|
|
|
|
|
|
#[cfg(not(unix))]
|
|
|
|
let terminate = std::future::pending::<()>();
|
|
|
|
|
|
|
|
tokio::select! {
|
|
|
|
_ = ctrl_c => {},
|
|
|
|
_ = terminate => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-14 15:19:41 -04:00
|
|
|
async fn health() -> (StatusCode, Json<HealthResponse>) {
|
|
|
|
(StatusCode::OK, Json(HealthResponse { ok: true }))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
struct HealthResponse {
|
|
|
|
ok: bool,
|
2024-07-14 14:52:45 -04:00
|
|
|
}
|