Skip to content

Commit

Permalink
feat: multi pod client
Browse files Browse the repository at this point in the history
  • Loading branch information
veeso committed Sep 29, 2024
1 parent a843013 commit 6cf4b78
Show file tree
Hide file tree
Showing 5 changed files with 793 additions and 26 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ tokio-util = "0.7"

[dev-dependencies]
env_logger = "^0.11"
k8s-openapi = { version = "0.22", features = ["v1_30"] }
kube = { version = "0.92", features = ["client", "config", "runtime", "ws"] }
pretty_assertions = "1"
rand = "^0.8.4"
Expand Down
44 changes: 22 additions & 22 deletions src/client.rs → src/kube_container_fs.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! ## SCP
//! ## Kube Container FS
//!
//! Scp remote fs implementation
//! The `KubeContainerFs` client is a client that allows you to interact with a container in a pod.
use std::ops::Range;
use std::path::{Path, PathBuf};
Expand All @@ -27,17 +27,17 @@ static LS_RE: Lazy<Regex> = lazy_regex!(
r#"^([\-ld])([\-rwxsStT]{9})\s+(\d+)\s+(.+)\s+(.+)\s+(\d+)\s+(\w{3}\s+\d{1,2}\s+(?:\d{1,2}:\d{1,2}|\d{4}))\s+(.+)$"#
);

/// Kube "filesystem" client
pub struct KubeFs {
config: Option<Config>,
container: String,
pod_name: String,
pods: Option<Api<Pod>>,
/// Kube "filesystem" client to interact with a container in a pod
pub struct KubeContainerFs {
pub(crate) config: Option<Config>,
pub(crate) container: String,
pub(crate) pod_name: String,
pub(crate) pods: Option<Api<Pod>>,
runtime: Arc<Runtime>,
wrkdir: PathBuf,
pub(crate) wrkdir: PathBuf,
}

impl KubeFs {
impl KubeContainerFs {
/// Creates a new `KubeFs`
///
/// If `config()` is not called then, it will try to use the configuration from the default kubeconfig file
Expand Down Expand Up @@ -321,7 +321,7 @@ impl KubeFs {
}
}

impl RemoteFs for KubeFs {
impl RemoteFs for KubeContainerFs {
fn connect(&mut self) -> RemoteResult<Welcome> {
debug!("Initializing Kube connection...");
let api = self.runtime.block_on(async {
Expand Down Expand Up @@ -863,7 +863,7 @@ mod test {
.build()
.unwrap(),
);
let mut client = KubeFs::new("test", "test", &rt);
let mut client = KubeContainerFs::new("test", "test", &rt);
assert!(client.config.is_none());
assert_eq!(client.is_connected(), false);
}
Expand All @@ -876,7 +876,7 @@ mod test {
.build()
.unwrap(),
);
let mut client = KubeFs::new("aaaaaa", "test", &rt);
let mut client = KubeContainerFs::new("aaaaaa", "test", &rt);
assert!(client.connect().is_err());
}

Expand Down Expand Up @@ -1490,7 +1490,7 @@ mod test {
.build()
.unwrap(),
);
let client = KubeFs::new("test", "test", &rt);
let client = KubeContainerFs::new("test", "test", &rt);
assert_eq!(
client.get_name_and_link("Cargo.toml"),
(String::from("Cargo.toml"), None)
Expand All @@ -1509,7 +1509,7 @@ mod test {
.build()
.unwrap(),
);
let client = KubeFs::new("test", "test", &rt);
let client = KubeContainerFs::new("test", "test", &rt);
// File
let entry = client
.parse_ls_output(
Expand Down Expand Up @@ -1550,7 +1550,7 @@ mod test {
.build()
.unwrap(),
);
let client = KubeFs::new("test", "test", &rt);
let client = KubeContainerFs::new("test", "test", &rt);
// Directory
let entry = client
.parse_ls_output(
Expand Down Expand Up @@ -1595,7 +1595,7 @@ mod test {
.build()
.unwrap(),
);
let client = KubeFs::new("test", "test", &rt);
let client = KubeContainerFs::new("test", "test", &rt);
// File
let entry = client
.parse_ls_output(
Expand Down Expand Up @@ -1623,7 +1623,7 @@ mod test {
.build()
.unwrap(),
);
let client = KubeFs::new("test", "test", &rt);
let client = KubeContainerFs::new("test", "test", &rt);
assert!(client
.parse_ls_output(
Path::new("/tmp"),
Expand Down Expand Up @@ -1660,7 +1660,7 @@ mod test {
.build()
.unwrap(),
);
let mut client = KubeFs::new("test", "test", &rt);
let mut client = KubeContainerFs::new("test", "test", &rt);
assert!(client.change_dir(Path::new("/tmp")).is_err());
assert!(client
.copy(Path::new("/nowhere"), PathBuf::from("/culonia").as_path())
Expand Down Expand Up @@ -1693,7 +1693,7 @@ mod test {
// -- test utils

#[cfg(feature = "integration-tests")]
fn setup_client() -> (Api<Pod>, KubeFs) {
fn setup_client() -> (Api<Pod>, KubeContainerFs) {
// setup pod with random name

use kube::api::PostParams;
Expand Down Expand Up @@ -1784,7 +1784,7 @@ mod test {
pods
});

let mut client = KubeFs::new(&pod_name, "alpine", &runtime).config(config.clone());
let mut client = KubeContainerFs::new(&pod_name, "alpine", &runtime).config(config.clone());
client.connect().expect("connection failed");
// Create wrkdir
let tempdir = PathBuf::from(generate_tempdir());
Expand All @@ -1799,7 +1799,7 @@ mod test {
}

#[cfg(feature = "integration-tests")]
fn finalize_client(pods: Api<Pod>, mut client: KubeFs) {
fn finalize_client(pods: Api<Pod>, mut client: KubeContainerFs) {
// Get working directory

use kube::api::DeleteParams;
Expand Down
Loading

0 comments on commit 6cf4b78

Please sign in to comment.