Compare commits

..

2 Commits

Author SHA1 Message Date
31e59937dc Merge pull request 'feat: Initial setup for monitoring and alerting' (#48) from feat/monitor into master
Reviewed-on: NationTech/harmony#48
Reviewed-by: johnride <jg@nationtech.io>
2025-06-03 18:17:13 +00:00
60f2f31d6c feat: Add TenantScore and TenantInterpret (#45)
Reviewed-on: NationTech/harmony#45
Co-authored-by: Jean-Gabriel Gill-Couture <jg@nationtech.io>
Co-committed-by: Jean-Gabriel Gill-Couture <jg@nationtech.io>
2025-05-30 13:13:43 +00:00
7 changed files with 101 additions and 148 deletions

View File

@@ -10,3 +10,9 @@ impl Id {
Self { value }
}
}
impl std::fmt::Display for Id {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.value)
}
}

View File

@@ -20,6 +20,7 @@ pub enum InterpretName {
Panic,
OPNSense,
K3dInstallation,
TenantInterpret,
}
impl std::fmt::Display for InterpretName {
@@ -35,6 +36,7 @@ impl std::fmt::Display for InterpretName {
InterpretName::Panic => f.write_str("Panic"),
InterpretName::OPNSense => f.write_str("OPNSense"),
InterpretName::K3dInstallation => f.write_str("K3dInstallation"),
InterpretName::TenantInterpret => f.write_str("Tenant"),
}
}
}

View File

@@ -1,11 +1,10 @@
use std::{collections::HashMap, process::Command, sync::Arc};
use std::{process::Command, sync::Arc};
use async_trait::async_trait;
use inquire::Confirm;
use log::{info, warn};
use tokio::sync::{Mutex, OnceCell};
use tokio::sync::OnceCell;
use crate::score::Score;
use crate::{
executors::ExecutorError,
interpret::{InterpretError, Outcome},
@@ -18,7 +17,6 @@ use crate::{
use super::{
HelmCommand, K8sclient, Topology,
k8s::K8sClient,
oberservability::monitoring::{AlertReceiver, AlertReceiverProvision},
tenant::{
ResourceLimits, TenantConfig, TenantManager, TenantNetworkPolicy, k8s::K8sTenantManager,
},
@@ -39,7 +37,6 @@ enum K8sSource {
pub struct K8sAnywhereTopology {
k8s_state: OnceCell<Option<K8sState>>,
tenant_manager: OnceCell<K8sTenantManager>,
pub alert_receivers: Mutex<HashMap<String, OnceCell<AlertReceiver>>>,
}
#[async_trait]
@@ -64,29 +61,9 @@ impl K8sAnywhereTopology {
Self {
k8s_state: OnceCell::new(),
tenant_manager: OnceCell::new(),
alert_receivers: Mutex::new(HashMap::new()),
}
}
pub async fn initialize_alert_receiver<C>(
&self,
config: &C,
inventory: &Inventory,
) -> Result<AlertReceiver, InterpretError>
where
Self: Topology + HelmCommand,
C: AlertReceiverProvision<Self> + Send + Sync,
{
let score = config.get_deployment_score();
let interpret = score.create_interpret();
interpret.execute(inventory, self).await?;
Ok(AlertReceiver {
receiver_id: config.alert_receiver_id(),
receiver_installed: true,
})
}
fn is_helm_available(&self) -> Result<(), String> {
let version_result = Command::new("helm")
.arg("version")

View File

@@ -1,18 +1,14 @@
use async_trait::async_trait;
use dyn_clone::DynClone;
use serde::Serialize;
use std::fmt::Debug;
use url::Url;
use crate::interpret::InterpretError;
use crate::inventory::Inventory;
use crate::score::Score;
use crate::topology::HelmCommand;
use crate::{interpret::Outcome, topology::Topology};
/// Represents an entity responsible for collecting and organizing observability data
/// from various telemetry sources such as Prometheus or Datadog
/// from various telemetry sources
/// A `Monitor` abstracts the logic required to scrape, aggregate, and structure
/// monitoring data, enabling consistent processing regardless of the underlying data source.
#[async_trait]
@@ -30,26 +26,6 @@ pub trait Monitor<T: Topology>: Debug + Send + Sync {
) -> Result<Outcome, InterpretError>;
}
#[async_trait]
pub trait EnsureAlertReceiver<T: Topology>: Debug + DynClone + Send + Sync {
async fn ensure_alert_receiver(
&self,
inventory: Inventory,
topology: &T,
) -> Result<Outcome, InterpretError>;
}
dyn_clone::clone_trait_object!(<T> EnsureAlertReceiver<T>);
#[derive(Debug, Clone, Serialize)]
pub struct AlertReceiver {
pub receiver_id: String,
pub receiver_installed: bool,
}
/// Provides the ability to turn an alert config into an executable score
/// for the topology
pub trait AlertReceiverProvision<T: Topology + HelmCommand> {
fn get_deployment_score(&self) -> Box<dyn Score<T>>;
fn alert_receiver_id(&self) -> String;
}

View File

@@ -12,4 +12,5 @@ pub mod load_balancer;
pub mod monitoring;
pub mod okd;
pub mod opnsense;
pub mod tenant;
pub mod tftp;

View File

@@ -1,104 +1,32 @@
use super::discord_alert_manager::discord_alert_manager_score;
use async_trait::async_trait;
use serde::Serialize;
use serde_yaml::Value;
use tokio::sync::OnceCell;
use serde_json::Value;
use url::Url;
use crate::{
interpret::{Interpret, InterpretError, Outcome},
inventory::Inventory,
score::Score,
topology::{
HelmCommand, K8sAnywhereTopology, Topology,
oberservability::monitoring::{AlertReceiverProvision, EnsureAlertReceiver},
},
interpret::{InterpretError, Outcome},
topology::K8sAnywhereTopology,
};
#[derive(Debug, Clone, Serialize)]
#[derive(Debug, Clone)]
pub struct DiscordWebhookConfig {
pub webhook_url: Url,
pub name: String,
pub send_resolved_notifications: bool,
}
#[async_trait]
impl<T: Topology + DiscordWebhookReceiver> EnsureAlertReceiver<T> for DiscordWebhookConfig {
async fn ensure_alert_receiver(
&self,
inventory: Inventory,
topology: &T,
) -> Result<Outcome, InterpretError> {
topology
.ensure_discord_webhook_receiver(&inventory, self.clone())
.await
}
}
impl<T: Topology + HelmCommand> AlertReceiverProvision<T> for DiscordWebhookConfig {
fn get_deployment_score(&self) -> Box<dyn Score<T>> {
Box::new(DiscordWebhookReceiverScore {
config: self.clone(),
})
}
fn alert_receiver_id(&self) -> String {
self.name.clone()
}
}
#[async_trait]
pub trait DiscordWebhookReceiver {
async fn ensure_discord_webhook_receiver(
fn deploy_discord_webhook_receiver(
&self,
inventory: &Inventory,
config: DiscordWebhookConfig,
_notification_adapter_id: &str,
) -> Result<Outcome, InterpretError>;
fn delete_discord_webhook_receiver(
&self,
config: DiscordWebhookConfig,
) -> Result<Outcome, InterpretError>;
}
#[async_trait]
impl DiscordWebhookReceiver for K8sAnywhereTopology {
async fn ensure_discord_webhook_receiver(
&self,
inventory: &Inventory,
config: DiscordWebhookConfig,
) -> Result<Outcome, InterpretError> {
let receiver_key = config.name.clone();
let mut adapters_map_guard = self.alert_receivers.lock().await;
let cell = adapters_map_guard
.entry(receiver_key.clone())
.or_insert_with(OnceCell::new);
if let Some(initialized_receiver) = cell.get() {
return Ok(Outcome::success(format!(
"Discord Webhook adapter for '{}' already initialized.",
initialized_receiver.receiver_id
)));
}
let final_state = cell
.get_or_try_init(|| async { self.initialize_alert_receiver(&config, inventory).await })
.await?;
Ok(Outcome::success(format!(
"Discord Webhook Receiver for '{}' ensured/initialized.",
final_state.receiver_id
)))
}
fn delete_discord_webhook_receiver(
&self,
_config: DiscordWebhookConfig,
) -> Result<Outcome, InterpretError> {
todo!()
}
_notification_adapter_id: &str,
) -> Result<Outcome, InterpretError>;
}
// trait used to generate alert manager config values impl<T: Topology + AlertManagerConfig> Monitor for KubePrometheus
pub trait AlertManagerConfig<T> {
fn get_alert_manager_config(&self) -> Result<Value, InterpretError>;
}
@@ -110,22 +38,18 @@ impl<T: DiscordWebhookReceiver> AlertManagerConfig<T> for DiscordWebhookConfig {
}
}
#[derive(Debug, Clone, Serialize)]
struct DiscordWebhookReceiverScore {
config: DiscordWebhookConfig,
}
impl<T: Topology + HelmCommand> Score<T> for DiscordWebhookReceiverScore {
fn create_interpret(&self) -> Box<dyn Interpret<T>> {
discord_alert_manager_score(
self.config.webhook_url.clone(),
self.config.name.clone(),
self.config.name.clone(),
)
.create_interpret()
#[async_trait]
impl DiscordWebhookReceiver for K8sAnywhereTopology {
fn deploy_discord_webhook_receiver(
&self,
_notification_adapter_id: &str,
) -> Result<Outcome, InterpretError> {
todo!()
}
fn name(&self) -> String {
"DiscordWebhookReceiverScore".to_string()
fn delete_discord_webhook_receiver(
&self,
_notification_adapter_id: &str,
) -> Result<Outcome, InterpretError> {
todo!()
}
}

View File

@@ -0,0 +1,67 @@
use async_trait::async_trait;
use serde::Serialize;
use crate::{
data::{Id, Version},
interpret::{Interpret, InterpretError, InterpretName, InterpretStatus, Outcome},
inventory::Inventory,
score::Score,
topology::{
Topology,
tenant::{TenantConfig, TenantManager},
},
};
#[derive(Debug, Serialize, Clone)]
pub struct TenantScore {
config: TenantConfig,
}
impl<T: Topology + TenantManager> Score<T> for TenantScore {
fn create_interpret(&self) -> Box<dyn crate::interpret::Interpret<T>> {
Box::new(TenantInterpret {
tenant_config: self.config.clone(),
})
}
fn name(&self) -> String {
format!("{} TenantScore", self.config.name)
}
}
#[derive(Debug)]
pub struct TenantInterpret {
tenant_config: TenantConfig,
}
#[async_trait]
impl<T: Topology + TenantManager> Interpret<T> for TenantInterpret {
async fn execute(
&self,
_inventory: &Inventory,
topology: &T,
) -> Result<Outcome, InterpretError> {
topology.provision_tenant(&self.tenant_config).await?;
Ok(Outcome::success(format!(
"Successfully provisioned tenant {} with id {}",
self.tenant_config.name, self.tenant_config.id
)))
}
fn get_name(&self) -> InterpretName {
InterpretName::TenantInterpret
}
fn get_version(&self) -> Version {
todo!()
}
fn get_status(&self) -> InterpretStatus {
todo!()
}
fn get_children(&self) -> Vec<Id> {
todo!()
}
}