| Crates.io | herosal-kubernetes |
| lib.rs | herosal-kubernetes |
| version | 0.1.1 |
| created_at | 2025-12-14 02:30:08.979076+00 |
| updated_at | 2025-12-14 13:34:26.41512+00 |
| description | SAL Kubernetes - Kubernetes cluster management and operations using kube-rs SDK |
| homepage | |
| repository | https://github.com/threefoldtech/sal |
| max_upload_size | |
| id | 1983664 |
| size | 468,886 |
herosal-kubernetes)Kubernetes cluster management and operations for the Herosal library.
Add this to your Cargo.toml:
[dependencies]
herosal-kubernetes = "0.1.0"
This package provides a high-level interface for managing Kubernetes clusters using the kube-rs SDK. It focuses on namespace-scoped operations through the KubernetesManager factory pattern.
Understanding the difference between labels and environment variables is crucial for effective Kubernetes deployments:
app=my-app, tier=backend, environment=production, version=v1.2.3process.env, os.environ, etc. in your applicationNODE_ENV=production, DATABASE_URL=postgres://..., API_KEY=secret// Labels: For Kubernetes resource management
let mut labels = HashMap::new();
labels.insert("app".to_string(), "web-api".to_string()); // Service discovery
labels.insert("tier".to_string(), "backend".to_string()); // Architecture layer
labels.insert("environment".to_string(), "production".to_string()); // Deployment stage
labels.insert("version".to_string(), "v2.1.0".to_string()); // Release version
// Environment Variables: For application configuration
let mut env_vars = HashMap::new();
env_vars.insert("NODE_ENV".to_string(), "production".to_string()); // Runtime mode
env_vars.insert("DATABASE_URL".to_string(), "postgres://db:5432/app".to_string()); // DB connection
env_vars.insert("REDIS_URL".to_string(), "redis://cache:6379".to_string()); // Cache connection
env_vars.insert("LOG_LEVEL".to_string(), "info".to_string()); // Logging config
Deploy complete applications with labels and environment variables:
use herosal_kubernetes::KubernetesManager;
use std::collections::HashMap;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let km = KubernetesManager::new("default").await?;
// Configure labels for Kubernetes resource organization
let mut labels = HashMap::new();
labels.insert("app".to_string(), "my-app".to_string());
labels.insert("tier".to_string(), "backend".to_string());
// Configure environment variables for the container
let mut env_vars = HashMap::new();
env_vars.insert("NODE_ENV".to_string(), "production".to_string());
env_vars.insert("DATABASE_URL".to_string(), "postgres://db:5432/myapp".to_string());
env_vars.insert("API_KEY".to_string(), "secret-api-key".to_string());
// Deploy application with deployment + service
km.deploy_application(
"my-app", // name
"node:18-alpine", // image
3, // replicas
3000, // port
Some(labels), // Kubernetes labels
Some(env_vars), // container environment variables
).await?;
println!("✅ Application deployed successfully!");
Ok(())
}
use herosal_kubernetes::KubernetesManager;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a manager for the "default" namespace
let km = KubernetesManager::new("default").await?;
// List all pods in the namespace
let pods = km.pods_list().await?;
println!("Found {} pods", pods.len());
// Create a namespace (no error if it already exists)
km.namespace_create("my-namespace").await?;
// Delete resources matching a pattern
km.delete("test-.*").await?;
Ok(())
}
// Create Kubernetes manager for namespace
let km = kubernetes_manager_new("default");
// Deploy application with labels and environment variables
deploy_application(km, "my-app", "node:18-alpine", 3, 3000, #{
"app": "my-app",
"tier": "backend",
"environment": "production"
}, #{
"NODE_ENV": "production",
"DATABASE_URL": "postgres://db:5432/myapp",
"API_KEY": "secret-api-key"
});
print("✅ Application deployed!");
// Basic operations
let pods = pods_list(km);
print("Found " + pods.len() + " pods");
namespace_create(km, "my-namespace");
delete(km, "test-.*");
The package uses the standard Kubernetes configuration methods:
In-cluster configuration (when running in a pod)
Kubeconfig file (~/.kube/config or KUBECONFIG environment variable)
Service account tokens
The main interface for Kubernetes operations. Each instance is scoped to a single namespace.
KubernetesManager::new(namespace) - Create a manager for the specified namespacedeploy_application(name, image, replicas, port, labels, env_vars) - Deploy complete application with deployment and servicedeployment_create(name, image, replicas, labels, env_vars) - Create deployment with environment variables and labelspod_create(name, image, labels, env_vars) - Create pod with environment variables and labelsservice_create(name, selector, port, target_port) - Create service with port mappingconfigmap_create(name, data) - Create configmap with datasecret_create(name, data, secret_type) - Create secret with data and optional typepods_list() - List all pods in the namespaceservices_list() - List all services in the namespacedeployments_list() - List all deployments in the namespaceconfigmaps_list() - List all configmaps in the namespacesecrets_list() - List all secrets in the namespacepod_get(name) - Get a specific pod by nameservice_get(name) - Get a specific service by namedeployment_get(name) - Get a specific deployment by namepod_delete(name) - Delete a specific pod by nameservice_delete(name) - Delete a specific service by namedeployment_delete(name) - Delete a specific deployment by nameconfigmap_delete(name) - Delete a specific configmap by namesecret_delete(name) - Delete a specific secret by namedelete(pattern) - Delete all resources matching a PCRE patternnamespace_create(name) - Create a namespace (idempotent)namespace_exists(name) - Check if a namespace existsnamespaces_list() - List all namespaces (cluster-wide)resource_counts() - Get counts of all resource types in the namespacenamespace() - Get the namespace this manager operates onWhen using the Rhai integration, the following functions are available:
Manager Creation & Application Deployment:
kubernetes_manager_new(namespace) - Create a KubernetesManagerdeploy_application(km, name, image, replicas, port, labels, env_vars) - Deploy application with environment variablesResource Listing:
pods_list(km) - List podsservices_list(km) - List servicesdeployments_list(km) - List deploymentsconfigmaps_list(km) - List configmapssecrets_list(km) - List secretsnamespaces_list(km) - List all namespacesresource_counts(km) - Get resource countsResource Operations:
delete(km, pattern) - Delete resources matching patternpod_delete(km, name) - Delete specific podservice_delete(km, name) - Delete specific servicedeployment_delete(km, name) - Delete specific deploymentconfigmap_delete(km, name) - Delete specific configmapsecret_delete(km, name) - Delete specific secretNamespace Functions:
namespace_create(km, name) - Create namespacenamespace_exists(km, name) - Check namespace existencenamespace_delete(km, name) - Delete namespacenamespace(km) - Get manager's namespaceThe examples/kubernetes/clusters/ directory contains comprehensive examples:
Run with: cargo run --example <name> --features kubernetes
postgres - PostgreSQL database deployment with environment variablesredis - Redis cache deployment with configurationgeneric - Multiple application deployments (nginx, node.js, mongodb)Run with: ./target/debug/herodo examples/kubernetes/clusters/<script>.rhai
postgres.rhai - PostgreSQL cluster deployment scriptredis.rhai - Redis cluster deployment scriptlet 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());
km.deploy_application("postgres", "postgres:15", 1, 5432, Some(labels), Some(env_vars)).await?;
let mut env_vars = HashMap::new();
env_vars.insert("REDIS_PASSWORD".to_string(), "redispassword".to_string());
env_vars.insert("REDIS_MAXMEMORY".to_string(), "256mb".to_string());
km.deploy_application("redis", "redis:7-alpine", 3, 6379, None, Some(env_vars)).await?;
# Unit tests (no cluster required)
cargo test --package herosal-kubernetes
# Integration tests (requires cluster)
KUBERNETES_TEST_ENABLED=1 cargo test --package herosal-kubernetes
# Rhai integration tests
KUBERNETES_TEST_ENABLED=1 cargo test --package herosal-kubernetes --features rhai
# Run specific test suites
cargo test --package herosal-kubernetes deployment_env_vars_test
cargo test --package herosal-kubernetes edge_cases_test
# Rhai environment variables test
KUBERNETES_TEST_ENABLED=1 ./target/debug/herodo kubernetes/tests/rhai/env_vars_test.rhai
Kubernetes Cluster: Integration tests require a running Kubernetes cluster
Environment Variable: Set KUBERNETES_TEST_ENABLED=1 to enable integration tests
Permissions: Tests require permissions to create/delete resources in the default namespace
// Production labels for monitoring and management
let mut labels = HashMap::new();
labels.insert("app".to_string(), "web-api".to_string());
labels.insert("version".to_string(), "v1.2.3".to_string());
labels.insert("environment".to_string(), "production".to_string());
labels.insert("team".to_string(), "backend".to_string());
// Non-sensitive environment variables
let mut env_vars = HashMap::new();
env_vars.insert("NODE_ENV".to_string(), "production".to_string());
env_vars.insert("LOG_LEVEL".to_string(), "info".to_string());
env_vars.insert("PORT".to_string(), "3000".to_string());
// Note: Use Kubernetes secrets for DATABASE_URL, API_KEY, etc.
km.deploy_application("web-api", "myapp:v1.2.3", 3, 3000, Some(labels), Some(env_vars)).await?;