feat: added the steps to install discord-webhook-receiver for k8s anywhere topology if not already installed #50

Closed
wjro wants to merge 6 commits from feat/discord-webhook-receiver into master
2 changed files with 35 additions and 18 deletions
Showing only changes of commit ea4709a409 - Show all commits

View File

@ -1,10 +1,12 @@
use async_trait::async_trait;
use dyn_clone::DynClone;
use serde::Serialize;
use std::fmt::Debug;
use crate::interpret::InterpretError;
use crate::inventory::Inventory;
use crate::{interpret::Outcome, topology::Topology};
/// Represents an entity responsible for collecting and organizing observability data
@ -26,6 +28,13 @@ 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,

View File

@ -1,5 +1,3 @@
use std::sync::Arc;
use super::discord_alert_manager::discord_alert_manager_score;
use async_trait::async_trait;
use serde::Serialize;
@ -13,7 +11,7 @@ use crate::{
inventory::Inventory,
score::Score,
topology::{
HelmCommand, K8sAnywhereTopology, Topology, oberservability::monitoring::AlertReceiver,
oberservability::monitoring::{AlertReceiver, EnsureAlertReceiver}, HelmCommand, K8sAnywhereTopology, Topology
},
};
@ -24,10 +22,18 @@ pub struct DiscordWebhookConfig {
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
}
}
#[async_trait]
pub trait DiscordWebhookReceiver {
async fn deploy_discord_webhook_receiver(
async fn ensure_discord_webhook_receiver(
&self,
inventory: &Inventory,
config: DiscordWebhookConfig,
) -> Result<Outcome, InterpretError>;
fn delete_discord_webhook_receiver(
@ -36,21 +42,11 @@ pub trait DiscordWebhookReceiver {
) -> Result<Outcome, InterpretError>;
}
pub trait AlertManagerConfig<T> {
fn get_alert_manager_config(&self) -> Result<Value, InterpretError>;
}
#[async_trait]
impl<T: DiscordWebhookReceiver> AlertManagerConfig<T> for DiscordWebhookConfig {
fn get_alert_manager_config(&self) -> Result<Value, InterpretError> {
todo!()
}
}
#[async_trait]
impl DiscordWebhookReceiver for K8sAnywhereTopology {
async fn deploy_discord_webhook_receiver(
async fn ensure_discord_webhook_receiver(
&self,
inventory: &Inventory,
config: DiscordWebhookConfig,
) -> Result<Outcome, InterpretError> {
let receiver_key = config.name.clone();
johnride marked this conversation as resolved Outdated

This should not be called deploy as this will not deploy every time it is called. We use "ensure" for this behavior.

This should not be called deploy as this will not deploy every time it is called. We use "ensure" for this behavior.
@ -69,7 +65,7 @@ impl DiscordWebhookReceiver for K8sAnywhereTopology {
let final_state = cell
.get_or_try_init(|| async {
initialize_discord_webhook_receiver(config.clone(), self).await
initialize_discord_webhook_receiver(config.clone(), inventory, self).await
})
.await?;
@ -89,6 +85,7 @@ impl DiscordWebhookReceiver for K8sAnywhereTopology {
async fn initialize_discord_webhook_receiver(
conf: DiscordWebhookConfig,
inventory: &Inventory,
topology: &K8sAnywhereTopology,
) -> Result<AlertReceiver, InterpretError> {
johnride marked this conversation as resolved Outdated

This function looks really weird, I feel like this is a hack to link the config and the dependency together.

The need to link them makes sense, I guess the webhook sender needs to know the webhook config when it is being deployed. But I am sure there can be a cleaner way than this.

This function looks really weird, I feel like this is a hack to link the config and the dependency together. The need to link them makes sense, I guess the webhook sender needs to know the webhook config when it is being deployed. But I am sure there can be a cleaner way than this.
println!(
@ -98,7 +95,6 @@ async fn initialize_discord_webhook_receiver(
let score = DiscordWebhookReceiverScore {
config: conf.clone(),
};
let inventory = Inventory::autoload();
let interpret = score.create_interpret();
interpret.execute(&inventory, topology).await?;
@ -108,6 +104,18 @@ async fn initialize_discord_webhook_receiver(
receiver_installed: true,
})
}
pub trait AlertManagerConfig<T> {
fn get_alert_manager_config(&self) -> Result<Value, InterpretError>;
}
#[async_trait]
impl<T: DiscordWebhookReceiver> AlertManagerConfig<T> for DiscordWebhookConfig {
fn get_alert_manager_config(&self) -> Result<Value, InterpretError> {
todo!()
}
}
#[derive(Debug, Clone, Serialize)]
struct DiscordWebhookReceiverScore {
config: DiscordWebhookConfig,