|
|
|
@@ -59,605 +59,12 @@ where
|
|
|
|
|
rt.block_on(future).map_err(kubernetes_error_to_rhai_error)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Create a new KubernetesManager for the specified namespace
|
|
|
|
|
///
|
|
|
|
|
/// # Arguments
|
|
|
|
|
///
|
|
|
|
|
/// * `namespace` - The Kubernetes namespace to operate on
|
|
|
|
|
///
|
|
|
|
|
/// # Returns
|
|
|
|
|
///
|
|
|
|
|
/// * `Result<KubernetesManager, Box<EvalAltResult>>` - The manager instance or an error
|
|
|
|
|
fn kubernetes_manager_new(namespace: String) -> Result<KubernetesManager, Box<EvalAltResult>> {
|
|
|
|
|
execute_async(KubernetesManager::new(namespace))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// List all pods in the namespace
|
|
|
|
|
///
|
|
|
|
|
/// # Arguments
|
|
|
|
|
///
|
|
|
|
|
/// * `km` - The KubernetesManager instance
|
|
|
|
|
///
|
|
|
|
|
/// # Returns
|
|
|
|
|
///
|
|
|
|
|
/// * `Result<Array, Box<EvalAltResult>>` - Array of pod names or an error
|
|
|
|
|
fn pods_list(km: &mut KubernetesManager) -> Result<Array, Box<EvalAltResult>> {
|
|
|
|
|
let pods = execute_async(km.pods_list())?;
|
|
|
|
|
|
|
|
|
|
let pod_names: Array = pods
|
|
|
|
|
.iter()
|
|
|
|
|
.filter_map(|pod| pod.metadata.name.as_ref())
|
|
|
|
|
.map(|name| Dynamic::from(name.clone()))
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
Ok(pod_names)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// List all services in the namespace
|
|
|
|
|
///
|
|
|
|
|
/// # Arguments
|
|
|
|
|
///
|
|
|
|
|
/// * `km` - The KubernetesManager instance
|
|
|
|
|
///
|
|
|
|
|
/// # Returns
|
|
|
|
|
///
|
|
|
|
|
/// * `Result<Array, Box<EvalAltResult>>` - Array of service names or an error
|
|
|
|
|
fn services_list(km: &mut KubernetesManager) -> Result<Array, Box<EvalAltResult>> {
|
|
|
|
|
let services = execute_async(km.services_list())?;
|
|
|
|
|
|
|
|
|
|
let service_names: Array = services
|
|
|
|
|
.iter()
|
|
|
|
|
.filter_map(|service| service.metadata.name.as_ref())
|
|
|
|
|
.map(|name| Dynamic::from(name.clone()))
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
Ok(service_names)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// List all deployments in the namespace
|
|
|
|
|
///
|
|
|
|
|
/// # Arguments
|
|
|
|
|
///
|
|
|
|
|
/// * `km` - The KubernetesManager instance
|
|
|
|
|
///
|
|
|
|
|
/// # Returns
|
|
|
|
|
///
|
|
|
|
|
/// * `Result<Array, Box<EvalAltResult>>` - Array of deployment names or an error
|
|
|
|
|
fn deployments_list(km: &mut KubernetesManager) -> Result<Array, Box<EvalAltResult>> {
|
|
|
|
|
let deployments = execute_async(km.deployments_list())?;
|
|
|
|
|
|
|
|
|
|
let deployment_names: Array = deployments
|
|
|
|
|
.iter()
|
|
|
|
|
.filter_map(|deployment| deployment.metadata.name.as_ref())
|
|
|
|
|
.map(|name| Dynamic::from(name.clone()))
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
Ok(deployment_names)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// List all configmaps in the namespace
|
|
|
|
|
///
|
|
|
|
|
/// # Arguments
|
|
|
|
|
///
|
|
|
|
|
/// * `km` - The KubernetesManager instance
|
|
|
|
|
///
|
|
|
|
|
/// # Returns
|
|
|
|
|
///
|
|
|
|
|
/// * `Result<Array, Box<EvalAltResult>>` - Array of configmap names or an error
|
|
|
|
|
fn configmaps_list(km: &mut KubernetesManager) -> Result<Array, Box<EvalAltResult>> {
|
|
|
|
|
let configmaps = execute_async(km.configmaps_list())?;
|
|
|
|
|
|
|
|
|
|
let configmap_names: Array = configmaps
|
|
|
|
|
.iter()
|
|
|
|
|
.filter_map(|configmap| configmap.metadata.name.as_ref())
|
|
|
|
|
.map(|name| Dynamic::from(name.clone()))
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
Ok(configmap_names)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// List all secrets in the namespace
|
|
|
|
|
///
|
|
|
|
|
/// # Arguments
|
|
|
|
|
///
|
|
|
|
|
/// * `km` - The KubernetesManager instance
|
|
|
|
|
///
|
|
|
|
|
/// # Returns
|
|
|
|
|
///
|
|
|
|
|
/// * `Result<Array, Box<EvalAltResult>>` - Array of secret names or an error
|
|
|
|
|
fn secrets_list(km: &mut KubernetesManager) -> Result<Array, Box<EvalAltResult>> {
|
|
|
|
|
let secrets = execute_async(km.secrets_list())?;
|
|
|
|
|
|
|
|
|
|
let secret_names: Array = secrets
|
|
|
|
|
.iter()
|
|
|
|
|
.filter_map(|secret| secret.metadata.name.as_ref())
|
|
|
|
|
.map(|name| Dynamic::from(name.clone()))
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
Ok(secret_names)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Delete resources matching a PCRE pattern
|
|
|
|
|
///
|
|
|
|
|
/// # Arguments
|
|
|
|
|
///
|
|
|
|
|
/// * `km` - The KubernetesManager instance
|
|
|
|
|
/// * `pattern` - PCRE pattern to match resource names against
|
|
|
|
|
///
|
|
|
|
|
/// # Returns
|
|
|
|
|
///
|
|
|
|
|
/// * `Result<i64, Box<EvalAltResult>>` - Number of resources deleted or an error
|
|
|
|
|
///
|
|
|
|
|
/// Create a pod with a single container (backward compatible version)
|
|
|
|
|
///
|
|
|
|
|
/// # Arguments
|
|
|
|
|
///
|
|
|
|
|
/// * `km` - Mutable reference to KubernetesManager
|
|
|
|
|
/// * `name` - Name of the pod
|
|
|
|
|
/// * `image` - Container image to use
|
|
|
|
|
/// * `labels` - Optional labels as a Map
|
|
|
|
|
///
|
|
|
|
|
/// # Returns
|
|
|
|
|
///
|
|
|
|
|
/// * `Result<String, Box<EvalAltResult>>` - Pod name or an error
|
|
|
|
|
fn pod_create(
|
|
|
|
|
km: &mut KubernetesManager,
|
|
|
|
|
name: String,
|
|
|
|
|
image: String,
|
|
|
|
|
labels: Map,
|
|
|
|
|
) -> Result<String, Box<EvalAltResult>> {
|
|
|
|
|
let labels_map: Option<std::collections::HashMap<String, String>> = if labels.is_empty() {
|
|
|
|
|
None
|
|
|
|
|
} else {
|
|
|
|
|
Some(
|
|
|
|
|
labels
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|(k, v)| (k.to_string(), v.to_string()))
|
|
|
|
|
.collect(),
|
|
|
|
|
)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let pod = execute_async(km.pod_create(&name, &image, labels_map, None))?;
|
|
|
|
|
Ok(pod.metadata.name.unwrap_or(name))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Create a pod with a single container and environment variables
|
|
|
|
|
///
|
|
|
|
|
/// # Arguments
|
|
|
|
|
///
|
|
|
|
|
/// * `km` - Mutable reference to KubernetesManager
|
|
|
|
|
/// * `name` - Name of the pod
|
|
|
|
|
/// * `image` - Container image to use
|
|
|
|
|
/// * `labels` - Optional labels as a Map
|
|
|
|
|
/// * `env_vars` - Optional environment variables as a Map
|
|
|
|
|
///
|
|
|
|
|
/// # Returns
|
|
|
|
|
///
|
|
|
|
|
/// * `Result<String, Box<EvalAltResult>>` - Pod name or an error
|
|
|
|
|
fn pod_create_with_env(
|
|
|
|
|
km: &mut KubernetesManager,
|
|
|
|
|
name: String,
|
|
|
|
|
image: String,
|
|
|
|
|
labels: Map,
|
|
|
|
|
env_vars: Map,
|
|
|
|
|
) -> Result<String, Box<EvalAltResult>> {
|
|
|
|
|
let labels_map: Option<std::collections::HashMap<String, String>> = if labels.is_empty() {
|
|
|
|
|
None
|
|
|
|
|
} else {
|
|
|
|
|
Some(
|
|
|
|
|
labels
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|(k, v)| (k.to_string(), v.to_string()))
|
|
|
|
|
.collect(),
|
|
|
|
|
)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let env_vars_map = convert_rhai_map_to_env_vars(env_vars);
|
|
|
|
|
|
|
|
|
|
let pod = execute_async(km.pod_create(&name, &image, labels_map, env_vars_map))?;
|
|
|
|
|
Ok(pod.metadata.name.unwrap_or(name))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Create a service
|
|
|
|
|
///
|
|
|
|
|
/// # Arguments
|
|
|
|
|
///
|
|
|
|
|
/// * `km` - Mutable reference to KubernetesManager
|
|
|
|
|
/// * `name` - Name of the service
|
|
|
|
|
/// * `selector` - Labels to select pods as a Map
|
|
|
|
|
/// * `port` - Port to expose
|
|
|
|
|
/// * `target_port` - Target port on pods (optional, defaults to port)
|
|
|
|
|
///
|
|
|
|
|
/// # Returns
|
|
|
|
|
///
|
|
|
|
|
/// * `Result<String, Box<EvalAltResult>>` - Service name or an error
|
|
|
|
|
fn service_create(
|
|
|
|
|
km: &mut KubernetesManager,
|
|
|
|
|
name: String,
|
|
|
|
|
selector: Map,
|
|
|
|
|
port: i64,
|
|
|
|
|
target_port: i64,
|
|
|
|
|
) -> Result<String, Box<EvalAltResult>> {
|
|
|
|
|
let selector_map: std::collections::HashMap<String, String> = selector
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|(k, v)| (k.to_string(), v.to_string()))
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
let target_port_opt = if target_port == 0 {
|
|
|
|
|
None
|
|
|
|
|
} else {
|
|
|
|
|
Some(target_port as i32)
|
|
|
|
|
};
|
|
|
|
|
let service =
|
|
|
|
|
execute_async(km.service_create(&name, selector_map, port as i32, target_port_opt))?;
|
|
|
|
|
Ok(service.metadata.name.unwrap_or(name))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Create a deployment
|
|
|
|
|
///
|
|
|
|
|
/// # Arguments
|
|
|
|
|
///
|
|
|
|
|
/// * `km` - Mutable reference to KubernetesManager
|
|
|
|
|
/// * `name` - Name of the deployment
|
|
|
|
|
/// * `image` - Container image to use
|
|
|
|
|
/// * `replicas` - Number of replicas
|
|
|
|
|
/// * `labels` - Optional labels as a Map
|
|
|
|
|
///
|
|
|
|
|
/// # Returns
|
|
|
|
|
///
|
|
|
|
|
/// * `Result<String, Box<EvalAltResult>>` - Deployment name or an error
|
|
|
|
|
fn deployment_create(
|
|
|
|
|
km: &mut KubernetesManager,
|
|
|
|
|
name: String,
|
|
|
|
|
image: String,
|
|
|
|
|
replicas: i64,
|
|
|
|
|
labels: Map,
|
|
|
|
|
env_vars: Map,
|
|
|
|
|
) -> Result<String, Box<EvalAltResult>> {
|
|
|
|
|
let labels_map: Option<std::collections::HashMap<String, String>> = if labels.is_empty() {
|
|
|
|
|
None
|
|
|
|
|
} else {
|
|
|
|
|
Some(
|
|
|
|
|
labels
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|(k, v)| (k.to_string(), v.to_string()))
|
|
|
|
|
.collect(),
|
|
|
|
|
)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let env_vars_map = convert_rhai_map_to_env_vars(env_vars);
|
|
|
|
|
|
|
|
|
|
let deployment = execute_async(km.deployment_create(
|
|
|
|
|
&name,
|
|
|
|
|
&image,
|
|
|
|
|
replicas as i32,
|
|
|
|
|
labels_map,
|
|
|
|
|
env_vars_map,
|
|
|
|
|
))?;
|
|
|
|
|
Ok(deployment.metadata.name.unwrap_or(name))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Create a ConfigMap
|
|
|
|
|
///
|
|
|
|
|
/// # Arguments
|
|
|
|
|
///
|
|
|
|
|
/// * `km` - Mutable reference to KubernetesManager
|
|
|
|
|
/// * `name` - Name of the ConfigMap
|
|
|
|
|
/// * `data` - Data as a Map
|
|
|
|
|
///
|
|
|
|
|
/// # Returns
|
|
|
|
|
///
|
|
|
|
|
/// * `Result<String, Box<EvalAltResult>>` - ConfigMap name or an error
|
|
|
|
|
fn configmap_create(
|
|
|
|
|
km: &mut KubernetesManager,
|
|
|
|
|
name: String,
|
|
|
|
|
data: Map,
|
|
|
|
|
) -> Result<String, Box<EvalAltResult>> {
|
|
|
|
|
let data_map: std::collections::HashMap<String, String> = data
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|(k, v)| (k.to_string(), v.to_string()))
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
let configmap = execute_async(km.configmap_create(&name, data_map))?;
|
|
|
|
|
Ok(configmap.metadata.name.unwrap_or(name))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Create a Secret
|
|
|
|
|
///
|
|
|
|
|
/// # Arguments
|
|
|
|
|
///
|
|
|
|
|
/// * `km` - Mutable reference to KubernetesManager
|
|
|
|
|
/// * `name` - Name of the Secret
|
|
|
|
|
/// * `data` - Data as a Map (will be base64 encoded)
|
|
|
|
|
/// * `secret_type` - Type of secret (optional, defaults to "Opaque")
|
|
|
|
|
///
|
|
|
|
|
/// # Returns
|
|
|
|
|
///
|
|
|
|
|
/// * `Result<String, Box<EvalAltResult>>` - Secret name or an error
|
|
|
|
|
fn secret_create(
|
|
|
|
|
km: &mut KubernetesManager,
|
|
|
|
|
name: String,
|
|
|
|
|
data: Map,
|
|
|
|
|
secret_type: String,
|
|
|
|
|
) -> Result<String, Box<EvalAltResult>> {
|
|
|
|
|
let data_map: std::collections::HashMap<String, String> = data
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|(k, v)| (k.to_string(), v.to_string()))
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
let secret_type_opt = if secret_type.is_empty() {
|
|
|
|
|
None
|
|
|
|
|
} else {
|
|
|
|
|
Some(secret_type.as_str())
|
|
|
|
|
};
|
|
|
|
|
let secret = execute_async(km.secret_create(&name, data_map, secret_type_opt))?;
|
|
|
|
|
Ok(secret.metadata.name.unwrap_or(name))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Get a pod by name
|
|
|
|
|
///
|
|
|
|
|
/// # Arguments
|
|
|
|
|
///
|
|
|
|
|
/// * `km` - Mutable reference to KubernetesManager
|
|
|
|
|
/// * `name` - Name of the pod to get
|
|
|
|
|
///
|
|
|
|
|
/// # Returns
|
|
|
|
|
///
|
|
|
|
|
/// * `Result<String, Box<EvalAltResult>>` - Pod name or an error
|
|
|
|
|
fn pod_get(km: &mut KubernetesManager, name: String) -> Result<String, Box<EvalAltResult>> {
|
|
|
|
|
let pod = execute_async(km.pod_get(&name))?;
|
|
|
|
|
Ok(pod.metadata.name.unwrap_or(name))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Get a service by name
|
|
|
|
|
///
|
|
|
|
|
/// # Arguments
|
|
|
|
|
///
|
|
|
|
|
/// * `km` - Mutable reference to KubernetesManager
|
|
|
|
|
/// * `name` - Name of the service to get
|
|
|
|
|
///
|
|
|
|
|
/// # Returns
|
|
|
|
|
///
|
|
|
|
|
/// * `Result<String, Box<EvalAltResult>>` - Service name or an error
|
|
|
|
|
fn service_get(km: &mut KubernetesManager, name: String) -> Result<String, Box<EvalAltResult>> {
|
|
|
|
|
let service = execute_async(km.service_get(&name))?;
|
|
|
|
|
Ok(service.metadata.name.unwrap_or(name))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Get a deployment by name
|
|
|
|
|
///
|
|
|
|
|
/// # Arguments
|
|
|
|
|
///
|
|
|
|
|
/// * `km` - Mutable reference to KubernetesManager
|
|
|
|
|
/// * `name` - Name of the deployment to get
|
|
|
|
|
///
|
|
|
|
|
/// # Returns
|
|
|
|
|
///
|
|
|
|
|
/// * `Result<String, Box<EvalAltResult>>` - Deployment name or an error
|
|
|
|
|
fn deployment_get(km: &mut KubernetesManager, name: String) -> Result<String, Box<EvalAltResult>> {
|
|
|
|
|
let deployment = execute_async(km.deployment_get(&name))?;
|
|
|
|
|
Ok(deployment.metadata.name.unwrap_or(name))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn delete(km: &mut KubernetesManager, pattern: String) -> Result<i64, Box<EvalAltResult>> {
|
|
|
|
|
let deleted_count = execute_async(km.delete(&pattern))?;
|
|
|
|
|
|
|
|
|
|
Ok(deleted_count as i64)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Create a namespace (idempotent operation)
|
|
|
|
|
///
|
|
|
|
|
/// # Arguments
|
|
|
|
|
///
|
|
|
|
|
/// * `km` - The KubernetesManager instance
|
|
|
|
|
/// * `name` - The name of the namespace to create
|
|
|
|
|
///
|
|
|
|
|
/// # Returns
|
|
|
|
|
///
|
|
|
|
|
/// * `Result<(), Box<EvalAltResult>>` - Success or an error
|
|
|
|
|
fn namespace_create(km: &mut KubernetesManager, name: String) -> Result<(), Box<EvalAltResult>> {
|
|
|
|
|
execute_async(km.namespace_create(&name))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Delete a namespace (destructive operation)
|
|
|
|
|
///
|
|
|
|
|
/// # Arguments
|
|
|
|
|
///
|
|
|
|
|
/// * `km` - Mutable reference to KubernetesManager
|
|
|
|
|
/// * `name` - Name of the namespace to delete
|
|
|
|
|
///
|
|
|
|
|
/// # Returns
|
|
|
|
|
///
|
|
|
|
|
/// * `Result<(), Box<EvalAltResult>>` - Success or an error
|
|
|
|
|
fn namespace_delete(km: &mut KubernetesManager, name: String) -> Result<(), Box<EvalAltResult>> {
|
|
|
|
|
execute_async(km.namespace_delete(&name))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Check if a namespace exists
|
|
|
|
|
///
|
|
|
|
|
/// # Arguments
|
|
|
|
|
///
|
|
|
|
|
/// * `km` - The KubernetesManager instance
|
|
|
|
|
/// * `name` - The name of the namespace to check
|
|
|
|
|
///
|
|
|
|
|
/// # Returns
|
|
|
|
|
///
|
|
|
|
|
/// * `Result<bool, Box<EvalAltResult>>` - True if namespace exists, false otherwise
|
|
|
|
|
fn namespace_exists(km: &mut KubernetesManager, name: String) -> Result<bool, Box<EvalAltResult>> {
|
|
|
|
|
execute_async(km.namespace_exists(&name))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// List all namespaces
|
|
|
|
|
///
|
|
|
|
|
/// # Arguments
|
|
|
|
|
///
|
|
|
|
|
/// * `km` - The KubernetesManager instance
|
|
|
|
|
///
|
|
|
|
|
/// # Returns
|
|
|
|
|
///
|
|
|
|
|
/// * `Result<Array, Box<EvalAltResult>>` - Array of namespace names or an error
|
|
|
|
|
fn namespaces_list(km: &mut KubernetesManager) -> Result<Array, Box<EvalAltResult>> {
|
|
|
|
|
let namespaces = execute_async(km.namespaces_list())?;
|
|
|
|
|
|
|
|
|
|
let namespace_names: Array = namespaces
|
|
|
|
|
.iter()
|
|
|
|
|
.filter_map(|ns| ns.metadata.name.as_ref())
|
|
|
|
|
.map(|name| Dynamic::from(name.clone()))
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
Ok(namespace_names)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Get resource counts for the namespace
|
|
|
|
|
///
|
|
|
|
|
/// # Arguments
|
|
|
|
|
///
|
|
|
|
|
/// * `km` - The KubernetesManager instance
|
|
|
|
|
///
|
|
|
|
|
/// # Returns
|
|
|
|
|
///
|
|
|
|
|
/// * `Result<Map, Box<EvalAltResult>>` - Map of resource counts by type or an error
|
|
|
|
|
fn resource_counts(km: &mut KubernetesManager) -> Result<Map, Box<EvalAltResult>> {
|
|
|
|
|
let counts = execute_async(km.resource_counts())?;
|
|
|
|
|
|
|
|
|
|
let mut rhai_map = Map::new();
|
|
|
|
|
for (key, value) in counts {
|
|
|
|
|
rhai_map.insert(key.into(), Dynamic::from(value as i64));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(rhai_map)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Deploy a complete application with deployment and service
|
|
|
|
|
///
|
|
|
|
|
/// # Arguments
|
|
|
|
|
///
|
|
|
|
|
/// * `km` - Mutable reference to KubernetesManager
|
|
|
|
|
/// * `name` - Name of the application
|
|
|
|
|
/// * `image` - Container image to use
|
|
|
|
|
/// * `replicas` - Number of replicas
|
|
|
|
|
/// * `port` - Port the application listens on
|
|
|
|
|
/// * `labels` - Optional labels as a Map
|
|
|
|
|
/// * `env_vars` - Optional environment variables as a Map
|
|
|
|
|
///
|
|
|
|
|
/// # Returns
|
|
|
|
|
///
|
|
|
|
|
/// * `Result<String, Box<EvalAltResult>>` - Success message or an error
|
|
|
|
|
fn deploy_application(
|
|
|
|
|
km: &mut KubernetesManager,
|
|
|
|
|
name: String,
|
|
|
|
|
image: String,
|
|
|
|
|
replicas: i64,
|
|
|
|
|
port: i64,
|
|
|
|
|
labels: Map,
|
|
|
|
|
env_vars: Map,
|
|
|
|
|
) -> Result<String, Box<EvalAltResult>> {
|
|
|
|
|
let labels_map: Option<std::collections::HashMap<String, String>> = if labels.is_empty() {
|
|
|
|
|
None
|
|
|
|
|
} else {
|
|
|
|
|
Some(
|
|
|
|
|
labels
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|(k, v)| (k.to_string(), v.to_string()))
|
|
|
|
|
.collect(),
|
|
|
|
|
)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let env_vars_map = convert_rhai_map_to_env_vars(env_vars);
|
|
|
|
|
|
|
|
|
|
execute_async(km.deploy_application(
|
|
|
|
|
&name,
|
|
|
|
|
&image,
|
|
|
|
|
replicas as i32,
|
|
|
|
|
port as i32,
|
|
|
|
|
labels_map,
|
|
|
|
|
env_vars_map,
|
|
|
|
|
))?;
|
|
|
|
|
|
|
|
|
|
Ok(format!("Successfully deployed application '{name}'"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Delete a specific pod by name
|
|
|
|
|
///
|
|
|
|
|
/// # Arguments
|
|
|
|
|
///
|
|
|
|
|
/// * `km` - The KubernetesManager instance
|
|
|
|
|
/// * `name` - The name of the pod to delete
|
|
|
|
|
///
|
|
|
|
|
/// # Returns
|
|
|
|
|
///
|
|
|
|
|
/// * `Result<(), Box<EvalAltResult>>` - Success or an error
|
|
|
|
|
fn pod_delete(km: &mut KubernetesManager, name: String) -> Result<(), Box<EvalAltResult>> {
|
|
|
|
|
execute_async(km.pod_delete(&name))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Delete a specific service by name
|
|
|
|
|
///
|
|
|
|
|
/// # Arguments
|
|
|
|
|
///
|
|
|
|
|
/// * `km` - The KubernetesManager instance
|
|
|
|
|
/// * `name` - The name of the service to delete
|
|
|
|
|
///
|
|
|
|
|
/// # Returns
|
|
|
|
|
///
|
|
|
|
|
/// * `Result<(), Box<EvalAltResult>>` - Success or an error
|
|
|
|
|
fn service_delete(km: &mut KubernetesManager, name: String) -> Result<(), Box<EvalAltResult>> {
|
|
|
|
|
execute_async(km.service_delete(&name))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Delete a specific deployment by name
|
|
|
|
|
///
|
|
|
|
|
/// # Arguments
|
|
|
|
|
///
|
|
|
|
|
/// * `km` - The KubernetesManager instance
|
|
|
|
|
/// * `name` - The name of the deployment to delete
|
|
|
|
|
///
|
|
|
|
|
/// # Returns
|
|
|
|
|
///
|
|
|
|
|
/// * `Result<(), Box<EvalAltResult>>` - Success or an error
|
|
|
|
|
fn deployment_delete(km: &mut KubernetesManager, name: String) -> Result<(), Box<EvalAltResult>> {
|
|
|
|
|
execute_async(km.deployment_delete(&name))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Delete a ConfigMap by name
|
|
|
|
|
///
|
|
|
|
|
/// # Arguments
|
|
|
|
|
///
|
|
|
|
|
/// * `km` - Mutable reference to KubernetesManager
|
|
|
|
|
/// * `name` - Name of the ConfigMap to delete
|
|
|
|
|
///
|
|
|
|
|
/// # Returns
|
|
|
|
|
///
|
|
|
|
|
/// * `Result<(), Box<EvalAltResult>>` - Success or an error
|
|
|
|
|
fn configmap_delete(km: &mut KubernetesManager, name: String) -> Result<(), Box<EvalAltResult>> {
|
|
|
|
|
execute_async(km.configmap_delete(&name))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Delete a Secret by name
|
|
|
|
|
///
|
|
|
|
|
/// # Arguments
|
|
|
|
|
///
|
|
|
|
|
/// * `km` - Mutable reference to KubernetesManager
|
|
|
|
|
/// * `name` - Name of the Secret to delete
|
|
|
|
|
///
|
|
|
|
|
/// # Returns
|
|
|
|
|
///
|
|
|
|
|
/// * `Result<(), Box<EvalAltResult>>` - Success or an error
|
|
|
|
|
fn secret_delete(km: &mut KubernetesManager, name: String) -> Result<(), Box<EvalAltResult>> {
|
|
|
|
|
execute_async(km.secret_delete(&name))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Get the namespace this manager operates on
|
|
|
|
|
///
|
|
|
|
|
/// # Arguments
|
|
|
|
|
///
|
|
|
|
|
/// * `km` - The KubernetesManager instance
|
|
|
|
|
///
|
|
|
|
|
/// # Returns
|
|
|
|
|
///
|
|
|
|
|
/// * `String` - The namespace name
|
|
|
|
|
fn kubernetes_manager_namespace(km: &mut KubernetesManager) -> String {
|
|
|
|
|
km.namespace().to_string()
|
|
|
|
|
/// Helper function for error conversion
|
|
|
|
|
fn kubernetes_error_to_rhai_error(error: KubernetesError) -> Box<EvalAltResult> {
|
|
|
|
|
Box::new(EvalAltResult::ErrorRuntime(
|
|
|
|
|
format!("Kubernetes error: {error}").into(),
|
|
|
|
|
rhai::Position::NONE,
|
|
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Register Kubernetes module functions with the Rhai engine
|
|
|
|
@@ -720,10 +127,293 @@ pub fn register_kubernetes_module(engine: &mut Engine) -> Result<(), Box<EvalAlt
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Helper function for error conversion
|
|
|
|
|
fn kubernetes_error_to_rhai_error(error: KubernetesError) -> Box<EvalAltResult> {
|
|
|
|
|
Box::new(EvalAltResult::ErrorRuntime(
|
|
|
|
|
format!("Kubernetes error: {error}").into(),
|
|
|
|
|
rhai::Position::NONE,
|
|
|
|
|
))
|
|
|
|
|
// KubernetesManager constructor and methods
|
|
|
|
|
fn kubernetes_manager_new(namespace: String) -> Result<KubernetesManager, Box<EvalAltResult>> {
|
|
|
|
|
execute_async(KubernetesManager::new(namespace))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn kubernetes_manager_namespace(km: &mut KubernetesManager) -> String {
|
|
|
|
|
km.namespace().to_string()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Resource listing functions
|
|
|
|
|
fn pods_list(km: &mut KubernetesManager) -> Result<Array, Box<EvalAltResult>> {
|
|
|
|
|
let pods = execute_async(km.pods_list())?;
|
|
|
|
|
let pod_names: Array = pods
|
|
|
|
|
.iter()
|
|
|
|
|
.filter_map(|pod| pod.metadata.name.as_ref())
|
|
|
|
|
.map(|name| Dynamic::from(name.clone()))
|
|
|
|
|
.collect();
|
|
|
|
|
Ok(pod_names)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn services_list(km: &mut KubernetesManager) -> Result<Array, Box<EvalAltResult>> {
|
|
|
|
|
let services = execute_async(km.services_list())?;
|
|
|
|
|
let service_names: Array = services
|
|
|
|
|
.iter()
|
|
|
|
|
.filter_map(|service| service.metadata.name.as_ref())
|
|
|
|
|
.map(|name| Dynamic::from(name.clone()))
|
|
|
|
|
.collect();
|
|
|
|
|
Ok(service_names)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn deployments_list(km: &mut KubernetesManager) -> Result<Array, Box<EvalAltResult>> {
|
|
|
|
|
let deployments = execute_async(km.deployments_list())?;
|
|
|
|
|
let deployment_names: Array = deployments
|
|
|
|
|
.iter()
|
|
|
|
|
.filter_map(|deployment| deployment.metadata.name.as_ref())
|
|
|
|
|
.map(|name| Dynamic::from(name.clone()))
|
|
|
|
|
.collect();
|
|
|
|
|
Ok(deployment_names)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn configmaps_list(km: &mut KubernetesManager) -> Result<Array, Box<EvalAltResult>> {
|
|
|
|
|
let configmaps = execute_async(km.configmaps_list())?;
|
|
|
|
|
let configmap_names: Array = configmaps
|
|
|
|
|
.iter()
|
|
|
|
|
.filter_map(|configmap| configmap.metadata.name.as_ref())
|
|
|
|
|
.map(|name| Dynamic::from(name.clone()))
|
|
|
|
|
.collect();
|
|
|
|
|
Ok(configmap_names)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn secrets_list(km: &mut KubernetesManager) -> Result<Array, Box<EvalAltResult>> {
|
|
|
|
|
let secrets = execute_async(km.secrets_list())?;
|
|
|
|
|
let secret_names: Array = secrets
|
|
|
|
|
.iter()
|
|
|
|
|
.filter_map(|secret| secret.metadata.name.as_ref())
|
|
|
|
|
.map(|name| Dynamic::from(name.clone()))
|
|
|
|
|
.collect();
|
|
|
|
|
Ok(secret_names)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Resource creation functions
|
|
|
|
|
fn pod_create(
|
|
|
|
|
km: &mut KubernetesManager,
|
|
|
|
|
name: String,
|
|
|
|
|
image: String,
|
|
|
|
|
labels: Map,
|
|
|
|
|
) -> Result<String, Box<EvalAltResult>> {
|
|
|
|
|
let labels_map: Option<std::collections::HashMap<String, String>> = if labels.is_empty() {
|
|
|
|
|
None
|
|
|
|
|
} else {
|
|
|
|
|
Some(
|
|
|
|
|
labels
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|(k, v)| (k.to_string(), v.to_string()))
|
|
|
|
|
.collect(),
|
|
|
|
|
)
|
|
|
|
|
};
|
|
|
|
|
let pod = execute_async(km.pod_create(&name, &image, labels_map, None))?;
|
|
|
|
|
Ok(pod.metadata.name.unwrap_or(name))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn pod_create_with_env(
|
|
|
|
|
km: &mut KubernetesManager,
|
|
|
|
|
name: String,
|
|
|
|
|
image: String,
|
|
|
|
|
labels: Map,
|
|
|
|
|
env_vars: Map,
|
|
|
|
|
) -> Result<String, Box<EvalAltResult>> {
|
|
|
|
|
let labels_map: Option<std::collections::HashMap<String, String>> = if labels.is_empty() {
|
|
|
|
|
None
|
|
|
|
|
} else {
|
|
|
|
|
Some(
|
|
|
|
|
labels
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|(k, v)| (k.to_string(), v.to_string()))
|
|
|
|
|
.collect(),
|
|
|
|
|
)
|
|
|
|
|
};
|
|
|
|
|
let env_vars_map = convert_rhai_map_to_env_vars(env_vars);
|
|
|
|
|
let pod = execute_async(km.pod_create(&name, &image, labels_map, env_vars_map))?;
|
|
|
|
|
Ok(pod.metadata.name.unwrap_or(name))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn service_create(
|
|
|
|
|
km: &mut KubernetesManager,
|
|
|
|
|
name: String,
|
|
|
|
|
selector: Map,
|
|
|
|
|
port: i64,
|
|
|
|
|
target_port: i64,
|
|
|
|
|
) -> Result<String, Box<EvalAltResult>> {
|
|
|
|
|
let selector_map: std::collections::HashMap<String, String> = selector
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|(k, v)| (k.to_string(), v.to_string()))
|
|
|
|
|
.collect();
|
|
|
|
|
let target_port_opt = if target_port == 0 {
|
|
|
|
|
None
|
|
|
|
|
} else {
|
|
|
|
|
Some(target_port as i32)
|
|
|
|
|
};
|
|
|
|
|
let service =
|
|
|
|
|
execute_async(km.service_create(&name, selector_map, port as i32, target_port_opt))?;
|
|
|
|
|
Ok(service.metadata.name.unwrap_or(name))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn deployment_create(
|
|
|
|
|
km: &mut KubernetesManager,
|
|
|
|
|
name: String,
|
|
|
|
|
image: String,
|
|
|
|
|
replicas: i64,
|
|
|
|
|
labels: Map,
|
|
|
|
|
env_vars: Map,
|
|
|
|
|
) -> Result<String, Box<EvalAltResult>> {
|
|
|
|
|
let labels_map: Option<std::collections::HashMap<String, String>> = if labels.is_empty() {
|
|
|
|
|
None
|
|
|
|
|
} else {
|
|
|
|
|
Some(
|
|
|
|
|
labels
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|(k, v)| (k.to_string(), v.to_string()))
|
|
|
|
|
.collect(),
|
|
|
|
|
)
|
|
|
|
|
};
|
|
|
|
|
let env_vars_map = convert_rhai_map_to_env_vars(env_vars);
|
|
|
|
|
let deployment = execute_async(km.deployment_create(
|
|
|
|
|
&name,
|
|
|
|
|
&image,
|
|
|
|
|
replicas as i32,
|
|
|
|
|
labels_map,
|
|
|
|
|
env_vars_map,
|
|
|
|
|
))?;
|
|
|
|
|
Ok(deployment.metadata.name.unwrap_or(name))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn configmap_create(
|
|
|
|
|
km: &mut KubernetesManager,
|
|
|
|
|
name: String,
|
|
|
|
|
data: Map,
|
|
|
|
|
) -> Result<String, Box<EvalAltResult>> {
|
|
|
|
|
let data_map: std::collections::HashMap<String, String> = data
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|(k, v)| (k.to_string(), v.to_string()))
|
|
|
|
|
.collect();
|
|
|
|
|
let configmap = execute_async(km.configmap_create(&name, data_map))?;
|
|
|
|
|
Ok(configmap.metadata.name.unwrap_or(name))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn secret_create(
|
|
|
|
|
km: &mut KubernetesManager,
|
|
|
|
|
name: String,
|
|
|
|
|
data: Map,
|
|
|
|
|
secret_type: String,
|
|
|
|
|
) -> Result<String, Box<EvalAltResult>> {
|
|
|
|
|
let data_map: std::collections::HashMap<String, String> = data
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|(k, v)| (k.to_string(), v.to_string()))
|
|
|
|
|
.collect();
|
|
|
|
|
let secret_type_opt = if secret_type.is_empty() {
|
|
|
|
|
None
|
|
|
|
|
} else {
|
|
|
|
|
Some(secret_type.as_str())
|
|
|
|
|
};
|
|
|
|
|
let secret = execute_async(km.secret_create(&name, data_map, secret_type_opt))?;
|
|
|
|
|
Ok(secret.metadata.name.unwrap_or(name))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Resource get functions
|
|
|
|
|
fn pod_get(km: &mut KubernetesManager, name: String) -> Result<String, Box<EvalAltResult>> {
|
|
|
|
|
let pod = execute_async(km.pod_get(&name))?;
|
|
|
|
|
Ok(pod.metadata.name.unwrap_or(name))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn service_get(km: &mut KubernetesManager, name: String) -> Result<String, Box<EvalAltResult>> {
|
|
|
|
|
let service = execute_async(km.service_get(&name))?;
|
|
|
|
|
Ok(service.metadata.name.unwrap_or(name))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn deployment_get(km: &mut KubernetesManager, name: String) -> Result<String, Box<EvalAltResult>> {
|
|
|
|
|
let deployment = execute_async(km.deployment_get(&name))?;
|
|
|
|
|
Ok(deployment.metadata.name.unwrap_or(name))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Resource deletion functions
|
|
|
|
|
fn delete(km: &mut KubernetesManager, pattern: String) -> Result<i64, Box<EvalAltResult>> {
|
|
|
|
|
let deleted_count = execute_async(km.delete(&pattern))?;
|
|
|
|
|
Ok(deleted_count as i64)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn pod_delete(km: &mut KubernetesManager, name: String) -> Result<(), Box<EvalAltResult>> {
|
|
|
|
|
execute_async(km.pod_delete(&name))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn service_delete(km: &mut KubernetesManager, name: String) -> Result<(), Box<EvalAltResult>> {
|
|
|
|
|
execute_async(km.service_delete(&name))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn deployment_delete(km: &mut KubernetesManager, name: String) -> Result<(), Box<EvalAltResult>> {
|
|
|
|
|
execute_async(km.deployment_delete(&name))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn configmap_delete(km: &mut KubernetesManager, name: String) -> Result<(), Box<EvalAltResult>> {
|
|
|
|
|
execute_async(km.configmap_delete(&name))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn secret_delete(km: &mut KubernetesManager, name: String) -> Result<(), Box<EvalAltResult>> {
|
|
|
|
|
execute_async(km.secret_delete(&name))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Namespace management functions
|
|
|
|
|
fn namespace_create(km: &mut KubernetesManager, name: String) -> Result<(), Box<EvalAltResult>> {
|
|
|
|
|
execute_async(km.namespace_create(&name))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn namespace_delete(km: &mut KubernetesManager, name: String) -> Result<(), Box<EvalAltResult>> {
|
|
|
|
|
execute_async(km.namespace_delete(&name))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn namespace_exists(km: &mut KubernetesManager, name: String) -> Result<bool, Box<EvalAltResult>> {
|
|
|
|
|
execute_async(km.namespace_exists(&name))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn namespaces_list(km: &mut KubernetesManager) -> Result<Array, Box<EvalAltResult>> {
|
|
|
|
|
let namespaces = execute_async(km.namespaces_list())?;
|
|
|
|
|
let namespace_names: Array = namespaces
|
|
|
|
|
.iter()
|
|
|
|
|
.filter_map(|ns| ns.metadata.name.as_ref())
|
|
|
|
|
.map(|name| Dynamic::from(name.clone()))
|
|
|
|
|
.collect();
|
|
|
|
|
Ok(namespace_names)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Utility and convenience functions
|
|
|
|
|
fn resource_counts(km: &mut KubernetesManager) -> Result<Map, Box<EvalAltResult>> {
|
|
|
|
|
let counts = execute_async(km.resource_counts())?;
|
|
|
|
|
let mut rhai_map = Map::new();
|
|
|
|
|
for (key, value) in counts {
|
|
|
|
|
rhai_map.insert(key.into(), Dynamic::from(value as i64));
|
|
|
|
|
}
|
|
|
|
|
Ok(rhai_map)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn deploy_application(
|
|
|
|
|
km: &mut KubernetesManager,
|
|
|
|
|
name: String,
|
|
|
|
|
image: String,
|
|
|
|
|
replicas: i64,
|
|
|
|
|
port: i64,
|
|
|
|
|
labels: Map,
|
|
|
|
|
env_vars: Map,
|
|
|
|
|
) -> Result<String, Box<EvalAltResult>> {
|
|
|
|
|
let labels_map: Option<std::collections::HashMap<String, String>> = if labels.is_empty() {
|
|
|
|
|
None
|
|
|
|
|
} else {
|
|
|
|
|
Some(
|
|
|
|
|
labels
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|(k, v)| (k.to_string(), v.to_string()))
|
|
|
|
|
.collect(),
|
|
|
|
|
)
|
|
|
|
|
};
|
|
|
|
|
let env_vars_map = convert_rhai_map_to_env_vars(env_vars);
|
|
|
|
|
execute_async(km.deploy_application(
|
|
|
|
|
&name,
|
|
|
|
|
&image,
|
|
|
|
|
replicas as i32,
|
|
|
|
|
port as i32,
|
|
|
|
|
labels_map,
|
|
|
|
|
env_vars_map,
|
|
|
|
|
))?;
|
|
|
|
|
Ok(format!("Successfully deployed application '{name}'"))
|
|
|
|
|
}
|
|
|
|
|