From f66533aa899a224fc16035cee9a2823d6f346b59 Mon Sep 17 00:00:00 2001 From: tahahawa Date: Tue, 6 May 2025 21:17:02 -0400 Subject: [PATCH] add ingress score --- Cargo.lock | 1 + examples/lamp/Cargo.toml | 1 + examples/lamp/src/main.rs | 9 +++- harmony/src/modules/k8s/ingress.rs | 72 ++++++++++++++++++++++++++++++ harmony/src/modules/k8s/mod.rs | 1 + harmony/src/modules/lamp.rs | 18 ++++++++ 6 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 harmony/src/modules/k8s/ingress.rs diff --git a/Cargo.lock b/Cargo.lock index f84d847..ee85ed7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1032,6 +1032,7 @@ dependencies = [ "harmony_cli", "harmony_macros", "harmony_types", + "k8s-openapi", "log", "tokio", "url", diff --git a/examples/lamp/Cargo.toml b/examples/lamp/Cargo.toml index a433e79..9584ac3 100644 --- a/examples/lamp/Cargo.toml +++ b/examples/lamp/Cargo.toml @@ -16,3 +16,4 @@ harmony_macros = { path = "../../harmony_macros" } log = { workspace = true } env_logger = { workspace = true } url = { workspace = true } +k8s-openapi = { version = "0.24.0", features = ["v1_30"] } diff --git a/examples/lamp/src/main.rs b/examples/lamp/src/main.rs index 06d8534..a027c05 100644 --- a/examples/lamp/src/main.rs +++ b/examples/lamp/src/main.rs @@ -3,12 +3,19 @@ use harmony::{ inventory::Inventory, maestro::Maestro, modules::{ + k8s::{self, ingress::K8sIngressScore, resource::K8sResourceScore}, + { lamp::{LAMPConfig, LAMPScore}, + }, monitoring::monitoring_alerting::MonitoringAlertingStackScore, }, - topology::{K8sAnywhereTopology, Url}, + topology::{K8sAnywhereTopology, Url, k8s::K8sClient}, }; +use k8s_openapi::{ + api::networking::v1::{Ingress, IngressRule, IngressSpec, IngressStatus}, + apimachinery::pkg::apis::meta::v1::ObjectMeta, +}; #[tokio::main] async fn main() { // This here is the whole configuration to diff --git a/harmony/src/modules/k8s/ingress.rs b/harmony/src/modules/k8s/ingress.rs new file mode 100644 index 0000000..fb511a7 --- /dev/null +++ b/harmony/src/modules/k8s/ingress.rs @@ -0,0 +1,72 @@ +use k8s_openapi::api::networking::v1::Ingress; +use serde::Serialize; +use serde_json::json; + +use crate::{ + interpret::Interpret, + score::Score, + topology::{K8sclient, Topology}, +}; + +use super::resource::{K8sResourceInterpret, K8sResourceScore}; + +#[derive(Debug, Clone, Serialize)] +pub struct K8sIngressScore { + pub name: String, + pub host: String, + pub backend_service: String, + pub port: String, + pub path: Option, + pub path_type: Option, + pub namespace: Option, +} + +impl Score for K8sIngressScore { + fn create_interpret(&self) -> Box> { + let path = match self.path.clone() { + Some(p) => p, + None => "/".to_string(), + }; + let path_type = match self.path_type.clone() { + Some(p) => p, + None => "Prefix".to_string(), + }; + + let ingress = json!( + { + "metadata": { + "name": self.name + }, + "spec": { + "rules": [ + { "host": self.host, + "http": { + "paths": [ + { + "path": path, + "pathType": path_type, + "backend": [ + { + "service": self.backend_service, + "port": self.port + } + ] + } + ] + } + } + ] + } + } + ); + + let ingress: Ingress = serde_json::from_value(ingress).unwrap(); + Box::new(K8sResourceInterpret { + score: K8sResourceScore::single(ingress.clone(), self.namespace.clone()), + }) + } + + fn name(&self) -> String { + "K8sIngressScore".to_string() + } +} diff --git a/harmony/src/modules/k8s/mod.rs b/harmony/src/modules/k8s/mod.rs index 97e238f..90781f9 100644 --- a/harmony/src/modules/k8s/mod.rs +++ b/harmony/src/modules/k8s/mod.rs @@ -1,3 +1,4 @@ pub mod deployment; +pub mod ingress; pub mod namespace; pub mod resource; diff --git a/harmony/src/modules/lamp.rs b/harmony/src/modules/lamp.rs index 637ba92..df38ead 100644 --- a/harmony/src/modules/lamp.rs +++ b/harmony/src/modules/lamp.rs @@ -13,6 +13,7 @@ use log::{debug, info}; use serde::Serialize; use crate::config::{REGISTRY_PROJECT, REGISTRY_URL}; +use crate::modules::k8s::ingress::K8sIngressScore; use crate::topology::HelmCommand; use crate::{ data::{Id, Version}, @@ -132,6 +133,23 @@ impl Interpret for LAMPInterpret { info!("LAMP deployment_score {deployment_score:?}"); + let lamp_ingress = K8sIngressScore { + name: "lamp-ingress".to_string(), + host: "test".to_string(), + backend_service: "test".to_string(), + port: "8080".to_string(), + path: None, + path_type: None, + namespace: self.get_namespace().map(|nbs| nbs.to_string()), + }; + + lamp_ingress + .create_interpret() + .execute(inventory, topology) + .await?; + + info!("LAMP deployment_score {deployment_score:?}"); + Ok(Outcome::success( "Successfully deployed LAMP Stack!".to_string(), ))