2.3 KiB · text History df2320f
use kube::Client;
use mutantops::{api, controller, metrics};
use std::net::SocketAddr;
use tracing::{error, info, warn};

/// Address the HTTP API listens on.
const DEFAULT_API_ADDR: &str = "0.0.0.0:8080";

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    tracing_subscriber::fmt::init();

    let client = Client::try_default().await?;

    // Requests about one Mutation are authorized by that Mutation's own
    // generated token. The admin token, optional, covers the rest: listing,
    // creating and deleting Mutations.
    let admin_token = std::env::var("MUTANT_API_TOKEN").ok().filter(|t| !t.is_empty());
    let insecure = std::env::var("MUTANT_API_INSECURE").is_ok_and(|v| v == "true");
    if insecure {
        warn!("MUTANT_API_INSECURE=true: HTTP API is serving without authentication");
    } else if admin_token.is_none() {
        info!("no MUTANT_API_TOKEN: only Mutation tokens are accepted");
    }

    let addr: SocketAddr = std::env::var("MUTANT_API_ADDR")
        .unwrap_or_else(|_| DEFAULT_API_ADDR.into())
        .parse()?;

    // Metrics get their own listener, which the API's Service does not name.
    // A failure here is logged, not fatal: the operator matters more than
    // its metrics.
    let metrics_addr: SocketAddr = std::env::var("MUTANT_METRICS_ADDR")
        .unwrap_or_else(|_| metrics::DEFAULT_METRICS_ADDR.into())
        .parse()?;
    tokio::spawn(async move {
        let res = async {
            let listener = tokio::net::TcpListener::bind(metrics_addr).await?;
            info!(addr = %metrics_addr, "metrics listening");
            metrics::serve(listener).await
        };
        if let Err(e) = res.await {
            error!(addr = %metrics_addr, "metrics listener failed: {e}");
        }
    });

    let server = async {
        let listener = tokio::net::TcpListener::bind(addr).await?;
        info!(%addr, "HTTP API listening");
        axum::serve(listener, api::router(api::AppState::new(client.clone(), admin_token, insecure)))
            .with_graceful_shutdown(async {
                let _ = tokio::signal::ctrl_c().await;
            })
            .await?;
        anyhow::Ok(())
    };

    // The controller stops on SIGINT/SIGTERM; when it does, so does the API.
    tokio::select! {
        () = controller::run(client.clone()) => {}
        res = server => res?,
    }
    Ok(())
}