Commit Diff


commit - a2e0f5a3782cd1c8d92f9b3db506d504f103e88c
commit + c3d360eb390f1acc6cab4a67a30ee2757802c3ff
blob - 3dd15aaaf3b787b2d6f339e2dba538fc1bdd14bd
blob + c06b7f0cdb978e8585579725632a0387d224fd65
--- Cargo.lock
+++ Cargo.lock
@@ -415,7 +415,7 @@ dependencies = [
  "rustls-pki-types",
  "tokio",
  "tokio-rustls 0.26.4",
- "tower 0.5.2",
+ "tower",
  "tracing",
 ]
 
@@ -1749,21 +1749,6 @@ dependencies = [
 
 [[package]]
 name = "jsonpath-rust"
-version = "0.5.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "19d8fe85bd70ff715f31ce8c739194b423d79811a19602115d611a3ec85d6200"
-dependencies = [
- "lazy_static",
- "once_cell",
- "pest",
- "pest_derive",
- "regex",
- "serde_json",
- "thiserror 1.0.69",
-]
-
-[[package]]
-name = "jsonpath-rust"
 version = "0.7.5"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "0c00ae348f9f8fd2d09f82a98ca381c60df9e0820d8d79fce43e649b4dc3128b"
@@ -1824,7 +1809,7 @@ dependencies = [
  "base64 0.22.1",
  "http 1.3.1",
  "k8s-openapi 0.22.0",
- "kube 0.91.0",
+ "kube",
  "pem",
  "percent-encoding",
  "rustls 0.23.35",
@@ -1885,15 +1870,18 @@ name = "kopsd"
 version = "0.1.0"
 dependencies = [
  "anyhow",
+ "aws-config",
+ "aws-credential-types",
  "chrono",
  "clap",
  "config",
  "daemonize",
  "futures",
  "k8s-openapi 0.26.0",
+ "kops_aws_eks",
  "kops_log",
  "kops_protocol",
- "kube 2.0.1",
+ "kube",
  "kube-runtime",
  "serde",
  "tokio",
@@ -1902,66 +1890,18 @@ dependencies = [
 
 [[package]]
 name = "kube"
-version = "0.91.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "264461a7ebf4fb0fcf23e4c7e4f9387c5696ee61d003de207d9b5a895ff37bfa"
-dependencies = [
- "k8s-openapi 0.22.0",
- "kube-client 0.91.0",
- "kube-core 0.91.0",
-]
-
-[[package]]
-name = "kube"
 version = "2.0.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "48e7bb0b6a46502cc20e4575b6ff401af45cfea150b34ba272a3410b78aa014e"
 dependencies = [
  "k8s-openapi 0.26.0",
- "kube-client 2.0.1",
- "kube-core 2.0.1",
+ "kube-client",
+ "kube-core",
  "kube-runtime",
 ]
 
 [[package]]
 name = "kube-client"
-version = "0.91.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "47164ad6c47398ee4bdf90509c7b44026229721cb1377eb4623a1ec2a00a85e9"
-dependencies = [
- "base64 0.22.1",
- "bytes",
- "chrono",
- "either",
- "futures",
- "home",
- "http 1.3.1",
- "http-body 1.0.1",
- "http-body-util",
- "hyper 1.8.1",
- "hyper-rustls 0.27.7",
- "hyper-timeout",
- "hyper-util",
- "jsonpath-rust 0.5.1",
- "k8s-openapi 0.22.0",
- "kube-core 0.91.0",
- "pem",
- "rustls 0.23.35",
- "rustls-pemfile 2.2.0",
- "secrecy 0.8.0",
- "serde",
- "serde_json",
- "serde_yaml",
- "thiserror 1.0.69",
- "tokio",
- "tokio-util",
- "tower 0.4.13",
- "tower-http 0.5.2",
- "tracing",
-]
-
-[[package]]
-name = "kube-client"
 version = "2.0.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "4987d57a184d2b5294fdad3d7fc7f278899469d21a4da39a8f6ca16426567a36"
@@ -1979,40 +1919,25 @@ dependencies = [
  "hyper-rustls 0.27.7",
  "hyper-timeout",
  "hyper-util",
- "jsonpath-rust 0.7.5",
+ "jsonpath-rust",
  "k8s-openapi 0.26.0",
- "kube-core 2.0.1",
+ "kube-core",
  "pem",
  "rustls 0.23.35",
- "secrecy 0.10.3",
+ "secrecy",
  "serde",
  "serde_json",
  "serde_yaml",
  "thiserror 2.0.17",
  "tokio",
  "tokio-util",
- "tower 0.5.2",
- "tower-http 0.6.6",
+ "tower",
+ "tower-http",
  "tracing",
 ]
 
 [[package]]
 name = "kube-core"
-version = "0.91.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "2797d3044a238825432129cd9537e12c2a6dacbbb5352381af5ea55e1505ed4f"
-dependencies = [
- "chrono",
- "form_urlencoded",
- "http 1.3.1",
- "k8s-openapi 0.22.0",
- "serde",
- "serde_json",
- "thiserror 1.0.69",
-]
-
-[[package]]
-name = "kube-core"
 version = "2.0.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "914bbb770e7bb721a06e3538c0edd2babed46447d128f7c21caa68747060ee73"
@@ -2045,7 +1970,7 @@ dependencies = [
  "hostname",
  "json-patch",
  "k8s-openapi 0.26.0",
- "kube-client 2.0.1",
+ "kube-client",
  "parking_lot",
  "pin-project",
  "serde",
@@ -2652,16 +2577,6 @@ dependencies = [
 
 [[package]]
 name = "secrecy"
-version = "0.8.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "9bd1c54ea06cfd2f6b63219704de0b9b4f72dcc2b8fdef820be6cd799780e91e"
-dependencies = [
- "serde",
- "zeroize",
-]
-
-[[package]]
-name = "secrecy"
 version = "0.10.3"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "e891af845473308773346dc847b2c23ee78fe442e0472ac50e22a18a93d3ae5a"
@@ -3123,23 +3038,6 @@ dependencies = [
 
 [[package]]
 name = "tower"
-version = "0.4.13"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "b8fa9be0de6cf49e536ce1851f987bd21a43b771b09473c3549a6c853db37c1c"
-dependencies = [
- "futures-core",
- "futures-util",
- "pin-project",
- "pin-project-lite",
- "tokio",
- "tokio-util",
- "tower-layer",
- "tower-service",
- "tracing",
-]
-
-[[package]]
-name = "tower"
 version = "0.5.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "d039ad9159c98b70ecfd540b2573b97f7f52c3e8d9f8ad57a24b916a536975f9"
@@ -3157,25 +3055,6 @@ dependencies = [
 
 [[package]]
 name = "tower-http"
-version = "0.5.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "1e9cd434a998747dd2c4276bc96ee2e0c7a2eadf3cae88e52be55a05fa9053f5"
-dependencies = [
- "base64 0.21.7",
- "bitflags",
- "bytes",
- "http 1.3.1",
- "http-body 1.0.1",
- "http-body-util",
- "mime",
- "pin-project-lite",
- "tower-layer",
- "tower-service",
- "tracing",
-]
-
-[[package]]
-name = "tower-http"
 version = "0.6.6"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "adc82fd73de2a9722ac5da747f12383d2bfdb93591ee6c58486e0097890f05f2"
blob - 9264dbf83668cb2f085f885a3a07b54120134f78
blob + 453cf180b91dcb0e26ffac83c8c8f5709cbc3ac1
--- Cargo.toml
+++ Cargo.toml
@@ -47,7 +47,7 @@ daemonize = "=0.5.0"
 dialoguer = { version = "0.12.0", features = ["fuzzy-select"] }
 futures = "0.3.31"
 k8s-openapi = { version = "0.26.0", features = ["latest"] }
-kube = { version = "2.0.1", features = ["runtime", "config"] }
+kube = { version = "2.0.1", features = ["runtime", "config", "client","rustls-tls"] }
 kube-runtime = "2.0.1"
 serde = { version = "=1.0.228", features = ["derive"] }
 tokio = { version = "=1.48.0", features = ["full"] }
blob - f689bb30b908a39353fd2117bf2215072cc598bd
blob + ea3d2350e9f881fdba0e0553526f71a6cda4b273
--- crates/kops_aws_eks/Cargo.toml
+++ crates/kops_aws_eks/Cargo.toml
@@ -14,7 +14,7 @@ aws-smithy-runtime-api = "1.9.2"
 base64 = "0.22"
 http = "1"
 k8s-openapi = { version = "0.22", features = ["v1_30"] }
-kube = { version = "0.91", features = ["client", "rustls-tls"] }
+kube.workspace = true
 pem = "3.0.6"
 percent-encoding = "2.3"
 rustls = { version = "0.23", default-features = false, features = ["ring"] }
blob - b864dc2460daa34dbd91e17458347e79b1fc01cd
blob + 1c932f2e2a9201537b6ebd268bfdf28f3169afd8
--- crates/kops_aws_eks/src/lib.rs
+++ crates/kops_aws_eks/src/lib.rs
@@ -57,6 +57,8 @@ pub async fn create_kube_client(
         write_timeout: None,
         proxy_url: None,
         tls_server_name: None,
+        disable_compression: false,
+        headers: Vec::new(),
     };
 
     let client = kube::Client::try_from(kubeconfig)?;
blob - d4f8871404773124280108e5f09e2bc43cb1e1e2
blob + ac4dc0727bc6c39b8ec4446906c4b3195aec5f04
--- kopsd/Cargo.toml
+++ kopsd/Cargo.toml
@@ -13,6 +13,8 @@ rust-version.workspace = true
 description.workspace = true
 
 [dependencies]
+aws-credential-types.workspace = true
+aws-config.workspace = true
 anyhow.workspace = true
 chrono.workspace = true
 clap.workspace = true
@@ -22,6 +24,7 @@ futures.workspace = true
 k8s-openapi.workspace = true
 kops_log.workspace = true
 kops_protocol.workspace = true
+kops_aws_eks.workspace = true
 kube.workspace = true
 kube-runtime.workspace = true
 serde.workspace = true
blob - 642a800b6eaba03c8c9461394017e7a6e09938db
blob + d701aba2147c46cd2d85e29e4c6b187a720850ec
--- kopsd/src/handler.rs
+++ kopsd/src/handler.rs
@@ -15,6 +15,7 @@
 //
 
 use std::sync::Arc;
+use anyhow::Context;
 
 use chrono::{TimeZone, Utc};
 use k8s_openapi::api::core::v1::Pod;
@@ -95,18 +96,69 @@ impl Handler {
 
     async fn start_clusters_for_profile(
         &self,
-        _profile: &str,
+        profile: &str,
     ) -> anyhow::Result<()> {
+        let session = {
+            let map = self
+                .state
+                .aws_sessions
+                .lock()
+                .unwrap();
+                // .context("failed to lock aws_sessions map")?;
+
+            map.get(profile)
+                .cloned()
+                .context("no aws session stored for this profile")?
+        };
+
+        // for (name, cfg) in &self.state.clusters {
+            // if cfg.session_name != profile {
+            //     continue;
+            // }
+
+            // // Se cluster já está rodando, não faz nada
+            // if self.state.clusters.contains_key(name) {
+            //     continue;
+            // }
+
+        let name = String::from("eks-platform-dev");
+            tracing::info!(
+                "starting cluster worker for cluster '{}' (profile '{}')",
+                name,
+                profile
+            );
+
+            let sdk_config = sdk_config_from_session(&session).await?;
+
+            let client = kops_aws_eks::create_kube_client(&sdk_config, &name)
+                .await
+                .with_context(|| format!("failed to create kube client for cluster {}", name))?;
+
+            let cluster_state = crate::kube_worker::init_cluster_state(name.clone(), client)
+                .await
+                .with_context(|| format!("failed to start worker for cluster {}", name))?;
+
+            self.state
+    .clusters
+    .lock()
+    .unwrap()
+    .insert(name.clone(), cluster_state);
+
+        // }
+
         Ok(())
     }
 
+
+
     async fn handle_env(&self, req: EnvRequest) -> Response {
         let cluster = req
             .cluster
             .as_deref()
             .unwrap_or_else(|| self.state.default_cluster());
 
-        let Some(cs) = self.state.clusters.get(cluster) else {
+        let clusters = self.state.clusters.lock().unwrap();
+        let Some(cs) = clusters.get(cluster) else {
             return Response::Error {
                 message: format!("cluster not found: {cluster}"),
             };
@@ -228,7 +280,8 @@ impl Handler {
             .as_deref()
             .unwrap_or_else(|| self.state.default_cluster());
 
-        let Some(cluster_state) = self.state.clusters.get(cluster_name) else {
+        let clusters = self.state.clusters.lock().unwrap();
+        let Some(cluster_state) = clusters.get(cluster_name) else {
             return Response::Error {
                 message: format!("cluster not found: {cluster_name}"),
             };
@@ -291,3 +344,38 @@ impl Handler {
     //     // Response::ResetOk
     // }
 }
+
+use aws_config::{Region, SdkConfig};
+use aws_credential_types::{provider::SharedCredentialsProvider, Credentials};
+
+pub async fn sdk_config_from_session(
+    session: &AwsSession,
+) -> anyhow::Result<SdkConfig> {
+    // 1. Cria objeto Credentials a partir da sessão
+    let creds = Credentials::new(
+        session.access_key_id.clone(),
+        session.secret_access_key.clone(),
+        Some(session.session_token.clone()),
+        Some(session.expires_at.into()),
+        "kops-sso-session-dev",
+    );
+
+    let creds_provider = SharedCredentialsProvider::new(creds);
+
+    // 2. Resolve região
+    let region = session
+        .region
+        .clone()
+        .unwrap_or_else(|| "us-east-1".to_string());
+
+    let region = Region::new(region);
+
+    // 3. Monta o SdkConfig manualmente
+    let sdk_config = aws_config::from_env()
+        .region(region)
+        .credentials_provider(creds_provider)
+        .load()
+        .await;
+
+    Ok(sdk_config)
+}
blob - 182b3cbd5c4a80b87d3d291fceb4e7acb55f81e9
blob + 3a71fd16e37f4cd44855d4b4c0b18e7e5d78c7a3
--- kopsd/src/kube_worker.rs
+++ kopsd/src/kube_worker.rs
@@ -38,7 +38,7 @@ use crate::state::{ClusterName, ClusterState};
 /// a background reflector task to keep the Store<Pod> up-to-date.
 pub async fn init_cluster_state(
     cluster_name: ClusterName,
-    client: Client,
+    client: kube::Client,
 ) -> Result<Arc<ClusterState>> {
     // let cluster_name: ClusterName = cfg.name.clone();
 
blob - 9ca43d9b329c06d88504c32f44d8ceab81065458
blob + 67308c0bc156afb2e40dd1113aec4ddb2cd4f831
--- kopsd/src/server.rs
+++ kopsd/src/server.rs
@@ -81,7 +81,7 @@ fn run_fg(config: &KopsdConfig) -> Result<()> {
         // let state =
         //     Arc::new(DaemonState { clusters: clusters_map, default_cluster });
         let state = Arc::new(DaemonState {
-            clusters: HashMap::new(),
+            clusters: Mutex::new(HashMap::new()),
             default_cluster,
             aws_sessions: Mutex::new(HashMap::new()),
         });
blob - dce48c20e482bdd63cfd465aef92e72ca3a29b58
blob + 07567c7ddf9209dc6f4e5f2472d185312b9c0263
--- kopsd/src/state.rs
+++ kopsd/src/state.rs
@@ -39,7 +39,7 @@ pub type ProfileName = String;
 
 /// Global daemon state shared by handlers.
 pub struct DaemonState {
-    pub clusters: HashMap<ClusterName, Arc<ClusterState>>,
+    pub clusters: Mutex<HashMap<ClusterName, Arc<ClusterState>>>,
     pub default_cluster: ClusterName,
 
     /// AWS sessions keyed by logical profile name ("dev", "prod", ...).