fix: correctly pass namespace and monitoring stack to topology so it can be used to init the maestro and exec the score

This commit is contained in:
Willem 2025-05-06 10:02:21 -04:00
parent 88270ece61
commit 472a3c1051
2 changed files with 58 additions and 13 deletions

View File

@ -15,24 +15,32 @@ use crate::{
inventory::Inventory,
maestro::Maestro,
modules::monitoring::monitoring_alerting::MonitoringAlertingStackScore,
score::Score,
};
use super::{HelmCommand, Topology};
use super::{HelmCommand, K8sAnywhereTopology, Topology};
#[derive(Clone, Debug)]
struct MonitoringState {
message: String,
}
#[derive(Clone, Debug)]
#[derive(Debug)]
pub struct MonitoringAlertingTopology {
monitoring_state: OnceCell<Option<MonitoringState>>,
namespace: String,
monitoring_stack: Vec<Box<dyn Score<MonitoringAlertingTopology>>>,
}
impl MonitoringAlertingTopology {
pub fn new() -> Self {
pub fn new(
namespace: String,
monitoring_stack: Vec<Box<dyn Score<MonitoringAlertingTopology>>>,
) -> Self {
Self {
monitoring_state: OnceCell::new(),
namespace,
monitoring_stack,
}
}
@ -78,7 +86,7 @@ impl MonitoringAlertingTopology {
&self,
) -> Result<Option<MonitoringState>, InterpretError> {
let inventory = Inventory::autoload();
let topology = MonitoringAlertingTopology::new();
let topology = K8sAnywhereTopology::new();
let mut maestro = match Maestro::initialize(inventory, topology).await {
Ok(m) => m,
Err(e) => {
@ -86,7 +94,7 @@ impl MonitoringAlertingTopology {
std::process::exit(1);
}
};
maestro.register_all(vec![Box::new(MonitoringAlertingStackScore::default())]);
maestro.register_all(vec![Box::new(MonitoringAlertingStackScore::new(self.monitoring_stack.clone(), self.namespace.clone()))]);
let state = match self.get_monitoring_state().await {
Ok(_) => MonitoringState {
message: "Monitoring Stack Ready".to_string(),
@ -98,6 +106,12 @@ impl MonitoringAlertingTopology {
}
impl<T: Topology> Clone for Box<dyn Score<T>> {
fn clone(&self) -> Box<dyn Score<T>> {
self.clone_box()
}
}
#[async_trait]
impl Topology for MonitoringAlertingTopology {
fn name(&self) -> &str {
@ -108,7 +122,8 @@ impl Topology for MonitoringAlertingTopology {
let state = if let Some(state) = self.get_monitoring_state().await? {
state
} else {
self.try_install_monitoring_stack().await?
self.try_install_monitoring_stack()
.await?
.ok_or_else(|| InterpretError::new("Failed to install monitoring stack".into()))?
};

View File

@ -13,10 +13,16 @@ use crate::{
use super::kube_prometheus::kube_prometheus_score;
#[derive(Debug)]
pub struct MonitoringAlertingStackScore {
pub monitoring_stack: Vec<Box<dyn Score<MonitoringAlertingTopology>>>,
pub namespace: String,
}
impl MonitoringAlertingStackScore {
pub fn new(monitoring_stack: Vec<Box<dyn Score<MonitoringAlertingTopology>>>, namespace: String) -> Self {
Self { monitoring_stack, namespace }
}
}
impl Default for MonitoringAlertingStackScore {
@ -24,15 +30,21 @@ impl Default for MonitoringAlertingStackScore {
let ns = "monitoring";
Self {
monitoring_stack: vec![
Box::new(kube_prometheus_score(ns)) as Box<dyn Score<MonitoringAlertingTopology>>,
Box::new(kube_prometheus_score(ns)) as Box<dyn Score<MonitoringAlertingTopology>>
],
namespace: ns.to_string()
}
}
}
impl Clone for MonitoringAlertingStackScore {
fn clone(&self) -> Self {
Self {
monitoring_stack: self.monitoring_stack.iter().map(|s| s.clone_box()).collect(),
monitoring_stack: self
.monitoring_stack
.iter()
.map(|s| s.clone_box())
.collect(),
namespace: self.namespace.clone(),
}
}
}
@ -44,7 +56,11 @@ impl Serialize for MonitoringAlertingStackScore {
{
use serde::ser::SerializeStruct;
let mut s = serializer.serialize_struct("MonitoringAlertingStackScore", 1)?;
let monitoring_values: Vec<_> = self.monitoring_stack.iter().map(|m| m.serialize()).collect();
let monitoring_values: Vec<_> = self
.monitoring_stack
.iter()
.map(|m| m.serialize())
.collect();
s.serialize_field("monitoring", &monitoring_values)?;
s.end()
}
@ -54,7 +70,12 @@ impl<T: Topology> Score<T> for MonitoringAlertingStackScore {
fn create_interpret(&self) -> Box<dyn Interpret<T>> {
Box::new(MonitoringAlertingStackInterpret {
score: MonitoringAlertingStackScore {
monitoring_stack: self.monitoring_stack.iter().map(|s| s.clone_box()).collect(),
monitoring_stack: self
.monitoring_stack
.iter()
.map(|s| s.clone_box())
.collect(),
namespace: self.namespace.clone(),
},
})
}
@ -69,7 +90,6 @@ struct MonitoringAlertingStackInterpret {
pub score: MonitoringAlertingStackScore,
}
#[async_trait]
impl<T: Topology> Interpret<T> for MonitoringAlertingStackInterpret {
async fn execute(
@ -77,8 +97,18 @@ impl<T: Topology> Interpret<T> for MonitoringAlertingStackInterpret {
_inventory: &Inventory,
_topology: &T,
) -> Result<Outcome, InterpretError> {
todo!()
let inventory = Inventory::autoload();
let topology = MonitoringAlertingTopology::new(self.score.namespace.clone(), self.score.monitoring_stack.clone());
let mut maestro = match Maestro::initialize(inventory, topology).await {
Ok(m) => m,
Err(e) => {
println!("failed to initialize Maestro: {}", e);
std::process::exit(1);
}
};
Ok(Outcome::success(format!("monitoring stack installed in {} namespace",self.score.namespace )))
}
fn get_name(&self) -> InterpretName {
todo!()