chore: Move ADR helper files into folders with their corresponding ADR number
This commit is contained in:
9
adr/003-abstractions/topology2/Cargo.toml
Normal file
9
adr/003-abstractions/topology2/Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "example-topology2"
|
||||
edition = "2024"
|
||||
version.workspace = true
|
||||
readme.workspace = true
|
||||
license.workspace = true
|
||||
publish = false
|
||||
|
||||
[dependencies]
|
||||
183
adr/003-abstractions/topology2/src/main.rs
Normal file
183
adr/003-abstractions/topology2/src/main.rs
Normal file
@@ -0,0 +1,183 @@
|
||||
// Clean capability-based design using type parameters
|
||||
|
||||
trait Capability {}
|
||||
|
||||
trait K8sCapability: Capability {
|
||||
fn deploy_k8s_resource(&self, resource_yaml: &str);
|
||||
fn execute_kubectl(&self, command: &str) -> String;
|
||||
}
|
||||
|
||||
trait LinuxCapability: Capability {
|
||||
fn execute_command(&self, command: &str, args: &[&str]);
|
||||
fn download_file(&self, url: &str, destination: &str) -> Result<(), String>;
|
||||
}
|
||||
|
||||
trait LoadBalancerCapability: Capability {
|
||||
fn configure_load_balancer(&self, services: &[&str], port: u16);
|
||||
fn get_load_balancer_status(&self) -> String;
|
||||
}
|
||||
|
||||
// Score trait with capability type parameter
|
||||
trait Score<C: ?Sized> {
|
||||
fn execute(&self, capability: &C) -> String;
|
||||
}
|
||||
|
||||
// Topology implementations with marker trait
|
||||
trait Topology {}
|
||||
|
||||
struct K3DTopology {}
|
||||
impl Topology for K3DTopology {}
|
||||
impl Capability for K3DTopology {}
|
||||
impl K8sCapability for K3DTopology {
|
||||
fn deploy_k8s_resource(&self, resource_yaml: &str) {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn execute_kubectl(&self, command: &str) -> String {
|
||||
todo!()
|
||||
}
|
||||
// Implementation...
|
||||
}
|
||||
|
||||
struct LinuxTopology {}
|
||||
impl Topology for LinuxTopology {}
|
||||
impl Capability for LinuxTopology {}
|
||||
impl LinuxCapability for LinuxTopology {
|
||||
fn execute_command(&self, command: &str, args: &[&str]) {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn download_file(&self, url: &str, destination: &str) -> Result<(), String> {
|
||||
todo!()
|
||||
}
|
||||
// Implementation...
|
||||
}
|
||||
|
||||
struct OKDHaClusterTopology {}
|
||||
impl Topology for OKDHaClusterTopology {}
|
||||
impl Capability for OKDHaClusterTopology {}
|
||||
impl K8sCapability for OKDHaClusterTopology {
|
||||
fn deploy_k8s_resource(&self, resource_yaml: &str) {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn execute_kubectl(&self, command: &str) -> String {
|
||||
todo!()
|
||||
}
|
||||
// Implementation...
|
||||
}
|
||||
impl LinuxCapability for OKDHaClusterTopology {
|
||||
fn execute_command(&self, command: &str, args: &[&str]) {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn download_file(&self, url: &str, destination: &str) -> Result<(), String> {
|
||||
todo!()
|
||||
}
|
||||
// Implementation...
|
||||
}
|
||||
impl LoadBalancerCapability for OKDHaClusterTopology {
|
||||
fn configure_load_balancer(&self, services: &[&str], port: u16) {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn get_load_balancer_status(&self) -> String {
|
||||
todo!()
|
||||
}
|
||||
// Implementation...
|
||||
}
|
||||
|
||||
// Score implementations
|
||||
struct LAMPScore {}
|
||||
impl Score<dyn K8sCapability> for LAMPScore {
|
||||
fn execute(&self, capability: &dyn K8sCapability) -> String {
|
||||
todo!()
|
||||
// Implementation...
|
||||
}
|
||||
}
|
||||
|
||||
struct BinaryScore {}
|
||||
impl Score<dyn LinuxCapability> for BinaryScore {
|
||||
fn execute(&self, capability: &dyn LinuxCapability) -> String {
|
||||
todo!()
|
||||
// Implementation...
|
||||
}
|
||||
}
|
||||
|
||||
struct LoadBalancerScore {}
|
||||
impl Score<dyn LoadBalancerCapability> for LoadBalancerScore {
|
||||
fn execute(&self, capability: &dyn LoadBalancerCapability) -> String {
|
||||
todo!()
|
||||
// Implementation...
|
||||
}
|
||||
}
|
||||
|
||||
// Generic Maestro
|
||||
struct Maestro<T> {
|
||||
topology: T,
|
||||
scores: Vec<Box<dyn FnMut(&T) -> String>>,
|
||||
}
|
||||
|
||||
impl<T: 'static> Maestro<T> {
|
||||
fn new(topology: T) -> Self {
|
||||
Self {
|
||||
topology,
|
||||
scores: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn interpret_all(&mut self) -> Vec<String> {
|
||||
self.scores.iter_mut()
|
||||
.map(|score| score(&self.topology))
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
// Capability-specific extensions
|
||||
impl<T: K8sCapability + 'static> Maestro<T> {
|
||||
fn register_k8s_score<S: Score<dyn K8sCapability> + 'static>(&mut self, score: S) {
|
||||
let score_box = Box::new(move |topology: &T| {
|
||||
score.execute(topology as &dyn K8sCapability)
|
||||
});
|
||||
self.scores.push(score_box);
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: LinuxCapability + 'static> Maestro<T> {
|
||||
fn register_linux_score<S: Score<dyn LinuxCapability> + 'static>(&mut self, score: S) {
|
||||
let score_box = Box::new(move |topology: &T| {
|
||||
score.execute(topology as &dyn LinuxCapability)
|
||||
});
|
||||
self.scores.push(score_box);
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: LoadBalancerCapability + 'static> Maestro<T> {
|
||||
fn register_lb_score<S: Score<dyn LoadBalancerCapability> + 'static>(&mut self, score: S) {
|
||||
let score_box = Box::new(move |topology: &T| {
|
||||
score.execute(topology as &dyn LoadBalancerCapability)
|
||||
});
|
||||
self.scores.push(score_box);
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// Example usage
|
||||
let k3d = K3DTopology {};
|
||||
let mut k3d_maestro = Maestro::new(k3d);
|
||||
|
||||
// These will compile because K3D implements K8sCapability
|
||||
k3d_maestro.register_k8s_score(LAMPScore {});
|
||||
|
||||
// This would not compile because K3D doesn't implement LoadBalancerCapability
|
||||
// k3d_maestro.register_lb_score(LoadBalancerScore {});
|
||||
|
||||
let linux = LinuxTopology {};
|
||||
let mut linux_maestro = Maestro::new(linux);
|
||||
|
||||
// This will compile because Linux implements LinuxCapability
|
||||
linux_maestro.register_linux_score(BinaryScore {});
|
||||
|
||||
// This would not compile because Linux doesn't implement K8sCapability
|
||||
// linux_maestro.register_k8s_score(LAMPScore {});
|
||||
}
|
||||
324
adr/003-abstractions/topology2/src/main_capabilities.rs
Normal file
324
adr/003-abstractions/topology2/src/main_capabilities.rs
Normal file
@@ -0,0 +1,324 @@
|
||||
fn main() {
|
||||
// Create various topologies
|
||||
let okd_topology = OKDHaClusterTopology::new();
|
||||
let k3d_topology = K3DTopology::new();
|
||||
let linux_topology = LinuxTopology::new();
|
||||
|
||||
// Create scores
|
||||
let lamp_score = LAMPScore::new("MySQL 8.0", "PHP 8.1", "Apache 2.4");
|
||||
let binary_score = BinaryScore::new("https://example.com/binary", vec!["--arg1", "--arg2"]);
|
||||
let load_balancer_score = LoadBalancerScore::new(vec!["service1", "service2"], 80);
|
||||
|
||||
// Example 1: Running LAMP stack on OKD
|
||||
println!("\n=== Deploying LAMP stack on OKD cluster ===");
|
||||
lamp_score.execute(&okd_topology);
|
||||
|
||||
// Example 2: Running LAMP stack on K3D
|
||||
println!("\n=== Deploying LAMP stack on K3D cluster ===");
|
||||
lamp_score.execute(&k3d_topology);
|
||||
|
||||
// Example 3: Running binary on Linux host
|
||||
println!("\n=== Running binary on Linux host ===");
|
||||
binary_score.execute(&linux_topology);
|
||||
|
||||
// Example 4: Running binary on OKD (which can also run Linux commands)
|
||||
println!("\n=== Running binary on OKD host ===");
|
||||
binary_score.execute(&okd_topology);
|
||||
|
||||
// Example 5: Load balancer configuration on OKD
|
||||
println!("\n=== Configuring load balancer on OKD ===");
|
||||
load_balancer_score.execute(&okd_topology);
|
||||
|
||||
// The following would not compile:
|
||||
// load_balancer_score.execute(&k3d_topology); // K3D doesn't implement LoadBalancerCapability
|
||||
// lamp_score.execute(&linux_topology); // Linux doesn't implement K8sCapability
|
||||
}
|
||||
|
||||
// Base Topology trait
|
||||
trait Topology {
|
||||
fn name(&self) -> &str;
|
||||
}
|
||||
|
||||
// Define capabilities
|
||||
trait K8sCapability {
|
||||
fn deploy_k8s_resource(&self, resource_yaml: &str);
|
||||
fn execute_kubectl(&self, command: &str) -> String;
|
||||
}
|
||||
|
||||
trait OKDCapability: K8sCapability {
|
||||
fn execute_oc(&self, command: &str) -> String;
|
||||
}
|
||||
|
||||
trait LinuxCapability {
|
||||
fn execute_command(&self, command: &str, args: &[&str]) -> String;
|
||||
fn download_file(&self, url: &str, destination: &str) -> Result<(), String>;
|
||||
}
|
||||
|
||||
trait LoadBalancerCapability {
|
||||
fn configure_load_balancer(&self, services: &[&str], port: u16);
|
||||
fn get_load_balancer_status(&self) -> String;
|
||||
}
|
||||
|
||||
trait FirewallCapability {
|
||||
fn open_port(&self, port: u16, protocol: &str);
|
||||
fn close_port(&self, port: u16, protocol: &str);
|
||||
}
|
||||
|
||||
trait RouterCapability {
|
||||
fn configure_route(&self, service: &str, hostname: &str);
|
||||
}
|
||||
|
||||
// Topology implementations
|
||||
struct OKDHaClusterTopology {
|
||||
cluster_name: String,
|
||||
}
|
||||
|
||||
impl OKDHaClusterTopology {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
cluster_name: "okd-ha-cluster".to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Topology for OKDHaClusterTopology {
|
||||
fn name(&self) -> &str {
|
||||
&self.cluster_name
|
||||
}
|
||||
}
|
||||
|
||||
impl K8sCapability for OKDHaClusterTopology {
|
||||
fn deploy_k8s_resource(&self, resource_yaml: &str) {
|
||||
println!("Deploying K8s resource on OKD cluster: {}", resource_yaml);
|
||||
}
|
||||
|
||||
fn execute_kubectl(&self, command: &str) -> String {
|
||||
println!("Executing kubectl command on OKD cluster: {}", command);
|
||||
"kubectl command output".to_string()
|
||||
}
|
||||
}
|
||||
|
||||
impl OKDCapability for OKDHaClusterTopology {
|
||||
fn execute_oc(&self, command: &str) -> String {
|
||||
println!("Executing oc command on OKD cluster: {}", command);
|
||||
"oc command output".to_string()
|
||||
}
|
||||
}
|
||||
|
||||
impl LinuxCapability for OKDHaClusterTopology {
|
||||
fn execute_command(&self, command: &str, args: &[&str]) -> String {
|
||||
println!(
|
||||
"Executing command '{}' with args {:?} on OKD node",
|
||||
command, args
|
||||
);
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn download_file(&self, url: &str, destination: &str) -> Result<(), String> {
|
||||
println!(
|
||||
"Downloading file from {} to {} on OKD node",
|
||||
url, destination
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl LoadBalancerCapability for OKDHaClusterTopology {
|
||||
fn configure_load_balancer(&self, services: &[&str], port: u16) {
|
||||
println!(
|
||||
"Configuring load balancer for services {:?} on port {} in OKD",
|
||||
services, port
|
||||
);
|
||||
}
|
||||
|
||||
fn get_load_balancer_status(&self) -> String {
|
||||
"OKD Load Balancer: HEALTHY".to_string()
|
||||
}
|
||||
}
|
||||
|
||||
impl FirewallCapability for OKDHaClusterTopology {
|
||||
fn open_port(&self, port: u16, protocol: &str) {
|
||||
println!(
|
||||
"Opening port {} with protocol {} on OKD firewall",
|
||||
port, protocol
|
||||
);
|
||||
}
|
||||
|
||||
fn close_port(&self, port: u16, protocol: &str) {
|
||||
println!(
|
||||
"Closing port {} with protocol {} on OKD firewall",
|
||||
port, protocol
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
impl RouterCapability for OKDHaClusterTopology {
|
||||
fn configure_route(&self, service: &str, hostname: &str) {
|
||||
println!(
|
||||
"Configuring route for service {} with hostname {} on OKD",
|
||||
service, hostname
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
struct K3DTopology {
|
||||
cluster_name: String,
|
||||
}
|
||||
|
||||
impl K3DTopology {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
cluster_name: "k3d-local".to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Topology for K3DTopology {
|
||||
fn name(&self) -> &str {
|
||||
&self.cluster_name
|
||||
}
|
||||
}
|
||||
|
||||
impl K8sCapability for K3DTopology {
|
||||
fn deploy_k8s_resource(&self, resource_yaml: &str) {
|
||||
println!("Deploying K8s resource on K3D cluster: {}", resource_yaml);
|
||||
}
|
||||
|
||||
fn execute_kubectl(&self, command: &str) -> String {
|
||||
println!("Executing kubectl command on K3D cluster: {}", command);
|
||||
"kubectl command output from K3D".to_string()
|
||||
}
|
||||
}
|
||||
|
||||
struct LinuxTopology {
|
||||
hostname: String,
|
||||
}
|
||||
|
||||
impl LinuxTopology {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
hostname: "linux-host".to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Topology for LinuxTopology {
|
||||
fn name(&self) -> &str {
|
||||
&self.hostname
|
||||
}
|
||||
}
|
||||
|
||||
impl LinuxCapability for LinuxTopology {
|
||||
fn execute_command(&self, command: &str, args: &[&str]) -> String {
|
||||
println!(
|
||||
"Executing command '{}' with args {:?} on Linux host",
|
||||
command, args
|
||||
);
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn download_file(&self, url: &str, destination: &str) -> Result<(), String> {
|
||||
println!(
|
||||
"Downloading file from {} to {} on Linux host",
|
||||
url, destination
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
// Score implementations
|
||||
struct LAMPScore {
|
||||
mysql_version: String,
|
||||
php_version: String,
|
||||
apache_version: String,
|
||||
}
|
||||
|
||||
impl LAMPScore {
|
||||
fn new(mysql_version: &str, php_version: &str, apache_version: &str) -> Self {
|
||||
Self {
|
||||
mysql_version: mysql_version.to_string(),
|
||||
php_version: php_version.to_string(),
|
||||
apache_version: apache_version.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
fn execute<T: K8sCapability>(&self, topology: &T) {
|
||||
// Deploy MySQL
|
||||
topology.deploy_k8s_resource("mysql-deployment.yaml");
|
||||
|
||||
// Deploy PHP
|
||||
topology.deploy_k8s_resource("php-deployment.yaml");
|
||||
|
||||
// Deploy Apache
|
||||
topology.deploy_k8s_resource("apache-deployment.yaml");
|
||||
|
||||
// Create service
|
||||
topology.deploy_k8s_resource("lamp-service.yaml");
|
||||
|
||||
// Check deployment
|
||||
let status = topology.execute_kubectl("get pods -l app=lamp");
|
||||
println!("LAMP deployment status: {}", status);
|
||||
}
|
||||
}
|
||||
|
||||
struct BinaryScore {
|
||||
url: String,
|
||||
args: Vec<String>,
|
||||
}
|
||||
|
||||
impl BinaryScore {
|
||||
fn new(url: &str, args: Vec<&str>) -> Self {
|
||||
Self {
|
||||
url: url.to_string(),
|
||||
args: args.iter().map(|s| s.to_string()).collect(),
|
||||
}
|
||||
}
|
||||
|
||||
fn execute<T: LinuxCapability>(&self, topology: &T) {
|
||||
let destination = "/tmp/binary";
|
||||
|
||||
match topology.download_file(&self.url, destination) {
|
||||
Ok(_) => {
|
||||
println!("Binary downloaded successfully");
|
||||
|
||||
// Convert args to slice of &str
|
||||
let args: Vec<&str> = self.args.iter().map(|s| s.as_str()).collect();
|
||||
|
||||
// Execute the binary
|
||||
topology.execute_command(destination, &args);
|
||||
println!("Binary execution completed");
|
||||
}
|
||||
Err(e) => {
|
||||
println!("Failed to download binary: {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct LoadBalancerScore {
|
||||
services: Vec<String>,
|
||||
port: u16,
|
||||
}
|
||||
|
||||
impl LoadBalancerScore {
|
||||
fn new(services: Vec<&str>, port: u16) -> Self {
|
||||
Self {
|
||||
services: services.iter().map(|s| s.to_string()).collect(),
|
||||
port,
|
||||
}
|
||||
}
|
||||
|
||||
fn execute<T: LoadBalancerCapability>(&self, topology: &T) {
|
||||
println!("Configuring load balancer for services");
|
||||
|
||||
// Convert services to slice of &str
|
||||
let services: Vec<&str> = self.services.iter().map(|s| s.as_str()).collect();
|
||||
|
||||
// Configure load balancer
|
||||
topology.configure_load_balancer(&services, self.port);
|
||||
|
||||
// Check status
|
||||
let status = topology.get_load_balancer_status();
|
||||
println!("Load balancer status: {}", status);
|
||||
}
|
||||
}
|
||||
34
adr/003-abstractions/topology2/src/main_v1.rs
Normal file
34
adr/003-abstractions/topology2/src/main_v1.rs
Normal file
@@ -0,0 +1,34 @@
|
||||
fn main() {}
|
||||
|
||||
trait Topology {}
|
||||
|
||||
struct DummyTopology {}
|
||||
|
||||
impl Topology for DummyTopology {}
|
||||
|
||||
impl Topology for LampTopology {}
|
||||
|
||||
struct LampTopology {}
|
||||
|
||||
struct Maestro {
|
||||
topology: Box<dyn Topology>,
|
||||
}
|
||||
|
||||
trait Score {
|
||||
type Topology: Topology;
|
||||
fn execute(&self, topology: &Self::Topology);
|
||||
}
|
||||
|
||||
struct K8sScore {}
|
||||
impl Score for K8sScore {
|
||||
type Topology = LampTopology;
|
||||
fn execute(&self, topology: &Box<dyn Self::Topology>) {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
impl Maestro {
|
||||
pub fn execute<T: Topology>(&self, score: Box<dyn Score<Topology = T>>) {
|
||||
score.execute(&self.topology);
|
||||
}
|
||||
}
|
||||
76
adr/003-abstractions/topology2/src/main_v2.rs
Normal file
76
adr/003-abstractions/topology2/src/main_v2.rs
Normal file
@@ -0,0 +1,76 @@
|
||||
fn main() {
|
||||
// Example usage
|
||||
let lamp_topology = LampTopology {};
|
||||
let k8s_score = K8sScore {};
|
||||
let docker_topology = DockerTopology{};
|
||||
|
||||
// Type-safe execution
|
||||
let maestro = Maestro::new(Box::new(docker_topology));
|
||||
maestro.execute(&k8s_score); // This will work
|
||||
|
||||
// This would fail at compile time if we tried:
|
||||
// let dummy_topology = DummyTopology {};
|
||||
// let maestro = Maestro::new(Box::new(dummy_topology));
|
||||
// maestro.execute(&k8s_score); // Error: expected LampTopology, found DummyTopology
|
||||
}
|
||||
|
||||
// Base trait for all topologies
|
||||
trait Topology {
|
||||
// Common topology methods could go here
|
||||
fn topology_type(&self) -> &str;
|
||||
}
|
||||
|
||||
struct DummyTopology {}
|
||||
impl Topology for DummyTopology {
|
||||
fn topology_type(&self) -> &str { "Dummy" }
|
||||
}
|
||||
|
||||
struct LampTopology {}
|
||||
impl Topology for LampTopology {
|
||||
fn topology_type(&self) -> &str { "LAMP" }
|
||||
}
|
||||
|
||||
struct DockerTopology {}
|
||||
|
||||
impl Topology for DockerTopology {
|
||||
fn topology_type(&self) -> &str {
|
||||
todo!("DockerTopology")
|
||||
}
|
||||
}
|
||||
|
||||
// The Score trait with an associated type for the required topology
|
||||
trait Score {
|
||||
type RequiredTopology: Topology + ?Sized;
|
||||
fn execute(&self, topology: &Self::RequiredTopology);
|
||||
fn score_type(&self) -> &str;
|
||||
}
|
||||
|
||||
// A score that requires LampTopology
|
||||
struct K8sScore {}
|
||||
impl Score for K8sScore {
|
||||
type RequiredTopology = DockerTopology;
|
||||
|
||||
fn execute(&self, topology: &Self::RequiredTopology) {
|
||||
println!("Executing K8sScore on {} topology", topology.topology_type());
|
||||
// Implementation details...
|
||||
}
|
||||
|
||||
fn score_type(&self) -> &str { "K8s" }
|
||||
}
|
||||
|
||||
// A generic maestro that can work with any topology type
|
||||
struct Maestro<T: Topology + ?Sized> {
|
||||
topology: Box<T>,
|
||||
}
|
||||
|
||||
impl<T: Topology + ?Sized> Maestro<T> {
|
||||
pub fn new(topology: Box<T>) -> Self {
|
||||
Maestro { topology }
|
||||
}
|
||||
|
||||
// Execute a score that requires this specific topology type
|
||||
pub fn execute<S: Score<RequiredTopology = T>>(&self, score: &S) {
|
||||
println!("Maestro executing {} score", score.score_type());
|
||||
score.execute(&*self.topology);
|
||||
}
|
||||
}
|
||||
360
adr/003-abstractions/topology2/src/main_v4.rs
Normal file
360
adr/003-abstractions/topology2/src/main_v4.rs
Normal file
@@ -0,0 +1,360 @@
|
||||
fn main() {
|
||||
// Create topologies
|
||||
let okd_topology = OKDHaClusterTopology::new();
|
||||
let k3d_topology = K3DTopology::new();
|
||||
let linux_topology = LinuxTopology::new();
|
||||
|
||||
// Create scores - boxing them as trait objects for dynamic dispatch
|
||||
let scores: Vec<Box<dyn Score>> = vec![
|
||||
Box::new(LAMPScore::new("MySQL 8.0", "PHP 8.1", "Apache 2.4")),
|
||||
Box::new(BinaryScore::new("https://example.com/binary", vec!["--arg1", "--arg2"])),
|
||||
Box::new(LoadBalancerScore::new(vec!["service1", "service2"], 80)),
|
||||
];
|
||||
|
||||
// Running scores on OKD topology (which has all capabilities)
|
||||
println!("\n=== Running all scores on OKD HA Cluster ===");
|
||||
for score in &scores {
|
||||
match score.execute(&okd_topology) {
|
||||
Ok(result) => println!("Score executed successfully: {}", result),
|
||||
Err(e) => println!("Failed to execute score: {}", e),
|
||||
}
|
||||
}
|
||||
|
||||
// Running scores on K3D topology (only has K8s capability)
|
||||
println!("\n=== Running scores on K3D Cluster ===");
|
||||
for score in &scores {
|
||||
match score.execute(&k3d_topology) {
|
||||
Ok(result) => println!("Score executed successfully: {}", result),
|
||||
Err(e) => println!("Failed to execute score: {}", e),
|
||||
}
|
||||
}
|
||||
|
||||
// Running scores on Linux topology (only has Linux capability)
|
||||
println!("\n=== Running scores on Linux Host ===");
|
||||
for score in &scores {
|
||||
match score.execute(&linux_topology) {
|
||||
Ok(result) => println!("Score executed successfully: {}", result),
|
||||
Err(e) => println!("Failed to execute score: {}", e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Base Topology trait
|
||||
trait Topology: Any {
|
||||
fn name(&self) -> &str;
|
||||
|
||||
// This method allows us to get type information at runtime
|
||||
fn as_any(&self) -> &dyn Any;
|
||||
}
|
||||
|
||||
// Use Any trait for runtime type checking
|
||||
use std::any::Any;
|
||||
|
||||
// Define capabilities
|
||||
trait K8sCapability {
|
||||
fn deploy_k8s_resource(&self, resource_yaml: &str);
|
||||
fn execute_kubectl(&self, command: &str) -> String;
|
||||
}
|
||||
|
||||
trait OKDCapability: K8sCapability {
|
||||
fn execute_oc(&self, command: &str) -> String;
|
||||
}
|
||||
|
||||
trait LinuxCapability {
|
||||
fn execute_command(&self, command: &str, args: &[&str]);
|
||||
fn download_file(&self, url: &str, destination: &str) -> Result<(), String>;
|
||||
}
|
||||
|
||||
trait LoadBalancerCapability {
|
||||
fn configure_load_balancer(&self, services: &[&str], port: u16);
|
||||
fn get_load_balancer_status(&self) -> String;
|
||||
}
|
||||
|
||||
// Base Score trait with dynamic dispatch
|
||||
trait Score {
|
||||
// Generic execute method that takes any topology
|
||||
fn execute(&self, topology: &dyn Topology) -> Result<String, String>;
|
||||
|
||||
// Optional method to get score type for better error messages
|
||||
fn score_type(&self) -> &str;
|
||||
}
|
||||
|
||||
// Topology implementations
|
||||
struct OKDHaClusterTopology {
|
||||
cluster_name: String,
|
||||
}
|
||||
|
||||
impl OKDHaClusterTopology {
|
||||
fn new() -> Self {
|
||||
Self { cluster_name: "okd-ha-cluster".to_string() }
|
||||
}
|
||||
}
|
||||
|
||||
impl Topology for OKDHaClusterTopology {
|
||||
fn name(&self) -> &str {
|
||||
&self.cluster_name
|
||||
}
|
||||
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl K8sCapability for OKDHaClusterTopology {
|
||||
fn deploy_k8s_resource(&self, resource_yaml: &str) {
|
||||
println!("Deploying K8s resource on OKD cluster: {}", resource_yaml);
|
||||
}
|
||||
|
||||
fn execute_kubectl(&self, command: &str) -> String {
|
||||
println!("Executing kubectl command on OKD cluster: {}", command);
|
||||
"kubectl command output".to_string()
|
||||
}
|
||||
}
|
||||
|
||||
impl OKDCapability for OKDHaClusterTopology {
|
||||
fn execute_oc(&self, command: &str) -> String {
|
||||
println!("Executing oc command on OKD cluster: {}", command);
|
||||
"oc command output".to_string()
|
||||
}
|
||||
}
|
||||
|
||||
impl LinuxCapability for OKDHaClusterTopology {
|
||||
fn execute_command(&self, command: &str, args: &[&str]) {
|
||||
println!("Executing command '{}' with args {:?} on OKD node", command, args);
|
||||
}
|
||||
|
||||
fn download_file(&self, url: &str, destination: &str) -> Result<(), String> {
|
||||
println!("Downloading file from {} to {} on OKD node", url, destination);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl LoadBalancerCapability for OKDHaClusterTopology {
|
||||
fn configure_load_balancer(&self, services: &[&str], port: u16) {
|
||||
println!("Configuring load balancer for services {:?} on port {} in OKD", services, port);
|
||||
}
|
||||
|
||||
fn get_load_balancer_status(&self) -> String {
|
||||
"OKD Load Balancer: HEALTHY".to_string()
|
||||
}
|
||||
}
|
||||
|
||||
struct K3DTopology {
|
||||
cluster_name: String,
|
||||
}
|
||||
|
||||
impl K3DTopology {
|
||||
fn new() -> Self {
|
||||
Self { cluster_name: "k3d-local".to_string() }
|
||||
}
|
||||
}
|
||||
|
||||
impl Topology for K3DTopology {
|
||||
fn name(&self) -> &str {
|
||||
&self.cluster_name
|
||||
}
|
||||
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl K8sCapability for K3DTopology {
|
||||
fn deploy_k8s_resource(&self, resource_yaml: &str) {
|
||||
println!("Deploying K8s resource on K3D cluster: {}", resource_yaml);
|
||||
}
|
||||
|
||||
fn execute_kubectl(&self, command: &str) -> String {
|
||||
println!("Executing kubectl command on K3D cluster: {}", command);
|
||||
"kubectl command output from K3D".to_string()
|
||||
}
|
||||
}
|
||||
|
||||
struct LinuxTopology {
|
||||
hostname: String,
|
||||
}
|
||||
|
||||
impl LinuxTopology {
|
||||
fn new() -> Self {
|
||||
Self { hostname: "linux-host".to_string() }
|
||||
}
|
||||
}
|
||||
|
||||
impl Topology for LinuxTopology {
|
||||
fn name(&self) -> &str {
|
||||
&self.hostname
|
||||
}
|
||||
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl LinuxCapability for LinuxTopology {
|
||||
fn execute_command(&self, command: &str, args: &[&str]) {
|
||||
println!("Executing command '{}' with args {:?} on Linux host", command, args);
|
||||
}
|
||||
|
||||
fn download_file(&self, url: &str, destination: &str) -> Result<(), String> {
|
||||
println!("Downloading file from {} to {} on Linux host", url, destination);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
// Score implementations using dynamic capability checks
|
||||
struct LAMPScore {
|
||||
mysql_version: String,
|
||||
php_version: String,
|
||||
apache_version: String,
|
||||
}
|
||||
|
||||
impl LAMPScore {
|
||||
fn new(mysql_version: &str, php_version: &str, apache_version: &str) -> Self {
|
||||
Self {
|
||||
mysql_version: mysql_version.to_string(),
|
||||
php_version: php_version.to_string(),
|
||||
apache_version: apache_version.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
// Helper method for typesafe execution
|
||||
fn execute_with_k8s(&self, topology: &dyn K8sCapability) -> String {
|
||||
println!("Deploying LAMP stack with MySQL {}, PHP {}, Apache {}",
|
||||
self.mysql_version, self.php_version, self.apache_version);
|
||||
|
||||
// Deploy MySQL
|
||||
topology.deploy_k8s_resource("mysql-deployment.yaml");
|
||||
|
||||
// Deploy PHP
|
||||
topology.deploy_k8s_resource("php-deployment.yaml");
|
||||
|
||||
// Deploy Apache
|
||||
topology.deploy_k8s_resource("apache-deployment.yaml");
|
||||
|
||||
// Create service
|
||||
topology.deploy_k8s_resource("lamp-service.yaml");
|
||||
|
||||
// Check deployment
|
||||
let status = topology.execute_kubectl("get pods -l app=lamp");
|
||||
format!("LAMP deployment status: {}", status)
|
||||
}
|
||||
}
|
||||
|
||||
impl Score for LAMPScore {
|
||||
fn execute(&self, topology: &dyn Topology) -> Result<String, String> {
|
||||
// Try to downcast to K8sCapability
|
||||
if let Some(k8s_topology) = topology.as_any().downcast_ref::<OKDHaClusterTopology>() {
|
||||
Ok(self.execute_with_k8s(k8s_topology))
|
||||
} else if let Some(k8s_topology) = topology.as_any().downcast_ref::<K3DTopology>() {
|
||||
Ok(self.execute_with_k8s(k8s_topology))
|
||||
} else {
|
||||
Err(format!("LAMPScore requires K8sCapability but topology {} doesn't provide it",
|
||||
topology.name()))
|
||||
}
|
||||
}
|
||||
|
||||
fn score_type(&self) -> &str {
|
||||
"LAMP"
|
||||
}
|
||||
}
|
||||
|
||||
struct BinaryScore {
|
||||
url: String,
|
||||
args: Vec<String>,
|
||||
}
|
||||
|
||||
impl BinaryScore {
|
||||
fn new(url: &str, args: Vec<&str>) -> Self {
|
||||
Self {
|
||||
url: url.to_string(),
|
||||
args: args.iter().map(|s| s.to_string()).collect(),
|
||||
}
|
||||
}
|
||||
|
||||
// Helper method for typesafe execution
|
||||
fn execute_with_linux(&self, topology: &dyn LinuxCapability) -> Result<String, String> {
|
||||
let destination = "/tmp/binary";
|
||||
|
||||
// Download the binary
|
||||
println!("Preparing to run binary from {}", self.url);
|
||||
|
||||
match topology.download_file(&self.url, destination) {
|
||||
Ok(_) => {
|
||||
println!("Binary downloaded successfully");
|
||||
|
||||
// Convert args to slice of &str
|
||||
let args: Vec<&str> = self.args.iter().map(|s| s.as_str()).collect();
|
||||
|
||||
// Execute the binary
|
||||
topology.execute_command(destination, &args);
|
||||
Ok("Binary execution completed successfully".to_string())
|
||||
},
|
||||
Err(e) => {
|
||||
Err(format!("Failed to download binary: {}", e))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Score for BinaryScore {
|
||||
fn execute(&self, topology: &dyn Topology) -> Result<String, String> {
|
||||
// Try to downcast to LinuxCapability
|
||||
if let Some(linux_topology) = topology.as_any().downcast_ref::<OKDHaClusterTopology>() {
|
||||
self.execute_with_linux(linux_topology)
|
||||
} else if let Some(linux_topology) = topology.as_any().downcast_ref::<LinuxTopology>() {
|
||||
self.execute_with_linux(linux_topology)
|
||||
} else {
|
||||
Err(format!("BinaryScore requires LinuxCapability but topology {} doesn't provide it",
|
||||
topology.name()))
|
||||
}
|
||||
}
|
||||
|
||||
fn score_type(&self) -> &str {
|
||||
"Binary"
|
||||
}
|
||||
}
|
||||
|
||||
struct LoadBalancerScore {
|
||||
services: Vec<String>,
|
||||
port: u16,
|
||||
}
|
||||
|
||||
impl LoadBalancerScore {
|
||||
fn new(services: Vec<&str>, port: u16) -> Self {
|
||||
Self {
|
||||
services: services.iter().map(|s| s.to_string()).collect(),
|
||||
port,
|
||||
}
|
||||
}
|
||||
|
||||
// Helper method for typesafe execution
|
||||
fn execute_with_lb(&self, topology: &dyn LoadBalancerCapability) -> String {
|
||||
println!("Configuring load balancer for services");
|
||||
|
||||
// Convert services to slice of &str
|
||||
let services: Vec<&str> = self.services.iter().map(|s| s.as_str()).collect();
|
||||
|
||||
// Configure load balancer
|
||||
topology.configure_load_balancer(&services, self.port);
|
||||
|
||||
// Check status
|
||||
let status = topology.get_load_balancer_status();
|
||||
format!("Load balancer configured successfully. Status: {}", status)
|
||||
}
|
||||
}
|
||||
|
||||
impl Score for LoadBalancerScore {
|
||||
fn execute(&self, topology: &dyn Topology) -> Result<String, String> {
|
||||
// Only OKDHaClusterTopology implements LoadBalancerCapability
|
||||
if let Some(lb_topology) = topology.as_any().downcast_ref::<OKDHaClusterTopology>() {
|
||||
Ok(self.execute_with_lb(lb_topology))
|
||||
} else {
|
||||
Err(format!("LoadBalancerScore requires LoadBalancerCapability but topology {} doesn't provide it",
|
||||
topology.name()))
|
||||
}
|
||||
}
|
||||
|
||||
fn score_type(&self) -> &str {
|
||||
"LoadBalancer"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user