//! K3s cluster setup with private networking
//!
//! This example shows how to set up a K3s cluster on cloud instances
//! that have both public and private IP addresses (e.g., Hetzner Cloud).
//!
//! Run with:
//! ```bash
//! cargo run --example private_network
//! ```

use lmrc_k3s::K3sManager;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    // Initialize tracing for logs
    tracing_subscriber::fmt::init();

    // Create a K3s manager with custom version and disabled components
    let manager = K3sManager::builder()
        .version("v1.28.5+k3s1")
        .token("my-secure-cluster-token")
        .disable(vec![
            "traefik".to_string(),   // Disable Traefik ingress
            "servicelb".to_string(), // Disable built-in load balancer
        ])
        .build();

    // Define node IPs - using private IPs for cluster communication
    let master_public_ip = "203.0.113.10";
    let master_private_ip = "10.0.0.10";

    let worker1_public_ip = "203.0.113.11";
    let worker1_private_ip = "10.0.0.11";

    let worker2_public_ip = "203.0.113.12";
    let worker2_private_ip = "10.0.0.12";

    println!("Setting up K3s cluster with private networking...");

    // Install K3s on master with private IP for internal communication
    println!("Installing K3s on master node: {}", master_public_ip);
    manager
        .install_master(master_public_ip, Some(master_private_ip), false)
        .await?;

    // Join workers using private IPs for cluster communication
    println!("Joining worker node: {}", worker1_public_ip);
    manager
        .join_worker(
            worker1_public_ip,
            master_private_ip, // Workers connect to master via private IP
            Some(worker1_private_ip),
        )
        .await?;

    println!("Joining worker node: {}", worker2_public_ip);
    manager
        .join_worker(
            worker2_public_ip,
            master_private_ip,
            Some(worker2_private_ip),
        )
        .await?;

    // Download kubeconfig - use public IP for external access
    println!("Downloading kubeconfig...");
    manager
        .download_kubeconfig(master_public_ip, "kubeconfig")
        .await?;

    println!("Cluster setup complete!");
    println!("Cluster is using private network for internal communication");

    Ok(())
}
