Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions crates/faas-containerd/src/impls/cni/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,30 @@ use gateway::types::function::Query;

#[derive(Debug, Clone, Hash, Eq, PartialEq)]
pub struct Endpoint {
pub service: String,
pub function_name: String,
pub namespace: String,
}

impl Endpoint {
pub fn new(service: &str, namespace: &str) -> Self {
pub fn new(function_name: &str, namespace: &str) -> Self {
Self {
service: service.to_string(),
function_name: function_name.to_string(),
namespace: namespace.to_string(),
}
}
}

/// format `<namespace>-<service>` as netns name, also the identifier of each function
/// format `<namespace>-<function_name>` as netns name, also the identifier of each function
impl std::fmt::Display for Endpoint {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}-{}", self.namespace, self.service)
write!(f, "{}-{}", self.namespace, self.function_name)
}
}

impl From<Query> for Endpoint {
fn from(query: Query) -> Self {
Self {
service: query.service,
function_name: query.function_name,
namespace: query
.namespace
.unwrap_or(consts::DEFAULT_FUNCTION_NAMESPACE.to_string()),
Expand Down
8 changes: 4 additions & 4 deletions crates/faas-containerd/src/impls/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl ContainerdService {
metadata: &ContainerStaticMetadata,
) -> Result<Container, ContainerError> {
let container = Container {
id: metadata.endpoint.service.clone(),
id: metadata.endpoint.function_name.clone(),
image: metadata.image.clone(),
runtime: Some(Runtime {
name: "io.containerd.runc.v2".to_string(),
Expand All @@ -35,7 +35,7 @@ impl ContainerdService {
ContainerError::Internal
})?),
snapshotter: crate::consts::DEFAULT_SNAPSHOTTER.to_string(),
snapshot_key: metadata.endpoint.service.clone(),
snapshot_key: metadata.endpoint.function_name.clone(),
..Default::default()
};

Expand All @@ -58,7 +58,7 @@ impl ContainerdService {
/// 删除容器
pub async fn delete_container(&self, endpoint: &Endpoint) -> Result<(), ContainerError> {
let Endpoint {
service: cid,
function_name: cid,
namespace: ns,
} = endpoint;
let mut cc = self.client.containers();
Expand All @@ -79,7 +79,7 @@ impl ContainerdService {
let mut cc = self.client.containers();

let request = GetContainerRequest {
id: endpoint.service.clone(),
id: endpoint.function_name.clone(),
};

let resp = cc
Expand Down
2 changes: 1 addition & 1 deletion crates/faas-containerd/src/impls/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl From<function::Deployment> for ContainerStaticMetadata {
ContainerStaticMetadata {
image: info.image,
endpoint: Endpoint::new(
&info.service,
&info.function_name,
&info
.namespace
.unwrap_or(consts::DEFAULT_FUNCTION_NAMESPACE.to_string()),
Expand Down
4 changes: 2 additions & 2 deletions crates/faas-containerd/src/impls/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl ContainerdService {
.get_parent_snapshot(&container.image, &container.endpoint.namespace)
.await?;
self.do_prepare_snapshot(
&container.endpoint.service,
&container.endpoint.function_name,
&container.endpoint.namespace,
parent_snapshot,
)
Expand Down Expand Up @@ -117,7 +117,7 @@ impl ContainerdService {
let mut sc = self.client.snapshots();
let req = RemoveSnapshotRequest {
snapshotter: crate::consts::DEFAULT_SNAPSHOTTER.to_string(),
key: endpoint.service.clone(),
key: endpoint.function_name.clone(),
};
sc.remove(with_namespace!(req, endpoint.namespace))
.await
Expand Down
2 changes: 1 addition & 1 deletion crates/faas-containerd/src/impls/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ impl ContainerdService {

let spec = generate_default_unix_spec(
&metadata.endpoint.namespace,
&metadata.endpoint.service,
&metadata.endpoint.function_name,
&rt_conf,
)?;
let spec_json = serde_json::to_string(&spec).map_err(|e| {
Expand Down
6 changes: 3 additions & 3 deletions crates/faas-containerd/src/impls/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl ContainerdService {
/// 创建并启动任务
pub async fn new_task(&self, mounts: Vec<Mount>, endpoint: &Endpoint) -> Result<(), TaskError> {
let Endpoint {
service: cid,
function_name: cid,
namespace: ns,
} = endpoint;
// let mounts = self.get_mounts(cid, ns).await?;
Expand Down Expand Up @@ -103,7 +103,7 @@ impl ContainerdService {

pub async fn get_task(&self, endpoint: &Endpoint) -> Result<Process, TaskError> {
let Endpoint {
service: cid,
function_name: cid,
namespace: ns,
} = endpoint;
let mut tc = self.client.tasks();
Expand Down Expand Up @@ -179,7 +179,7 @@ impl ContainerdService {
/// 杀死并删除任务
pub async fn kill_task_with_timeout(&self, endpoint: &Endpoint) -> Result<(), TaskError> {
let Endpoint {
service: cid,
function_name: cid,
namespace: ns,
} = endpoint;
let kill_timeout = Duration::from_secs(5);
Expand Down
4 changes: 2 additions & 2 deletions crates/faas-containerd/src/provider/function/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl ContainerdProvider {
let mut statuses: Vec<Status> = Vec::new();
for container in containers {
let endpoint = Endpoint {
service: container.id.clone(),
function_name: container.id.clone(),
namespace: namespace.clone(),
};
let created_at = container.created_at.unwrap().to_string();
Expand All @@ -43,7 +43,7 @@ impl ContainerdProvider {

// 大部分字段并未实现,使用None填充
let status = Status {
name: endpoint.service,
function_name: endpoint.function_name,
namespace: Some(endpoint.namespace),
image: container.image,
env_process: None,
Expand Down
2 changes: 1 addition & 1 deletion crates/faas-containerd/src/provider/function/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl ContainerdProvider {

// 大部分字段并未实现,使用None填充
let status = Status {
name: container.id,
function_name: container.id,
namespace: Some(endpoint.namespace),
image: container.image,
env_process: None,
Expand Down
2 changes: 1 addition & 1 deletion crates/faas-containerd/src/provider/function/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::provider::ContainerdProvider;
impl ContainerdProvider {
pub(crate) async fn _update(&self, param: Deployment) -> Result<(), UpdateError> {
let function = Query {
service: param.service.clone(),
function_name: param.function_name.clone(),
namespace: param.namespace.clone(),
};
self._delete(function).await.map_err(|e| {
Expand Down
2 changes: 1 addition & 1 deletion crates/gateway/src/bootstrap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ mod tests {
let meta = ProxyQuery::from_str(&any).unwrap();
HttpResponse::Ok().body(format!(
"{}|{}|{}",
meta.query.service,
meta.query.function_name,
meta.query.namespace.unwrap_or_default(),
meta.path
))
Expand Down
18 changes: 9 additions & 9 deletions crates/gateway/src/handlers/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,35 @@ pub async fn deploy<P: Provider>(
provider: web::Data<P>,
info: web::Json<Deployment>,
) -> Result<HttpResponse, DeployError> {
let service = info.0.service.clone();
let function_name = info.0.function_name.clone();
(*provider).deploy(info.0).await.map(|()| {
HttpResponse::Accepted().body(format!("function {} was created successfully", service))
HttpResponse::Accepted().body(format!("function {} was created successfully", function_name))
})
}

pub async fn update<P: Provider>(
provider: web::Data<P>,
info: web::Json<Deployment>,
) -> Result<HttpResponse, UpdateError> {
let service = info.0.service.clone();
let function_name = info.0.function_name.clone();
(*provider).update(info.0).await.map(|()| {
HttpResponse::Accepted().body(format!("function {} was updated successfully", service))
HttpResponse::Accepted().body(format!("function {} was updated successfully", function_name))
})
}

pub async fn delete<P: Provider>(
provider: web::Data<P>,
info: web::Json<Delete>,
) -> Result<HttpResponse, DeleteError> {
let service = info.0.function_name.clone();
let function_name = info.0.function_name.clone();
let query = Query {
service: service.clone(),
function_name: function_name.clone(),
namespace: Some(info.0.namespace),
};
(*provider)
.delete(query)
.await
.map(|()| HttpResponse::Ok().body(format!("function {} was deleted successfully", service)))
.map(|()| HttpResponse::Ok().body(format!("function {} was deleted successfully", function_name)))
}

#[derive(Debug, Deserialize)]
Expand All @@ -65,11 +65,11 @@ pub struct StatusParam {

pub async fn status<P: Provider>(
provider: web::Data<P>,
name: web::Path<String>,
function_name: web::Path<String>,
info: web::Query<StatusParam>,
) -> Result<HttpResponse, ResolveError> {
let query = Query {
service: name.into_inner(),
function_name: function_name.into_inner(),
namespace: info.namespace.clone(),
};
let status = (*provider).status(query).await?;
Expand Down
4 changes: 2 additions & 2 deletions crates/gateway/src/handlers/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ impl FromStr for ProxyQuery {
} else {
(path, "".to_owned())
};
let (service, namespace) = identifier
let (function_name, namespace) = identifier
.rsplit_once('.')
.map(|(s, n)| (s.to_string(), Some(n.to_string())))
.unwrap_or((identifier.to_string(), None));
Ok(ProxyQuery {
query: Query { service, namespace },
query: Query { function_name, namespace },
path: rest_path,
})
}
Expand Down
10 changes: 5 additions & 5 deletions crates/gateway/src/types/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use serde::{Deserialize, Serialize};
#[serde(rename_all = "camelCase")]
pub struct Deployment {
/// Service is the name of the function deployment
pub service: String,
pub function_name: String,

/// Image is a fully-qualified container image
pub image: String,
Expand Down Expand Up @@ -73,7 +73,7 @@ pub struct Usage {
#[serde(rename_all = "camelCase")]
pub struct Status {
/// The name of the function
pub name: String,
pub function_name: String,

/// The fully qualified docker image name of the function
pub image: String,
Expand Down Expand Up @@ -128,7 +128,7 @@ pub struct Status {
#[derive(Eq, Hash, PartialEq, Clone, Debug)]
pub struct Query {
/// Name of deployed function
pub service: String,
pub function_name: String,

/// Namespace of deployed function
pub namespace: Option<String>,
Expand All @@ -141,12 +141,12 @@ impl FromStr for Query {
fn from_str(function_name: &str) -> Result<Self, Self::Err> {
Ok(if let Some(index) = function_name.rfind('.') {
Self {
service: function_name[..index].to_string(),
function_name: function_name[..index].to_string(),
namespace: Some(function_name[index + 1..].to_string()),
}
} else {
Self {
service: function_name.to_string(),
function_name: function_name.to_string(),
namespace: Some("default".to_string()),
}
})
Expand Down
Loading
Loading