Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve controller error to be module-wide #223

Merged
merged 1 commit into from
Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 37 additions & 5 deletions controller/src/controller.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use super::{
gthao313 marked this conversation as resolved.
Show resolved Hide resolved
error::{self, Result},
metrics::{BrupopControllerMetrics, BrupopHostsData},
statemachine::determine_next_node_spec,
};
Expand All @@ -20,6 +19,9 @@ use tracing::{event, instrument, Level};
// Defines the length time after which the controller will take actions.
const ACTION_INTERVAL: Duration = Duration::from_secs(2);

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

/// The BrupopController orchestrates updates across a cluster of Bottlerocket nodes.
pub struct BrupopController<T: BottlerocketShadowClient> {
node_client: T,
Expand Down Expand Up @@ -89,11 +91,13 @@ impl<T: BottlerocketShadowClient> BrupopController<T> {

self.node_client
.update_node_spec(
&node.selector().context(error::NodeSelectorCreation)?,
&node
.selector()
.context(controllerclient_error::NodeSelectorCreation)?,
&desired_spec,
)
.await
.context(error::UpdateNodeSpec)
.context(controllerclient_error::UpdateNodeSpec)
} else {
// Otherwise, we need to ensure that the node is making progress in a timely fashion.

Expand Down Expand Up @@ -149,7 +153,11 @@ impl<T: BottlerocketShadowClient> BrupopController<T> {

*hosts_version_count_map.entry(current_version).or_default() += 1;
*hosts_state_count_map
.entry(serde_plain::to_string(&current_state).context(error::Assertion)?)
.entry(serde_plain::to_string(&current_state).context(
controllerclient_error::Assertion {
msg: "unable to parse current_state".to_string(),
},
)?)
.or_default() += 1;
}
}
Expand Down Expand Up @@ -209,7 +217,8 @@ impl<T: BottlerocketShadowClient> BrupopController<T> {
// Get node and BottlerocketShadow names
#[instrument]
fn get_associated_bottlerocketshadow_name() -> Result<String> {
let associated_node_name = env::var("MY_NODE_NAME").context(error::GetNodeName)?;
let associated_node_name =
env::var("MY_NODE_NAME").context(controllerclient_error::GetNodeName)?;
let associated_bottlerocketshadow_name = brs_name_from_node_name(&associated_node_name);

event!(
Expand Down Expand Up @@ -388,3 +397,26 @@ pub(crate) mod test {
assert_eq!(test_shadows, expected_result);
}
}
pub mod controllerclient_error {
use models::node::BottlerocketShadowError;
use snafu::Snafu;

#[derive(Debug, Snafu)]
#[snafu(visibility = "pub")]
pub enum Error {
#[snafu(display("Controller failed due to {}: '{}'", msg, source))]
Assertion {
msg: String,
source: serde_plain::Error,
},

#[snafu(display("Unable to get host controller pod node name: {}", source))]
GetNodeName { source: std::env::VarError },

#[snafu(display("Failed to update node spec via kubernetes API: '{}'", source))]
UpdateNodeSpec { source: BottlerocketShadowError },

#[snafu(display("Could not determine selector for node: '{}'", source))]
NodeSelectorCreation { source: BottlerocketShadowError },
}
}
43 changes: 0 additions & 43 deletions controller/src/error.rs

This file was deleted.

1 change: 0 additions & 1 deletion controller/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
mod controller;
mod metrics;

pub mod error;
pub mod statemachine;
pub mod telemetry;

Expand Down
35 changes: 27 additions & 8 deletions controller/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
use controller::{
error::{self, Result},
telemetry::vending_metrics,
BrupopController,
};
use controller::{telemetry::vending_metrics, BrupopController};
use models::{
constants::{CONTROLLER, CONTROLLER_INTERNAL_PORT, NAMESPACE},
node::{BottlerocketShadow, K8SBottlerocketShadowClient},
Expand All @@ -24,13 +20,16 @@ use tracing_subscriber::{layer::SubscriberExt, EnvFilter, Registry};

const DEFAULT_TRACE_LEVEL: &str = "info";

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

#[actix_web::main]
async fn main() -> Result<()> {
init_telemetry()?;

let k8s_client = kube::client::Client::try_default()
.await
.context(error::ClientCreate)?;
.context(controller_error::ClientCreate)?;

// The `BrupopController` needs a `reflector::Store`, which is updated by a reflector
// that runs concurrently. We'll create the store and run the reflector here.
Expand Down Expand Up @@ -68,7 +67,7 @@ async fn main() -> Result<()> {
.service(vending_metrics)
})
.bind(format!("0.0.0.0:{}", CONTROLLER_INTERNAL_PORT))
.context(error::PrometheusServerError)?
.context(controller_error::PrometheusServerError)?
.run();

// TODO if any of these fails, we should write to the k8s termination log and exit.
Expand Down Expand Up @@ -96,7 +95,27 @@ fn init_telemetry() -> Result<()> {
.with(env_filter)
.with(JsonStorageLayer)
.with(stdio_formatting_layer);
tracing::subscriber::set_global_default(subscriber).context(error::TracingConfiguration)?;
tracing::subscriber::set_global_default(subscriber)
.context(controller_error::TracingConfiguration)?;

Ok(())
}

pub mod controller_error {
use snafu::Snafu;

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

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

#[snafu(display("Error running prometheus HTTP server: '{}'", source))]
PrometheusServerError { source: std::io::Error },
}
}