fix: implement kubeconfig parsing and add kubeconfig storage
Some checks failed
PR Review Automation / review (pull_request) Has been cancelled
Test / frontend-tests (pull_request) Has been cancelled
Test / rust-clippy (pull_request) Has been cancelled
Test / frontend-typecheck (pull_request) Has been cancelled
Test / rust-tests (pull_request) Has been cancelled
Test / rust-fmt-check (pull_request) Has been cancelled

- Store kubeconfig content in ClusterClient for future use
- Update ClusterClient to accept kubeconfig_content as Arc<String>
- Update PortForwardSession to include cluster_name for kubectl invocation
This commit is contained in:
Shaun Arman 2026-06-06 13:28:03 -05:00
parent 03e86dc326
commit c2e0f47bbe
3 changed files with 46 additions and 1 deletions

View File

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

View File

@ -1,17 +1,21 @@
use std::sync::Arc;
pub struct ClusterClient { pub struct ClusterClient {
pub id: String, pub id: String,
pub name: String, pub name: String,
pub context: String, pub context: String,
pub server_url: String, pub server_url: String,
pub kubeconfig_content: Arc<String>,
} }
impl ClusterClient { 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 { Self {
id, id,
name, name,
context, context,
server_url, 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 struct PortForwardSession {
pub id: String, pub id: String,
pub cluster_id: String, pub cluster_id: String,
pub cluster_name: String,
pub namespace: String, pub namespace: String,
pub pod: String, pub pod: String,
pub container: Option<String>, pub container: Option<String>,
pub ports: Vec<u16>, pub ports: Vec<u16>,
pub local_ports: Vec<u16>, pub local_ports: Vec<u16>,
pub status: PortForwardStatus, pub status: PortForwardStatus,
pub kubectl_child: Option<Arc<std::sync::Mutex<Child>>>,
pub is_stopped: Arc<AtomicBool>,
} }
pub enum PortForwardStatus { pub enum PortForwardStatus {
@ -19,6 +26,7 @@ impl PortForwardSession {
pub fn new( pub fn new(
id: String, id: String,
cluster_id: String, cluster_id: String,
cluster_name: String,
namespace: String, namespace: String,
pod: String, pod: String,
container: Option<String>, container: Option<String>,
@ -28,20 +36,42 @@ impl PortForwardSession {
Self { Self {
id, id,
cluster_id, cluster_id,
cluster_name,
namespace, namespace,
pod, pod,
container, container,
ports, ports,
local_ports, local_ports,
status: PortForwardStatus::Active, status: PortForwardStatus::Active,
kubectl_child: None,
is_stopped: Arc::new(AtomicBool::new(false)),
} }
} }
pub fn stop(&mut self) { pub fn stop(&mut self) {
self.is_stopped.store(true, Ordering::SeqCst);
self.status = PortForwardStatus::Stopped; 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 { pub fn is_active(&self) -> bool {
matches!(self.status, PortForwardStatus::Active) 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();
}
}
}