mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-08-08 03:24:15 +00:00
kata-types: Replace StorageHandlerManager with type alias
Removed the `StorageHandlerManager` struct and its associated implementations and introduced a type alias `StorageHandlerManager` for `HandlerManager` to simplify the code. The new type alias maintains the same functionality while reducing redundancy. Signed-off-by: ChengyuZhu6 <chengyu.zhu@intel.com>
This commit is contained in:
parent
281f0d7f29
commit
0b3ad2f830
@ -12,13 +12,19 @@ pub struct HandlerManager<H> {
|
|||||||
handlers: HashMap<String, H>,
|
handlers: HashMap<String, H>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<H> Default for HandlerManager<H> {
|
impl<H> Default for HandlerManager<H>
|
||||||
|
where
|
||||||
|
H: Clone,
|
||||||
|
{
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self::new()
|
Self::new()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<H> HandlerManager<H> {
|
impl<H> HandlerManager<H>
|
||||||
|
where
|
||||||
|
H: Clone,
|
||||||
|
{
|
||||||
/// Create a new instance of `HandlerManager`.
|
/// Create a new instance of `HandlerManager`.
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
@ -27,15 +33,19 @@ impl<H> HandlerManager<H> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Register a handler.
|
/// Register a handler.
|
||||||
pub fn add_handler(&mut self, id: &str, handler: H) -> Result<()> {
|
pub fn add_handler(&mut self, ids: &[&str], handler: H) -> Result<()> {
|
||||||
|
for &id in ids {
|
||||||
match self.handlers.entry(id.to_string()) {
|
match self.handlers.entry(id.to_string()) {
|
||||||
Entry::Occupied(_) => Err(anyhow!("handler for {} already exists", id)),
|
Entry::Occupied(_) => {
|
||||||
|
return Err(anyhow!("handler for {} already exists", id));
|
||||||
|
}
|
||||||
Entry::Vacant(entry) => {
|
Entry::Vacant(entry) => {
|
||||||
entry.insert(handler);
|
entry.insert(handler.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Get handler with specified `id`.
|
/// Get handler with specified `id`.
|
||||||
pub fn handler(&self, id: &str) -> Option<&H> {
|
pub fn handler(&self, id: &str) -> Option<&H> {
|
||||||
|
@ -5,10 +5,11 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
use anyhow::{anyhow, Context, Error, Result};
|
use anyhow::{anyhow, Context, Error, Result};
|
||||||
use std::collections::hash_map::Entry;
|
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
use std::{collections::HashMap, fs, path::PathBuf};
|
use std::{collections::HashMap, fs, path::PathBuf};
|
||||||
|
|
||||||
|
use crate::handler::HandlerManager;
|
||||||
|
|
||||||
/// 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:";
|
||||||
|
|
||||||
@ -59,6 +60,9 @@ pub const KATA_VIRTUAL_VOLUME_LAYER_NYDUS_FS: &str = "layer_nydus_fs";
|
|||||||
/// Download and extra container image inside guest vm.
|
/// Download and extra container image inside guest vm.
|
||||||
pub const KATA_VIRTUAL_VOLUME_IMAGE_GUEST_PULL: &str = "image_guest_pull";
|
pub const KATA_VIRTUAL_VOLUME_IMAGE_GUEST_PULL: &str = "image_guest_pull";
|
||||||
|
|
||||||
|
/// Manager to manage registered storage device handlers.
|
||||||
|
pub type StorageHandlerManager<H> = HandlerManager<H>;
|
||||||
|
|
||||||
/// Information about a mount.
|
/// Information about a mount.
|
||||||
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
|
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
|
||||||
pub struct Mount {
|
pub struct Mount {
|
||||||
@ -441,57 +445,6 @@ pub trait StorageDevice: Send + Sync {
|
|||||||
fn cleanup(&self) -> Result<()>;
|
fn cleanup(&self) -> Result<()>;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Manager to manage registered storage device handlers.
|
|
||||||
pub struct StorageHandlerManager<H> {
|
|
||||||
handlers: HashMap<String, H>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<H> Default for StorageHandlerManager<H>
|
|
||||||
where
|
|
||||||
H: Clone,
|
|
||||||
{
|
|
||||||
fn default() -> Self {
|
|
||||||
Self::new()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<H> StorageHandlerManager<H>
|
|
||||||
where
|
|
||||||
H: Clone,
|
|
||||||
{
|
|
||||||
/// Create a new instance of `StorageHandlerManager`.
|
|
||||||
pub fn new() -> Self {
|
|
||||||
Self {
|
|
||||||
handlers: HashMap::new(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Register a storage device handler.
|
|
||||||
pub fn add_handler(&mut self, ids: &[&str], handler: H) -> Result<()> {
|
|
||||||
for &id in ids {
|
|
||||||
match self.handlers.entry(id.to_string()) {
|
|
||||||
Entry::Occupied(_) => {
|
|
||||||
return Err(anyhow!("storage handler for {} already exists", id));
|
|
||||||
}
|
|
||||||
Entry::Vacant(entry) => {
|
|
||||||
entry.insert(handler.clone());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Get storage handler with specified `id`.
|
|
||||||
pub fn handler(&self, id: &str) -> Option<&H> {
|
|
||||||
self.handlers.get(id)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Get names of registered handlers.
|
|
||||||
pub fn get_handlers(&self) -> Vec<String> {
|
|
||||||
self.handlers.keys().map(|v| v.to_string()).collect()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Join user provided volume path with kata direct-volume root path.
|
/// Join user provided volume path with kata direct-volume root path.
|
||||||
///
|
///
|
||||||
/// The `volume_path` is base64-url-encoded and then safely joined to the `prefix`
|
/// The `volume_path` is base64-url-encoded and then safely joined to the `prefix`
|
||||||
|
Loading…
Reference in New Issue
Block a user