Skip to content

Commit

Permalink
Merge pull request #279 from jpmcb/apiserver-error-refactor
Browse files Browse the repository at this point in the history
Refactors `apiserver` errors to be module specific
  • Loading branch information
jpmcb authored Oct 11, 2022
2 parents df38392 + ea44780 commit 6dcf070
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 25 deletions.
3 changes: 2 additions & 1 deletion apiserver/src/api/drain.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::error;
use super::Result;
use super::{APIServerSettings, ApiserverCommonHeaders};
use crate::error::{self, Result};
use models::node::BottlerocketShadowClient;

use actix_web::{
Expand Down
12 changes: 0 additions & 12 deletions apiserver/src/error.rs → apiserver/src/api/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,9 @@ use models::node::BottlerocketShadowError;
use actix_web::error::ResponseError;
use snafu::Snafu;

/// The crate-wide result type.
pub type Result<T> = std::result::Result<T, Error>;

/// The crate-wide error type.
#[derive(Debug, Snafu)]
#[snafu(visibility(pub))]
pub enum Error {
#[snafu(display("Unable to create client: '{}'", source))]
ClientCreate { source: kube::Error },

#[snafu(display("Unable to parse HTTP header. Missing '{}'", missing_header))]
HTTPHeaderParse { missing_header: &'static str },

Expand All @@ -28,11 +21,6 @@ pub enum Error {
#[snafu(display("Error running HTTP server: '{}'", source))]
HttpServerError { source: std::io::Error },

#[snafu(display("Error configuring tracing: '{}'", source))]
TracingConfiguration {
source: tracing::subscriber::SetGlobalDefaultError,
},

#[snafu(display("The Kubernetes WATCH on Pod objects has failed."))]
KubernetesWatcherFailed {},

Expand Down
5 changes: 4 additions & 1 deletion apiserver/src/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! This module contains the brupop API server. Endpoints are stored in submodules, separated
//! by the resource on which they act.
mod drain;
pub mod error;
mod node;
mod ping;

Expand All @@ -11,7 +12,6 @@ use crate::{
HEADER_BRUPOP_NODE_NAME, HEADER_BRUPOP_NODE_UID, NODE_CORDON_AND_DRAIN_ENDPOINT,
NODE_RESOURCE_ENDPOINT, NODE_UNCORDON_ENDPOINT, REMOVE_NODE_EXCLUSION_TO_LB_ENDPOINT,
},
error::{self, Result},
telemetry,
};
use models::constants::{
Expand Down Expand Up @@ -46,6 +46,9 @@ use std::convert::TryFrom;
// The set of API endpoints for which `tracing::Span`s will not be recorded.
pub const NO_TELEMETRY_ENDPOINTS: &[&str] = &[APISERVER_HEALTH_CHECK_ROUTE];

/// The API module-wide result type.
type Result<T> = std::result::Result<T, error::Error>;

/// A struct containing information intended to be passed to the apiserver via HTTP headers.
pub(crate) struct ApiserverCommonHeaders {
pub node_selector: BottlerocketShadowSelector,
Expand Down
4 changes: 3 additions & 1 deletion apiserver/src/api/node.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use super::{APIServerSettings, ApiserverCommonHeaders};
use crate::error::{self, Result};
use crate::webhook::ConversionRequest;

use models::node::{BottlerocketShadowClient, BottlerocketShadowStatus};
Expand All @@ -14,6 +13,9 @@ use snafu::ResultExt;
use std::convert::TryFrom;
use tracing::{event, Level};

use super::error;
use super::Result;

/// HTTP endpoint which creates BottlerocketShadow custom resources on behalf of the caller.
pub(crate) async fn create_bottlerocket_shadow_resource<T: BottlerocketShadowClient>(
settings: web::Data<APIServerSettings<T>>,
Expand Down
2 changes: 0 additions & 2 deletions apiserver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ pub mod api;
#[cfg(feature = "server")]
mod auth;
#[cfg(feature = "server")]
pub mod error;
#[cfg(feature = "server")]
pub mod telemetry;

#[cfg(feature = "client")]
Expand Down
34 changes: 29 additions & 5 deletions apiserver/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use apiserver::api::{self, APIServerSettings};
use apiserver::error::{self, Result};
use apiserver::telemetry::init_telemetry;
use apiserver_error::{StartServerSnafu, StartTelemetrySnafu};
use models::constants::APISERVER_INTERNAL_PORT;
use models::node::K8SBottlerocketShadowClient;
use tracing::{event, Level};
Expand Down Expand Up @@ -28,18 +28,42 @@ async fn main() {
opentelemetry::global::shutdown_tracer_provider();
}

async fn run_server() -> Result<()> {
init_telemetry()?;
async fn run_server() -> Result<(), apiserver_error::Error> {
init_telemetry().context(StartTelemetrySnafu)?;

let prometheus_exporter = opentelemetry_prometheus::exporter().init();

let k8s_client = kube::client::Client::try_default()
.await
.context(error::ClientCreateSnafu)?;
.context(apiserver_error::K8sClientCreateSnafu)?;

let settings = APIServerSettings {
node_client: K8SBottlerocketShadowClient::new(k8s_client.clone()),
server_port: APISERVER_INTERNAL_PORT as u16,
};

api::run_server(settings, k8s_client, Some(prometheus_exporter)).await
api::run_server(settings, k8s_client, Some(prometheus_exporter))
.await
.context(StartServerSnafu)
}

pub mod apiserver_error {
use snafu::Snafu;

#[derive(Debug, Snafu)]
#[snafu(visibility(pub))]
pub enum Error {
#[snafu(display("Unable to create client: '{}'", source))]
K8sClientCreate { source: kube::Error },

#[snafu(display("Unable to start API server telemetry: '{}'", source))]
StartTelemetry {
source: apiserver::telemetry::telemetry_error::Error,
},

#[snafu(display("Unable to start API server: '{}'", source))]
StartServer {
source: apiserver::api::error::Error,
},
}
}
18 changes: 15 additions & 3 deletions apiserver/src/telemetry.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::api::NO_TELEMETRY_ENDPOINTS;
use crate::constants::HEADER_BRUPOP_NODE_NAME;
use crate::error::{self, Result};
use models::constants::APISERVER;

use actix_web::dev::{ServiceRequest, ServiceResponse};
Expand Down Expand Up @@ -55,7 +54,7 @@ impl RootSpanBuilder for BrupopApiserverRootSpanBuilder {
}

/// Initializes global tracing and telemetry state for the apiserver.
pub fn init_telemetry() -> Result<()> {
pub fn init_telemetry() -> Result<(), telemetry_error::Error> {
opentelemetry::global::set_text_map_propagator(TraceContextPropagator::new());

let env_filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info"));
Expand All @@ -65,7 +64,20 @@ pub fn init_telemetry() -> Result<()> {
.with(JsonStorageLayer)
.with(stdio_formatting_layer);
tracing::subscriber::set_global_default(subscriber)
.context(error::TracingConfigurationSnafu)?;
.context(telemetry_error::TracingConfigurationSnafu)?;

Ok(())
}

pub mod telemetry_error {
use snafu::Snafu;

#[derive(Debug, Snafu)]
#[snafu(visibility(pub))]
pub enum Error {
#[snafu(display("Error configuring tracing: '{}'", source))]
TracingConfiguration {
source: tracing::subscriber::SetGlobalDefaultError,
},
}
}

0 comments on commit 6dcf070

Please sign in to comment.