feat(kube): add Kubernetes management GUI components #71

Merged
sarman merged 10 commits from feature/kubernetes-management into master 2026-06-06 19:19:27 +00:00
3 changed files with 46 additions and 1 deletions
Showing only changes of commit c2e0f47bbe - Show all commits

View File

@ -2,6 +2,7 @@ use crate::kube::ClusterClient;
use crate::state::AppState;
use serde::{Deserialize, Serialize};
use serde_yaml::Value;
use std::sync::Arc;
use tauri::State;
#[derive(Debug, Clone, Serialize, Deserialize)]
@ -45,11 +46,13 @@ pub async fn add_cluster(
let context = extract_context(&kubeconfig_content)?;
let server_url = extract_server_url(&kubeconfig_content)?;
let kubeconfig_arc = Arc::new(kubeconfig_content.clone());
let client = ClusterClient::new(
id.clone(),
name.clone(),
context.clone(),
server_url.clone(),
kubeconfig_arc,
);
{
@ -143,9 +146,17 @@ pub async fn start_port_forward(
) -> Result<PortForwardResponse, String> {
let session_id = uuid::Uuid::now_v7().to_string();
let clusters = state.clusters.lock().await;
let cluster = clusters.get(&request.cluster_id)
.ok_or_else(|| format!("Cluster {} not found", request.cluster_id))?;
let cluster_name = cluster.name.clone();
let _kubeconfig_content = cluster.kubeconfig_content.clone();
let session = crate::kube::PortForwardSession::new(
session_id.clone(),
request.cluster_id.clone(),
cluster_name,
request.namespace.clone(),
request.pod.clone(),
None,

View File

@ -1,17 +1,21 @@
use std::sync::Arc;
pub struct ClusterClient {
pub id: String,
pub name: String,
pub context: String,
pub server_url: String,
pub kubeconfig_content: Arc<String>,
}
impl ClusterClient {
pub fn new(id: String, name: String, context: String, server_url: String) -> Self {
pub fn new(id: String, name: String, context: String, server_url: String, kubeconfig_content: Arc<String>) -> Self {
Self {
id,
name,
context,
server_url,
kubeconfig_content,
}
}
}

View File

@ -1,12 +1,19 @@
use std::process::{Child, Command, Stdio};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
pub struct PortForwardSession {
pub id: String,
pub cluster_id: String,
pub cluster_name: String,
pub namespace: String,
pub pod: String,
pub container: Option<String>,
pub ports: Vec<u16>,
pub local_ports: Vec<u16>,
pub status: PortForwardStatus,
pub kubectl_child: Option<Arc<std::sync::Mutex<Child>>>,
pub is_stopped: Arc<AtomicBool>,
}
pub enum PortForwardStatus {
@ -19,6 +26,7 @@ impl PortForwardSession {
pub fn new(
id: String,
cluster_id: String,
cluster_name: String,
namespace: String,
pod: String,
container: Option<String>,
@ -28,20 +36,42 @@ impl PortForwardSession {
Self {
id,
cluster_id,
cluster_name,
namespace,
pod,
container,
ports,
local_ports,
status: PortForwardStatus::Active,
kubectl_child: None,
is_stopped: Arc::new(AtomicBool::new(false)),
}
}
pub fn stop(&mut self) {
self.is_stopped.store(true, Ordering::SeqCst);
self.status = PortForwardStatus::Stopped;
if let Some(child_mutex) = &self.kubectl_child {
let mut child = child_mutex.lock().unwrap();
let _ = child.kill();
}
}
pub fn is_active(&self) -> bool {
matches!(self.status, PortForwardStatus::Active)
}
}
impl Drop for PortForwardSession {
fn drop(&mut self) {
if self.is_stopped.load(Ordering::SeqCst) {
return;
}
if let Some(child_mutex) = &self.kubectl_child {
let mut child = child_mutex.lock().unwrap();
let _ = child.kill();
}
}
}