diff --git a/Cargo.lock b/Cargo.lock index 2769781..3a47a6d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1160,6 +1160,7 @@ version = "0.1.0" dependencies = [ "harmony", "harmony_cli", + "harmony_macros", "tokio", "url", ] diff --git a/examples/monitoring/Cargo.toml b/examples/monitoring/Cargo.toml index d188b78..1c35330 100644 --- a/examples/monitoring/Cargo.toml +++ b/examples/monitoring/Cargo.toml @@ -8,5 +8,6 @@ license.workspace = true [dependencies] harmony = { version = "0.1.0", path = "../../harmony" } harmony_cli = { version = "0.1.0", path = "../../harmony_cli" } +harmony_macros = { version = "0.1.0", path = "../../harmony_macros" } tokio.workspace = true url.workspace = true diff --git a/examples/monitoring/src/main.rs b/examples/monitoring/src/main.rs index c2522f3..e7d5f22 100644 --- a/examples/monitoring/src/main.rs +++ b/examples/monitoring/src/main.rs @@ -18,6 +18,8 @@ use harmony::{ topology::{K8sAnywhereTopology, Url}, }; +use harmony_macros::http_scheme; + #[tokio::main] async fn main() { let discord_receiver = DiscordWebhook { @@ -41,9 +43,34 @@ async fn main() { ], ); + let service_monitor_endpoint = ServiceMonitorEndpoint { + port: Some(80), + target_port: None, + bearer_token_file: None, + interval: None, + path: "/metrics".to_string(), + scheme: http_scheme!("http"), + tls_config: None, + metric_relabelings: vec![], + relabelings: vec![], + }; + + let service_monitor = ServiceMonitor { + name: "test service monitor".to_string(), + additional_labels: None, + job_label: None, + target_labels: vec![], + pod_target_labels: vec![], + selector: None, + match_labels: None, + namespace_selector: None, + endpoints: vec![service_monitor_endpoint], + fallback_scrape_protocol: None, + }; let alerting_score = HelmPrometheusAlertingScore { receivers: vec![Box::new(discord_receiver)], rules: vec![Box::new(additional_rules), Box::new(additional_rules2)], + service_monitors: vec![service_monitor], }; let mut maestro = Maestro::::initialize( Inventory::autoload(), diff --git a/harmony/src/modules/monitoring/kube_prometheus/helm/kube_prometheus_helm_chart.rs b/harmony/src/modules/monitoring/kube_prometheus/helm/kube_prometheus_helm_chart.rs index f36b991..8b86b88 100644 --- a/harmony/src/modules/monitoring/kube_prometheus/helm/kube_prometheus_helm_chart.rs +++ b/harmony/src/modules/monitoring/kube_prometheus/helm/kube_prometheus_helm_chart.rs @@ -101,12 +101,14 @@ nodeExporter: enabled: {node_exporter} prometheusOperator: enabled: {prometheus_operator} + "#, ); + let prometheus_config = crate::modules::monitoring::kube_prometheus::types::PrometheusConfigValues { prometheus: PrometheusConfig { - prometheus, + prometheus: bool::from_str(prometheus.as_str()).expect("couldn't parse bool"), additional_service_monitors: config.additional_service_monitors.clone(), }, }; diff --git a/harmony/src/modules/monitoring/kube_prometheus/helm_prometheus_alert_score.rs b/harmony/src/modules/monitoring/kube_prometheus/helm_prometheus_alert_score.rs index 8844309..954f6b9 100644 --- a/harmony/src/modules/monitoring/kube_prometheus/helm_prometheus_alert_score.rs +++ b/harmony/src/modules/monitoring/kube_prometheus/helm_prometheus_alert_score.rs @@ -4,6 +4,7 @@ use serde::Serialize; use super::{helm::config::KubePrometheusConfig, prometheus::Prometheus}; use crate::{ + modules::monitoring::kube_prometheus::types::ServiceMonitor, score::Score, topology::{ HelmCommand, Topology, @@ -15,14 +16,18 @@ use crate::{ pub struct HelmPrometheusAlertingScore { pub receivers: Vec>>, pub rules: Vec>>, + pub service_monitors: Vec, } impl Score for HelmPrometheusAlertingScore { fn create_interpret(&self) -> Box> { + let config = Arc::new(Mutex::new(KubePrometheusConfig::new())); + config + .try_lock() + .expect("couldn't lock config") + .additional_service_monitors = self.service_monitors.clone(); Box::new(AlertingInterpret { - sender: Prometheus { - config: Arc::new(Mutex::new(KubePrometheusConfig::new())), - }, + sender: Prometheus { config }, receivers: self.receivers.clone(), rules: self.rules.clone(), }) diff --git a/harmony/src/modules/monitoring/kube_prometheus/types.rs b/harmony/src/modules/monitoring/kube_prometheus/types.rs index 874f953..d3942ba 100644 --- a/harmony/src/modules/monitoring/kube_prometheus/types.rs +++ b/harmony/src/modules/monitoring/kube_prometheus/types.rs @@ -64,7 +64,7 @@ pub struct PrometheusConfigValues { #[derive(Debug, Clone, Serialize)] #[serde(rename_all = "camelCase")] pub struct PrometheusConfig { - pub prometheus: String, + pub prometheus: bool, pub additional_service_monitors: Vec, } @@ -73,23 +73,23 @@ pub struct PrometheusConfig { pub struct ServiceMonitorTLSConfig { // ## Path to the CA file // ## - pub ca_file: String, + pub ca_file: Option, // ## Path to client certificate file // ## - pub cert_file: String, + pub cert_file: Option, // ## Skip certificate verification // ## - pub insecure_skip_verify: bool, + pub insecure_skip_verify: Option, // ## Path to client key file // ## - pub key_file: String, + pub key_file: Option, // ## Server name used to verify host name // ## - pub server_name: String, + pub server_name: Option, } #[derive(Debug, Clone, Serialize)] @@ -97,19 +97,19 @@ pub struct ServiceMonitorTLSConfig { pub struct ServiceMonitorEndpoint { // ## Name of the endpoint's service port // ## Mutually exclusive with targetPort - pub port: String, + pub port: Option, // ## Name or number of the endpoint's target port // ## Mutually exclusive with port - pub target_port: String, + pub target_port: Option, // ## File containing bearer token to be used when scraping targets // ## - pub bearer_token_file: String, + pub bearer_token_file: Option, // ## Interval at which metrics should be scraped // ## - pub interval: String, + pub interval: Option, // ## HTTP path to scrape for metrics // ## @@ -121,7 +121,7 @@ pub struct ServiceMonitorEndpoint { // ## TLS configuration to use when scraping the endpoint // ## - pub tls_config: ServiceMonitorTLSConfig, + pub tls_config: Option, // ## MetricRelabelConfigs to apply to samples after scraping, but before ingestion. // ## ref: https://github.com/prometheus-operator/prometheus-operator/blob/main/Documentation/api-reference/api.md#relabelconfig @@ -149,11 +149,11 @@ pub struct ServiceMonitor { pub name: String, // # Additional labels to set used for the ServiceMonitorSelector. Together with standard labels from the chart - pub additional_labels: Mapping, + pub additional_labels: Option, // # Service label for use in assembling a job name of the form