feat: Add Kubernetes examples and update dependencies
- Add Kubernetes examples demonstrating deployment of various applications (PostgreSQL, Redis, generic). This improves the documentation and provides practical usage examples. - Add `tokio` dependency for async examples. This enables the use of asynchronous operations in the examples. - Add `once_cell` dependency for improved resource management in Kubernetes module. This allows efficient management of singletons and other resources.
This commit is contained in:
@@ -23,7 +23,8 @@ mod crud_tests {
|
||||
|
||||
// Create a test namespace for our operations
|
||||
let test_namespace = "sal-crud-test";
|
||||
let km = KubernetesManager::new("default").await
|
||||
let km = KubernetesManager::new("default")
|
||||
.await
|
||||
.expect("Should connect to cluster");
|
||||
|
||||
// Clean up any existing test namespace
|
||||
@@ -34,50 +35,80 @@ mod crud_tests {
|
||||
println!("\n=== CREATE Operations ===");
|
||||
|
||||
// 1. Create namespace
|
||||
km.namespace_create(test_namespace).await
|
||||
km.namespace_create(test_namespace)
|
||||
.await
|
||||
.expect("Should create test namespace");
|
||||
println!("✅ Created namespace: {}", test_namespace);
|
||||
|
||||
// Switch to test namespace
|
||||
let test_km = KubernetesManager::new(test_namespace).await
|
||||
let test_km = KubernetesManager::new(test_namespace)
|
||||
.await
|
||||
.expect("Should connect to test namespace");
|
||||
|
||||
// 2. Create ConfigMap
|
||||
let mut config_data = HashMap::new();
|
||||
config_data.insert("app.properties".to_string(), "debug=true\nport=8080".to_string());
|
||||
config_data.insert("config.yaml".to_string(), "key: value\nenv: test".to_string());
|
||||
|
||||
let configmap = test_km.configmap_create("test-config", config_data).await
|
||||
config_data.insert(
|
||||
"app.properties".to_string(),
|
||||
"debug=true\nport=8080".to_string(),
|
||||
);
|
||||
config_data.insert(
|
||||
"config.yaml".to_string(),
|
||||
"key: value\nenv: test".to_string(),
|
||||
);
|
||||
|
||||
let configmap = test_km
|
||||
.configmap_create("test-config", config_data)
|
||||
.await
|
||||
.expect("Should create ConfigMap");
|
||||
println!("✅ Created ConfigMap: {}", configmap.metadata.name.unwrap_or_default());
|
||||
println!(
|
||||
"✅ Created ConfigMap: {}",
|
||||
configmap.metadata.name.unwrap_or_default()
|
||||
);
|
||||
|
||||
// 3. Create Secret
|
||||
let mut secret_data = HashMap::new();
|
||||
secret_data.insert("username".to_string(), "testuser".to_string());
|
||||
secret_data.insert("password".to_string(), "secret123".to_string());
|
||||
|
||||
let secret = test_km.secret_create("test-secret", secret_data, None).await
|
||||
|
||||
let secret = test_km
|
||||
.secret_create("test-secret", secret_data, None)
|
||||
.await
|
||||
.expect("Should create Secret");
|
||||
println!("✅ Created Secret: {}", secret.metadata.name.unwrap_or_default());
|
||||
println!(
|
||||
"✅ Created Secret: {}",
|
||||
secret.metadata.name.unwrap_or_default()
|
||||
);
|
||||
|
||||
// 4. Create Pod
|
||||
let mut pod_labels = HashMap::new();
|
||||
pod_labels.insert("app".to_string(), "test-app".to_string());
|
||||
pod_labels.insert("version".to_string(), "v1".to_string());
|
||||
|
||||
let pod = test_km.pod_create("test-pod", "nginx:alpine", Some(pod_labels.clone())).await
|
||||
|
||||
let pod = test_km
|
||||
.pod_create("test-pod", "nginx:alpine", Some(pod_labels.clone()), None)
|
||||
.await
|
||||
.expect("Should create Pod");
|
||||
println!("✅ Created Pod: {}", pod.metadata.name.unwrap_or_default());
|
||||
|
||||
// 5. Create Service
|
||||
let service = test_km.service_create("test-service", pod_labels.clone(), 80, Some(80)).await
|
||||
let service = test_km
|
||||
.service_create("test-service", pod_labels.clone(), 80, Some(80))
|
||||
.await
|
||||
.expect("Should create Service");
|
||||
println!("✅ Created Service: {}", service.metadata.name.unwrap_or_default());
|
||||
println!(
|
||||
"✅ Created Service: {}",
|
||||
service.metadata.name.unwrap_or_default()
|
||||
);
|
||||
|
||||
// 6. Create Deployment
|
||||
let deployment = test_km.deployment_create("test-deployment", "nginx:alpine", 2, Some(pod_labels)).await
|
||||
let deployment = test_km
|
||||
.deployment_create("test-deployment", "nginx:alpine", 2, Some(pod_labels), None)
|
||||
.await
|
||||
.expect("Should create Deployment");
|
||||
println!("✅ Created Deployment: {}", deployment.metadata.name.unwrap_or_default());
|
||||
println!(
|
||||
"✅ Created Deployment: {}",
|
||||
deployment.metadata.name.unwrap_or_default()
|
||||
);
|
||||
|
||||
// READ operations
|
||||
println!("\n=== READ Operations ===");
|
||||
@@ -89,10 +120,16 @@ mod crud_tests {
|
||||
let services = test_km.services_list().await.expect("Should list services");
|
||||
println!("✅ Listed {} services", services.len());
|
||||
|
||||
let deployments = test_km.deployments_list().await.expect("Should list deployments");
|
||||
let deployments = test_km
|
||||
.deployments_list()
|
||||
.await
|
||||
.expect("Should list deployments");
|
||||
println!("✅ Listed {} deployments", deployments.len());
|
||||
|
||||
let configmaps = test_km.configmaps_list().await.expect("Should list configmaps");
|
||||
let configmaps = test_km
|
||||
.configmaps_list()
|
||||
.await
|
||||
.expect("Should list configmaps");
|
||||
println!("✅ Listed {} configmaps", configmaps.len());
|
||||
|
||||
let secrets = test_km.secrets_list().await.expect("Should list secrets");
|
||||
@@ -100,43 +137,81 @@ mod crud_tests {
|
||||
|
||||
// Get specific resources
|
||||
let pod = test_km.pod_get("test-pod").await.expect("Should get pod");
|
||||
println!("✅ Retrieved pod: {}", pod.metadata.name.unwrap_or_default());
|
||||
println!(
|
||||
"✅ Retrieved pod: {}",
|
||||
pod.metadata.name.unwrap_or_default()
|
||||
);
|
||||
|
||||
let service = test_km.service_get("test-service").await.expect("Should get service");
|
||||
println!("✅ Retrieved service: {}", service.metadata.name.unwrap_or_default());
|
||||
let service = test_km
|
||||
.service_get("test-service")
|
||||
.await
|
||||
.expect("Should get service");
|
||||
println!(
|
||||
"✅ Retrieved service: {}",
|
||||
service.metadata.name.unwrap_or_default()
|
||||
);
|
||||
|
||||
let deployment = test_km.deployment_get("test-deployment").await.expect("Should get deployment");
|
||||
println!("✅ Retrieved deployment: {}", deployment.metadata.name.unwrap_or_default());
|
||||
let deployment = test_km
|
||||
.deployment_get("test-deployment")
|
||||
.await
|
||||
.expect("Should get deployment");
|
||||
println!(
|
||||
"✅ Retrieved deployment: {}",
|
||||
deployment.metadata.name.unwrap_or_default()
|
||||
);
|
||||
|
||||
// Resource counts
|
||||
let counts = test_km.resource_counts().await.expect("Should get resource counts");
|
||||
let counts = test_km
|
||||
.resource_counts()
|
||||
.await
|
||||
.expect("Should get resource counts");
|
||||
println!("✅ Resource counts: {:?}", counts);
|
||||
|
||||
// DELETE operations
|
||||
println!("\n=== DELETE Operations ===");
|
||||
|
||||
// Delete individual resources
|
||||
test_km.pod_delete("test-pod").await.expect("Should delete pod");
|
||||
test_km
|
||||
.pod_delete("test-pod")
|
||||
.await
|
||||
.expect("Should delete pod");
|
||||
println!("✅ Deleted pod");
|
||||
|
||||
test_km.service_delete("test-service").await.expect("Should delete service");
|
||||
test_km
|
||||
.service_delete("test-service")
|
||||
.await
|
||||
.expect("Should delete service");
|
||||
println!("✅ Deleted service");
|
||||
|
||||
test_km.deployment_delete("test-deployment").await.expect("Should delete deployment");
|
||||
test_km
|
||||
.deployment_delete("test-deployment")
|
||||
.await
|
||||
.expect("Should delete deployment");
|
||||
println!("✅ Deleted deployment");
|
||||
|
||||
test_km.configmap_delete("test-config").await.expect("Should delete configmap");
|
||||
test_km
|
||||
.configmap_delete("test-config")
|
||||
.await
|
||||
.expect("Should delete configmap");
|
||||
println!("✅ Deleted configmap");
|
||||
|
||||
test_km.secret_delete("test-secret").await.expect("Should delete secret");
|
||||
test_km
|
||||
.secret_delete("test-secret")
|
||||
.await
|
||||
.expect("Should delete secret");
|
||||
println!("✅ Deleted secret");
|
||||
|
||||
// Verify resources are deleted
|
||||
let final_counts = test_km.resource_counts().await.expect("Should get final resource counts");
|
||||
let final_counts = test_km
|
||||
.resource_counts()
|
||||
.await
|
||||
.expect("Should get final resource counts");
|
||||
println!("✅ Final resource counts: {:?}", final_counts);
|
||||
|
||||
// Delete the test namespace
|
||||
km.namespace_delete(test_namespace).await.expect("Should delete test namespace");
|
||||
km.namespace_delete(test_namespace)
|
||||
.await
|
||||
.expect("Should delete test namespace");
|
||||
println!("✅ Deleted test namespace");
|
||||
|
||||
println!("\n🎉 All CRUD operations completed successfully!");
|
||||
@@ -151,11 +226,12 @@ mod crud_tests {
|
||||
|
||||
println!("🔍 Testing error handling in CRUD operations...");
|
||||
|
||||
let km = KubernetesManager::new("default").await
|
||||
let km = KubernetesManager::new("default")
|
||||
.await
|
||||
.expect("Should connect to cluster");
|
||||
|
||||
// Test creating resources with invalid names
|
||||
let result = km.pod_create("", "nginx", None).await;
|
||||
let result = km.pod_create("", "nginx", None, None).await;
|
||||
assert!(result.is_err(), "Should fail with empty pod name");
|
||||
println!("✅ Empty pod name properly rejected");
|
||||
|
||||
@@ -166,7 +242,10 @@ mod crud_tests {
|
||||
|
||||
// Test deleting non-existent resources
|
||||
let result = km.service_delete("non-existent-service").await;
|
||||
assert!(result.is_err(), "Should fail to delete non-existent service");
|
||||
assert!(
|
||||
result.is_err(),
|
||||
"Should fail to delete non-existent service"
|
||||
);
|
||||
println!("✅ Non-existent service deletion properly handled");
|
||||
|
||||
println!("✅ Error handling in CRUD operations is robust");
|
||||
|
Reference in New Issue
Block a user