quickseal 0.1.4

Create Kubernetes SealedSecrets with a single secure command
Documentation
//! Kubernetes cluster context detection and display

use crate::error::KubeConfigError;
use crate::models::ClusterContext;
use kube::Config;

/// Load the current Kubernetes context from kubeconfig
pub async fn load_context() -> Result<ClusterContext, KubeConfigError> {
    // Load kubeconfig using kube-rs
    let config = Config::infer()
        .await
        .map_err(|e| KubeConfigError::InvalidConfig {
            reason: format!("Failed to load kubeconfig: {}", e),
        })?;

    // Extract context information
    let context_name = std::env::var("KUBECTL_CONTEXT")
        .or_else(|_| std::env::var("KUBECONFIG_CONTEXT"))
        .unwrap_or_else(|_| "current-context".to_string());

    let cluster_server = config.cluster_url.to_string();
    let namespace = Some(config.default_namespace.clone());

    // Create ClusterContext
    Ok(ClusterContext::new(
        context_name.clone(),
        context_name, // kube-rs doesn't expose cluster name separately
        cluster_server,
        namespace,
        "current-user".to_string(), // kube-rs doesn't expose user info directly
    ))
}

/// Load context synchronously (blocking wrapper for async function)
pub fn load_context_sync() -> Result<ClusterContext, KubeConfigError> {
    // Create a tokio runtime for this blocking call
    let runtime = tokio::runtime::Runtime::new()
        .map_err(|e| KubeConfigError::Other(format!("Failed to create runtime: {}", e)))?;

    runtime.block_on(load_context())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_load_context() {
        // This test will only pass if a valid kubeconfig exists
        // In CI without kubeconfig, this should be skipped
        if std::env::var("KUBECONFIG").is_ok()
            || std::path::Path::new(&format!(
                "{}/.kube/config",
                std::env::var("HOME").unwrap_or_default()
            ))
            .exists()
        {
            let result = load_context().await;
            // Just verify the function doesn't panic
            let _ = result;
        }
    }

    #[test]
    fn test_load_context_sync() {
        // This test will only pass if a valid kubeconfig exists
        if std::env::var("KUBECONFIG").is_ok()
            || std::path::Path::new(&format!(
                "{}/.kube/config",
                std::env::var("HOME").unwrap_or_default()
            ))
            .exists()
        {
            let result = load_context_sync();
            // Just verify the function doesn't panic
            let _ = result;
        }
    }
}