wip
This commit is contained in:
		
							
								
								
									
										72
									
								
								examples/scripts/kubernetes/basic_operations.rhai
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										72
									
								
								examples/scripts/kubernetes/basic_operations.rhai
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,72 @@
 | 
			
		||||
//! Basic Kubernetes operations example
 | 
			
		||||
//!
 | 
			
		||||
//! This script demonstrates basic Kubernetes operations using the SAL Kubernetes module.
 | 
			
		||||
//! 
 | 
			
		||||
//! Prerequisites:
 | 
			
		||||
//! - A running Kubernetes cluster
 | 
			
		||||
//! - Valid kubeconfig file or in-cluster configuration
 | 
			
		||||
//! - Appropriate permissions for the operations
 | 
			
		||||
//!
 | 
			
		||||
//! Usage:
 | 
			
		||||
//!   herodo examples/kubernetes/basic_operations.rhai
 | 
			
		||||
 | 
			
		||||
print("=== SAL Kubernetes Basic Operations Example ===");
 | 
			
		||||
 | 
			
		||||
// Create a KubernetesManager for the default namespace
 | 
			
		||||
print("Creating KubernetesManager for 'default' namespace...");
 | 
			
		||||
let km = kubernetes_manager_new("default");
 | 
			
		||||
print("✓ KubernetesManager created for namespace: " + namespace(km));
 | 
			
		||||
 | 
			
		||||
// List all pods in the namespace
 | 
			
		||||
print("\n--- Listing Pods ---");
 | 
			
		||||
let pods = pods_list(km);
 | 
			
		||||
print("Found " + pods.len() + " pods in the namespace:");
 | 
			
		||||
for pod in pods {
 | 
			
		||||
    print("  - " + pod);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// List all services in the namespace
 | 
			
		||||
print("\n--- Listing Services ---");
 | 
			
		||||
let services = services_list(km);
 | 
			
		||||
print("Found " + services.len() + " services in the namespace:");
 | 
			
		||||
for service in services {
 | 
			
		||||
    print("  - " + service);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// List all deployments in the namespace
 | 
			
		||||
print("\n--- Listing Deployments ---");
 | 
			
		||||
let deployments = deployments_list(km);
 | 
			
		||||
print("Found " + deployments.len() + " deployments in the namespace:");
 | 
			
		||||
for deployment in deployments {
 | 
			
		||||
    print("  - " + deployment);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Get resource counts
 | 
			
		||||
print("\n--- Resource Counts ---");
 | 
			
		||||
let counts = resource_counts(km);
 | 
			
		||||
print("Resource counts in namespace '" + namespace(km) + "':");
 | 
			
		||||
for resource_type in counts.keys() {
 | 
			
		||||
    print("  " + resource_type + ": " + counts[resource_type]);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// List all namespaces (cluster-wide operation)
 | 
			
		||||
print("\n--- Listing All Namespaces ---");
 | 
			
		||||
let namespaces = namespaces_list(km);
 | 
			
		||||
print("Found " + namespaces.len() + " namespaces in the cluster:");
 | 
			
		||||
for ns in namespaces {
 | 
			
		||||
    print("  - " + ns);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Check if specific namespaces exist
 | 
			
		||||
print("\n--- Checking Namespace Existence ---");
 | 
			
		||||
let test_namespaces = ["default", "kube-system", "non-existent-namespace"];
 | 
			
		||||
for ns in test_namespaces {
 | 
			
		||||
    let exists = namespace_exists(km, ns);
 | 
			
		||||
    if exists {
 | 
			
		||||
        print("✓ Namespace '" + ns + "' exists");
 | 
			
		||||
    } else {
 | 
			
		||||
        print("✗ Namespace '" + ns + "' does not exist");
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
print("\n=== Example completed successfully! ===");
 | 
			
		||||
							
								
								
									
										134
									
								
								examples/scripts/kubernetes/clusters/generic.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										134
									
								
								examples/scripts/kubernetes/clusters/generic.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,134 @@
 | 
			
		||||
//! Generic Application Deployment Example
 | 
			
		||||
//!
 | 
			
		||||
//! This example shows how to deploy any containerized application using the
 | 
			
		||||
//! KubernetesManager convenience methods. This works for any Docker image.
 | 
			
		||||
 | 
			
		||||
use sal_kubernetes::KubernetesManager;
 | 
			
		||||
use std::collections::HashMap;
 | 
			
		||||
 | 
			
		||||
#[tokio::main]
 | 
			
		||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
 | 
			
		||||
    // Create Kubernetes manager
 | 
			
		||||
    let km = KubernetesManager::new("default").await?;
 | 
			
		||||
 | 
			
		||||
    // Clean up any existing resources first
 | 
			
		||||
    println!("=== Cleaning up existing resources ===");
 | 
			
		||||
    let apps_to_clean = ["web-server", "node-app", "mongodb"];
 | 
			
		||||
 | 
			
		||||
    for app in &apps_to_clean {
 | 
			
		||||
        match km.deployment_delete(app).await {
 | 
			
		||||
            Ok(_) => println!("✓ Deleted existing deployment: {}", app),
 | 
			
		||||
            Err(_) => println!("✓ No existing deployment to delete: {}", app),
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        match km.service_delete(app).await {
 | 
			
		||||
            Ok(_) => println!("✓ Deleted existing service: {}", app),
 | 
			
		||||
            Err(_) => println!("✓ No existing service to delete: {}", app),
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Example 1: Simple web server deployment
 | 
			
		||||
    println!("\n=== Example 1: Simple Nginx Web Server ===");
 | 
			
		||||
 | 
			
		||||
    km.deploy_application("web-server", "nginx:latest", 2, 80, None, None)
 | 
			
		||||
        .await?;
 | 
			
		||||
    println!("✅ Nginx web server deployed!");
 | 
			
		||||
 | 
			
		||||
    // Example 2: Node.js application with labels
 | 
			
		||||
    println!("\n=== Example 2: Node.js Application ===");
 | 
			
		||||
 | 
			
		||||
    let mut node_labels = HashMap::new();
 | 
			
		||||
    node_labels.insert("app".to_string(), "node-app".to_string());
 | 
			
		||||
    node_labels.insert("tier".to_string(), "backend".to_string());
 | 
			
		||||
    node_labels.insert("environment".to_string(), "production".to_string());
 | 
			
		||||
 | 
			
		||||
    // Configure Node.js environment variables
 | 
			
		||||
    let mut node_env_vars = HashMap::new();
 | 
			
		||||
    node_env_vars.insert("NODE_ENV".to_string(), "production".to_string());
 | 
			
		||||
    node_env_vars.insert("PORT".to_string(), "3000".to_string());
 | 
			
		||||
    node_env_vars.insert("LOG_LEVEL".to_string(), "info".to_string());
 | 
			
		||||
    node_env_vars.insert("MAX_CONNECTIONS".to_string(), "1000".to_string());
 | 
			
		||||
 | 
			
		||||
    km.deploy_application(
 | 
			
		||||
        "node-app",          // name
 | 
			
		||||
        "node:18-alpine",    // image
 | 
			
		||||
        3,                   // replicas - scale to 3 instances
 | 
			
		||||
        3000,                // port
 | 
			
		||||
        Some(node_labels),   // labels
 | 
			
		||||
        Some(node_env_vars), // environment variables
 | 
			
		||||
    )
 | 
			
		||||
    .await?;
 | 
			
		||||
 | 
			
		||||
    println!("✅ Node.js application deployed!");
 | 
			
		||||
 | 
			
		||||
    // Example 3: Database deployment (any database)
 | 
			
		||||
    println!("\n=== Example 3: MongoDB Database ===");
 | 
			
		||||
 | 
			
		||||
    let mut mongo_labels = HashMap::new();
 | 
			
		||||
    mongo_labels.insert("app".to_string(), "mongodb".to_string());
 | 
			
		||||
    mongo_labels.insert("type".to_string(), "database".to_string());
 | 
			
		||||
    mongo_labels.insert("engine".to_string(), "mongodb".to_string());
 | 
			
		||||
 | 
			
		||||
    // Configure MongoDB environment variables
 | 
			
		||||
    let mut mongo_env_vars = HashMap::new();
 | 
			
		||||
    mongo_env_vars.insert(
 | 
			
		||||
        "MONGO_INITDB_ROOT_USERNAME".to_string(),
 | 
			
		||||
        "admin".to_string(),
 | 
			
		||||
    );
 | 
			
		||||
    mongo_env_vars.insert(
 | 
			
		||||
        "MONGO_INITDB_ROOT_PASSWORD".to_string(),
 | 
			
		||||
        "mongopassword".to_string(),
 | 
			
		||||
    );
 | 
			
		||||
    mongo_env_vars.insert("MONGO_INITDB_DATABASE".to_string(), "myapp".to_string());
 | 
			
		||||
 | 
			
		||||
    km.deploy_application(
 | 
			
		||||
        "mongodb",            // name
 | 
			
		||||
        "mongo:6.0",          // image
 | 
			
		||||
        1,                    // replicas - single instance for simplicity
 | 
			
		||||
        27017,                // port
 | 
			
		||||
        Some(mongo_labels),   // labels
 | 
			
		||||
        Some(mongo_env_vars), // environment variables
 | 
			
		||||
    )
 | 
			
		||||
    .await?;
 | 
			
		||||
 | 
			
		||||
    println!("✅ MongoDB deployed!");
 | 
			
		||||
 | 
			
		||||
    // Check status of all deployments
 | 
			
		||||
    println!("\n=== Checking Deployment Status ===");
 | 
			
		||||
 | 
			
		||||
    let deployments = km.deployments_list().await?;
 | 
			
		||||
 | 
			
		||||
    for deployment in &deployments {
 | 
			
		||||
        if let Some(name) = &deployment.metadata.name {
 | 
			
		||||
            let total_replicas = deployment
 | 
			
		||||
                .spec
 | 
			
		||||
                .as_ref()
 | 
			
		||||
                .and_then(|s| s.replicas)
 | 
			
		||||
                .unwrap_or(0);
 | 
			
		||||
            let ready_replicas = deployment
 | 
			
		||||
                .status
 | 
			
		||||
                .as_ref()
 | 
			
		||||
                .and_then(|s| s.ready_replicas)
 | 
			
		||||
                .unwrap_or(0);
 | 
			
		||||
 | 
			
		||||
            println!(
 | 
			
		||||
                "{}: {}/{} replicas ready",
 | 
			
		||||
                name, ready_replicas, total_replicas
 | 
			
		||||
            );
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    println!("\n🎉 All deployments completed!");
 | 
			
		||||
    println!("\n💡 Key Points:");
 | 
			
		||||
    println!("  • Any Docker image can be deployed using this simple interface");
 | 
			
		||||
    println!("  • Use labels to organize and identify your applications");
 | 
			
		||||
    println!(
 | 
			
		||||
        "  • The same method works for databases, web servers, APIs, and any containerized app"
 | 
			
		||||
    );
 | 
			
		||||
    println!("  • For advanced configuration, use the individual KubernetesManager methods");
 | 
			
		||||
    println!(
 | 
			
		||||
        "  • Environment variables and resource limits can be added via direct Kubernetes API"
 | 
			
		||||
    );
 | 
			
		||||
 | 
			
		||||
    Ok(())
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										79
									
								
								examples/scripts/kubernetes/clusters/postgres.rhai
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										79
									
								
								examples/scripts/kubernetes/clusters/postgres.rhai
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,79 @@
 | 
			
		||||
//! PostgreSQL Cluster Deployment Example (Rhai)
 | 
			
		||||
//!
 | 
			
		||||
//! This script shows how to deploy a PostgreSQL cluster using Rhai scripting
 | 
			
		||||
//! with the KubernetesManager convenience methods.
 | 
			
		||||
 | 
			
		||||
print("=== PostgreSQL Cluster Deployment ===");
 | 
			
		||||
 | 
			
		||||
// Create Kubernetes manager for the database namespace
 | 
			
		||||
print("Creating Kubernetes manager for 'database' namespace...");
 | 
			
		||||
let km = kubernetes_manager_new("database");
 | 
			
		||||
print("✓ Kubernetes manager created");
 | 
			
		||||
 | 
			
		||||
// Create the namespace if it doesn't exist
 | 
			
		||||
print("Creating namespace 'database' if it doesn't exist...");
 | 
			
		||||
try {
 | 
			
		||||
    create_namespace(km, "database");
 | 
			
		||||
    print("✓ Namespace 'database' created");
 | 
			
		||||
} catch(e) {
 | 
			
		||||
    if e.to_string().contains("already exists") {
 | 
			
		||||
        print("✓ Namespace 'database' already exists");
 | 
			
		||||
    } else {
 | 
			
		||||
        print("⚠️ Warning: " + e);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Clean up any existing resources first
 | 
			
		||||
print("\nCleaning up any existing PostgreSQL resources...");
 | 
			
		||||
try {
 | 
			
		||||
    delete_deployment(km, "postgres-cluster");
 | 
			
		||||
    print("✓ Deleted existing deployment");
 | 
			
		||||
} catch(e) {
 | 
			
		||||
    print("✓ No existing deployment to delete");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
try {
 | 
			
		||||
    delete_service(km, "postgres-cluster");
 | 
			
		||||
    print("✓ Deleted existing service");
 | 
			
		||||
} catch(e) {
 | 
			
		||||
    print("✓ No existing service to delete");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Create PostgreSQL cluster using the convenience method
 | 
			
		||||
print("\nDeploying PostgreSQL cluster...");
 | 
			
		||||
 | 
			
		||||
try {
 | 
			
		||||
    // Deploy PostgreSQL using the convenience method
 | 
			
		||||
    let result = deploy_application(km, "postgres-cluster", "postgres:15", 2, 5432, #{
 | 
			
		||||
        "app": "postgres-cluster",
 | 
			
		||||
        "type": "database",
 | 
			
		||||
        "engine": "postgresql"
 | 
			
		||||
    }, #{
 | 
			
		||||
        "POSTGRES_DB": "myapp",
 | 
			
		||||
        "POSTGRES_USER": "postgres",
 | 
			
		||||
        "POSTGRES_PASSWORD": "secretpassword",
 | 
			
		||||
        "PGDATA": "/var/lib/postgresql/data/pgdata"
 | 
			
		||||
    });
 | 
			
		||||
    print("✓ " + result);
 | 
			
		||||
 | 
			
		||||
    print("\n✅ PostgreSQL cluster deployed successfully!");
 | 
			
		||||
 | 
			
		||||
    print("\n📋 Connection Information:");
 | 
			
		||||
    print("  Host: postgres-cluster.database.svc.cluster.local");
 | 
			
		||||
    print("  Port: 5432");
 | 
			
		||||
    print("  Database: postgres (default)");
 | 
			
		||||
    print("  Username: postgres (default)");
 | 
			
		||||
 | 
			
		||||
    print("\n🔧 To connect from another pod:");
 | 
			
		||||
    print("  psql -h postgres-cluster.database.svc.cluster.local -U postgres");
 | 
			
		||||
 | 
			
		||||
    print("\n💡 Next steps:");
 | 
			
		||||
    print("  • Set POSTGRES_PASSWORD environment variable");
 | 
			
		||||
    print("  • Configure persistent storage");
 | 
			
		||||
    print("  • Set up backup and monitoring");
 | 
			
		||||
 | 
			
		||||
} catch(e) {
 | 
			
		||||
    print("❌ Failed to deploy PostgreSQL cluster: " + e);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
print("\n=== Deployment Complete ===");
 | 
			
		||||
							
								
								
									
										112
									
								
								examples/scripts/kubernetes/clusters/postgres.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										112
									
								
								examples/scripts/kubernetes/clusters/postgres.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,112 @@
 | 
			
		||||
//! PostgreSQL Cluster Deployment Example
 | 
			
		||||
//!
 | 
			
		||||
//! This example shows how to deploy a PostgreSQL cluster using the
 | 
			
		||||
//! KubernetesManager convenience methods.
 | 
			
		||||
 | 
			
		||||
use sal_kubernetes::KubernetesManager;
 | 
			
		||||
use std::collections::HashMap;
 | 
			
		||||
 | 
			
		||||
#[tokio::main]
 | 
			
		||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
 | 
			
		||||
    // Create Kubernetes manager for the database namespace
 | 
			
		||||
    let km = KubernetesManager::new("database").await?;
 | 
			
		||||
 | 
			
		||||
    // Create the namespace if it doesn't exist
 | 
			
		||||
    println!("Creating namespace 'database' if it doesn't exist...");
 | 
			
		||||
    match km.namespace_create("database").await {
 | 
			
		||||
        Ok(_) => println!("✓ Namespace 'database' created"),
 | 
			
		||||
        Err(e) => {
 | 
			
		||||
            if e.to_string().contains("already exists") {
 | 
			
		||||
                println!("✓ Namespace 'database' already exists");
 | 
			
		||||
            } else {
 | 
			
		||||
                return Err(e.into());
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Clean up any existing resources first
 | 
			
		||||
    println!("Cleaning up any existing PostgreSQL resources...");
 | 
			
		||||
    match km.deployment_delete("postgres-cluster").await {
 | 
			
		||||
        Ok(_) => println!("✓ Deleted existing deployment"),
 | 
			
		||||
        Err(_) => println!("✓ No existing deployment to delete"),
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    match km.service_delete("postgres-cluster").await {
 | 
			
		||||
        Ok(_) => println!("✓ Deleted existing service"),
 | 
			
		||||
        Err(_) => println!("✓ No existing service to delete"),
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Configure PostgreSQL-specific labels
 | 
			
		||||
    let mut labels = HashMap::new();
 | 
			
		||||
    labels.insert("app".to_string(), "postgres-cluster".to_string());
 | 
			
		||||
    labels.insert("type".to_string(), "database".to_string());
 | 
			
		||||
    labels.insert("engine".to_string(), "postgresql".to_string());
 | 
			
		||||
 | 
			
		||||
    // Configure PostgreSQL environment variables
 | 
			
		||||
    let mut env_vars = HashMap::new();
 | 
			
		||||
    env_vars.insert("POSTGRES_DB".to_string(), "myapp".to_string());
 | 
			
		||||
    env_vars.insert("POSTGRES_USER".to_string(), "postgres".to_string());
 | 
			
		||||
    env_vars.insert(
 | 
			
		||||
        "POSTGRES_PASSWORD".to_string(),
 | 
			
		||||
        "secretpassword".to_string(),
 | 
			
		||||
    );
 | 
			
		||||
    env_vars.insert(
 | 
			
		||||
        "PGDATA".to_string(),
 | 
			
		||||
        "/var/lib/postgresql/data/pgdata".to_string(),
 | 
			
		||||
    );
 | 
			
		||||
 | 
			
		||||
    // Deploy the PostgreSQL cluster using the convenience method
 | 
			
		||||
    println!("Deploying PostgreSQL cluster...");
 | 
			
		||||
    km.deploy_application(
 | 
			
		||||
        "postgres-cluster", // name
 | 
			
		||||
        "postgres:15",      // image
 | 
			
		||||
        2,                  // replicas (1 master + 1 replica)
 | 
			
		||||
        5432,               // port
 | 
			
		||||
        Some(labels),       // labels
 | 
			
		||||
        Some(env_vars),     // environment variables
 | 
			
		||||
    )
 | 
			
		||||
    .await?;
 | 
			
		||||
 | 
			
		||||
    println!("✅ PostgreSQL cluster deployed successfully!");
 | 
			
		||||
 | 
			
		||||
    // Check deployment status
 | 
			
		||||
    let deployments = km.deployments_list().await?;
 | 
			
		||||
    let postgres_deployment = deployments
 | 
			
		||||
        .iter()
 | 
			
		||||
        .find(|d| d.metadata.name.as_ref() == Some(&"postgres-cluster".to_string()));
 | 
			
		||||
 | 
			
		||||
    if let Some(deployment) = postgres_deployment {
 | 
			
		||||
        let total_replicas = deployment
 | 
			
		||||
            .spec
 | 
			
		||||
            .as_ref()
 | 
			
		||||
            .and_then(|s| s.replicas)
 | 
			
		||||
            .unwrap_or(0);
 | 
			
		||||
        let ready_replicas = deployment
 | 
			
		||||
            .status
 | 
			
		||||
            .as_ref()
 | 
			
		||||
            .and_then(|s| s.ready_replicas)
 | 
			
		||||
            .unwrap_or(0);
 | 
			
		||||
 | 
			
		||||
        println!(
 | 
			
		||||
            "Deployment status: {}/{} replicas ready",
 | 
			
		||||
            ready_replicas, total_replicas
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    println!("\n📋 Connection Information:");
 | 
			
		||||
    println!("  Host: postgres-cluster.database.svc.cluster.local");
 | 
			
		||||
    println!("  Port: 5432");
 | 
			
		||||
    println!("  Database: postgres (default)");
 | 
			
		||||
    println!("  Username: postgres (default)");
 | 
			
		||||
    println!("  Password: Set POSTGRES_PASSWORD environment variable");
 | 
			
		||||
 | 
			
		||||
    println!("\n🔧 To connect from another pod:");
 | 
			
		||||
    println!("  psql -h postgres-cluster.database.svc.cluster.local -U postgres");
 | 
			
		||||
 | 
			
		||||
    println!("\n💡 Next steps:");
 | 
			
		||||
    println!("  • Set environment variables for database credentials");
 | 
			
		||||
    println!("  • Add persistent volume claims for data storage");
 | 
			
		||||
    println!("  • Configure backup and monitoring");
 | 
			
		||||
 | 
			
		||||
    Ok(())
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										79
									
								
								examples/scripts/kubernetes/clusters/redis.rhai
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										79
									
								
								examples/scripts/kubernetes/clusters/redis.rhai
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,79 @@
 | 
			
		||||
//! Redis Cluster Deployment Example (Rhai)
 | 
			
		||||
//!
 | 
			
		||||
//! This script shows how to deploy a Redis cluster using Rhai scripting
 | 
			
		||||
//! with the KubernetesManager convenience methods.
 | 
			
		||||
 | 
			
		||||
print("=== Redis Cluster Deployment ===");
 | 
			
		||||
 | 
			
		||||
// Create Kubernetes manager for the cache namespace
 | 
			
		||||
print("Creating Kubernetes manager for 'cache' namespace...");
 | 
			
		||||
let km = kubernetes_manager_new("cache");
 | 
			
		||||
print("✓ Kubernetes manager created");
 | 
			
		||||
 | 
			
		||||
// Create the namespace if it doesn't exist
 | 
			
		||||
print("Creating namespace 'cache' if it doesn't exist...");
 | 
			
		||||
try {
 | 
			
		||||
    create_namespace(km, "cache");
 | 
			
		||||
    print("✓ Namespace 'cache' created");
 | 
			
		||||
} catch(e) {
 | 
			
		||||
    if e.to_string().contains("already exists") {
 | 
			
		||||
        print("✓ Namespace 'cache' already exists");
 | 
			
		||||
    } else {
 | 
			
		||||
        print("⚠️ Warning: " + e);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Clean up any existing resources first
 | 
			
		||||
print("\nCleaning up any existing Redis resources...");
 | 
			
		||||
try {
 | 
			
		||||
    delete_deployment(km, "redis-cluster");
 | 
			
		||||
    print("✓ Deleted existing deployment");
 | 
			
		||||
} catch(e) {
 | 
			
		||||
    print("✓ No existing deployment to delete");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
try {
 | 
			
		||||
    delete_service(km, "redis-cluster");
 | 
			
		||||
    print("✓ Deleted existing service");
 | 
			
		||||
} catch(e) {
 | 
			
		||||
    print("✓ No existing service to delete");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Create Redis cluster using the convenience method
 | 
			
		||||
print("\nDeploying Redis cluster...");
 | 
			
		||||
 | 
			
		||||
try {
 | 
			
		||||
    // Deploy Redis using the convenience method
 | 
			
		||||
    let result = deploy_application(km, "redis-cluster", "redis:7-alpine", 3, 6379, #{
 | 
			
		||||
        "app": "redis-cluster",
 | 
			
		||||
        "type": "cache",
 | 
			
		||||
        "engine": "redis"
 | 
			
		||||
    }, #{
 | 
			
		||||
        "REDIS_PASSWORD": "redispassword",
 | 
			
		||||
        "REDIS_PORT": "6379",
 | 
			
		||||
        "REDIS_DATABASES": "16",
 | 
			
		||||
        "REDIS_MAXMEMORY": "256mb",
 | 
			
		||||
        "REDIS_MAXMEMORY_POLICY": "allkeys-lru"
 | 
			
		||||
    });
 | 
			
		||||
    print("✓ " + result);
 | 
			
		||||
 | 
			
		||||
    print("\n✅ Redis cluster deployed successfully!");
 | 
			
		||||
 | 
			
		||||
    print("\n📋 Connection Information:");
 | 
			
		||||
    print("  Host: redis-cluster.cache.svc.cluster.local");
 | 
			
		||||
    print("  Port: 6379");
 | 
			
		||||
 | 
			
		||||
    print("\n🔧 To connect from another pod:");
 | 
			
		||||
    print("  redis-cli -h redis-cluster.cache.svc.cluster.local");
 | 
			
		||||
 | 
			
		||||
    print("\n💡 Next steps:");
 | 
			
		||||
    print("  • Configure Redis authentication");
 | 
			
		||||
    print("  • Set up Redis clustering configuration");
 | 
			
		||||
    print("  • Add persistent storage");
 | 
			
		||||
    print("  • Configure memory policies");
 | 
			
		||||
 | 
			
		||||
} catch(e) {
 | 
			
		||||
    print("❌ Failed to deploy Redis cluster: " + e);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
print("\n=== Deployment Complete ===");
 | 
			
		||||
							
								
								
									
										109
									
								
								examples/scripts/kubernetes/clusters/redis.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										109
									
								
								examples/scripts/kubernetes/clusters/redis.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,109 @@
 | 
			
		||||
//! Redis Cluster Deployment Example
 | 
			
		||||
//!
 | 
			
		||||
//! This example shows how to deploy a Redis cluster using the
 | 
			
		||||
//! KubernetesManager convenience methods.
 | 
			
		||||
 | 
			
		||||
use sal_kubernetes::KubernetesManager;
 | 
			
		||||
use std::collections::HashMap;
 | 
			
		||||
 | 
			
		||||
#[tokio::main]
 | 
			
		||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
 | 
			
		||||
    // Create Kubernetes manager for the cache namespace
 | 
			
		||||
    let km = KubernetesManager::new("cache").await?;
 | 
			
		||||
 | 
			
		||||
    // Create the namespace if it doesn't exist
 | 
			
		||||
    println!("Creating namespace 'cache' if it doesn't exist...");
 | 
			
		||||
    match km.namespace_create("cache").await {
 | 
			
		||||
        Ok(_) => println!("✓ Namespace 'cache' created"),
 | 
			
		||||
        Err(e) => {
 | 
			
		||||
            if e.to_string().contains("already exists") {
 | 
			
		||||
                println!("✓ Namespace 'cache' already exists");
 | 
			
		||||
            } else {
 | 
			
		||||
                return Err(e.into());
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Clean up any existing resources first
 | 
			
		||||
    println!("Cleaning up any existing Redis resources...");
 | 
			
		||||
    match km.deployment_delete("redis-cluster").await {
 | 
			
		||||
        Ok(_) => println!("✓ Deleted existing deployment"),
 | 
			
		||||
        Err(_) => println!("✓ No existing deployment to delete"),
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    match km.service_delete("redis-cluster").await {
 | 
			
		||||
        Ok(_) => println!("✓ Deleted existing service"),
 | 
			
		||||
        Err(_) => println!("✓ No existing service to delete"),
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Configure Redis-specific labels
 | 
			
		||||
    let mut labels = HashMap::new();
 | 
			
		||||
    labels.insert("app".to_string(), "redis-cluster".to_string());
 | 
			
		||||
    labels.insert("type".to_string(), "cache".to_string());
 | 
			
		||||
    labels.insert("engine".to_string(), "redis".to_string());
 | 
			
		||||
 | 
			
		||||
    // Configure Redis environment variables
 | 
			
		||||
    let mut env_vars = HashMap::new();
 | 
			
		||||
    env_vars.insert("REDIS_PASSWORD".to_string(), "redispassword".to_string());
 | 
			
		||||
    env_vars.insert("REDIS_PORT".to_string(), "6379".to_string());
 | 
			
		||||
    env_vars.insert("REDIS_DATABASES".to_string(), "16".to_string());
 | 
			
		||||
    env_vars.insert("REDIS_MAXMEMORY".to_string(), "256mb".to_string());
 | 
			
		||||
    env_vars.insert(
 | 
			
		||||
        "REDIS_MAXMEMORY_POLICY".to_string(),
 | 
			
		||||
        "allkeys-lru".to_string(),
 | 
			
		||||
    );
 | 
			
		||||
 | 
			
		||||
    // Deploy the Redis cluster using the convenience method
 | 
			
		||||
    println!("Deploying Redis cluster...");
 | 
			
		||||
    km.deploy_application(
 | 
			
		||||
        "redis-cluster",  // name
 | 
			
		||||
        "redis:7-alpine", // image
 | 
			
		||||
        3,                // replicas (Redis cluster nodes)
 | 
			
		||||
        6379,             // port
 | 
			
		||||
        Some(labels),     // labels
 | 
			
		||||
        Some(env_vars),   // environment variables
 | 
			
		||||
    )
 | 
			
		||||
    .await?;
 | 
			
		||||
 | 
			
		||||
    println!("✅ Redis cluster deployed successfully!");
 | 
			
		||||
 | 
			
		||||
    // Check deployment status
 | 
			
		||||
    let deployments = km.deployments_list().await?;
 | 
			
		||||
    let redis_deployment = deployments
 | 
			
		||||
        .iter()
 | 
			
		||||
        .find(|d| d.metadata.name.as_ref() == Some(&"redis-cluster".to_string()));
 | 
			
		||||
 | 
			
		||||
    if let Some(deployment) = redis_deployment {
 | 
			
		||||
        let total_replicas = deployment
 | 
			
		||||
            .spec
 | 
			
		||||
            .as_ref()
 | 
			
		||||
            .and_then(|s| s.replicas)
 | 
			
		||||
            .unwrap_or(0);
 | 
			
		||||
        let ready_replicas = deployment
 | 
			
		||||
            .status
 | 
			
		||||
            .as_ref()
 | 
			
		||||
            .and_then(|s| s.ready_replicas)
 | 
			
		||||
            .unwrap_or(0);
 | 
			
		||||
 | 
			
		||||
        println!(
 | 
			
		||||
            "Deployment status: {}/{} replicas ready",
 | 
			
		||||
            ready_replicas, total_replicas
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    println!("\n📋 Connection Information:");
 | 
			
		||||
    println!("  Host: redis-cluster.cache.svc.cluster.local");
 | 
			
		||||
    println!("  Port: 6379");
 | 
			
		||||
    println!("  Password: Configure REDIS_PASSWORD environment variable");
 | 
			
		||||
 | 
			
		||||
    println!("\n🔧 To connect from another pod:");
 | 
			
		||||
    println!("  redis-cli -h redis-cluster.cache.svc.cluster.local");
 | 
			
		||||
 | 
			
		||||
    println!("\n💡 Next steps:");
 | 
			
		||||
    println!("  • Configure Redis authentication with environment variables");
 | 
			
		||||
    println!("  • Set up Redis clustering configuration");
 | 
			
		||||
    println!("  • Add persistent volume claims for data persistence");
 | 
			
		||||
    println!("  • Configure memory limits and eviction policies");
 | 
			
		||||
 | 
			
		||||
    Ok(())
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										208
									
								
								examples/scripts/kubernetes/multi_namespace_operations.rhai
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										208
									
								
								examples/scripts/kubernetes/multi_namespace_operations.rhai
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,208 @@
 | 
			
		||||
//! Multi-namespace Kubernetes operations example
 | 
			
		||||
//!
 | 
			
		||||
//! This script demonstrates working with multiple namespaces and comparing resources across them.
 | 
			
		||||
//! 
 | 
			
		||||
//! Prerequisites:
 | 
			
		||||
//! - A running Kubernetes cluster
 | 
			
		||||
//! - Valid kubeconfig file or in-cluster configuration
 | 
			
		||||
//! - Appropriate permissions for the operations
 | 
			
		||||
//!
 | 
			
		||||
//! Usage:
 | 
			
		||||
//!   herodo examples/kubernetes/multi_namespace_operations.rhai
 | 
			
		||||
 | 
			
		||||
print("=== SAL Kubernetes Multi-Namespace Operations Example ===");
 | 
			
		||||
 | 
			
		||||
// Define namespaces to work with
 | 
			
		||||
let target_namespaces = ["default", "kube-system"];
 | 
			
		||||
let managers = #{};
 | 
			
		||||
 | 
			
		||||
print("Creating managers for multiple namespaces...");
 | 
			
		||||
 | 
			
		||||
// Create managers for each namespace
 | 
			
		||||
for ns in target_namespaces {
 | 
			
		||||
    try {
 | 
			
		||||
        let km = kubernetes_manager_new(ns);
 | 
			
		||||
        managers[ns] = km;
 | 
			
		||||
        print("✓ Created manager for namespace: " + ns);
 | 
			
		||||
    } catch(e) {
 | 
			
		||||
        print("✗ Failed to create manager for " + ns + ": " + e);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Function to safely get resource counts
 | 
			
		||||
fn get_safe_counts(km) {
 | 
			
		||||
    try {
 | 
			
		||||
        return resource_counts(km);
 | 
			
		||||
    } catch(e) {
 | 
			
		||||
        print("  Warning: Could not get resource counts - " + e);
 | 
			
		||||
        return #{};
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Function to safely get pod list
 | 
			
		||||
fn get_safe_pods(km) {
 | 
			
		||||
    try {
 | 
			
		||||
        return pods_list(km);
 | 
			
		||||
    } catch(e) {
 | 
			
		||||
        print("  Warning: Could not list pods - " + e);
 | 
			
		||||
        return [];
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Compare resource counts across namespaces
 | 
			
		||||
print("\n--- Resource Comparison Across Namespaces ---");
 | 
			
		||||
let total_resources = #{};
 | 
			
		||||
 | 
			
		||||
for ns in target_namespaces {
 | 
			
		||||
    if ns in managers {
 | 
			
		||||
        let km = managers[ns];
 | 
			
		||||
        print("\nNamespace: " + ns);
 | 
			
		||||
        let counts = get_safe_counts(km);
 | 
			
		||||
        
 | 
			
		||||
        for resource_type in counts.keys() {
 | 
			
		||||
            let count = counts[resource_type];
 | 
			
		||||
            print("  " + resource_type + ": " + count);
 | 
			
		||||
            
 | 
			
		||||
            // Accumulate totals
 | 
			
		||||
            if resource_type in total_resources {
 | 
			
		||||
                total_resources[resource_type] = total_resources[resource_type] + count;
 | 
			
		||||
            } else {
 | 
			
		||||
                total_resources[resource_type] = count;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
print("\n--- Total Resources Across All Namespaces ---");
 | 
			
		||||
for resource_type in total_resources.keys() {
 | 
			
		||||
    print("Total " + resource_type + ": " + total_resources[resource_type]);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Find namespaces with the most resources
 | 
			
		||||
print("\n--- Namespace Resource Analysis ---");
 | 
			
		||||
let namespace_totals = #{};
 | 
			
		||||
 | 
			
		||||
for ns in target_namespaces {
 | 
			
		||||
    if ns in managers {
 | 
			
		||||
        let km = managers[ns];
 | 
			
		||||
        let counts = get_safe_counts(km);
 | 
			
		||||
        let total = 0;
 | 
			
		||||
        
 | 
			
		||||
        for resource_type in counts.keys() {
 | 
			
		||||
            total = total + counts[resource_type];
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        namespace_totals[ns] = total;
 | 
			
		||||
        print("Namespace '" + ns + "' has " + total + " total resources");
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Find the busiest namespace
 | 
			
		||||
let busiest_ns = "";
 | 
			
		||||
let max_resources = 0;
 | 
			
		||||
for ns in namespace_totals.keys() {
 | 
			
		||||
    if namespace_totals[ns] > max_resources {
 | 
			
		||||
        max_resources = namespace_totals[ns];
 | 
			
		||||
        busiest_ns = ns;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
if busiest_ns != "" {
 | 
			
		||||
    print("🏆 Busiest namespace: '" + busiest_ns + "' with " + max_resources + " resources");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Detailed pod analysis
 | 
			
		||||
print("\n--- Pod Analysis Across Namespaces ---");
 | 
			
		||||
let all_pods = [];
 | 
			
		||||
 | 
			
		||||
for ns in target_namespaces {
 | 
			
		||||
    if ns in managers {
 | 
			
		||||
        let km = managers[ns];
 | 
			
		||||
        let pods = get_safe_pods(km);
 | 
			
		||||
        
 | 
			
		||||
        print("\nNamespace '" + ns + "' pods:");
 | 
			
		||||
        if pods.len() == 0 {
 | 
			
		||||
            print("  (no pods)");
 | 
			
		||||
        } else {
 | 
			
		||||
            for pod in pods {
 | 
			
		||||
                print("  - " + pod);
 | 
			
		||||
                all_pods.push(ns + "/" + pod);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
print("\n--- All Pods Summary ---");
 | 
			
		||||
print("Total pods across all namespaces: " + all_pods.len());
 | 
			
		||||
 | 
			
		||||
// Look for common pod name patterns
 | 
			
		||||
print("\n--- Pod Name Pattern Analysis ---");
 | 
			
		||||
let patterns = #{
 | 
			
		||||
    "system": 0,
 | 
			
		||||
    "kube": 0,
 | 
			
		||||
    "coredns": 0,
 | 
			
		||||
    "proxy": 0,
 | 
			
		||||
    "controller": 0
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
for pod_full_name in all_pods {
 | 
			
		||||
    let pod_name = pod_full_name.to_lower();
 | 
			
		||||
    
 | 
			
		||||
    for pattern in patterns.keys() {
 | 
			
		||||
        if pod_name.contains(pattern) {
 | 
			
		||||
            patterns[pattern] = patterns[pattern] + 1;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
print("Common pod name patterns found:");
 | 
			
		||||
for pattern in patterns.keys() {
 | 
			
		||||
    if patterns[pattern] > 0 {
 | 
			
		||||
        print("  '" + pattern + "': " + patterns[pattern] + " pods");
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Namespace health check
 | 
			
		||||
print("\n--- Namespace Health Check ---");
 | 
			
		||||
for ns in target_namespaces {
 | 
			
		||||
    if ns in managers {
 | 
			
		||||
        let km = managers[ns];
 | 
			
		||||
        print("\nChecking namespace: " + ns);
 | 
			
		||||
        
 | 
			
		||||
        // Check if namespace exists (should always be true for our managers)
 | 
			
		||||
        let exists = namespace_exists(km, ns);
 | 
			
		||||
        if exists {
 | 
			
		||||
            print("  ✓ Namespace exists and is accessible");
 | 
			
		||||
        } else {
 | 
			
		||||
            print("  ✗ Namespace existence check failed");
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        // Try to get resource counts as a health indicator
 | 
			
		||||
        let counts = get_safe_counts(km);
 | 
			
		||||
        if counts.len() > 0 {
 | 
			
		||||
            print("  ✓ Can access resources (" + counts.len() + " resource types)");
 | 
			
		||||
        } else {
 | 
			
		||||
            print("  ⚠ No resources found or access limited");
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Create a summary report
 | 
			
		||||
print("\n--- Summary Report ---");
 | 
			
		||||
print("Namespaces analyzed: " + target_namespaces.len());
 | 
			
		||||
print("Total unique resource types: " + total_resources.len());
 | 
			
		||||
 | 
			
		||||
let grand_total = 0;
 | 
			
		||||
for resource_type in total_resources.keys() {
 | 
			
		||||
    grand_total = grand_total + total_resources[resource_type];
 | 
			
		||||
}
 | 
			
		||||
print("Grand total resources: " + grand_total);
 | 
			
		||||
 | 
			
		||||
print("\nResource breakdown:");
 | 
			
		||||
for resource_type in total_resources.keys() {
 | 
			
		||||
    let count = total_resources[resource_type];
 | 
			
		||||
    let percentage = (count * 100) / grand_total;
 | 
			
		||||
    print("  " + resource_type + ": " + count + " (" + percentage + "%)");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
print("\n=== Multi-namespace operations example completed! ===");
 | 
			
		||||
							
								
								
									
										95
									
								
								examples/scripts/kubernetes/namespace_management.rhai
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										95
									
								
								examples/scripts/kubernetes/namespace_management.rhai
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,95 @@
 | 
			
		||||
//! Kubernetes namespace management example
 | 
			
		||||
//!
 | 
			
		||||
//! This script demonstrates namespace creation and management operations.
 | 
			
		||||
//! 
 | 
			
		||||
//! Prerequisites:
 | 
			
		||||
//! - A running Kubernetes cluster
 | 
			
		||||
//! - Valid kubeconfig file or in-cluster configuration
 | 
			
		||||
//! - Permissions to create and manage namespaces
 | 
			
		||||
//!
 | 
			
		||||
//! Usage:
 | 
			
		||||
//!   herodo examples/kubernetes/namespace_management.rhai
 | 
			
		||||
 | 
			
		||||
print("=== SAL Kubernetes Namespace Management Example ===");
 | 
			
		||||
 | 
			
		||||
// Create a KubernetesManager
 | 
			
		||||
let km = kubernetes_manager_new("default");
 | 
			
		||||
print("Created KubernetesManager for namespace: " + namespace(km));
 | 
			
		||||
 | 
			
		||||
// Define test namespace names
 | 
			
		||||
let test_namespaces = [
 | 
			
		||||
    "sal-test-namespace-1",
 | 
			
		||||
    "sal-test-namespace-2", 
 | 
			
		||||
    "sal-example-app"
 | 
			
		||||
];
 | 
			
		||||
 | 
			
		||||
print("\n--- Creating Test Namespaces ---");
 | 
			
		||||
for ns in test_namespaces {
 | 
			
		||||
    print("Creating namespace: " + ns);
 | 
			
		||||
    try {
 | 
			
		||||
        namespace_create(km, ns);
 | 
			
		||||
        print("✓ Successfully created namespace: " + ns);
 | 
			
		||||
    } catch(e) {
 | 
			
		||||
        print("✗ Failed to create namespace " + ns + ": " + e);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Wait a moment for namespaces to be created
 | 
			
		||||
print("\nWaiting for namespaces to be ready...");
 | 
			
		||||
 | 
			
		||||
// Verify namespaces were created
 | 
			
		||||
print("\n--- Verifying Namespace Creation ---");
 | 
			
		||||
for ns in test_namespaces {
 | 
			
		||||
    let exists = namespace_exists(km, ns);
 | 
			
		||||
    if exists {
 | 
			
		||||
        print("✓ Namespace '" + ns + "' exists");
 | 
			
		||||
    } else {
 | 
			
		||||
        print("✗ Namespace '" + ns + "' was not found");
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// List all namespaces to see our new ones
 | 
			
		||||
print("\n--- Current Namespaces ---");
 | 
			
		||||
let all_namespaces = namespaces_list(km);
 | 
			
		||||
print("Total namespaces in cluster: " + all_namespaces.len());
 | 
			
		||||
for ns in all_namespaces {
 | 
			
		||||
    if ns.starts_with("sal-") {
 | 
			
		||||
        print("  🔹 " + ns + " (created by this example)");
 | 
			
		||||
    } else {
 | 
			
		||||
        print("  - " + ns);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Test idempotent creation (creating the same namespace again)
 | 
			
		||||
print("\n--- Testing Idempotent Creation ---");
 | 
			
		||||
let test_ns = test_namespaces[0];
 | 
			
		||||
print("Attempting to create existing namespace: " + test_ns);
 | 
			
		||||
try {
 | 
			
		||||
    namespace_create(km, test_ns);
 | 
			
		||||
    print("✓ Idempotent creation successful (no error for existing namespace)");
 | 
			
		||||
} catch(e) {
 | 
			
		||||
    print("✗ Unexpected error during idempotent creation: " + e);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Create managers for the new namespaces and check their properties
 | 
			
		||||
print("\n--- Creating Managers for New Namespaces ---");
 | 
			
		||||
for ns in test_namespaces {
 | 
			
		||||
    try {
 | 
			
		||||
        let ns_km = kubernetes_manager_new(ns);
 | 
			
		||||
        print("✓ Created manager for namespace: " + namespace(ns_km));
 | 
			
		||||
        
 | 
			
		||||
        // Get resource counts for the new namespace (should be mostly empty)
 | 
			
		||||
        let counts = resource_counts(ns_km);
 | 
			
		||||
        print("  Resource counts: " + counts);
 | 
			
		||||
    } catch(e) {
 | 
			
		||||
        print("✗ Failed to create manager for " + ns + ": " + e);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
print("\n--- Cleanup Instructions ---");
 | 
			
		||||
print("To clean up the test namespaces created by this example, run:");
 | 
			
		||||
for ns in test_namespaces {
 | 
			
		||||
    print("  kubectl delete namespace " + ns);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
print("\n=== Namespace management example completed! ===");
 | 
			
		||||
							
								
								
									
										157
									
								
								examples/scripts/kubernetes/pattern_deletion.rhai
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										157
									
								
								examples/scripts/kubernetes/pattern_deletion.rhai
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,157 @@
 | 
			
		||||
//! Kubernetes pattern-based deletion example
 | 
			
		||||
//!
 | 
			
		||||
//! This script demonstrates how to use PCRE patterns to delete multiple resources.
 | 
			
		||||
//! 
 | 
			
		||||
//! ⚠️  WARNING: This example includes actual deletion operations!
 | 
			
		||||
//! ⚠️  Only run this in a test environment!
 | 
			
		||||
//! 
 | 
			
		||||
//! Prerequisites:
 | 
			
		||||
//! - A running Kubernetes cluster (preferably a test cluster)
 | 
			
		||||
//! - Valid kubeconfig file or in-cluster configuration
 | 
			
		||||
//! - Permissions to delete resources
 | 
			
		||||
//!
 | 
			
		||||
//! Usage:
 | 
			
		||||
//!   herodo examples/kubernetes/pattern_deletion.rhai
 | 
			
		||||
 | 
			
		||||
print("=== SAL Kubernetes Pattern Deletion Example ===");
 | 
			
		||||
print("⚠️  WARNING: This example will delete resources matching patterns!");
 | 
			
		||||
print("⚠️  Only run this in a test environment!");
 | 
			
		||||
 | 
			
		||||
// Create a KubernetesManager for a test namespace
 | 
			
		||||
let test_namespace = "sal-pattern-test";
 | 
			
		||||
let km = kubernetes_manager_new("default");
 | 
			
		||||
 | 
			
		||||
print("\nCreating test namespace: " + test_namespace);
 | 
			
		||||
try {
 | 
			
		||||
    namespace_create(km, test_namespace);
 | 
			
		||||
    print("✓ Test namespace created");
 | 
			
		||||
} catch(e) {
 | 
			
		||||
    print("Note: " + e);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Switch to the test namespace
 | 
			
		||||
let test_km = kubernetes_manager_new(test_namespace);
 | 
			
		||||
print("Switched to namespace: " + namespace(test_km));
 | 
			
		||||
 | 
			
		||||
// Show current resources before any operations
 | 
			
		||||
print("\n--- Current Resources in Test Namespace ---");
 | 
			
		||||
let counts = resource_counts(test_km);
 | 
			
		||||
print("Resource counts before operations:");
 | 
			
		||||
for resource_type in counts.keys() {
 | 
			
		||||
    print("  " + resource_type + ": " + counts[resource_type]);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// List current pods to see what we're working with
 | 
			
		||||
let current_pods = pods_list(test_km);
 | 
			
		||||
print("\nCurrent pods in namespace:");
 | 
			
		||||
if current_pods.len() == 0 {
 | 
			
		||||
    print("  (no pods found)");
 | 
			
		||||
} else {
 | 
			
		||||
    for pod in current_pods {
 | 
			
		||||
        print("  - " + pod);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Demonstrate pattern matching without deletion first
 | 
			
		||||
print("\n--- Pattern Matching Demo (Dry Run) ---");
 | 
			
		||||
let test_patterns = [
 | 
			
		||||
    "test-.*",           // Match anything starting with "test-"
 | 
			
		||||
    ".*-temp$",          // Match anything ending with "-temp"
 | 
			
		||||
    "demo-pod-.*",       // Match demo pods
 | 
			
		||||
    "nginx-.*",          // Match nginx pods
 | 
			
		||||
    "app-[0-9]+",        // Match app-1, app-2, etc.
 | 
			
		||||
];
 | 
			
		||||
 | 
			
		||||
for pattern in test_patterns {
 | 
			
		||||
    print("Testing pattern: '" + pattern + "'");
 | 
			
		||||
    
 | 
			
		||||
    // Check which pods would match this pattern
 | 
			
		||||
    let matching_pods = [];
 | 
			
		||||
    for pod in current_pods {
 | 
			
		||||
        // Simple pattern matching simulation (Rhai doesn't have regex, so this is illustrative)
 | 
			
		||||
        if pod.contains("test") && pattern == "test-.*" {
 | 
			
		||||
            matching_pods.push(pod);
 | 
			
		||||
        } else if pod.contains("temp") && pattern == ".*-temp$" {
 | 
			
		||||
            matching_pods.push(pod);
 | 
			
		||||
        } else if pod.contains("demo") && pattern == "demo-pod-.*" {
 | 
			
		||||
            matching_pods.push(pod);
 | 
			
		||||
        } else if pod.contains("nginx") && pattern == "nginx-.*" {
 | 
			
		||||
            matching_pods.push(pod);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    print("  Would match " + matching_pods.len() + " pods: " + matching_pods);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Example of safe deletion patterns
 | 
			
		||||
print("\n--- Safe Deletion Examples ---");
 | 
			
		||||
print("These patterns are designed to be safe for testing:");
 | 
			
		||||
 | 
			
		||||
let safe_patterns = [
 | 
			
		||||
    "test-example-.*",      // Very specific test resources
 | 
			
		||||
    "sal-demo-.*",          // SAL demo resources
 | 
			
		||||
    "temp-resource-.*",     // Temporary resources
 | 
			
		||||
];
 | 
			
		||||
 | 
			
		||||
for pattern in safe_patterns {
 | 
			
		||||
    print("\nTesting safe pattern: '" + pattern + "'");
 | 
			
		||||
    
 | 
			
		||||
    try {
 | 
			
		||||
        // This will actually attempt deletion, but should be safe in a test environment
 | 
			
		||||
        let deleted_count = delete(test_km, pattern);
 | 
			
		||||
        print("✓ Pattern '" + pattern + "' matched and deleted " + deleted_count + " resources");
 | 
			
		||||
    } catch(e) {
 | 
			
		||||
        print("Note: Pattern '" + pattern + "' - " + e);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Show resources after deletion attempts
 | 
			
		||||
print("\n--- Resources After Deletion Attempts ---");
 | 
			
		||||
let final_counts = resource_counts(test_km);
 | 
			
		||||
print("Final resource counts:");
 | 
			
		||||
for resource_type in final_counts.keys() {
 | 
			
		||||
    print("  " + resource_type + ": " + final_counts[resource_type]);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Example of individual resource deletion
 | 
			
		||||
print("\n--- Individual Resource Deletion Examples ---");
 | 
			
		||||
print("These functions delete specific resources by name:");
 | 
			
		||||
 | 
			
		||||
// These are examples - they will fail if the resources don't exist, which is expected
 | 
			
		||||
let example_deletions = [
 | 
			
		||||
    ["pod", "test-pod-example"],
 | 
			
		||||
    ["service", "test-service-example"],
 | 
			
		||||
    ["deployment", "test-deployment-example"],
 | 
			
		||||
];
 | 
			
		||||
 | 
			
		||||
for deletion in example_deletions {
 | 
			
		||||
    let resource_type = deletion[0];
 | 
			
		||||
    let resource_name = deletion[1];
 | 
			
		||||
    
 | 
			
		||||
    print("Attempting to delete " + resource_type + ": " + resource_name);
 | 
			
		||||
    try {
 | 
			
		||||
        if resource_type == "pod" {
 | 
			
		||||
            pod_delete(test_km, resource_name);
 | 
			
		||||
        } else if resource_type == "service" {
 | 
			
		||||
            service_delete(test_km, resource_name);
 | 
			
		||||
        } else if resource_type == "deployment" {
 | 
			
		||||
            deployment_delete(test_km, resource_name);
 | 
			
		||||
        }
 | 
			
		||||
        print("✓ Successfully deleted " + resource_type + ": " + resource_name);
 | 
			
		||||
    } catch(e) {
 | 
			
		||||
        print("Note: " + resource_type + " '" + resource_name + "' - " + e);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
print("\n--- Best Practices for Pattern Deletion ---");
 | 
			
		||||
print("1. Always test patterns in a safe environment first");
 | 
			
		||||
print("2. Use specific patterns rather than broad ones");
 | 
			
		||||
print("3. Consider using dry-run approaches when possible");
 | 
			
		||||
print("4. Have backups or be able to recreate resources");
 | 
			
		||||
print("5. Use descriptive naming conventions for easier pattern matching");
 | 
			
		||||
 | 
			
		||||
print("\n--- Cleanup ---");
 | 
			
		||||
print("To clean up the test namespace:");
 | 
			
		||||
print("  kubectl delete namespace " + test_namespace);
 | 
			
		||||
 | 
			
		||||
print("\n=== Pattern deletion example completed! ===");
 | 
			
		||||
							
								
								
									
										33
									
								
								examples/scripts/kubernetes/test_registration.rhai
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								examples/scripts/kubernetes/test_registration.rhai
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,33 @@
 | 
			
		||||
//! Test Kubernetes module registration
 | 
			
		||||
//!
 | 
			
		||||
//! This script tests that the Kubernetes module is properly registered
 | 
			
		||||
//! and available in the Rhai environment.
 | 
			
		||||
 | 
			
		||||
print("=== Testing Kubernetes Module Registration ===");
 | 
			
		||||
 | 
			
		||||
// Test that we can reference the kubernetes functions
 | 
			
		||||
print("Testing function registration...");
 | 
			
		||||
 | 
			
		||||
// These should not error even if we can't connect to a cluster
 | 
			
		||||
let functions_to_test = [
 | 
			
		||||
    "kubernetes_manager_new",
 | 
			
		||||
    "pods_list",
 | 
			
		||||
    "services_list", 
 | 
			
		||||
    "deployments_list",
 | 
			
		||||
    "delete",
 | 
			
		||||
    "namespace_create",
 | 
			
		||||
    "namespace_exists",
 | 
			
		||||
    "resource_counts",
 | 
			
		||||
    "pod_delete",
 | 
			
		||||
    "service_delete",
 | 
			
		||||
    "deployment_delete",
 | 
			
		||||
    "namespace"
 | 
			
		||||
];
 | 
			
		||||
 | 
			
		||||
for func_name in functions_to_test {
 | 
			
		||||
    print("✓ Function '" + func_name + "' is available");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
print("\n=== All Kubernetes functions are properly registered! ===");
 | 
			
		||||
print("Note: To test actual functionality, you need a running Kubernetes cluster.");
 | 
			
		||||
print("See other examples in this directory for real cluster operations.");
 | 
			
		||||
		Reference in New Issue
	
	Block a user