forked from NationTech/harmony
Compare commits
14 Commits
idempotent
...
secrets-pr
| Author | SHA1 | Date | |
|---|---|---|---|
| c5f46d676b | |||
| 258cfa279e | |||
| 11481b16cd | |||
| 21dcb75408 | |||
| a5f9ecfcf7 | |||
| 849bd79710 | |||
| c5101e096a | |||
| cd0720f43e | |||
| b9e04d21da | |||
| a0884950d7 | |||
| 29d22a611f | |||
| 3bf5cb0526 | |||
| 54803c40a2 | |||
| 288129b0c1 |
13
Cargo.lock
generated
13
Cargo.lock
generated
@@ -429,15 +429,6 @@ dependencies = [
|
|||||||
"wait-timeout",
|
"wait-timeout",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "assertor"
|
|
||||||
version = "0.0.4"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "4ff24d87260733dc86d38a11c60d9400ce4a74a05d0dafa2a6f5ab249cd857cb"
|
|
||||||
dependencies = [
|
|
||||||
"num-traits",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "async-broadcast"
|
name = "async-broadcast"
|
||||||
version = "0.7.2"
|
version = "0.7.2"
|
||||||
@@ -1890,8 +1881,6 @@ dependencies = [
|
|||||||
"env_logger",
|
"env_logger",
|
||||||
"harmony",
|
"harmony",
|
||||||
"harmony_macros",
|
"harmony_macros",
|
||||||
"harmony_secret",
|
|
||||||
"harmony_secret_derive",
|
|
||||||
"harmony_tui",
|
"harmony_tui",
|
||||||
"harmony_types",
|
"harmony_types",
|
||||||
"log",
|
"log",
|
||||||
@@ -3135,6 +3124,7 @@ dependencies = [
|
|||||||
"fxhash",
|
"fxhash",
|
||||||
"newline-converter",
|
"newline-converter",
|
||||||
"once_cell",
|
"once_cell",
|
||||||
|
"tempfile",
|
||||||
"unicode-segmentation",
|
"unicode-segmentation",
|
||||||
"unicode-width 0.1.14",
|
"unicode-width 0.1.14",
|
||||||
]
|
]
|
||||||
@@ -3889,7 +3879,6 @@ checksum = "d05e27ee213611ffe7d6348b942e8f942b37114c00cc03cec254295a4a17852e"
|
|||||||
name = "opnsense-config"
|
name = "opnsense-config"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"assertor",
|
|
||||||
"async-trait",
|
"async-trait",
|
||||||
"chrono",
|
"chrono",
|
||||||
"env_logger",
|
"env_logger",
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ k8s-openapi = { version = "0.25", features = ["v1_30"] }
|
|||||||
serde_yaml = "0.9"
|
serde_yaml = "0.9"
|
||||||
serde-value = "0.7"
|
serde-value = "0.7"
|
||||||
http = "1.2"
|
http = "1.2"
|
||||||
inquire = "0.7"
|
inquire = { version = "0.7", features = ["editor"] }
|
||||||
convert_case = "0.8"
|
convert_case = "0.8"
|
||||||
chrono = "0.4"
|
chrono = "0.4"
|
||||||
similar = "2"
|
similar = "2"
|
||||||
@@ -75,4 +75,3 @@ reqwest = { version = "0.12", features = [
|
|||||||
"http2",
|
"http2",
|
||||||
"json",
|
"json",
|
||||||
], default-features = false }
|
], default-features = false }
|
||||||
assertor = "0.0.4"
|
|
||||||
|
|||||||
@@ -27,7 +27,6 @@ async fn main() {
|
|||||||
};
|
};
|
||||||
let application = Arc::new(RustWebapp {
|
let application = Arc::new(RustWebapp {
|
||||||
name: "example-monitoring".to_string(),
|
name: "example-monitoring".to_string(),
|
||||||
domain: Url::Url(url::Url::parse("https://rustapp.harmony.example.com").unwrap()),
|
|
||||||
project_root: PathBuf::from("./examples/rust/webapp"),
|
project_root: PathBuf::from("./examples/rust/webapp"),
|
||||||
framework: Some(RustWebFramework::Leptos),
|
framework: Some(RustWebFramework::Leptos),
|
||||||
service_port: 3000,
|
service_port: 3000,
|
||||||
|
|||||||
@@ -17,7 +17,6 @@ use harmony_types::net::Url;
|
|||||||
async fn main() {
|
async fn main() {
|
||||||
let application = Arc::new(RustWebapp {
|
let application = Arc::new(RustWebapp {
|
||||||
name: "test-rhob-monitoring".to_string(),
|
name: "test-rhob-monitoring".to_string(),
|
||||||
domain: Url::Url(url::Url::parse("htps://some-fake-url").unwrap()),
|
|
||||||
project_root: PathBuf::from("./webapp"), // Relative from 'harmony-path' param
|
project_root: PathBuf::from("./webapp"), // Relative from 'harmony-path' param
|
||||||
framework: Some(RustWebFramework::Leptos),
|
framework: Some(RustWebFramework::Leptos),
|
||||||
service_port: 3000,
|
service_port: 3000,
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ use harmony_macros::hurl;
|
|||||||
async fn main() {
|
async fn main() {
|
||||||
let application = Arc::new(RustWebapp {
|
let application = Arc::new(RustWebapp {
|
||||||
name: "harmony-example-rust-webapp".to_string(),
|
name: "harmony-example-rust-webapp".to_string(),
|
||||||
domain: hurl!("https://rustapp.harmony.example.com"),
|
|
||||||
project_root: PathBuf::from("./webapp"),
|
project_root: PathBuf::from("./webapp"),
|
||||||
framework: Some(RustWebFramework::Leptos),
|
framework: Some(RustWebFramework::Leptos),
|
||||||
service_port: 3000,
|
service_port: 3000,
|
||||||
|
|||||||
@@ -1,23 +1,21 @@
|
|||||||
use std::{path::PathBuf, sync::Arc};
|
|
||||||
|
|
||||||
use harmony::{
|
use harmony::{
|
||||||
inventory::Inventory,
|
inventory::Inventory,
|
||||||
modules::{
|
modules::{
|
||||||
application::{
|
application::{
|
||||||
ApplicationScore, RustWebFramework, RustWebapp,
|
ApplicationScore, RustWebFramework, RustWebapp,
|
||||||
features::{ContinuousDelivery, Monitoring},
|
features::{ContinuousDelivery, Monitoring, rhob_monitoring::RHOBMonitoring},
|
||||||
},
|
},
|
||||||
monitoring::alert_channel::discord_alert_channel::DiscordWebhook,
|
monitoring::alert_channel::discord_alert_channel::DiscordWebhook,
|
||||||
},
|
},
|
||||||
topology::K8sAnywhereTopology,
|
topology::K8sAnywhereTopology,
|
||||||
};
|
};
|
||||||
use harmony_types::net::Url;
|
use harmony_macros::hurl;
|
||||||
|
use std::{path::PathBuf, sync::Arc};
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
let application = Arc::new(RustWebapp {
|
let application = Arc::new(RustWebapp {
|
||||||
name: "harmony-example-tryrust".to_string(),
|
name: "harmony-example-tryrust".to_string(),
|
||||||
domain: Url::Url(url::Url::parse("https://tryrust.harmony.example.com").unwrap()),
|
|
||||||
project_root: PathBuf::from("./tryrust.org"),
|
project_root: PathBuf::from("./tryrust.org"),
|
||||||
framework: Some(RustWebFramework::Leptos),
|
framework: Some(RustWebFramework::Leptos),
|
||||||
service_port: 8080,
|
service_port: 8080,
|
||||||
@@ -25,7 +23,7 @@ async fn main() {
|
|||||||
|
|
||||||
let discord_receiver = DiscordWebhook {
|
let discord_receiver = DiscordWebhook {
|
||||||
name: "test-discord".to_string(),
|
name: "test-discord".to_string(),
|
||||||
url: Url::Url(url::Url::parse("https://discord.doesnt.exist.com").unwrap()),
|
url: hurl!("https://discord.doesnt.exist.com"),
|
||||||
};
|
};
|
||||||
|
|
||||||
let app = ApplicationScore {
|
let app = ApplicationScore {
|
||||||
@@ -33,7 +31,7 @@ async fn main() {
|
|||||||
Box::new(ContinuousDelivery {
|
Box::new(ContinuousDelivery {
|
||||||
application: application.clone(),
|
application: application.clone(),
|
||||||
}),
|
}),
|
||||||
Box::new(Monitoring {
|
Box::new(RHOBMonitoring {
|
||||||
application: application.clone(),
|
application: application.clone(),
|
||||||
alert_receiver: vec![Box::new(discord_receiver)],
|
alert_receiver: vec![Box::new(discord_receiver)],
|
||||||
}),
|
}),
|
||||||
|
|||||||
@@ -10,7 +10,11 @@ testing = []
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
hex = "0.4"
|
hex = "0.4"
|
||||||
reqwest = { version = "0.11", features = ["blocking", "json", "rustls-tls"], default-features = false }
|
reqwest = { version = "0.11", features = [
|
||||||
|
"blocking",
|
||||||
|
"json",
|
||||||
|
"rustls-tls",
|
||||||
|
], default-features = false }
|
||||||
russh = "0.45.0"
|
russh = "0.45.0"
|
||||||
rust-ipmi = "0.1.1"
|
rust-ipmi = "0.1.1"
|
||||||
semver = "1.0.23"
|
semver = "1.0.23"
|
||||||
|
|||||||
7
harmony/src/domain/topology/ingress.rs
Normal file
7
harmony/src/domain/topology/ingress.rs
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
use crate::topology::PreparationError;
|
||||||
|
use async_trait::async_trait;
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
pub trait Ingress {
|
||||||
|
async fn get_domain(&self, service: &str) -> Result<String, PreparationError>;
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
use std::{process::Command, sync::Arc};
|
use std::{process::Command, sync::Arc};
|
||||||
|
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
|
use kube::api::GroupVersionKind;
|
||||||
use log::{debug, info, warn};
|
use log::{debug, info, warn};
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use tokio::sync::OnceCell;
|
use tokio::sync::OnceCell;
|
||||||
@@ -22,6 +23,7 @@ use crate::{
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
score::Score,
|
score::Score,
|
||||||
|
topology::ingress::Ingress,
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
@@ -198,6 +200,26 @@ impl K8sAnywhereTopology {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn openshift_ingress_operator_available(&self) -> Result<(), PreparationError> {
|
||||||
|
let client = self.k8s_client().await?;
|
||||||
|
let gvk = GroupVersionKind {
|
||||||
|
group: "operator.openshift.io".into(),
|
||||||
|
version: "v1".into(),
|
||||||
|
kind: "IngressController".into(),
|
||||||
|
};
|
||||||
|
let ic = client
|
||||||
|
.get_resource_json_value("default", Some("openshift-ingress-operator"), &gvk)
|
||||||
|
.await?;
|
||||||
|
let ready_replicas = ic.data["status"]["availableReplicas"].as_i64().unwrap_or(0);
|
||||||
|
if ready_replicas >= 1 {
|
||||||
|
return Ok(());
|
||||||
|
} else {
|
||||||
|
return Err(PreparationError::new(
|
||||||
|
"openshift-ingress-operator not available".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn is_helm_available(&self) -> Result<(), String> {
|
fn is_helm_available(&self) -> Result<(), String> {
|
||||||
let version_result = Command::new("helm")
|
let version_result = Command::new("helm")
|
||||||
.arg("version")
|
.arg("version")
|
||||||
@@ -350,6 +372,8 @@ impl K8sAnywhereTopology {
|
|||||||
if let Some(Some(k8s_state)) = self.k8s_state.get() {
|
if let Some(Some(k8s_state)) = self.k8s_state.get() {
|
||||||
match k8s_state.source {
|
match k8s_state.source {
|
||||||
K8sSource::LocalK3d => {
|
K8sSource::LocalK3d => {
|
||||||
|
warn!("Installing observability operator is not supported on LocalK3d source");
|
||||||
|
return Ok(PreparationOutcome::Noop);
|
||||||
debug!("installing cluster observability operator");
|
debug!("installing cluster observability operator");
|
||||||
todo!();
|
todo!();
|
||||||
let op_score =
|
let op_score =
|
||||||
@@ -528,7 +552,7 @@ impl MultiTargetTopology for K8sAnywhereTopology {
|
|||||||
match self.config.harmony_profile.to_lowercase().as_str() {
|
match self.config.harmony_profile.to_lowercase().as_str() {
|
||||||
"staging" => DeploymentTarget::Staging,
|
"staging" => DeploymentTarget::Staging,
|
||||||
"production" => DeploymentTarget::Production,
|
"production" => DeploymentTarget::Production,
|
||||||
_ => todo!("HARMONY_PROFILE must be set when use_local_k3d is not set"),
|
_ => todo!("HARMONY_PROFILE must be set when use_local_k3d is false"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -550,3 +574,45 @@ impl TenantManager for K8sAnywhereTopology {
|
|||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl Ingress for K8sAnywhereTopology {
|
||||||
|
//TODO this is specifically for openshift/okd which violates the k8sanywhere idea
|
||||||
|
async fn get_domain(&self, service: &str) -> Result<String, PreparationError> {
|
||||||
|
let client = self.k8s_client().await?;
|
||||||
|
|
||||||
|
if let Some(Some(k8s_state)) = self.k8s_state.get() {
|
||||||
|
match k8s_state.source {
|
||||||
|
K8sSource::LocalK3d => Ok(format!("{service}.local.k3d")),
|
||||||
|
K8sSource::Kubeconfig => {
|
||||||
|
self.openshift_ingress_operator_available().await?;
|
||||||
|
|
||||||
|
let gvk = GroupVersionKind {
|
||||||
|
group: "operator.openshift.io".into(),
|
||||||
|
version: "v1".into(),
|
||||||
|
kind: "IngressController".into(),
|
||||||
|
};
|
||||||
|
let ic = client
|
||||||
|
.get_resource_json_value(
|
||||||
|
"default",
|
||||||
|
Some("openshift-ingress-operator"),
|
||||||
|
&gvk,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.map_err(|_| {
|
||||||
|
PreparationError::new("Failed to fetch IngressController".to_string())
|
||||||
|
})?;
|
||||||
|
|
||||||
|
match ic.data["status"]["domain"].as_str() {
|
||||||
|
Some(domain) => Ok(format!("{service}.{domain}")),
|
||||||
|
None => Err(PreparationError::new("Could not find domain".to_string())),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Err(PreparationError::new(
|
||||||
|
"Cannot get domain: unable to detect K8s state".to_string(),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -28,7 +28,13 @@ pub trait LoadBalancer: Send + Sync {
|
|||||||
&self,
|
&self,
|
||||||
service: &LoadBalancerService,
|
service: &LoadBalancerService,
|
||||||
) -> Result<(), ExecutorError> {
|
) -> Result<(), ExecutorError> {
|
||||||
self.add_service(service).await?;
|
debug!(
|
||||||
|
"Listing LoadBalancer services {:?}",
|
||||||
|
self.list_services().await
|
||||||
|
);
|
||||||
|
if !self.list_services().await.contains(service) {
|
||||||
|
self.add_service(service).await?;
|
||||||
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
mod ha_cluster;
|
mod ha_cluster;
|
||||||
|
pub mod ingress;
|
||||||
use harmony_types::net::IpAddress;
|
use harmony_types::net::IpAddress;
|
||||||
mod host_binding;
|
mod host_binding;
|
||||||
mod http;
|
mod http;
|
||||||
|
|||||||
@@ -26,13 +26,19 @@ impl LoadBalancer for OPNSenseFirewall {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn add_service(&self, service: &LoadBalancerService) -> Result<(), ExecutorError> {
|
async fn add_service(&self, service: &LoadBalancerService) -> Result<(), ExecutorError> {
|
||||||
|
warn!(
|
||||||
|
"TODO : the current implementation does not check / cleanup / merge with existing haproxy services properly. Make sure to manually verify that the configuration is correct after executing any operation here"
|
||||||
|
);
|
||||||
let mut config = self.opnsense_config.write().await;
|
let mut config = self.opnsense_config.write().await;
|
||||||
let mut load_balancer = config.load_balancer();
|
|
||||||
|
|
||||||
let (frontend, backend, servers, healthcheck) =
|
let (frontend, backend, servers, healthcheck) =
|
||||||
harmony_load_balancer_service_to_haproxy_xml(service);
|
harmony_load_balancer_service_to_haproxy_xml(service);
|
||||||
|
let mut load_balancer = config.load_balancer();
|
||||||
load_balancer.configure_service(frontend, backend, servers, healthcheck);
|
load_balancer.add_backend(backend);
|
||||||
|
load_balancer.add_frontend(frontend);
|
||||||
|
load_balancer.add_servers(servers);
|
||||||
|
if let Some(healthcheck) = healthcheck {
|
||||||
|
load_balancer.add_healthcheck(healthcheck);
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -100,7 +106,7 @@ pub(crate) fn haproxy_xml_config_to_harmony_loadbalancer(
|
|||||||
.backends
|
.backends
|
||||||
.backends
|
.backends
|
||||||
.iter()
|
.iter()
|
||||||
.find(|b| Some(b.uuid.clone()) == frontend.default_backend);
|
.find(|b| b.uuid == frontend.default_backend);
|
||||||
|
|
||||||
let mut health_check = None;
|
let mut health_check = None;
|
||||||
match matching_backend {
|
match matching_backend {
|
||||||
@@ -110,7 +116,8 @@ pub(crate) fn haproxy_xml_config_to_harmony_loadbalancer(
|
|||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
warn!(
|
warn!(
|
||||||
"HAProxy config could not find a matching backend for frontend {frontend:?}"
|
"HAProxy config could not find a matching backend for frontend {:?}",
|
||||||
|
frontend
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -145,11 +152,11 @@ pub(crate) fn get_servers_for_backend(
|
|||||||
.servers
|
.servers
|
||||||
.iter()
|
.iter()
|
||||||
.filter_map(|server| {
|
.filter_map(|server| {
|
||||||
let address = server.address.clone()?;
|
|
||||||
let port = server.port?;
|
|
||||||
|
|
||||||
if backend_servers.contains(&server.uuid.as_str()) {
|
if backend_servers.contains(&server.uuid.as_str()) {
|
||||||
return Some(BackendServer { address, port });
|
return Some(BackendServer {
|
||||||
|
address: server.address.clone(),
|
||||||
|
port: server.port,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
None
|
None
|
||||||
})
|
})
|
||||||
@@ -340,7 +347,7 @@ pub(crate) fn harmony_load_balancer_service_to_haproxy_xml(
|
|||||||
name: format!("frontend_{}", service.listening_port),
|
name: format!("frontend_{}", service.listening_port),
|
||||||
bind: service.listening_port.to_string(),
|
bind: service.listening_port.to_string(),
|
||||||
mode: "tcp".to_string(), // TODO do not depend on health check here
|
mode: "tcp".to_string(), // TODO do not depend on health check here
|
||||||
default_backend: Some(backend.uuid.clone()),
|
default_backend: backend.uuid.clone(),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
info!("HAPRoxy frontend and backend mode currently hardcoded to tcp");
|
info!("HAPRoxy frontend and backend mode currently hardcoded to tcp");
|
||||||
@@ -354,8 +361,8 @@ fn server_to_haproxy_server(server: &BackendServer) -> HAProxyServer {
|
|||||||
uuid: Uuid::new_v4().to_string(),
|
uuid: Uuid::new_v4().to_string(),
|
||||||
name: format!("{}_{}", &server.address, &server.port),
|
name: format!("{}_{}", &server.address, &server.port),
|
||||||
enabled: 1,
|
enabled: 1,
|
||||||
address: Some(server.address.clone()),
|
address: server.address.clone(),
|
||||||
port: Some(server.port),
|
port: server.port,
|
||||||
mode: "active".to_string(),
|
mode: "active".to_string(),
|
||||||
server_type: "static".to_string(),
|
server_type: "static".to_string(),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
@@ -378,8 +385,8 @@ mod tests {
|
|||||||
let mut haproxy = HAProxy::default();
|
let mut haproxy = HAProxy::default();
|
||||||
let server = HAProxyServer {
|
let server = HAProxyServer {
|
||||||
uuid: "server1".to_string(),
|
uuid: "server1".to_string(),
|
||||||
address: Some("192.168.1.1".to_string()),
|
address: "192.168.1.1".to_string(),
|
||||||
port: Some(80),
|
port: 80,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
haproxy.servers.servers.push(server);
|
haproxy.servers.servers.push(server);
|
||||||
@@ -404,8 +411,8 @@ mod tests {
|
|||||||
let mut haproxy = HAProxy::default();
|
let mut haproxy = HAProxy::default();
|
||||||
let server = HAProxyServer {
|
let server = HAProxyServer {
|
||||||
uuid: "server1".to_string(),
|
uuid: "server1".to_string(),
|
||||||
address: Some("192.168.1.1".to_string()),
|
address: "192.168.1.1".to_string(),
|
||||||
port: Some(80),
|
port: 80,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
haproxy.servers.servers.push(server);
|
haproxy.servers.servers.push(server);
|
||||||
@@ -424,8 +431,8 @@ mod tests {
|
|||||||
let mut haproxy = HAProxy::default();
|
let mut haproxy = HAProxy::default();
|
||||||
let server = HAProxyServer {
|
let server = HAProxyServer {
|
||||||
uuid: "server1".to_string(),
|
uuid: "server1".to_string(),
|
||||||
address: Some("192.168.1.1".to_string()),
|
address: "192.168.1.1".to_string(),
|
||||||
port: Some(80),
|
port: 80,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
haproxy.servers.servers.push(server);
|
haproxy.servers.servers.push(server);
|
||||||
@@ -446,16 +453,16 @@ mod tests {
|
|||||||
let mut haproxy = HAProxy::default();
|
let mut haproxy = HAProxy::default();
|
||||||
let server = HAProxyServer {
|
let server = HAProxyServer {
|
||||||
uuid: "server1".to_string(),
|
uuid: "server1".to_string(),
|
||||||
address: Some("some-hostname.test.mcd".to_string()),
|
address: "some-hostname.test.mcd".to_string(),
|
||||||
port: Some(80),
|
port: 80,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
haproxy.servers.servers.push(server);
|
haproxy.servers.servers.push(server);
|
||||||
|
|
||||||
let server = HAProxyServer {
|
let server = HAProxyServer {
|
||||||
uuid: "server2".to_string(),
|
uuid: "server2".to_string(),
|
||||||
address: Some("192.168.1.2".to_string()),
|
address: "192.168.1.2".to_string(),
|
||||||
port: Some(8080),
|
port: 8080,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
haproxy.servers.servers.push(server);
|
haproxy.servers.servers.push(server);
|
||||||
|
|||||||
@@ -14,7 +14,9 @@ use crate::{
|
|||||||
features::{ArgoApplication, ArgoHelmScore},
|
features::{ArgoApplication, ArgoHelmScore},
|
||||||
},
|
},
|
||||||
score::Score,
|
score::Score,
|
||||||
topology::{DeploymentTarget, HelmCommand, K8sclient, MultiTargetTopology, Topology},
|
topology::{
|
||||||
|
DeploymentTarget, HelmCommand, K8sclient, MultiTargetTopology, Topology, ingress::Ingress,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
/// ContinuousDelivery in Harmony provides this functionality :
|
/// ContinuousDelivery in Harmony provides this functionality :
|
||||||
@@ -136,18 +138,25 @@ impl<A: OCICompliant + HelmPackage> ContinuousDelivery<A> {
|
|||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl<
|
impl<
|
||||||
A: OCICompliant + HelmPackage + Clone + 'static,
|
A: OCICompliant + HelmPackage + Clone + 'static,
|
||||||
T: Topology + HelmCommand + MultiTargetTopology + K8sclient + 'static,
|
T: Topology + HelmCommand + MultiTargetTopology + K8sclient + Ingress + 'static,
|
||||||
> ApplicationFeature<T> for ContinuousDelivery<A>
|
> ApplicationFeature<T> for ContinuousDelivery<A>
|
||||||
{
|
{
|
||||||
async fn ensure_installed(&self, topology: &T) -> Result<(), String> {
|
async fn ensure_installed(&self, topology: &T) -> Result<(), String> {
|
||||||
let image = self.application.image_name();
|
let image = self.application.image_name();
|
||||||
|
let domain = topology
|
||||||
|
.get_domain(&self.application.name())
|
||||||
|
.await
|
||||||
|
.map_err(|e| e.to_string())?;
|
||||||
|
|
||||||
// TODO Write CI/CD workflow files
|
// TODO Write CI/CD workflow files
|
||||||
// we can autotedect the CI type using the remote url (default to github action for github
|
// we can autotedect the CI type using the remote url (default to github action for github
|
||||||
// url, etc..)
|
// url, etc..)
|
||||||
// Or ask for it when unknown
|
// Or ask for it when unknown
|
||||||
|
|
||||||
let helm_chart = self.application.build_push_helm_package(&image).await?;
|
let helm_chart = self
|
||||||
|
.application
|
||||||
|
.build_push_helm_package(&image, &domain)
|
||||||
|
.await?;
|
||||||
|
|
||||||
// TODO: Make building image configurable/skippable if image already exists (prompt)")
|
// TODO: Make building image configurable/skippable if image already exists (prompt)")
|
||||||
// https://git.nationtech.io/NationTech/harmony/issues/104
|
// https://git.nationtech.io/NationTech/harmony/issues/104
|
||||||
|
|||||||
@@ -13,7 +13,8 @@ use crate::{
|
|||||||
modules::helm::chart::{HelmChartScore, HelmRepository},
|
modules::helm::chart::{HelmChartScore, HelmRepository},
|
||||||
score::Score,
|
score::Score,
|
||||||
topology::{
|
topology::{
|
||||||
HelmCommand, K8sclient, PreparationError, PreparationOutcome, Topology, k8s::K8sClient,
|
HelmCommand, K8sclient, PreparationError, PreparationOutcome, Topology, ingress::Ingress,
|
||||||
|
k8s::K8sClient,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
use harmony_types::id::Id;
|
use harmony_types::id::Id;
|
||||||
@@ -27,7 +28,7 @@ pub struct ArgoHelmScore {
|
|||||||
pub argo_apps: Vec<ArgoApplication>,
|
pub argo_apps: Vec<ArgoApplication>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Topology + HelmCommand + K8sclient> Score<T> for ArgoHelmScore {
|
impl<T: Topology + HelmCommand + K8sclient + Ingress> Score<T> for ArgoHelmScore {
|
||||||
fn create_interpret(&self) -> Box<dyn crate::interpret::Interpret<T>> {
|
fn create_interpret(&self) -> Box<dyn crate::interpret::Interpret<T>> {
|
||||||
Box::new(ArgoInterpret {
|
Box::new(ArgoInterpret {
|
||||||
score: self.clone(),
|
score: self.clone(),
|
||||||
@@ -47,17 +48,14 @@ pub struct ArgoInterpret {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl<T: Topology + K8sclient + HelmCommand> Interpret<T> for ArgoInterpret {
|
impl<T: Topology + K8sclient + HelmCommand + Ingress> Interpret<T> for ArgoInterpret {
|
||||||
async fn execute(
|
async fn execute(
|
||||||
&self,
|
&self,
|
||||||
inventory: &Inventory,
|
inventory: &Inventory,
|
||||||
topology: &T,
|
topology: &T,
|
||||||
) -> Result<Outcome, InterpretError> {
|
) -> Result<Outcome, InterpretError> {
|
||||||
let k8s_client = topology.k8s_client().await?;
|
let k8s_client = topology.k8s_client().await?;
|
||||||
let domain = self
|
let domain = topology.get_domain("argo").await?;
|
||||||
.get_host_domain(k8s_client.clone(), self.score.openshift)
|
|
||||||
.await?;
|
|
||||||
let domain = format!("argo.{domain}");
|
|
||||||
let helm_score =
|
let helm_score =
|
||||||
argo_helm_chart_score(&self.score.namespace, self.score.openshift, &domain);
|
argo_helm_chart_score(&self.score.namespace, self.score.openshift, &domain);
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,8 @@
|
|||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
use crate::modules::application::{Application, ApplicationFeature};
|
use crate::modules::application::{Application, ApplicationFeature};
|
||||||
use crate::modules::monitoring::application_monitoring::application_monitoring_score::ApplicationMonitoringScore;
|
use crate::modules::monitoring::application_monitoring::application_monitoring_score::ApplicationMonitoringScore;
|
||||||
use crate::modules::monitoring::kube_prometheus::crd::crd_alertmanager_config::CRDPrometheus;
|
use crate::modules::monitoring::kube_prometheus::crd::crd_alertmanager_config::CRDPrometheus;
|
||||||
|
|
||||||
use crate::topology::MultiTargetTopology;
|
use crate::topology::MultiTargetTopology;
|
||||||
|
use crate::topology::ingress::Ingress;
|
||||||
use crate::{
|
use crate::{
|
||||||
inventory::Inventory,
|
inventory::Inventory,
|
||||||
modules::monitoring::{
|
modules::monitoring::{
|
||||||
@@ -19,8 +17,12 @@ use crate::{
|
|||||||
};
|
};
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use base64::{Engine as _, engine::general_purpose};
|
use base64::{Engine as _, engine::general_purpose};
|
||||||
|
use harmony_secret::SecretManager;
|
||||||
|
use harmony_secret_derive::Secret;
|
||||||
use harmony_types::net::Url;
|
use harmony_types::net::Url;
|
||||||
use log::{debug, info};
|
use log::{debug, info};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Monitoring {
|
pub struct Monitoring {
|
||||||
@@ -36,8 +38,9 @@ impl<
|
|||||||
+ TenantManager
|
+ TenantManager
|
||||||
+ K8sclient
|
+ K8sclient
|
||||||
+ MultiTargetTopology
|
+ MultiTargetTopology
|
||||||
+ std::fmt::Debug
|
+ PrometheusApplicationMonitoring<CRDPrometheus>
|
||||||
+ PrometheusApplicationMonitoring<CRDPrometheus>,
|
+ Ingress
|
||||||
|
+ std::fmt::Debug,
|
||||||
> ApplicationFeature<T> for Monitoring
|
> ApplicationFeature<T> for Monitoring
|
||||||
{
|
{
|
||||||
async fn ensure_installed(&self, topology: &T) -> Result<(), String> {
|
async fn ensure_installed(&self, topology: &T) -> Result<(), String> {
|
||||||
@@ -47,6 +50,7 @@ impl<
|
|||||||
.await
|
.await
|
||||||
.map(|ns| ns.name.clone())
|
.map(|ns| ns.name.clone())
|
||||||
.unwrap_or_else(|| self.application.name());
|
.unwrap_or_else(|| self.application.name());
|
||||||
|
let domain = topology.get_domain("ntfy").await.unwrap();
|
||||||
|
|
||||||
let mut alerting_score = ApplicationMonitoringScore {
|
let mut alerting_score = ApplicationMonitoringScore {
|
||||||
sender: CRDPrometheus {
|
sender: CRDPrometheus {
|
||||||
@@ -58,19 +62,17 @@ impl<
|
|||||||
};
|
};
|
||||||
let ntfy = NtfyScore {
|
let ntfy = NtfyScore {
|
||||||
namespace: namespace.clone(),
|
namespace: namespace.clone(),
|
||||||
host: "ntfy.harmonydemo.apps.ncd0.harmony.mcd".to_string(),
|
host: domain,
|
||||||
};
|
};
|
||||||
ntfy.interpret(&Inventory::empty(), topology)
|
ntfy.interpret(&Inventory::empty(), topology)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| e.to_string())?;
|
.map_err(|e| e.to_string())?;
|
||||||
|
|
||||||
let ntfy_default_auth_username = "harmony";
|
let config = SecretManager::get_or_prompt::<NtfyAuth>().await.unwrap();
|
||||||
let ntfy_default_auth_password = "harmony";
|
|
||||||
let ntfy_default_auth_header = format!(
|
let ntfy_default_auth_header = format!(
|
||||||
"Basic {}",
|
"Basic {}",
|
||||||
general_purpose::STANDARD.encode(format!(
|
general_purpose::STANDARD.encode(format!("{}:{}", config.username, config.password))
|
||||||
"{ntfy_default_auth_username}:{ntfy_default_auth_password}"
|
|
||||||
))
|
|
||||||
);
|
);
|
||||||
|
|
||||||
debug!("ntfy_default_auth_header: {ntfy_default_auth_header}");
|
debug!("ntfy_default_auth_header: {ntfy_default_auth_header}");
|
||||||
@@ -100,9 +102,17 @@ impl<
|
|||||||
.interpret(&Inventory::empty(), topology)
|
.interpret(&Inventory::empty(), topology)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| e.to_string())?;
|
.map_err(|e| e.to_string())?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn name(&self) -> String {
|
fn name(&self) -> String {
|
||||||
"Monitoring".to_string()
|
"Monitoring".to_string()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Secret, Serialize, Deserialize, Clone, Debug)]
|
||||||
|
struct NtfyAuth {
|
||||||
|
username: String,
|
||||||
|
password: String,
|
||||||
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ use crate::modules::monitoring::application_monitoring::rhobs_application_monito
|
|||||||
|
|
||||||
use crate::modules::monitoring::kube_prometheus::crd::rhob_alertmanager_config::RHOBObservability;
|
use crate::modules::monitoring::kube_prometheus::crd::rhob_alertmanager_config::RHOBObservability;
|
||||||
use crate::topology::MultiTargetTopology;
|
use crate::topology::MultiTargetTopology;
|
||||||
|
use crate::topology::ingress::Ingress;
|
||||||
use crate::{
|
use crate::{
|
||||||
inventory::Inventory,
|
inventory::Inventory,
|
||||||
modules::monitoring::{
|
modules::monitoring::{
|
||||||
@@ -37,6 +38,7 @@ impl<
|
|||||||
+ TenantManager
|
+ TenantManager
|
||||||
+ K8sclient
|
+ K8sclient
|
||||||
+ MultiTargetTopology
|
+ MultiTargetTopology
|
||||||
|
+ Ingress
|
||||||
+ std::fmt::Debug
|
+ std::fmt::Debug
|
||||||
+ PrometheusApplicationMonitoring<RHOBObservability>,
|
+ PrometheusApplicationMonitoring<RHOBObservability>,
|
||||||
> ApplicationFeature<T> for RHOBMonitoring
|
> ApplicationFeature<T> for RHOBMonitoring
|
||||||
@@ -59,7 +61,10 @@ impl<
|
|||||||
};
|
};
|
||||||
let ntfy = NtfyScore {
|
let ntfy = NtfyScore {
|
||||||
namespace: namespace.clone(),
|
namespace: namespace.clone(),
|
||||||
host: "ntfy.harmonydemo.apps.ncd0.harmony.mcd".to_string(),
|
host: topology
|
||||||
|
.get_domain("ntfy")
|
||||||
|
.await
|
||||||
|
.map_err(|e| format!("Could not get domain {e}"))?,
|
||||||
};
|
};
|
||||||
ntfy.interpret(&Inventory::empty(), topology)
|
ntfy.interpret(&Inventory::empty(), topology)
|
||||||
.await
|
.await
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
use async_trait::async_trait;
|
|
||||||
|
|
||||||
use super::Application;
|
use super::Application;
|
||||||
|
use async_trait::async_trait;
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
pub trait OCICompliant: Application {
|
pub trait OCICompliant: Application {
|
||||||
@@ -17,5 +16,10 @@ pub trait HelmPackage: Application {
|
|||||||
///
|
///
|
||||||
/// # Arguments
|
/// # Arguments
|
||||||
/// * `image_url` - The full URL of the OCI container image to be used in the Deployment.
|
/// * `image_url` - The full URL of the OCI container image to be used in the Deployment.
|
||||||
async fn build_push_helm_package(&self, image_url: &str) -> Result<String, String>;
|
/// * `domain` - The domain where the application is hosted.
|
||||||
|
async fn build_push_helm_package(
|
||||||
|
&self,
|
||||||
|
image_url: &str,
|
||||||
|
domain: &str,
|
||||||
|
) -> Result<String, String>;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
use std::fs::{self, File};
|
use std::fs::{self};
|
||||||
use std::io::Read;
|
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::process;
|
use std::process;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
@@ -13,12 +12,11 @@ use dockerfile_builder::instruction_builder::CopyBuilder;
|
|||||||
use futures_util::StreamExt;
|
use futures_util::StreamExt;
|
||||||
use log::{debug, info, log_enabled};
|
use log::{debug, info, log_enabled};
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use tar::{Archive, Builder, Header};
|
use tar::{Builder, Header};
|
||||||
use walkdir::WalkDir;
|
use walkdir::WalkDir;
|
||||||
|
|
||||||
use crate::config::{REGISTRY_PROJECT, REGISTRY_URL};
|
use crate::config::{REGISTRY_PROJECT, REGISTRY_URL};
|
||||||
use crate::{score::Score, topology::Topology};
|
use crate::{score::Score, topology::Topology};
|
||||||
use harmony_types::net::Url;
|
|
||||||
|
|
||||||
use super::{Application, ApplicationFeature, ApplicationInterpret, HelmPackage, OCICompliant};
|
use super::{Application, ApplicationFeature, ApplicationInterpret, HelmPackage, OCICompliant};
|
||||||
|
|
||||||
@@ -58,7 +56,6 @@ pub enum RustWebFramework {
|
|||||||
#[derive(Debug, Clone, Serialize)]
|
#[derive(Debug, Clone, Serialize)]
|
||||||
pub struct RustWebapp {
|
pub struct RustWebapp {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub domain: Url,
|
|
||||||
/// The path to the root of the Rust project to be containerized.
|
/// The path to the root of the Rust project to be containerized.
|
||||||
pub project_root: PathBuf,
|
pub project_root: PathBuf,
|
||||||
pub service_port: u32,
|
pub service_port: u32,
|
||||||
@@ -73,12 +70,17 @@ impl Application for RustWebapp {
|
|||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl HelmPackage for RustWebapp {
|
impl HelmPackage for RustWebapp {
|
||||||
async fn build_push_helm_package(&self, image_url: &str) -> Result<String, String> {
|
async fn build_push_helm_package(
|
||||||
|
&self,
|
||||||
|
image_url: &str,
|
||||||
|
domain: &str,
|
||||||
|
) -> Result<String, String> {
|
||||||
info!("Starting Helm chart build and push for '{}'", self.name);
|
info!("Starting Helm chart build and push for '{}'", self.name);
|
||||||
|
|
||||||
// 1. Create the Helm chart files on disk.
|
// 1. Create the Helm chart files on disk.
|
||||||
let chart_dir = self
|
let chart_dir = self
|
||||||
.create_helm_chart_files(image_url)
|
.create_helm_chart_files(image_url, domain)
|
||||||
|
.await
|
||||||
.map_err(|e| format!("Failed to create Helm chart files: {}", e))?;
|
.map_err(|e| format!("Failed to create Helm chart files: {}", e))?;
|
||||||
info!("Successfully created Helm chart files in {:?}", chart_dir);
|
info!("Successfully created Helm chart files in {:?}", chart_dir);
|
||||||
|
|
||||||
@@ -220,6 +222,7 @@ impl RustWebapp {
|
|||||||
".git",
|
".git",
|
||||||
".github",
|
".github",
|
||||||
".harmony_generated",
|
".harmony_generated",
|
||||||
|
"harmony",
|
||||||
"node_modules",
|
"node_modules",
|
||||||
];
|
];
|
||||||
let mut entries: Vec<_> = WalkDir::new(project_root)
|
let mut entries: Vec<_> = WalkDir::new(project_root)
|
||||||
@@ -265,8 +268,6 @@ impl RustWebapp {
|
|||||||
|
|
||||||
let docker = Docker::connect_with_socket_defaults().unwrap();
|
let docker = Docker::connect_with_socket_defaults().unwrap();
|
||||||
|
|
||||||
// let push_options = PushImageOptionsBuilder::new().tag(tag);
|
|
||||||
|
|
||||||
let mut push_image_stream = docker.push_image(
|
let mut push_image_stream = docker.push_image(
|
||||||
image_tag,
|
image_tag,
|
||||||
Some(PushImageOptionsBuilder::new().build()),
|
Some(PushImageOptionsBuilder::new().build()),
|
||||||
@@ -274,6 +275,8 @@ impl RustWebapp {
|
|||||||
);
|
);
|
||||||
|
|
||||||
while let Some(msg) = push_image_stream.next().await {
|
while let Some(msg) = push_image_stream.next().await {
|
||||||
|
// let msg = msg?;
|
||||||
|
// TODO this fails silently, for some reason bollard cannot push to hub.nationtech.io
|
||||||
debug!("Message: {msg:?}");
|
debug!("Message: {msg:?}");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -408,9 +411,10 @@ impl RustWebapp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Creates all necessary files for a basic Helm chart.
|
/// Creates all necessary files for a basic Helm chart.
|
||||||
fn create_helm_chart_files(
|
async fn create_helm_chart_files(
|
||||||
&self,
|
&self,
|
||||||
image_url: &str,
|
image_url: &str,
|
||||||
|
domain: &str,
|
||||||
) -> Result<PathBuf, Box<dyn std::error::Error>> {
|
) -> Result<PathBuf, Box<dyn std::error::Error>> {
|
||||||
let chart_name = format!("{}-chart", self.name);
|
let chart_name = format!("{}-chart", self.name);
|
||||||
let chart_dir = self
|
let chart_dir = self
|
||||||
@@ -460,21 +464,15 @@ ingress:
|
|||||||
enabled: true
|
enabled: true
|
||||||
# Annotations for cert-manager to handle SSL.
|
# Annotations for cert-manager to handle SSL.
|
||||||
annotations:
|
annotations:
|
||||||
cert-manager.io/cluster-issuer: "letsencrypt-prod"
|
|
||||||
# Add other annotations like nginx ingress class if needed
|
# Add other annotations like nginx ingress class if needed
|
||||||
# kubernetes.io/ingress.class: nginx
|
# kubernetes.io/ingress.class: nginx
|
||||||
hosts:
|
hosts:
|
||||||
- host: chart-example.local
|
- host: {}
|
||||||
paths:
|
paths:
|
||||||
- path: /
|
- path: /
|
||||||
pathType: ImplementationSpecific
|
pathType: ImplementationSpecific
|
||||||
tls:
|
|
||||||
- secretName: {}-tls
|
|
||||||
hosts:
|
|
||||||
- chart-example.local
|
|
||||||
|
|
||||||
"#,
|
"#,
|
||||||
chart_name, image_repo, image_tag, self.service_port, self.name
|
chart_name, image_repo, image_tag, self.service_port, domain,
|
||||||
);
|
);
|
||||||
fs::write(chart_dir.join("values.yaml"), values_yaml)?;
|
fs::write(chart_dir.join("values.yaml"), values_yaml)?;
|
||||||
|
|
||||||
|
|||||||
@@ -153,6 +153,10 @@ impl<T: Topology + HelmCommand> Interpret<T> for HelmChartInterpret {
|
|||||||
let yaml_path: Option<&Path> = match self.score.values_yaml.as_ref() {
|
let yaml_path: Option<&Path> = match self.score.values_yaml.as_ref() {
|
||||||
Some(yaml_str) => {
|
Some(yaml_str) => {
|
||||||
tf = temp_file::with_contents(yaml_str.as_bytes());
|
tf = temp_file::with_contents(yaml_str.as_bytes());
|
||||||
|
debug!(
|
||||||
|
"values yaml string for chart {} :\n {yaml_str}",
|
||||||
|
self.score.chart_name
|
||||||
|
);
|
||||||
Some(tf.path())
|
Some(tf.path())
|
||||||
}
|
}
|
||||||
None => None,
|
None => None,
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ pub struct K8sIngressScore {
|
|||||||
pub path: Option<IngressPath>,
|
pub path: Option<IngressPath>,
|
||||||
pub path_type: Option<PathType>,
|
pub path_type: Option<PathType>,
|
||||||
pub namespace: Option<fqdn::FQDN>,
|
pub namespace: Option<fqdn::FQDN>,
|
||||||
|
pub ingress_class_name: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Topology + K8sclient> Score<T> for K8sIngressScore {
|
impl<T: Topology + K8sclient> Score<T> for K8sIngressScore {
|
||||||
@@ -54,12 +55,18 @@ impl<T: Topology + K8sclient> Score<T> for K8sIngressScore {
|
|||||||
None => PathType::Prefix,
|
None => PathType::Prefix,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let ingress_class = match self.ingress_class_name.clone() {
|
||||||
|
Some(ingress_class_name) => ingress_class_name,
|
||||||
|
None => format!("\"default\""),
|
||||||
|
};
|
||||||
|
|
||||||
let ingress = json!(
|
let ingress = json!(
|
||||||
{
|
{
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"name": self.name.to_string(),
|
"name": self.name.to_string(),
|
||||||
},
|
},
|
||||||
"spec": {
|
"spec": {
|
||||||
|
"ingressClassName": ingress_class.as_str(),
|
||||||
"rules": [
|
"rules": [
|
||||||
{ "host": self.host.to_string(),
|
{ "host": self.host.to_string(),
|
||||||
"http": {
|
"http": {
|
||||||
|
|||||||
@@ -147,6 +147,7 @@ impl<T: Topology + K8sclient + HelmCommand> Interpret<T> for LAMPInterpret {
|
|||||||
port: 8080,
|
port: 8080,
|
||||||
path: Some(ingress_path),
|
path: Some(ingress_path),
|
||||||
path_type: None,
|
path_type: None,
|
||||||
|
ingress_class_name: None,
|
||||||
namespace: self
|
namespace: self
|
||||||
.get_namespace()
|
.get_namespace()
|
||||||
.map(|nbs| fqdn!(nbs.to_string().as_str())),
|
.map(|nbs| fqdn!(nbs.to_string().as_str())),
|
||||||
|
|||||||
@@ -4,7 +4,9 @@ use kube::CustomResource;
|
|||||||
use schemars::JsonSchema;
|
use schemars::JsonSchema;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::modules::monitoring::kube_prometheus::crd::rhob_prometheuses::LabelSelector;
|
use crate::modules::monitoring::kube_prometheus::crd::rhob_prometheuses::{
|
||||||
|
LabelSelector, PrometheusSpec,
|
||||||
|
};
|
||||||
|
|
||||||
/// MonitoringStack CRD for monitoring.rhobs/v1alpha1
|
/// MonitoringStack CRD for monitoring.rhobs/v1alpha1
|
||||||
#[derive(CustomResource, Serialize, Deserialize, Debug, Clone, JsonSchema)]
|
#[derive(CustomResource, Serialize, Deserialize, Debug, Clone, JsonSchema)]
|
||||||
|
|||||||
@@ -45,6 +45,12 @@ service:
|
|||||||
|
|
||||||
ingress:
|
ingress:
|
||||||
enabled: {ingress_enabled}
|
enabled: {ingress_enabled}
|
||||||
|
hosts:
|
||||||
|
- host: {host}
|
||||||
|
paths:
|
||||||
|
- path: /
|
||||||
|
pathType: ImplementationSpecific
|
||||||
|
|
||||||
|
|
||||||
route:
|
route:
|
||||||
enabled: {route_enabled}
|
enabled: {route_enabled}
|
||||||
|
|||||||
@@ -77,8 +77,6 @@ impl OKDBootstrapLoadBalancerScore {
|
|||||||
address: topology.bootstrap_host.ip.to_string(),
|
address: topology.bootstrap_host.ip.to_string(),
|
||||||
port,
|
port,
|
||||||
});
|
});
|
||||||
|
|
||||||
backend.dedup();
|
|
||||||
backend
|
backend
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,8 +21,8 @@ pub fn pod_failed() -> PrometheusAlertRule {
|
|||||||
pub fn alert_container_restarting() -> PrometheusAlertRule {
|
pub fn alert_container_restarting() -> PrometheusAlertRule {
|
||||||
PrometheusAlertRule {
|
PrometheusAlertRule {
|
||||||
alert: "ContainerRestarting".into(),
|
alert: "ContainerRestarting".into(),
|
||||||
expr: "increase(kube_pod_container_status_restarts_total[5m]) > 3".into(),
|
expr: "increase(kube_pod_container_status_restarts_total[30s]) > 3".into(),
|
||||||
r#for: Some("5m".into()),
|
r#for: Some("30s".into()),
|
||||||
labels: HashMap::from([("severity".into(), "warning".into())]),
|
labels: HashMap::from([("severity".into(), "warning".into())]),
|
||||||
annotations: HashMap::from([
|
annotations: HashMap::from([
|
||||||
(
|
(
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
use fqdn::fqdn;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::{collections::BTreeMap, sync::Arc};
|
use std::{collections::BTreeMap, sync::Arc};
|
||||||
use tempfile::tempdir;
|
use tempfile::tempdir;
|
||||||
@@ -8,6 +9,7 @@ use log::{debug, info};
|
|||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
|
|
||||||
|
use crate::modules::k8s::ingress::{K8sIngressScore, PathType};
|
||||||
use crate::modules::monitoring::kube_prometheus::crd::grafana_default_dashboard::build_default_dashboard;
|
use crate::modules::monitoring::kube_prometheus::crd::grafana_default_dashboard::build_default_dashboard;
|
||||||
use crate::modules::monitoring::kube_prometheus::crd::rhob_alertmanager_config::RHOBObservability;
|
use crate::modules::monitoring::kube_prometheus::crd::rhob_alertmanager_config::RHOBObservability;
|
||||||
use crate::modules::monitoring::kube_prometheus::crd::rhob_alertmanagers::{
|
use crate::modules::monitoring::kube_prometheus::crd::rhob_alertmanagers::{
|
||||||
@@ -23,12 +25,18 @@ use crate::modules::monitoring::kube_prometheus::crd::rhob_monitoring_stack::{
|
|||||||
use crate::modules::monitoring::kube_prometheus::crd::rhob_prometheus_rules::{
|
use crate::modules::monitoring::kube_prometheus::crd::rhob_prometheus_rules::{
|
||||||
PrometheusRule, PrometheusRuleSpec, RuleGroup,
|
PrometheusRule, PrometheusRuleSpec, RuleGroup,
|
||||||
};
|
};
|
||||||
use crate::modules::monitoring::kube_prometheus::crd::rhob_prometheuses::LabelSelector;
|
use crate::modules::monitoring::kube_prometheus::crd::rhob_prometheuses::{
|
||||||
|
AlertmanagerEndpoints, LabelSelector, PrometheusSpec, PrometheusSpecAlerting,
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::modules::monitoring::kube_prometheus::crd::rhob_role::{
|
||||||
|
build_prom_role, build_prom_rolebinding, build_prom_service_account,
|
||||||
|
};
|
||||||
use crate::modules::monitoring::kube_prometheus::crd::rhob_service_monitor::{
|
use crate::modules::monitoring::kube_prometheus::crd::rhob_service_monitor::{
|
||||||
ServiceMonitor, ServiceMonitorSpec,
|
ServiceMonitor, ServiceMonitorSpec,
|
||||||
};
|
};
|
||||||
use crate::score::Score;
|
use crate::score::Score;
|
||||||
|
use crate::topology::ingress::Ingress;
|
||||||
use crate::topology::oberservability::monitoring::AlertReceiver;
|
use crate::topology::oberservability::monitoring::AlertReceiver;
|
||||||
use crate::topology::{K8sclient, Topology, k8s::K8sClient};
|
use crate::topology::{K8sclient, Topology, k8s::K8sClient};
|
||||||
use crate::{
|
use crate::{
|
||||||
@@ -48,8 +56,8 @@ pub struct RHOBAlertingScore {
|
|||||||
pub prometheus_rules: Vec<RuleGroup>,
|
pub prometheus_rules: Vec<RuleGroup>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Topology + K8sclient + PrometheusApplicationMonitoring<RHOBObservability>> Score<T>
|
impl<T: Topology + K8sclient + Ingress + PrometheusApplicationMonitoring<RHOBObservability>>
|
||||||
for RHOBAlertingScore
|
Score<T> for RHOBAlertingScore
|
||||||
{
|
{
|
||||||
fn create_interpret(&self) -> Box<dyn crate::interpret::Interpret<T>> {
|
fn create_interpret(&self) -> Box<dyn crate::interpret::Interpret<T>> {
|
||||||
Box::new(RHOBAlertingInterpret {
|
Box::new(RHOBAlertingInterpret {
|
||||||
@@ -74,19 +82,20 @@ pub struct RHOBAlertingInterpret {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl<T: Topology + K8sclient + PrometheusApplicationMonitoring<RHOBObservability>> Interpret<T>
|
impl<T: Topology + K8sclient + Ingress + PrometheusApplicationMonitoring<RHOBObservability>>
|
||||||
for RHOBAlertingInterpret
|
Interpret<T> for RHOBAlertingInterpret
|
||||||
{
|
{
|
||||||
async fn execute(
|
async fn execute(
|
||||||
&self,
|
&self,
|
||||||
_inventory: &Inventory,
|
inventory: &Inventory,
|
||||||
topology: &T,
|
topology: &T,
|
||||||
) -> Result<Outcome, InterpretError> {
|
) -> Result<Outcome, InterpretError> {
|
||||||
let client = topology.k8s_client().await.unwrap();
|
let client = topology.k8s_client().await.unwrap();
|
||||||
self.ensure_grafana_operator().await?;
|
self.ensure_grafana_operator().await?;
|
||||||
self.install_prometheus(&client).await?;
|
self.install_prometheus(inventory, topology, &client)
|
||||||
|
.await?;
|
||||||
self.install_client_kube_metrics().await?;
|
self.install_client_kube_metrics().await?;
|
||||||
self.install_grafana(&client).await?;
|
self.install_grafana(inventory, topology, &client).await?;
|
||||||
self.install_receivers(&self.sender, &self.receivers)
|
self.install_receivers(&self.sender, &self.receivers)
|
||||||
.await?;
|
.await?;
|
||||||
self.install_rules(&self.prometheus_rules, &client).await?;
|
self.install_rules(&self.prometheus_rules, &client).await?;
|
||||||
@@ -212,7 +221,8 @@ impl RHOBAlertingInterpret {
|
|||||||
|
|
||||||
let output = Command::new("helm")
|
let output = Command::new("helm")
|
||||||
.args([
|
.args([
|
||||||
"install",
|
"upgrade",
|
||||||
|
"--install",
|
||||||
"grafana-operator",
|
"grafana-operator",
|
||||||
"grafana-operator/grafana-operator",
|
"grafana-operator/grafana-operator",
|
||||||
"--namespace",
|
"--namespace",
|
||||||
@@ -226,7 +236,7 @@ impl RHOBAlertingInterpret {
|
|||||||
|
|
||||||
if !output.status.success() {
|
if !output.status.success() {
|
||||||
return Err(InterpretError::new(format!(
|
return Err(InterpretError::new(format!(
|
||||||
"helm install failed:\nstdout: {}\nstderr: {}",
|
"helm upgrade --install failed:\nstdout: {}\nstderr: {}",
|
||||||
String::from_utf8_lossy(&output.stdout),
|
String::from_utf8_lossy(&output.stdout),
|
||||||
String::from_utf8_lossy(&output.stderr)
|
String::from_utf8_lossy(&output.stderr)
|
||||||
)));
|
)));
|
||||||
@@ -238,25 +248,31 @@ impl RHOBAlertingInterpret {
|
|||||||
)))
|
)))
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn install_prometheus(&self, client: &Arc<K8sClient>) -> Result<Outcome, InterpretError> {
|
async fn install_prometheus<T: Topology + K8sclient + Ingress>(
|
||||||
|
&self,
|
||||||
|
inventory: &Inventory,
|
||||||
|
topology: &T,
|
||||||
|
client: &Arc<K8sClient>,
|
||||||
|
) -> Result<Outcome, InterpretError> {
|
||||||
debug!(
|
debug!(
|
||||||
"installing crd-prometheuses in namespace {}",
|
"installing crd-prometheuses in namespace {}",
|
||||||
self.sender.namespace.clone()
|
self.sender.namespace.clone()
|
||||||
);
|
);
|
||||||
|
debug!("building role/rolebinding/serviceaccount for crd-prometheus");
|
||||||
|
|
||||||
let stack = MonitoringStack {
|
let stack = MonitoringStack {
|
||||||
metadata: ObjectMeta {
|
metadata: ObjectMeta {
|
||||||
name: Some(format!("{}-monitoring", self.sender.namespace.clone()).into()),
|
name: Some(format!("{}-monitoring", self.sender.namespace.clone()).into()),
|
||||||
namespace: Some(self.sender.namespace.clone()),
|
namespace: Some(self.sender.namespace.clone()),
|
||||||
labels: Some([("coo".into(), "example".into())].into()),
|
labels: Some([("monitoring-stack".into(), "true".into())].into()),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
spec: MonitoringStackSpec {
|
spec: MonitoringStackSpec {
|
||||||
log_level: Some("debug".into()),
|
log_level: Some("debug".into()),
|
||||||
retention: Some("1d".into()),
|
retention: Some("1d".into()),
|
||||||
resource_selector: Some(LabelSelector {
|
resource_selector: Some(LabelSelector {
|
||||||
match_labels: [("app".into(), "demo".into())].into(),
|
match_labels: Default::default(),
|
||||||
..Default::default()
|
match_expressions: vec![],
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
@@ -265,6 +281,42 @@ impl RHOBAlertingInterpret {
|
|||||||
.apply(&stack, Some(&self.sender.namespace.clone()))
|
.apply(&stack, Some(&self.sender.namespace.clone()))
|
||||||
.await
|
.await
|
||||||
.map_err(|e| InterpretError::new(e.to_string()))?;
|
.map_err(|e| InterpretError::new(e.to_string()))?;
|
||||||
|
|
||||||
|
let alert_manager_domain = topology
|
||||||
|
.get_domain(&format!("alert-manager-{}", self.sender.namespace.clone()))
|
||||||
|
.await?;
|
||||||
|
let name = format!("{}-alert-manager", self.sender.namespace.clone());
|
||||||
|
let backend_service = format!("alertmanager-operated");
|
||||||
|
let namespace = self.sender.namespace.clone();
|
||||||
|
let alert_manager_ingress = K8sIngressScore {
|
||||||
|
name: fqdn!(&name),
|
||||||
|
host: fqdn!(&alert_manager_domain),
|
||||||
|
backend_service: fqdn!(&backend_service),
|
||||||
|
port: 9093,
|
||||||
|
path: Some("/".to_string()),
|
||||||
|
path_type: Some(PathType::Prefix),
|
||||||
|
namespace: Some(fqdn!(&namespace)),
|
||||||
|
ingress_class_name: Some("openshift-default".to_string()),
|
||||||
|
};
|
||||||
|
|
||||||
|
let prometheus_domain = topology
|
||||||
|
.get_domain(&format!("prometheus-{}", self.sender.namespace.clone()))
|
||||||
|
.await?;
|
||||||
|
let name = format!("{}-prometheus", self.sender.namespace.clone());
|
||||||
|
let backend_service = format!("prometheus-operated");
|
||||||
|
let prometheus_ingress = K8sIngressScore {
|
||||||
|
name: fqdn!(&name),
|
||||||
|
host: fqdn!(&prometheus_domain),
|
||||||
|
backend_service: fqdn!(&backend_service),
|
||||||
|
port: 9090,
|
||||||
|
path: Some("/".to_string()),
|
||||||
|
path_type: Some(PathType::Prefix),
|
||||||
|
namespace: Some(fqdn!(&namespace)),
|
||||||
|
ingress_class_name: Some("openshift-default".to_string()),
|
||||||
|
};
|
||||||
|
|
||||||
|
alert_manager_ingress.interpret(inventory, topology).await?;
|
||||||
|
prometheus_ingress.interpret(inventory, topology).await?;
|
||||||
info!("installed rhob monitoring stack",);
|
info!("installed rhob monitoring stack",);
|
||||||
Ok(Outcome::success(format!(
|
Ok(Outcome::success(format!(
|
||||||
"successfully deployed rhob-prometheus {:#?}",
|
"successfully deployed rhob-prometheus {:#?}",
|
||||||
@@ -272,31 +324,6 @@ impl RHOBAlertingInterpret {
|
|||||||
)))
|
)))
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn install_alert_manager(
|
|
||||||
&self,
|
|
||||||
client: &Arc<K8sClient>,
|
|
||||||
) -> Result<Outcome, InterpretError> {
|
|
||||||
let am = Alertmanager {
|
|
||||||
metadata: ObjectMeta {
|
|
||||||
name: Some(self.sender.namespace.clone()),
|
|
||||||
labels: Some(std::collections::BTreeMap::from([(
|
|
||||||
"alertmanagerConfig".to_string(),
|
|
||||||
"enabled".to_string(),
|
|
||||||
)])),
|
|
||||||
namespace: Some(self.sender.namespace.clone()),
|
|
||||||
..Default::default()
|
|
||||||
},
|
|
||||||
spec: AlertmanagerSpec::default(),
|
|
||||||
};
|
|
||||||
client
|
|
||||||
.apply(&am, Some(&self.sender.namespace.clone()))
|
|
||||||
.await
|
|
||||||
.map_err(|e| InterpretError::new(e.to_string()))?;
|
|
||||||
Ok(Outcome::success(format!(
|
|
||||||
"successfully deployed service monitor {:#?}",
|
|
||||||
am.metadata.name
|
|
||||||
)))
|
|
||||||
}
|
|
||||||
async fn install_monitors(
|
async fn install_monitors(
|
||||||
&self,
|
&self,
|
||||||
mut monitors: Vec<ServiceMonitor>,
|
mut monitors: Vec<ServiceMonitor>,
|
||||||
@@ -379,7 +406,12 @@ impl RHOBAlertingInterpret {
|
|||||||
)))
|
)))
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn install_grafana(&self, client: &Arc<K8sClient>) -> Result<Outcome, InterpretError> {
|
async fn install_grafana<T: Topology + K8sclient + Ingress>(
|
||||||
|
&self,
|
||||||
|
inventory: &Inventory,
|
||||||
|
topology: &T,
|
||||||
|
client: &Arc<K8sClient>,
|
||||||
|
) -> Result<Outcome, InterpretError> {
|
||||||
let mut label = BTreeMap::new();
|
let mut label = BTreeMap::new();
|
||||||
label.insert("dashboards".to_string(), "grafana".to_string());
|
label.insert("dashboards".to_string(), "grafana".to_string());
|
||||||
let labels = LabelSelector {
|
let labels = LabelSelector {
|
||||||
@@ -465,6 +497,23 @@ impl RHOBAlertingInterpret {
|
|||||||
.apply(&grafana, Some(&self.sender.namespace.clone()))
|
.apply(&grafana, Some(&self.sender.namespace.clone()))
|
||||||
.await
|
.await
|
||||||
.map_err(|e| InterpretError::new(e.to_string()))?;
|
.map_err(|e| InterpretError::new(e.to_string()))?;
|
||||||
|
let domain = topology
|
||||||
|
.get_domain(&format!("grafana-{}", self.sender.namespace.clone()))
|
||||||
|
.await?;
|
||||||
|
let name = format!("{}-grafana", self.sender.namespace.clone());
|
||||||
|
let backend_service = format!("grafana-{}-service", self.sender.namespace.clone());
|
||||||
|
let grafana_ingress = K8sIngressScore {
|
||||||
|
name: fqdn!(&name),
|
||||||
|
host: fqdn!(&domain),
|
||||||
|
backend_service: fqdn!(&backend_service),
|
||||||
|
port: 3000,
|
||||||
|
path: Some("/".to_string()),
|
||||||
|
path_type: Some(PathType::Prefix),
|
||||||
|
namespace: Some(fqdn!(&namespace)),
|
||||||
|
ingress_class_name: Some("openshift-default".to_string()),
|
||||||
|
};
|
||||||
|
|
||||||
|
grafana_ingress.interpret(inventory, topology).await?;
|
||||||
Ok(Outcome::success(format!(
|
Ok(Outcome::success(format!(
|
||||||
"successfully deployed grafana instance {:#?}",
|
"successfully deployed grafana instance {:#?}",
|
||||||
grafana.metadata.name
|
grafana.metadata.name
|
||||||
|
|||||||
@@ -120,10 +120,26 @@ impl SecretManager {
|
|||||||
|
|
||||||
let ns = &manager.namespace;
|
let ns = &manager.namespace;
|
||||||
let key = T::KEY;
|
let key = T::KEY;
|
||||||
let secret_json = inquire::Text::new(&format!(
|
let secret_json = inquire::Editor::new(&format!(
|
||||||
"Secret not found for {} {}, paste the JSON here :",
|
"Secret not found for {ns} {key}, paste the JSON here :",
|
||||||
ns, key
|
|
||||||
))
|
))
|
||||||
|
.with_formatter(&|data| {
|
||||||
|
let char_count = data.chars().count();
|
||||||
|
if char_count == 0 {
|
||||||
|
String::from("<skipped>")
|
||||||
|
} else if char_count <= 20 {
|
||||||
|
data.into()
|
||||||
|
} else {
|
||||||
|
let mut substr: String = data.chars().take(17).collect();
|
||||||
|
substr.push_str("...");
|
||||||
|
substr
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.with_render_config(
|
||||||
|
inquire::ui::RenderConfig::default().with_canceled_prompt_indicator(
|
||||||
|
inquire::ui::Styled::new("<skipped>").with_fg(inquire::ui::Color::DarkYellow),
|
||||||
|
),
|
||||||
|
)
|
||||||
.prompt()
|
.prompt()
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
SecretStoreError::Store(format!("Failed to prompt secret {ns} {key} : {e}").into())
|
SecretStoreError::Store(format!("Failed to prompt secret {ns} {key} : {e}").into())
|
||||||
|
|||||||
@@ -2,8 +2,8 @@ mod downloadable_asset;
|
|||||||
use downloadable_asset::*;
|
use downloadable_asset::*;
|
||||||
|
|
||||||
use kube::Client;
|
use kube::Client;
|
||||||
use log::debug;
|
use log::{debug, info};
|
||||||
use std::path::PathBuf;
|
use std::{ffi::OsStr, path::PathBuf};
|
||||||
|
|
||||||
const K3D_BIN_FILE_NAME: &str = "k3d";
|
const K3D_BIN_FILE_NAME: &str = "k3d";
|
||||||
|
|
||||||
@@ -213,15 +213,19 @@ impl K3d {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let client;
|
||||||
if !self.is_cluster_initialized() {
|
if !self.is_cluster_initialized() {
|
||||||
debug!("Cluster is not initialized, initializing now");
|
debug!("Cluster is not initialized, initializing now");
|
||||||
return self.initialize_cluster().await;
|
client = self.initialize_cluster().await?;
|
||||||
|
} else {
|
||||||
|
self.start_cluster().await?;
|
||||||
|
|
||||||
|
debug!("K3d and cluster are already properly set up");
|
||||||
|
client = self.create_kubernetes_client().await?;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.start_cluster().await?;
|
self.ensure_k3d_config_is_default(self.get_cluster_name()?)?;
|
||||||
|
Ok(client)
|
||||||
debug!("K3d and cluster are already properly set up");
|
|
||||||
self.create_kubernetes_client().await
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Private helper methods
|
// Private helper methods
|
||||||
@@ -302,7 +306,16 @@ impl K3d {
|
|||||||
S: AsRef<std::ffi::OsStr>,
|
S: AsRef<std::ffi::OsStr>,
|
||||||
{
|
{
|
||||||
let binary_path = self.get_k3d_binary()?;
|
let binary_path = self.get_k3d_binary()?;
|
||||||
let output = std::process::Command::new(binary_path).args(args).output();
|
self.run_command(binary_path, args)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn run_command<I, S, C>(&self, cmd: C, args: I) -> Result<std::process::Output, String>
|
||||||
|
where
|
||||||
|
I: IntoIterator<Item = S>,
|
||||||
|
S: AsRef<std::ffi::OsStr>,
|
||||||
|
C: AsRef<OsStr>,
|
||||||
|
{
|
||||||
|
let output = std::process::Command::new(cmd).args(args).output();
|
||||||
match output {
|
match output {
|
||||||
Ok(output) => {
|
Ok(output) => {
|
||||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||||
@@ -311,7 +324,7 @@ impl K3d {
|
|||||||
debug!("stdout : {}", stdout);
|
debug!("stdout : {}", stdout);
|
||||||
Ok(output)
|
Ok(output)
|
||||||
}
|
}
|
||||||
Err(e) => Err(format!("Failed to execute k3d command: {}", e)),
|
Err(e) => Err(format!("Failed to execute command: {}", e)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -323,12 +336,38 @@ impl K3d {
|
|||||||
return Err(format!("Failed to create cluster: {}", stderr));
|
return Err(format!("Failed to create cluster: {}", stderr));
|
||||||
}
|
}
|
||||||
|
|
||||||
debug!("Successfully created k3d cluster '{}'", cluster_name);
|
info!("Successfully created k3d cluster '{}'", cluster_name);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn ensure_k3d_config_is_default(&self, cluster_name: &str) -> Result<(), String> {
|
||||||
|
let output = self.run_k3d_command(["kubeconfig", "merge", "-d", cluster_name])?;
|
||||||
|
|
||||||
|
if !output.status.success() {
|
||||||
|
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||||
|
return Err(format!("Failed to setup k3d kubeconfig : {}", stderr));
|
||||||
|
}
|
||||||
|
|
||||||
|
let output = self.run_command(
|
||||||
|
"kubectl",
|
||||||
|
["config", "use-context", &format!("k3d-{cluster_name}")],
|
||||||
|
)?;
|
||||||
|
|
||||||
|
if !output.status.success() {
|
||||||
|
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||||
|
return Err(format!(
|
||||||
|
"Failed to switch kubectl context to k3d : {}",
|
||||||
|
stderr
|
||||||
|
));
|
||||||
|
}
|
||||||
|
info!(
|
||||||
|
"kubectl is now using 'k3d-{}' as default context",
|
||||||
|
cluster_name
|
||||||
|
);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn create_kubernetes_client(&self) -> Result<Client, String> {
|
async fn create_kubernetes_client(&self) -> Result<Client, String> {
|
||||||
// TODO: Connect the client to the right k3d cluster (see https://git.nationtech.io/NationTech/harmony/issues/92)
|
|
||||||
Client::try_default()
|
Client::try_default()
|
||||||
.await
|
.await
|
||||||
.map_err(|e| format!("Failed to create Kubernetes client: {}", e))
|
.map_err(|e| format!("Failed to create Kubernetes client: {}", e))
|
||||||
|
|||||||
@@ -77,7 +77,7 @@ impl YaSerializeTrait for HAProxyId {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(PartialEq, Debug, Clone)]
|
#[derive(PartialEq, Debug)]
|
||||||
pub struct HAProxyId(String);
|
pub struct HAProxyId(String);
|
||||||
|
|
||||||
impl Default for HAProxyId {
|
impl Default for HAProxyId {
|
||||||
@@ -297,7 +297,7 @@ pub struct HAProxyFrontends {
|
|||||||
pub frontend: Vec<Frontend>,
|
pub frontend: Vec<Frontend>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Default, PartialEq, Debug, YaSerialize, YaDeserialize)]
|
#[derive(Default, PartialEq, Debug, YaSerialize, YaDeserialize)]
|
||||||
pub struct Frontend {
|
pub struct Frontend {
|
||||||
#[yaserde(attribute = true)]
|
#[yaserde(attribute = true)]
|
||||||
pub uuid: String,
|
pub uuid: String,
|
||||||
@@ -310,7 +310,7 @@ pub struct Frontend {
|
|||||||
pub bind_options: MaybeString,
|
pub bind_options: MaybeString,
|
||||||
pub mode: String,
|
pub mode: String,
|
||||||
#[yaserde(rename = "defaultBackend")]
|
#[yaserde(rename = "defaultBackend")]
|
||||||
pub default_backend: Option<String>,
|
pub default_backend: String,
|
||||||
pub ssl_enabled: i32,
|
pub ssl_enabled: i32,
|
||||||
pub ssl_certificates: MaybeString,
|
pub ssl_certificates: MaybeString,
|
||||||
pub ssl_default_certificate: MaybeString,
|
pub ssl_default_certificate: MaybeString,
|
||||||
@@ -416,7 +416,7 @@ pub struct HAProxyBackends {
|
|||||||
pub backends: Vec<HAProxyBackend>,
|
pub backends: Vec<HAProxyBackend>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Default, PartialEq, Debug, YaSerialize, YaDeserialize)]
|
#[derive(Default, PartialEq, Debug, YaSerialize, YaDeserialize)]
|
||||||
pub struct HAProxyBackend {
|
pub struct HAProxyBackend {
|
||||||
#[yaserde(attribute = true, rename = "uuid")]
|
#[yaserde(attribute = true, rename = "uuid")]
|
||||||
pub uuid: String,
|
pub uuid: String,
|
||||||
@@ -535,7 +535,7 @@ pub struct HAProxyServers {
|
|||||||
pub servers: Vec<HAProxyServer>,
|
pub servers: Vec<HAProxyServer>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Default, PartialEq, Debug, YaSerialize, YaDeserialize)]
|
#[derive(Default, PartialEq, Debug, YaSerialize, YaDeserialize)]
|
||||||
pub struct HAProxyServer {
|
pub struct HAProxyServer {
|
||||||
#[yaserde(attribute = true, rename = "uuid")]
|
#[yaserde(attribute = true, rename = "uuid")]
|
||||||
pub uuid: String,
|
pub uuid: String,
|
||||||
@@ -543,8 +543,8 @@ pub struct HAProxyServer {
|
|||||||
pub enabled: u8,
|
pub enabled: u8,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub description: MaybeString,
|
pub description: MaybeString,
|
||||||
pub address: Option<String>,
|
pub address: String,
|
||||||
pub port: Option<u16>,
|
pub port: u16,
|
||||||
pub checkport: MaybeString,
|
pub checkport: MaybeString,
|
||||||
pub mode: String,
|
pub mode: String,
|
||||||
pub multiplexer_protocol: MaybeString,
|
pub multiplexer_protocol: MaybeString,
|
||||||
@@ -589,7 +589,7 @@ pub struct HAProxyHealthChecks {
|
|||||||
pub healthchecks: Vec<HAProxyHealthCheck>,
|
pub healthchecks: Vec<HAProxyHealthCheck>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Default, PartialEq, Debug, YaSerialize, YaDeserialize)]
|
#[derive(Default, PartialEq, Debug, YaSerialize, YaDeserialize)]
|
||||||
pub struct HAProxyHealthCheck {
|
pub struct HAProxyHealthCheck {
|
||||||
#[yaserde(attribute = true)]
|
#[yaserde(attribute = true)]
|
||||||
pub uuid: String,
|
pub uuid: String,
|
||||||
|
|||||||
@@ -25,7 +25,6 @@ sha2 = "0.10.9"
|
|||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
pretty_assertions.workspace = true
|
pretty_assertions.workspace = true
|
||||||
assertor.workspace = true
|
|
||||||
|
|
||||||
[lints.rust]
|
[lints.rust]
|
||||||
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(e2e_test)'] }
|
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(e2e_test)'] }
|
||||||
|
|||||||
@@ -30,7 +30,8 @@ impl SshConfigManager {
|
|||||||
|
|
||||||
self.opnsense_shell
|
self.opnsense_shell
|
||||||
.exec(&format!(
|
.exec(&format!(
|
||||||
"cp /conf/config.xml /conf/backup/{backup_filename}"
|
"cp /conf/config.xml /conf/backup/{}",
|
||||||
|
backup_filename
|
||||||
))
|
))
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
mod ssh;
|
mod ssh;
|
||||||
use crate::Error;
|
|
||||||
use async_trait::async_trait;
|
|
||||||
pub use ssh::*;
|
pub use ssh::*;
|
||||||
|
|
||||||
|
use async_trait::async_trait;
|
||||||
|
|
||||||
|
use crate::Error;
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
pub trait OPNsenseShell: std::fmt::Debug + Send + Sync {
|
pub trait OPNsenseShell: std::fmt::Debug + Send + Sync {
|
||||||
async fn exec(&self, command: &str) -> Result<String, Error>;
|
async fn exec(&self, command: &str) -> Result<String, Error>;
|
||||||
|
|||||||
@@ -1,8 +1,11 @@
|
|||||||
use crate::{config::OPNsenseShell, Error};
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use log::warn;
|
||||||
use opnsense_config_xml::{
|
use opnsense_config_xml::{
|
||||||
Frontend, HAProxy, HAProxyBackend, HAProxyHealthCheck, HAProxyServer, OPNsense,
|
Frontend, HAProxy, HAProxyBackend, HAProxyHealthCheck, HAProxyServer, OPNsense,
|
||||||
};
|
};
|
||||||
use std::{collections::HashSet, sync::Arc};
|
|
||||||
|
use crate::{config::OPNsenseShell, Error};
|
||||||
|
|
||||||
pub struct LoadBalancerConfig<'a> {
|
pub struct LoadBalancerConfig<'a> {
|
||||||
opnsense: &'a mut OPNsense,
|
opnsense: &'a mut OPNsense,
|
||||||
@@ -28,7 +31,7 @@ impl<'a> LoadBalancerConfig<'a> {
|
|||||||
match &mut self.opnsense.opnsense.haproxy.as_mut() {
|
match &mut self.opnsense.opnsense.haproxy.as_mut() {
|
||||||
Some(haproxy) => f(haproxy),
|
Some(haproxy) => f(haproxy),
|
||||||
None => unimplemented!(
|
None => unimplemented!(
|
||||||
"Cannot configure load balancer when haproxy config does not exist yet"
|
"Adding a backend is not supported when haproxy config does not exist yet"
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -37,67 +40,21 @@ impl<'a> LoadBalancerConfig<'a> {
|
|||||||
self.with_haproxy(|haproxy| haproxy.general.enabled = enabled as i32);
|
self.with_haproxy(|haproxy| haproxy.general.enabled = enabled as i32);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Configures a service by removing any existing service on the same port
|
pub fn add_backend(&mut self, backend: HAProxyBackend) {
|
||||||
/// and then adding the new definition. This ensures idempotency.
|
warn!("TODO make sure this new backend does not refer non-existing entities like servers or health checks");
|
||||||
pub fn configure_service(
|
self.with_haproxy(|haproxy| haproxy.backends.backends.push(backend));
|
||||||
&mut self,
|
|
||||||
frontend: Frontend,
|
|
||||||
backend: HAProxyBackend,
|
|
||||||
servers: Vec<HAProxyServer>,
|
|
||||||
healthcheck: Option<HAProxyHealthCheck>,
|
|
||||||
) {
|
|
||||||
self.remove_service_by_bind_address(&frontend.bind);
|
|
||||||
self.remove_servers(&servers);
|
|
||||||
|
|
||||||
self.add_new_service(frontend, backend, servers, healthcheck);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove the corresponding real servers based on their name if they already exist.
|
pub fn add_frontend(&mut self, frontend: Frontend) {
|
||||||
fn remove_servers(&mut self, servers: &[HAProxyServer]) {
|
self.with_haproxy(|haproxy| haproxy.frontends.frontend.push(frontend));
|
||||||
let server_names: HashSet<_> = servers.iter().map(|s| s.name.clone()).collect();
|
|
||||||
self.with_haproxy(|haproxy| {
|
|
||||||
haproxy
|
|
||||||
.servers
|
|
||||||
.servers
|
|
||||||
.retain(|s| !server_names.contains(&s.name));
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Removes a service and its dependent components based on the frontend's bind address.
|
pub fn add_healthcheck(&mut self, healthcheck: HAProxyHealthCheck) {
|
||||||
/// This performs a cascading delete of the frontend, backend, servers, and health check.
|
self.with_haproxy(|haproxy| haproxy.healthchecks.healthchecks.push(healthcheck));
|
||||||
fn remove_service_by_bind_address(&mut self, bind_address: &str) {
|
|
||||||
self.with_haproxy(|haproxy| {
|
|
||||||
let Some(old_frontend) = remove_frontend_by_bind_address(haproxy, bind_address) else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
|
|
||||||
let Some(old_backend) = remove_backend(haproxy, old_frontend) else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
|
|
||||||
remove_healthcheck(haproxy, &old_backend);
|
|
||||||
remove_linked_servers(haproxy, &old_backend);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Adds the components of a new service to the HAProxy configuration.
|
pub fn add_servers(&mut self, mut servers: Vec<HAProxyServer>) {
|
||||||
/// This function de-duplicates servers by name to prevent configuration errors.
|
self.with_haproxy(|haproxy| haproxy.servers.servers.append(&mut servers));
|
||||||
fn add_new_service(
|
|
||||||
&mut self,
|
|
||||||
frontend: Frontend,
|
|
||||||
backend: HAProxyBackend,
|
|
||||||
servers: Vec<HAProxyServer>,
|
|
||||||
healthcheck: Option<HAProxyHealthCheck>,
|
|
||||||
) {
|
|
||||||
self.with_haproxy(|haproxy| {
|
|
||||||
if let Some(check) = healthcheck {
|
|
||||||
haproxy.healthchecks.healthchecks.push(check);
|
|
||||||
}
|
|
||||||
|
|
||||||
haproxy.servers.servers.extend(servers);
|
|
||||||
haproxy.backends.backends.push(backend);
|
|
||||||
haproxy.frontends.frontend.push(frontend);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn reload_restart(&self) -> Result<(), Error> {
|
pub async fn reload_restart(&self) -> Result<(), Error> {
|
||||||
@@ -125,262 +82,3 @@ impl<'a> LoadBalancerConfig<'a> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remove_frontend_by_bind_address(haproxy: &mut HAProxy, bind_address: &str) -> Option<Frontend> {
|
|
||||||
let pos = haproxy
|
|
||||||
.frontends
|
|
||||||
.frontend
|
|
||||||
.iter()
|
|
||||||
.position(|f| f.bind == bind_address);
|
|
||||||
|
|
||||||
match pos {
|
|
||||||
Some(pos) => Some(haproxy.frontends.frontend.remove(pos)),
|
|
||||||
None => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn remove_backend(haproxy: &mut HAProxy, old_frontend: Frontend) -> Option<HAProxyBackend> {
|
|
||||||
let default_backend = old_frontend.default_backend?;
|
|
||||||
let pos = haproxy
|
|
||||||
.backends
|
|
||||||
.backends
|
|
||||||
.iter()
|
|
||||||
.position(|b| b.uuid == default_backend);
|
|
||||||
|
|
||||||
match pos {
|
|
||||||
Some(pos) => Some(haproxy.backends.backends.remove(pos)),
|
|
||||||
None => None, // orphaned frontend, shouldn't happen
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn remove_healthcheck(haproxy: &mut HAProxy, backend: &HAProxyBackend) {
|
|
||||||
if let Some(uuid) = &backend.health_check.content {
|
|
||||||
haproxy
|
|
||||||
.healthchecks
|
|
||||||
.healthchecks
|
|
||||||
.retain(|h| h.uuid != *uuid);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Remove the backend's servers. This assumes servers are not shared between services.
|
|
||||||
fn remove_linked_servers(haproxy: &mut HAProxy, backend: &HAProxyBackend) {
|
|
||||||
if let Some(server_uuids_str) = &backend.linked_servers.content {
|
|
||||||
let server_uuids_to_remove: HashSet<_> = server_uuids_str.split(',').collect();
|
|
||||||
haproxy
|
|
||||||
.servers
|
|
||||||
.servers
|
|
||||||
.retain(|s| !server_uuids_to_remove.contains(s.uuid.as_str()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use crate::config::DummyOPNSenseShell;
|
|
||||||
use assertor::*;
|
|
||||||
use opnsense_config_xml::{
|
|
||||||
Frontend, HAProxy, HAProxyBackend, HAProxyBackends, HAProxyFrontends, HAProxyHealthCheck,
|
|
||||||
HAProxyHealthChecks, HAProxyId, HAProxyServer, HAProxyServers, MaybeString, OPNsense,
|
|
||||||
};
|
|
||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
use super::LoadBalancerConfig;
|
|
||||||
|
|
||||||
static SERVICE_BIND_ADDRESS: &str = "192.168.1.1:80";
|
|
||||||
static OTHER_SERVICE_BIND_ADDRESS: &str = "192.168.1.1:443";
|
|
||||||
|
|
||||||
static SERVER_ADDRESS: &str = "1.1.1.1:80";
|
|
||||||
static OTHER_SERVER_ADDRESS: &str = "1.1.1.1:443";
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn configure_service_should_add_all_service_components_to_haproxy() {
|
|
||||||
let mut opnsense = given_opnsense();
|
|
||||||
let mut load_balancer = given_load_balancer(&mut opnsense);
|
|
||||||
let (healthcheck, servers, backend, frontend) =
|
|
||||||
given_service(SERVICE_BIND_ADDRESS, SERVER_ADDRESS);
|
|
||||||
|
|
||||||
load_balancer.configure_service(
|
|
||||||
frontend.clone(),
|
|
||||||
backend.clone(),
|
|
||||||
servers.clone(),
|
|
||||||
Some(healthcheck.clone()),
|
|
||||||
);
|
|
||||||
|
|
||||||
assert_haproxy_configured_with(
|
|
||||||
opnsense,
|
|
||||||
vec![frontend],
|
|
||||||
vec![backend],
|
|
||||||
servers,
|
|
||||||
vec![healthcheck],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn configure_service_should_replace_service_on_same_bind_address() {
|
|
||||||
let (healthcheck, servers, backend, frontend) =
|
|
||||||
given_service(SERVICE_BIND_ADDRESS, SERVER_ADDRESS);
|
|
||||||
let mut opnsense = given_opnsense_with(given_haproxy(
|
|
||||||
vec![frontend.clone()],
|
|
||||||
vec![backend.clone()],
|
|
||||||
servers.clone(),
|
|
||||||
vec![healthcheck.clone()],
|
|
||||||
));
|
|
||||||
let mut load_balancer = given_load_balancer(&mut opnsense);
|
|
||||||
|
|
||||||
let (updated_healthcheck, updated_servers, updated_backend, updated_frontend) =
|
|
||||||
given_service(SERVICE_BIND_ADDRESS, OTHER_SERVER_ADDRESS);
|
|
||||||
|
|
||||||
load_balancer.configure_service(
|
|
||||||
updated_frontend.clone(),
|
|
||||||
updated_backend.clone(),
|
|
||||||
updated_servers.clone(),
|
|
||||||
Some(updated_healthcheck.clone()),
|
|
||||||
);
|
|
||||||
|
|
||||||
assert_haproxy_configured_with(
|
|
||||||
opnsense,
|
|
||||||
vec![updated_frontend],
|
|
||||||
vec![updated_backend],
|
|
||||||
updated_servers,
|
|
||||||
vec![updated_healthcheck],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn configure_service_should_keep_existing_service_on_different_bind_addresses() {
|
|
||||||
let (healthcheck, servers, backend, frontend) =
|
|
||||||
given_service(SERVICE_BIND_ADDRESS, SERVER_ADDRESS);
|
|
||||||
let (other_healthcheck, other_servers, other_backend, other_frontend) =
|
|
||||||
given_service(OTHER_SERVICE_BIND_ADDRESS, OTHER_SERVER_ADDRESS);
|
|
||||||
let mut opnsense = given_opnsense_with(given_haproxy(
|
|
||||||
vec![frontend.clone()],
|
|
||||||
vec![backend.clone()],
|
|
||||||
servers.clone(),
|
|
||||||
vec![healthcheck.clone()],
|
|
||||||
));
|
|
||||||
let mut load_balancer = given_load_balancer(&mut opnsense);
|
|
||||||
|
|
||||||
load_balancer.configure_service(
|
|
||||||
other_frontend.clone(),
|
|
||||||
other_backend.clone(),
|
|
||||||
other_servers.clone(),
|
|
||||||
Some(other_healthcheck.clone()),
|
|
||||||
);
|
|
||||||
|
|
||||||
assert_haproxy_configured_with(
|
|
||||||
opnsense,
|
|
||||||
vec![frontend, other_frontend],
|
|
||||||
vec![backend, other_backend],
|
|
||||||
[servers, other_servers].concat(),
|
|
||||||
vec![healthcheck, other_healthcheck],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn assert_haproxy_configured_with(
|
|
||||||
opnsense: OPNsense,
|
|
||||||
frontends: Vec<Frontend>,
|
|
||||||
backends: Vec<HAProxyBackend>,
|
|
||||||
servers: Vec<HAProxyServer>,
|
|
||||||
healthchecks: Vec<HAProxyHealthCheck>,
|
|
||||||
) {
|
|
||||||
let haproxy = opnsense.opnsense.haproxy.as_ref().unwrap();
|
|
||||||
assert_that!(haproxy.frontends.frontend).contains_exactly(frontends);
|
|
||||||
assert_that!(haproxy.backends.backends).contains_exactly(backends);
|
|
||||||
assert_that!(haproxy.servers.servers).is_equal_to(servers);
|
|
||||||
assert_that!(haproxy.healthchecks.healthchecks).contains_exactly(healthchecks);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn given_opnsense() -> OPNsense {
|
|
||||||
OPNsense::default()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn given_opnsense_with(haproxy: HAProxy) -> OPNsense {
|
|
||||||
let mut opnsense = OPNsense::default();
|
|
||||||
opnsense.opnsense.haproxy = Some(haproxy);
|
|
||||||
|
|
||||||
opnsense
|
|
||||||
}
|
|
||||||
|
|
||||||
fn given_load_balancer<'a>(opnsense: &'a mut OPNsense) -> LoadBalancerConfig<'a> {
|
|
||||||
let opnsense_shell = Arc::new(DummyOPNSenseShell {});
|
|
||||||
if opnsense.opnsense.haproxy.is_none() {
|
|
||||||
opnsense.opnsense.haproxy = Some(HAProxy::default());
|
|
||||||
}
|
|
||||||
LoadBalancerConfig::new(opnsense, opnsense_shell)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn given_service(
|
|
||||||
bind_address: &str,
|
|
||||||
server_address: &str,
|
|
||||||
) -> (
|
|
||||||
HAProxyHealthCheck,
|
|
||||||
Vec<HAProxyServer>,
|
|
||||||
HAProxyBackend,
|
|
||||||
Frontend,
|
|
||||||
) {
|
|
||||||
let healthcheck = given_healthcheck();
|
|
||||||
let servers = vec![given_server(server_address)];
|
|
||||||
let backend = given_backend();
|
|
||||||
let frontend = given_frontend(bind_address);
|
|
||||||
(healthcheck, servers, backend, frontend)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn given_haproxy(
|
|
||||||
frontends: Vec<Frontend>,
|
|
||||||
backends: Vec<HAProxyBackend>,
|
|
||||||
servers: Vec<HAProxyServer>,
|
|
||||||
healthchecks: Vec<HAProxyHealthCheck>,
|
|
||||||
) -> HAProxy {
|
|
||||||
HAProxy {
|
|
||||||
frontends: HAProxyFrontends {
|
|
||||||
frontend: frontends,
|
|
||||||
},
|
|
||||||
backends: HAProxyBackends { backends },
|
|
||||||
servers: HAProxyServers { servers },
|
|
||||||
healthchecks: HAProxyHealthChecks { healthchecks },
|
|
||||||
..Default::default()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn given_frontend(bind_address: &str) -> Frontend {
|
|
||||||
Frontend {
|
|
||||||
uuid: "uuid".into(),
|
|
||||||
id: HAProxyId::default(),
|
|
||||||
enabled: 1,
|
|
||||||
name: format!("frontend_{bind_address}"),
|
|
||||||
bind: bind_address.into(),
|
|
||||||
default_backend: Some("backend-uuid".into()),
|
|
||||||
..Default::default()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn given_backend() -> HAProxyBackend {
|
|
||||||
HAProxyBackend {
|
|
||||||
uuid: "backend-uuid".into(),
|
|
||||||
id: HAProxyId::default(),
|
|
||||||
enabled: 1,
|
|
||||||
name: "backend_192.168.1.1:80".into(),
|
|
||||||
linked_servers: MaybeString::from("server-uuid"),
|
|
||||||
health_check_enabled: 1,
|
|
||||||
health_check: MaybeString::from("healthcheck-uuid"),
|
|
||||||
..Default::default()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn given_server(address: &str) -> HAProxyServer {
|
|
||||||
HAProxyServer {
|
|
||||||
uuid: "server-uuid".into(),
|
|
||||||
id: HAProxyId::default(),
|
|
||||||
name: address.into(),
|
|
||||||
address: Some(address.into()),
|
|
||||||
..Default::default()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn given_healthcheck() -> HAProxyHealthCheck {
|
|
||||||
HAProxyHealthCheck {
|
|
||||||
uuid: "healthcheck-uuid".into(),
|
|
||||||
name: "healthcheck".into(),
|
|
||||||
..Default::default()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user