- Reorganized examples into osiris/, sal/, and utils/ folders - Moved hardcoded scripts to separate .rhai files - Added signature() method to JobBuilder for job signing - Updated OSIRIS context to use block_in_place instead of runtime - Removed runtime field from OsirisContext - Added typed save() methods for Note and Event objects - Updated all examples to use new structure and APIs
		
			
				
	
	
		
			158 lines
		
	
	
		
			5.5 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
			
		
		
	
	
			158 lines
		
	
	
		
			5.5 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
//! 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! ===");
 |