runtime-rs/kata-ctl: Enhancement of DirectVolumeMount.

Move the get_volume_mount_info to kata-types/src/mount.rs.
If so, it becomes a common method of DirectVolumeMountInfo
and reduces duplicated code.

Fixes: #6701

Signed-off-by: alex.lyn <alex.lyn@antgroup.com>
This commit is contained in:
alex.lyn 2023-05-26 11:18:29 +08:00
parent 62b2838962
commit 5ddc4f94c5
7 changed files with 57 additions and 25 deletions

8
src/agent/Cargo.lock generated
View File

@ -882,6 +882,7 @@ dependencies = [
"num_cpus", "num_cpus",
"oci", "oci",
"regex", "regex",
"safe-path",
"serde", "serde",
"serde_json", "serde_json",
"slog", "slog",
@ -1767,6 +1768,13 @@ version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f3f6f92acf49d1b98f7a81226834412ada05458b7364277387724a237f062695" checksum = "f3f6f92acf49d1b98f7a81226834412ada05458b7364277387724a237f062695"
[[package]]
name = "safe-path"
version = "0.1.0"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "scan_fmt" name = "scan_fmt"
version = "0.2.6" version = "0.2.6"

1
src/libs/Cargo.lock generated
View File

@ -519,6 +519,7 @@ dependencies = [
"num_cpus", "num_cpus",
"oci", "oci",
"regex", "regex",
"safe-path",
"serde", "serde",
"serde_json", "serde_json",
"slog", "slog",

View File

@ -27,6 +27,7 @@ thiserror = "1.0"
toml = "0.5.8" toml = "0.5.8"
oci = { path = "../oci" } oci = { path = "../oci" }
safe-path = { path = "../safe-path" }
[dev-dependencies] [dev-dependencies]
tempfile = "3" tempfile = "3"

View File

@ -5,7 +5,7 @@
// //
use anyhow::{anyhow, Context, Result}; use anyhow::{anyhow, Context, Result};
use std::{collections::HashMap, path::PathBuf}; use std::{collections::HashMap, fs, path::PathBuf};
/// Prefix to mark a volume as Kata special. /// Prefix to mark a volume as Kata special.
pub const KATA_VOLUME_TYPE_PREFIX: &str = "kata:"; pub const KATA_VOLUME_TYPE_PREFIX: &str = "kata:";
@ -71,6 +71,27 @@ pub struct DirectVolumeMountInfo {
pub options: Vec<String>, pub options: Vec<String>,
} }
/// join_path joins user provided volumepath with kata direct-volume root path
/// the volume_path is base64-encoded and then safely joined to the end of path prefix
pub fn join_path(prefix: &str, volume_path: &str) -> Result<PathBuf> {
if volume_path.is_empty() {
return Err(anyhow!("volume path must not be empty"));
}
let b64_encoded_path = base64::encode(volume_path.as_bytes());
Ok(safe_path::scoped_join(prefix, b64_encoded_path)?)
}
/// get DirectVolume mountInfo from mountinfo.json.
pub fn get_volume_mount_info(volume_path: &str) -> Result<DirectVolumeMountInfo> {
let mount_info_file_path =
join_path(KATA_DIRECT_VOLUME_ROOT_PATH, volume_path)?.join(KATA_MOUNT_INFO_FILE_NAME);
let mount_info_file = fs::read_to_string(mount_info_file_path)?;
let mount_info: DirectVolumeMountInfo = serde_json::from_str(&mount_info_file)?;
Ok(mount_info)
}
/// Check whether a mount type is a marker for Kata specific volume. /// Check whether a mount type is a marker for Kata specific volume.
pub fn is_kata_special_volume(ty: &str) -> bool { pub fn is_kata_special_volume(ty: &str) -> bool {
ty.len() > KATA_VOLUME_TYPE_PREFIX.len() && ty.starts_with(KATA_VOLUME_TYPE_PREFIX) ty.len() > KATA_VOLUME_TYPE_PREFIX.len() && ty.starts_with(KATA_VOLUME_TYPE_PREFIX)

View File

@ -1481,6 +1481,7 @@ dependencies = [
"num_cpus", "num_cpus",
"oci", "oci",
"regex", "regex",
"safe-path 0.1.0",
"serde", "serde",
"serde_json", "serde_json",
"slog", "slog",

View File

@ -764,7 +764,9 @@ dependencies = [
"lazy_static", "lazy_static",
"num_cpus", "num_cpus",
"oci", "oci",
"proc-mounts",
"regex", "regex",
"safe-path",
"serde", "serde",
"serde_json", "serde_json",
"slog", "slog",
@ -1045,6 +1047,15 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "partition-identity"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9fa925f9becb532d758b0014b472c576869910929cf4c3f8054b386f19ab9e21"
dependencies = [
"thiserror",
]
[[package]] [[package]]
name = "percent-encoding" name = "percent-encoding"
version = "2.2.0" version = "2.2.0"
@ -1118,6 +1129,15 @@ dependencies = [
"unicode-ident", "unicode-ident",
] ]
[[package]]
name = "proc-mounts"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0d652f8435d0ab70bf4f3590a6a851d59604831a458086541b95238cc51ffcf2"
dependencies = [
"partition-identity",
]
[[package]] [[package]]
name = "prost" name = "prost"
version = "0.8.0" version = "0.8.0"

View File

@ -8,12 +8,12 @@ use crate::args::{DirectVolSubcommand, DirectVolumeCommand};
use anyhow::{anyhow, Ok, Result}; use anyhow::{anyhow, Ok, Result};
use futures::executor; use futures::executor;
use kata_types::mount::{ use kata_types::mount::{
DirectVolumeMountInfo, KATA_DIRECT_VOLUME_ROOT_PATH, KATA_MOUNT_INFO_FILE_NAME, get_volume_mount_info, join_path, DirectVolumeMountInfo, KATA_DIRECT_VOLUME_ROOT_PATH,
KATA_MOUNT_INFO_FILE_NAME,
}; };
use nix; use nix;
use reqwest::StatusCode; use reqwest::StatusCode;
use safe_path; use std::{fs, time::Duration};
use std::{fs, path::PathBuf, time::Duration};
use url; use url;
use agent::ResizeVolumeRequest; use agent::ResizeVolumeRequest;
@ -90,17 +90,6 @@ async fn stats(volume_path: &str) -> Result<Option<String>> {
Ok(Some(body)) Ok(Some(body))
} }
// join_path joins user provided volumepath with kata direct-volume root path
// the volume_path is base64-encoded and then safely joined to the end of path prefix
fn join_path(prefix: &str, volume_path: &str) -> Result<PathBuf> {
if volume_path.is_empty() {
return Err(anyhow!("volume path must not be empty"));
}
let b64_encoded_path = base64::encode(volume_path.as_bytes());
Ok(safe_path::scoped_join(prefix, b64_encoded_path)?)
}
// add writes the mount info (json string) of a direct volume into a filesystem path known to Kata Containers. // add writes the mount info (json string) of a direct volume into a filesystem path known to Kata Containers.
pub fn add(volume_path: &str, mount_info: &str) -> Result<Option<String>> { pub fn add(volume_path: &str, mount_info: &str) -> Result<Option<String>> {
let mount_info_dir_path = join_path(KATA_DIRECT_VOLUME_ROOT_PATH, volume_path)?; let mount_info_dir_path = join_path(KATA_DIRECT_VOLUME_ROOT_PATH, volume_path)?;
@ -129,15 +118,6 @@ pub fn remove(volume_path: &str) -> Result<Option<String>> {
Ok(None) Ok(None)
} }
pub fn get_volume_mount_info(volume_path: &str) -> Result<DirectVolumeMountInfo> {
let mount_info_file_path =
join_path(KATA_DIRECT_VOLUME_ROOT_PATH, volume_path)?.join(KATA_MOUNT_INFO_FILE_NAME);
let mount_info_file = fs::read_to_string(mount_info_file_path)?;
let mount_info: DirectVolumeMountInfo = serde_json::from_str(&mount_info_file)?;
Ok(mount_info)
}
// get_sandbox_id_for_volume finds the id of the first sandbox found in the dir. // get_sandbox_id_for_volume finds the id of the first sandbox found in the dir.
// We expect a direct-assigned volume is associated with only a sandbox at a time. // We expect a direct-assigned volume is associated with only a sandbox at a time.
pub fn get_sandbox_id_for_volume(volume_path: &str) -> Result<String> { pub fn get_sandbox_id_for_volume(volume_path: &str) -> Result<String> {
@ -170,7 +150,7 @@ mod tests {
use super::*; use super::*;
use kata_types::mount::DirectVolumeMountInfo; use kata_types::mount::DirectVolumeMountInfo;
use serial_test::serial; use serial_test::serial;
use std::{collections::HashMap, fs}; use std::{collections::HashMap, fs, path::PathBuf};
use tempfile::tempdir; use tempfile::tempdir;
use test_utils::skip_if_not_root; use test_utils::skip_if_not_root;