Skip to content

Commit

Permalink
fix: tests and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
veeso committed Sep 29, 2024
1 parent 6cf4b78 commit 689c6f0
Show file tree
Hide file tree
Showing 5 changed files with 1,092 additions and 83 deletions.
67 changes: 60 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<p align="center">~ Remotefs kube client ~</p>

<p align="center">Developed by <a href="https://veeso.github.io/" target="_blank">@veeso</a></p>
<p align="center">Current version: 0.2.0 (17/07/2024)</p>
<p align="center">Current version: 0.3.0 (29/09/2024)</p>

<p align="center">
<a href="https://opensource.org/licenses/MIT"
Expand Down Expand Up @@ -49,23 +49,76 @@ First of all you need to add **remotefs** and the client to your project depende

```toml
remotefs = "^0.2"
remotefs-kube = "^0.2"
remotefs-kube = "^0.3"
```

these features are supported:

- `find`: enable `find()` method for RemoteFs. (*enabled by default*)
- `no-log`: disable logging. By default, this library will log via the `log` crate.

### Kube client
The library provides two different clients:

Here is a basic usage example, with the `Kube` client, which is very similiar to the `Scp` client.
- **KubeMultiPodFs** client
- **KubeContainerFs** client

```rust,ignore
### Kube multi pod client

The MultiPod client gives access to all the pods with their own containers in a namespace.

This client creates an abstract file system with the following structure

- / (root)
- pod-a
- container-a
- / (container-a root)
- /bin
- /home
- ...
- container-b
- / (container-b root)
- ...
- pod-b
- container-c
- / (container-c root)
- ...

So paths have the following structure: `/pod-name/container-name/path/to/file`.

```rust

// import remotefs trait and client
use remotefs::RemoteFs;
use remotefs_kube::KubeMultiPodFs;
use std::path::Path;

let rt = Arc::new(
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap(),
);
let mut client: KubeMultiPodFs = KubeMultiPodFs::new(&rt);

// connect
assert!(client.connect().is_ok());
// get working directory
println!("Wrkdir: {}", client.pwd().ok().unwrap().display());
// change working directory
assert!(client.change_dir(Path::new("/my-pod/alpine/tmp")).is_ok());
// disconnect
assert!(client.disconnect().is_ok());
```

### Kube container client

Here is a basic usage example, with the `KubeContainerFs` client, which is used to connect and interact with a single container on a certain pod. This client gives the entire access to the container file system.

```rust

// import remotefs trait and client
use remotefs::RemoteFs;
use remotefs_ssh::{SshConfigParseRule, SftpFs, SshOpts};
use remotefs_kube::KubeContainerFs;
use std::path::Path;

let rt = Arc::new(
Expand All @@ -74,7 +127,7 @@ let rt = Arc::new(
.build()
.unwrap(),
);
let mut client: KubeFs = KubeFs::new("my-pod", &rt);
let mut client: KubeContainerFs = KubeContainerFs::new("my-pod", "container-name", &rt);

// connect
assert!(client.connect().is_ok());
Expand Down
33 changes: 9 additions & 24 deletions src/kube_container_fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1799,37 +1799,22 @@ mod test {
}

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

use kube::api::DeleteParams;
use kube::ResourceExt as _;
let wrkdir = client.pwd().ok().unwrap();
// Remove directory
assert!(client.remove_dir_all(wrkdir.as_path()).is_ok());
fn finalize_client(_pods: Api<Pod>, mut client: KubeContainerFs) {
assert!(client.disconnect().is_ok());

// cleanup pods
let pod_name = client.pod_name;
client.runtime.block_on(async {
let dp = DeleteParams::default();
pods.delete(&pod_name, &dp).await.unwrap().map_left(|pdel| {
info!("Deleting {pod_name} pod started: {:?}", pdel);
assert_eq!(pdel.name_any(), pod_name);
});
})
}

#[cfg(feature = "integration-tests")]
fn generate_pod_name() -> String {
use rand::distributions::{Alphanumeric, DistString};
use rand::thread_rng;
let random_string: String = Alphanumeric
.sample_string(&mut thread_rng(), 8)
.chars()
use rand::distributions::Alphanumeric;
use rand::{thread_rng, Rng as _};

let mut rng = thread_rng();
let random_string: String = std::iter::repeat(())
.map(|()| rng.sample(Alphanumeric))
.map(char::from)
.filter(|c| c.is_alphabetic())
.map(|c| c.to_ascii_lowercase())
.take(8)
.take(12)
.collect();

format!("test-{}", random_string)
Expand Down
Loading

0 comments on commit 689c6f0

Please sign in to comment.