Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

return grpc error with correct format #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
152 changes: 135 additions & 17 deletions crates/sandbox/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ macro_rules! ignore_not_found {
Ok(x) => Ok(x),
Err(e) => match e {
crate::error::Error::NotFound(_) => Ok(Default::default()),
_ => Err(e),
_ => Err(tonic::Status::new(tonic::Code::Internal, format!("{}", e))),
},
}
}};
Expand Down Expand Up @@ -53,9 +53,22 @@ where
return Err(tonic::Status::invalid_argument("sandbox id is empty"));
}
let base_dir = format!("{}/{}", self.dir, sandbox_data.id);
create_dir_all(&*base_dir).await?;
create_dir_all(&*base_dir).await.map_err(|e| {
tonic::Status::new(
tonic::Code::Internal,
format!("failed to create sandbox directory, {}", e),
)
})?;
let opt = SandboxOption::new(base_dir, sandbox_data);
self.sandboxer.create(&*req.sandbox_id, opt).await?;
self.sandboxer
.create(&*req.sandbox_id, opt)
.await
.map_err(|e| {
tonic::Status::new(
tonic::Code::Internal,
format!("failed to create sandbox, {}", e),
)
})?;
let resp = ControllerCreateResponse {
sandbox_id: req.sandbox_id.to_string(),
};
Expand All @@ -67,9 +80,23 @@ where
request: tonic::Request<ControllerStartRequest>,
) -> Result<tonic::Response<ControllerStartResponse>, tonic::Status> {
let req = request.get_ref();
self.sandboxer.start(&*req.sandbox_id).await?;
self.sandboxer.start(&*req.sandbox_id).await.map_err(|e| {
tonic::Status::new(
tonic::Code::Internal,
format!("failed to start sandbox {}, {}", req.sandbox_id, e),
)
})?;

let sandbox_mutex = self.sandboxer.sandbox(&*req.sandbox_id).await?;
let sandbox_mutex = self
.sandboxer
.sandbox(&*req.sandbox_id)
.await
.map_err(|e| {
tonic::Status::new(
tonic::Code::Internal,
format!("failed to get sandbox {}, {}", req.sandbox_id, e),
)
})?;
let sandbox = sandbox_mutex.lock().await;
let res = match sandbox.get_data() {
Ok(s) => s,
Expand Down Expand Up @@ -97,7 +124,11 @@ where
.unwrap_or_default();
return Err(tonic::Status::new(
tonic::Code::Internal,
format!("sandbox status is {}", status.to_string()),
format!(
"sandbox {} status is {}",
req.sandbox_id,
status.to_string()
),
));
}
};
Expand Down Expand Up @@ -133,7 +164,16 @@ where
request: Request<PrepareRequest>,
) -> Result<Response<PrepareResponse>, Status> {
let req = request.get_ref();
let sandbox_mutex = self.sandboxer.sandbox(&*req.sandbox_id).await?;
let sandbox_mutex = self
.sandboxer
.sandbox(&*req.sandbox_id)
.await
.map_err(|e| {
tonic::Status::new(
tonic::Code::Internal,
format!("failed to get sandbox {}, {}", req.sandbox_id, e),
)
})?;
let mut sandbox = sandbox_mutex.lock().await;
return if req.exec_id.is_empty() {
let container_data = ContainerData::new(req);
Expand All @@ -142,8 +182,24 @@ where
container_data, req.sandbox_id
);
let opt = ContainerOption::new(container_data);
sandbox.append_container(&*req.container_id, opt).await?;
let container = sandbox.container(&*req.container_id).await?;
sandbox
.append_container(&*req.container_id, opt)
.await
.map_err(|e| {
tonic::Status::new(
tonic::Code::Internal,
format!(
"failed to append container {} to sandbox {}, {}",
req.container_id, req.sandbox_id, e
),
)
})?;
let container = sandbox.container(&*req.container_id).await.map_err(|e| {
tonic::Status::new(
tonic::Code::Internal,
format!("failed to get container {}, {}", req.container_id, e),
)
})?;
let data = container.get_data()?;
let resp = PrepareResponse {
bundle: data.bundle.to_string(),
Expand All @@ -155,11 +211,24 @@ where
"append a process {:?} to container {} of sandbox {}",
process_date, req.container_id, req.sandbox_id
);
let container = sandbox.container(&*req.container_id).await?;
let container = sandbox.container(&*req.container_id).await.map_err(|e| {
tonic::Status::new(
tonic::Code::Internal,
format!("failed to get container {}, {}", req.container_id, e),
)
})?;
let mut data = container.get_data()?;
data.processes.push(process_date);
let opt = ContainerOption::new(data);
sandbox.update_container(&*req.container_id, opt).await?;
sandbox
.update_container(&*req.container_id, opt)
.await
.map_err(|e| {
tonic::Status::new(
tonic::Code::Internal,
format!("failed to update container {}, {}", req.container_id, e),
)
})?;
Ok(Response::new(PrepareResponse {
bundle: "".to_string(),
}))
Expand All @@ -171,7 +240,16 @@ where
request: Request<PurgeRequest>,
) -> Result<Response<PurgeResponse>, Status> {
let req = request.get_ref();
let sandbox_mutex = self.sandboxer.sandbox(&*req.sandbox_id).await?;
let sandbox_mutex = self
.sandboxer
.sandbox(&*req.sandbox_id)
.await
.map_err(|e| {
tonic::Status::new(
tonic::Code::Internal,
format!("failed to get sandbox {}, {}", req.sandbox_id, e),
)
})?;
let mut sandbox = sandbox_mutex.lock().await;
return if req.exec_id.is_empty() {
info!(
Expand All @@ -185,11 +263,24 @@ where
"remove process {} from container {} of sandbox {}",
req.exec_id, req.container_id, req.sandbox_id
);
let container = sandbox.container(&*req.container_id).await?;
let container = sandbox.container(&*req.container_id).await.map_err(|e| {
tonic::Status::new(
tonic::Code::Internal,
format!("failed to get container {}, {}", req.container_id, e),
)
})?;
let mut data = container.get_data()?;
data.processes.retain(|x| x.id != req.exec_id);
let opt = ContainerOption::new(data);
sandbox.update_container(&*req.container_id, opt).await?;
sandbox
.update_container(&*req.container_id, opt)
.await
.map_err(|e| {
tonic::Status::new(
tonic::Code::Internal,
format!("failed to update container {}, {}", req.container_id, e),
)
})?;
Ok(Response::new(PurgeResponse {}))
};
}
Expand Down Expand Up @@ -218,13 +309,31 @@ where
) -> Result<tonic::Response<ControllerWaitResponse>, tonic::Status> {
let req = request.get_ref();
let exit_signal = {
let sandbox_mutex = self.sandboxer.sandbox(&*req.sandbox_id).await?;
let sandbox_mutex = self
.sandboxer
.sandbox(&*req.sandbox_id)
.await
.map_err(|e| {
tonic::Status::new(
tonic::Code::Internal,
format!("failed to get sandbox {}, {}", req.sandbox_id, e),
)
})?;
let sandbox = sandbox_mutex.lock().await;
sandbox.exit_signal().await?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this error doesn't need to do map_err processing?

};

exit_signal.wait().await;
let sandbox_mutex = self.sandboxer.sandbox(&*req.sandbox_id).await?;
let sandbox_mutex = self
.sandboxer
.sandbox(&*req.sandbox_id)
.await
.map_err(|e| {
tonic::Status::new(
tonic::Code::Internal,
format!("failed to get sandbox, {}", e),
)
})?;
let sandbox = sandbox_mutex.lock().await;
let mut wait_resp = ControllerWaitResponse {
exit_status: 0,
Expand All @@ -248,7 +357,16 @@ where
request: tonic::Request<ControllerStatusRequest>,
) -> Result<tonic::Response<ControllerStatusResponse>, tonic::Status> {
let req = request.get_ref();
let sandbox_mutex = self.sandboxer.sandbox(&*req.sandbox_id).await?;
let sandbox_mutex = self
.sandboxer
.sandbox(&*req.sandbox_id)
.await
.map_err(|e| {
tonic::Status::new(
tonic::Code::Internal,
format!("failed to get sandbox {}, {}", req.sandbox_id, e),
)
})?;
let sandbox = sandbox_mutex.lock().await;
// TODO the state should match the definition in containerd
let (state, pid) = match sandbox.status()? {
Expand Down