Pass the kubernetes client as state.

This commit is contained in:
Tom Alexander 2024-07-25 20:04:30 -04:00
parent 6c15fc00b4
commit 3e3acbab7d
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
2 changed files with 17 additions and 6 deletions

View File

@ -1,4 +1,5 @@
#![forbid(unsafe_code)]
use std::sync::Arc;
use std::time::Duration;
use axum::http::StatusCode;
@ -43,14 +44,14 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.await
.expect("Set KUBECONFIG to a valid kubernetes config.");
let jobs: Api<PipelineRun> = Api::namespaced(kubernetes_client, "lighthouse");
// let jobs: Api<PipelineRun> = Api::namespaced(kubernetes_client, "lighthouse");
// let jobs: Api<PipelineRun> = Api::default_namespaced(kubernetes_client);
tracing::info!("Using crd: {}", serde_json::to_string(&PipelineRun::crd())?);
// tracing::info!("Using crd: {}", serde_json::to_string(&PipelineRun::crd())?);
let test_run: PipelineRun = serde_json::from_str(EXAMPLE_PIPELINE_RUN)?;
// let test_run: PipelineRun = serde_json::from_str(EXAMPLE_PIPELINE_RUN)?;
let pp = PostParams::default();
let created_run = jobs.create(&pp, &test_run).await?;
// let pp = PostParams::default();
// let created_run = jobs.create(&pp, &test_run).await?;
let app = Router::new()
.route("/hook", post(hook))
@ -60,7 +61,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
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 });
let listener = tokio::net::TcpListener::bind("0.0.0.0:9988").await?;
tracing::info!("listening on {}", listener.local_addr().unwrap());
@ -70,6 +72,11 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}
#[derive(Clone)]
struct AppState {
kubernetes_client: Client,
}
async fn shutdown_signal() {
let ctrl_c = async {
signal::ctrl_c()

View File

@ -1,10 +1,12 @@
use std::future::Future;
use std::sync::Arc;
use axum::async_trait;
use axum::body::Body;
use axum::body::Bytes;
use axum::extract::FromRequest;
use axum::extract::Request;
use axum::extract::State;
use axum::http::HeaderMap;
use axum::http::StatusCode;
use axum::middleware::Next;
@ -21,11 +23,13 @@ use sha2::Sha256;
use tracing::debug;
use crate::hook_push::HookPush;
use crate::AppState;
type HmacSha256 = Hmac<Sha256>;
pub(crate) async fn hook(
_headers: HeaderMap,
State(state): State<AppState>,
payload: HookRequest,
) -> (StatusCode, Json<HookResponse>) {
debug!("REQ: {:?}", payload);