diff --git a/Cargo.lock b/Cargo.lock index cad5a01..6e03b56 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -90,12 +90,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3b829e4e32b91e643de6eafe82b1d90675f5874230191a4ffbc1b336dec4d6bf" dependencies = [ "async-trait", - "axum-core 0.3.4", + "axum-core", "bitflags 1.3.2", "bytes", "futures-util", - "http 0.2.9", - "http-body 0.4.5", + "http", + "http-body", "hyper", "itoa", "matchit", @@ -115,33 +115,6 @@ dependencies = [ "tower-service", ] -[[package]] -name = "axum" -version = "0.7.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "810a80b128d70e6ed2bdf3fe8ed72c0ae56f5f5948d01c2753282dd92a84fce8" -dependencies = [ - "async-trait", - "axum-core 0.4.0", - "bytes", - "futures-util", - "http 1.0.0", - "http-body 1.0.0", - "http-body-util", - "itoa", - "matchit", - "memchr", - "mime", - "percent-encoding", - "pin-project-lite", - "rustversion", - "serde", - "sync_wrapper", - "tower", - "tower-layer", - "tower-service", -] - [[package]] name = "axum-core" version = "0.3.4" @@ -151,71 +124,14 @@ dependencies = [ "async-trait", "bytes", "futures-util", - "http 0.2.9", - "http-body 0.4.5", - "mime", - "rustversion", - "tower-layer", - "tower-service", -] - -[[package]] -name = "axum-core" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "de0ddc355eab88f4955090a823715df47acf0b7660aab7a69ad5ce6301ee3b73" -dependencies = [ - "async-trait", - "bytes", - "futures-util", - "http 1.0.0", - "http-body 1.0.0", - "http-body-util", + "http", + "http-body", "mime", - "pin-project-lite", "rustversion", - "sync_wrapper", - "tower-layer", - "tower-service", -] - -[[package]] -name = "axum-extra" -version = "0.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "523ae92256049a3b02d3bb4df80152386cd97ddba0c8c5077619bdc8c4b1859b" -dependencies = [ - "axum 0.7.1", - "axum-core 0.4.0", - "axum-macros", - "bytes", - "form_urlencoded", - "futures-util", - "http 1.0.0", - "http-body 1.0.0", - "http-body-util", - "mime", - "percent-encoding", - "pin-project-lite", - "serde", - "serde_html_form", - "tower", "tower-layer", "tower-service", ] -[[package]] -name = "axum-macros" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a2edad600410b905404c594e2523549f1bcd4bded1e252c8f74524ccce0b867" -dependencies = [ - "heck", - "proc-macro2", - "quote", - "syn 2.0.38", -] - [[package]] name = "backoff" version = "0.4.0" @@ -531,12 +447,6 @@ dependencies = [ "allocator-api2", ] -[[package]] -name = "heck" -version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" - [[package]] name = "hermit-abi" version = "0.3.3" @@ -563,17 +473,6 @@ dependencies = [ "itoa", ] -[[package]] -name = "http" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b32afd38673a8016f7c9ae69e5af41a58f81b1d31689040f2f1959594ce194ea" -dependencies = [ - "bytes", - "fnv", - "itoa", -] - [[package]] name = "http-body" version = "0.4.5" @@ -581,30 +480,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d5f38f16d184e36f2408a55281cd658ecbd3ca05cce6d6510a176eca393e26d1" dependencies = [ "bytes", - "http 0.2.9", - "pin-project-lite", -] - -[[package]] -name = "http-body" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1cac85db508abc24a2e48553ba12a996e87244a0395ce011e62b37158745d643" -dependencies = [ - "bytes", - "http 1.0.0", -] - -[[package]] -name = "http-body-util" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41cb79eb393015dadd30fc252023adb0b2400a0caee0fa2a077e6e21a551e840" -dependencies = [ - "bytes", - "futures-util", - "http 1.0.0", - "http-body 1.0.0", + "http", "pin-project-lite", ] @@ -636,8 +512,8 @@ dependencies = [ "futures-channel", "futures-core", "futures-util", - "http 0.2.9", - "http-body 0.4.5", + "http", + "http-body", "httparse", "httpdate", "itoa", @@ -656,7 +532,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec3efd23720e2049821a693cbc7e65ea87c72f1c58ff2f9522ff332b1491e590" dependencies = [ "futures-util", - "http 0.2.9", + "http", "hyper", "log", "rustls", @@ -797,8 +673,8 @@ dependencies = [ "either", "futures", "home", - "http 0.2.9", - "http-body 0.4.5", + "http", + "http-body", "hyper", "hyper-rustls", "hyper-timeout", @@ -829,7 +705,7 @@ checksum = "b5bba93d054786eba7994d03ce522f368ef7d48c88a1826faa28478d85fb63ae" dependencies = [ "chrono", "form_urlencoded", - "http 0.2.9", + "http", "json-patch", "k8s-openapi", "once_cell", @@ -1366,19 +1242,6 @@ dependencies = [ "syn 2.0.38", ] -[[package]] -name = "serde_html_form" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cde65b75f2603066b78d6fa239b2c07b43e06ead09435f60554d3912962b4a3c" -dependencies = [ - "form_urlencoded", - "indexmap", - "itoa", - "ryu", - "serde", -] - [[package]] name = "serde_json" version = "1.0.108" @@ -1644,8 +1507,8 @@ dependencies = [ "bytes", "futures-core", "futures-util", - "http 0.2.9", - "http-body 0.4.5", + "http", + "http-body", "http-range-header", "mime", "pin-project-lite", @@ -1780,8 +1643,7 @@ name = "version" version = "1.16.2" dependencies = [ "anyhow", - "axum 0.6.20", - "axum-extra", + "axum", "elegant-departure", "futures", "k8s-openapi", diff --git a/Cargo.toml b/Cargo.toml index 2c7bb9e..bffb524 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,6 @@ path = "version.rs" [dependencies] axum = "0.6.20" -axum-extra = { version = "0.9.0", features = ["typed-routing"] } tower-http = { version = "0.4.4", default-features = false, features = ["trace"] } futures = "0.3.30" tokio = { version = "1.35.1", features = ["macros", "rt-multi-thread", "signal"] } diff --git a/justfile b/justfile index 5f61b8c..d232b86 100644 --- a/justfile +++ b/justfile @@ -3,7 +3,7 @@ default: @just --list --unsorted run: - RUST_LOG=info,kube=debug,version=debug cargo run + RUST_LOG=debug,hyper=info,rustls=info cargo run fmt: cargo +nightly fmt diff --git a/rustfmt.toml b/rustfmt.toml index d570c9a..c3b076d 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -1,6 +1,5 @@ overflow_delimited_expr = true newline_style = "Unix" -imports_granularity = "Crate" reorder_impl_items = true blank_lines_upper_bound = 2 comment_width = 110 diff --git a/version.rs b/version.rs index 83b2dd6..3ddc4d2 100644 --- a/version.rs +++ b/version.rs @@ -1,12 +1,10 @@ -use axum::{extract::State, http::StatusCode, response::IntoResponse, routing, Json, Router}; -use axum_extra::routing::TypedPath; +use axum::extract::{Path, State}; +use axum::{http::StatusCode, response::IntoResponse, routing, Json, Router}; use futures::{future, StreamExt}; use k8s_openapi::api::apps::v1::Deployment; -use kube::{ - runtime::{reflector, watcher, WatchStreamExt}, - Api, Client, ResourceExt, -}; -use tracing::{debug, info, instrument, warn, Level}; +use kube::runtime::{reflector, watcher, WatchStreamExt}; +use kube::{Api, Client, ResourceExt}; +use tracing::{debug, info, warn}; #[derive(serde::Serialize, Clone)] struct Entry { @@ -31,35 +29,29 @@ fn deployment_to_entry(d: &Deployment) -> Option { Some(Entry { name, namespace, container, version }) } -#[instrument(skip(store))] -async fn get_versions(State(store): State) -> Json> { - let data = store.state().iter().filter_map(|d| deployment_to_entry(d)).collect(); - Json(data) -} - -#[derive(TypedPath, serde::Deserialize, Debug)] -#[typed_path("/versions/:namespace/:name")] +// - GET /versions/:namespace/:name +#[derive(serde::Deserialize, Debug)] struct EntryPath { name: String, namespace: String, } - -#[instrument(skip(store))] -async fn get_version(State(store): State, path: EntryPath) -> impl IntoResponse { - let key = reflector::ObjectRef::new(&path.name).within(&path.namespace); +async fn get_version(State(store): State, Path(p): Path) -> impl IntoResponse { + let key = reflector::ObjectRef::new(&p.name).within(&p.namespace); if let Some(Some(e)) = store.get(&key).map(|d| deployment_to_entry(&d)) { return Ok(Json(e)); } Err((StatusCode::NOT_FOUND, "not found")) } -async fn health() -> impl IntoResponse { - Json("healthy") +// - GET /versions +async fn get_versions(State(store): State) -> Json> { + let data = store.state().iter().filter_map(|d| deployment_to_entry(d)).collect(); + Json(data) } #[tokio::main] async fn main() -> anyhow::Result<()> { - tracing_subscriber::fmt().with_max_level(Level::DEBUG).init(); + tracing_subscriber::fmt::init(); let client = Client::try_default().await?; let api: Api = Api::all(client); @@ -67,19 +59,20 @@ async fn main() -> anyhow::Result<()> { let watch = reflector(writer, watcher(api, Default::default())) .default_backoff() .touched_objects() - .filter_map(|x| async move { Result::ok(x) }) - .for_each(|o| { - debug!("Saw {} in {}", o.name_any(), o.namespace().unwrap()); - future::ready(()) + .for_each(|r| { + future::ready(match r { + Ok(o) => debug!("Saw {} in {}", o.name_any(), o.namespace().unwrap()), + Err(e) => warn!("watcher error: {e}"), + }) }); let app = Router::new() .route("/versions", routing::get(get_versions)) .route("/versions/:namespace/:name", routing::get(get_version)) - .with_state(reader) + .with_state(reader) // routes can read from the reflector store .layer(tower_http::trace::TraceLayer::new_for_http()) // NB: routes added after TraceLayer are not traced - .route("/health", routing::get(health)); + .route("/health", routing::get(|| async { "up" })); let server = axum::Server::bind(&std::net::SocketAddr::from(([0, 0, 0, 0], 8000))) .serve(app.into_make_service())