Skip to content

Commit 14332e1

Browse files
authored
Merge branch 'main' into dev2
Signed-off-by: jokemanfire <[email protected]>
2 parents 7b84fcf + d584728 commit 14332e1

File tree

20 files changed

+270
-81
lines changed

20 files changed

+270
-81
lines changed

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ jobs:
222222
# run the example
223223
cargo run -p containerd-shim --example skeleton -- -namespace default -id 1234 -address "\\.\pipe\containerd-containerd" -publish-binary ./bin/containerd start
224224
ps skeleton
225-
cargo run -p containerd-shim-protos --example shim-proto-connect \\.\pipe\containerd-shim-17630016127144989388-pipe
225+
cargo run -p containerd-shim-protos --example shim-proto-connect \\.\pipe\containerd-shim-bc764c65e177434fcefe8257dc440be8b8acf7c96156320d965938f7e9ae1a35-pipe
226226
$skeleton = get-process skeleton -ErrorAction SilentlyContinue
227227
if ($skeleton) { exit 1 }
228228
- name: Run client

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ futures = "0.3.19"
3232
libc = "0.2.112"
3333
log = {version = "0.4.2", features=["kv_unstable"]}
3434
nix = "0.29"
35-
oci-spec = "0.6"
35+
oci-spec = "0.7"
3636
os_pipe = "1.1"
3737
prctl = "1.0.0"
3838
prost = "0.13"

crates/runc-shim/src/cgroup_memory.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,15 @@ pub async fn register_memory_event(
9393
let path = cg_dir.join(event_name);
9494
let event_file = fs::File::open(path.clone())
9595
.await
96-
.map_err(other_error!(e, "Error get path:"))?;
96+
.map_err(other_error!("Error get path:"))?;
9797

9898
let eventfd = EventFd::from_value_and_flags(0, EfdFlags::EFD_CLOEXEC)?;
9999

100100
let event_control_path = cg_dir.join("cgroup.event_control");
101101
let data = format!("{} {}", eventfd.as_raw_fd(), event_file.as_raw_fd());
102102
fs::write(&event_control_path, data.clone())
103103
.await
104-
.map_err(other_error!(e, "Error write eventfd:"))?;
104+
.map_err(other_error!("Error write eventfd:"))?;
105105

106106
let mut buf = [0u8; 8];
107107

crates/runc-shim/src/common.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ pub fn create_runc(
192192
}
193193
gopts
194194
.build()
195-
.map_err(other_error!(e, "unable to create runc instance"))
195+
.map_err(other_error!("unable to create runc instance"))
196196
}
197197

198198
#[derive(Default)]

crates/runc-shim/src/container.rs

+10
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ pub trait Container {
5656
async fn stats(&self) -> Result<Metrics>;
5757
async fn all_processes(&self) -> Result<Vec<ProcessInfo>>;
5858
async fn close_io(&mut self, exec_id: Option<&str>) -> Result<()>;
59+
async fn pause(&mut self) -> Result<()>;
60+
async fn resume(&mut self) -> Result<()>;
5961
}
6062

6163
#[async_trait]
@@ -212,6 +214,14 @@ where
212214
let process = self.get_mut_process(exec_id)?;
213215
process.close_io().await
214216
}
217+
218+
async fn pause(&mut self) -> Result<()> {
219+
self.init.pause().await
220+
}
221+
222+
async fn resume(&mut self) -> Result<()> {
223+
self.init.resume().await
224+
}
215225
}
216226

217227
impl<T, E, P> ContainerTemplate<T, E, P>

crates/runc-shim/src/processes.rs

+12
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ pub trait Process {
5555
async fn stats(&self) -> Result<Metrics>;
5656
async fn ps(&self) -> Result<Vec<ProcessInfo>>;
5757
async fn close_io(&mut self) -> Result<()>;
58+
async fn pause(&mut self) -> Result<()>;
59+
async fn resume(&mut self) -> Result<()>;
5860
}
5961

6062
#[async_trait]
@@ -65,6 +67,8 @@ pub trait ProcessLifecycle<P: Process> {
6567
async fn update(&self, p: &mut P, resources: &LinuxResources) -> Result<()>;
6668
async fn stats(&self, p: &P) -> Result<Metrics>;
6769
async fn ps(&self, p: &P) -> Result<Vec<ProcessInfo>>;
70+
async fn pause(&self, p: &mut P) -> Result<()>;
71+
async fn resume(&self, p: &mut P) -> Result<()>;
6872
}
6973

7074
pub struct ProcessTemplate<S> {
@@ -200,4 +204,12 @@ where
200204
}
201205
Ok(())
202206
}
207+
208+
async fn pause(&mut self) -> Result<()> {
209+
self.lifecycle.clone().pause(self).await
210+
}
211+
212+
async fn resume(&mut self) -> Result<()> {
213+
self.lifecycle.clone().resume(self).await
214+
}
203215
}

crates/runc-shim/src/runc.rs

+49-1
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ impl ProcessLifecycle<InitProcess> for RuncInitLifecycle {
360360
.runtime
361361
.ps(&p.id)
362362
.await
363-
.map_err(other_error!(e, "failed to execute runc ps"))?;
363+
.map_err(other_error!("failed to execute runc ps"))?;
364364
Ok(pids
365365
.iter()
366366
.map(|&x| ProcessInfo {
@@ -369,6 +369,46 @@ impl ProcessLifecycle<InitProcess> for RuncInitLifecycle {
369369
})
370370
.collect())
371371
}
372+
373+
#[cfg(target_os = "linux")]
374+
async fn pause(&self, p: &mut InitProcess) -> Result<()> {
375+
match p.state {
376+
Status::RUNNING => {
377+
p.state = Status::PAUSING;
378+
if let Err(e) = self.runtime.pause(p.id.as_str()).await {
379+
p.state = Status::RUNNING;
380+
return Err(runtime_error(&self.bundle, e, "OCI runtime pause failed").await);
381+
}
382+
p.state = Status::PAUSED;
383+
Ok(())
384+
}
385+
_ => Err(other!("cannot pause when in {:?} state", p.state)),
386+
}
387+
}
388+
389+
#[cfg(not(target_os = "linux"))]
390+
async fn pause(&self, _p: &mut InitProcess) -> Result<()> {
391+
Err(Error::Unimplemented("pause".to_string()))
392+
}
393+
394+
#[cfg(target_os = "linux")]
395+
async fn resume(&self, p: &mut InitProcess) -> Result<()> {
396+
match p.state {
397+
Status::PAUSED => {
398+
if let Err(e) = self.runtime.resume(p.id.as_str()).await {
399+
return Err(runtime_error(&self.bundle, e, "OCI runtime pause failed").await);
400+
}
401+
p.state = Status::RUNNING;
402+
Ok(())
403+
}
404+
_ => Err(other!("cannot resume when in {:?} state", p.state)),
405+
}
406+
}
407+
408+
#[cfg(not(target_os = "linux"))]
409+
async fn resume(&self, _p: &mut InitProcess) -> Result<()> {
410+
Err(Error::Unimplemented("resume".to_string()))
411+
}
372412
}
373413

374414
impl RuncInitLifecycle {
@@ -495,6 +535,14 @@ impl ProcessLifecycle<ExecProcess> for RuncExecLifecycle {
495535
async fn ps(&self, _p: &ExecProcess) -> Result<Vec<ProcessInfo>> {
496536
Err(Error::Unimplemented("exec ps".to_string()))
497537
}
538+
539+
async fn pause(&self, _p: &mut ExecProcess) -> Result<()> {
540+
Err(Error::Unimplemented("exec pause".to_string()))
541+
}
542+
543+
async fn resume(&self, _p: &mut ExecProcess) -> Result<()> {
544+
Err(Error::Unimplemented("exec resume".to_string()))
545+
}
498546
}
499547

500548
async fn copy_console(

crates/runc-shim/src/service.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ use containerd_shim::{
2727
event::Event,
2828
io_error,
2929
monitor::{Subject, Topic},
30-
protos::{events::task::TaskExit, protobuf::MessageDyn},
30+
protos::{events::task::TaskExit, protobuf::MessageDyn, ttrpc::context::with_timeout},
3131
util::{
3232
convert_to_timestamp, read_options, read_pid_from_file, read_runtime, read_spec, timestamp,
3333
write_str_to_file,
3434
},
35-
Config, Context, DeleteResponse, Error, Flags, StartOpts,
35+
Config, DeleteResponse, Error, Flags, StartOpts,
3636
};
3737
use log::{debug, error, warn};
3838
use tokio::sync::mpsc::{channel, Receiver, Sender};
@@ -218,8 +218,11 @@ async fn forward(
218218
) {
219219
tokio::spawn(async move {
220220
while let Some((topic, e)) = rx.recv().await {
221+
// While ttrpc push the event,give it a 5 seconds timeout.
222+
// Prevent event reporting from taking too long time.
223+
// Learnd from goshim's containerd/runtime/v2/shim/publisher.go
221224
publisher
222-
.publish(Context::default(), &topic, &ns, e)
225+
.publish(with_timeout(5000000000), &topic, &ns, e)
223226
.await
224227
.unwrap_or_else(|e| warn!("publish {} to containerd: {}", topic, e));
225228
}

crates/runc-shim/src/task.rs

+31-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ use std::path::Path;
5050

5151
#[cfg(target_os = "linux")]
5252
use cgroups_rs::hierarchies::is_cgroup2_unified_mode;
53+
use containerd_shim::{
54+
api::{PauseRequest, ResumeRequest},
55+
protos::events::task::{TaskPaused, TaskResumed},
56+
};
5357
#[cfg(target_os = "linux")]
5458
use containerd_shim::{
5559
error::{Error, Result},
@@ -143,7 +147,7 @@ async fn monitor_oom(id: &String, pid: u32, tx: EventSender) -> Result<()> {
143147
"memory.oom_control",
144148
)
145149
.await
146-
.map_err(other_error!(e, "register_memory_event failed:"))?;
150+
.map_err(other_error!("register_memory_event failed:"))?;
147151

148152
run_oom_monitor(rx, id.to_string(), tx);
149153
}
@@ -288,6 +292,32 @@ where
288292
})
289293
}
290294

295+
async fn pause(&self, _ctx: &TtrpcContext, req: PauseRequest) -> TtrpcResult<Empty> {
296+
info!("pause request for {:?}", req);
297+
let mut container = self.get_container(req.id()).await?;
298+
container.pause().await?;
299+
self.send_event(TaskPaused {
300+
container_id: req.id.to_string(),
301+
..Default::default()
302+
})
303+
.await;
304+
info!("pause request for {:?} returns successfully", req);
305+
Ok(Empty::new())
306+
}
307+
308+
async fn resume(&self, _ctx: &TtrpcContext, req: ResumeRequest) -> TtrpcResult<Empty> {
309+
info!("resume request for {:?}", req);
310+
let mut container = self.get_container(req.id()).await?;
311+
container.resume().await?;
312+
self.send_event(TaskResumed {
313+
container_id: req.id.to_string(),
314+
..Default::default()
315+
})
316+
.await;
317+
info!("resume request for {:?} returns successfully", req);
318+
Ok(Empty::new())
319+
}
320+
291321
async fn kill(&self, _ctx: &TtrpcContext, req: KillRequest) -> TtrpcResult<Empty> {
292322
info!("Kill request for {:?}", req);
293323
let mut container = self.get_container(req.id()).await?;

crates/runc/src/io.rs

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use std::{
2525
sync::Mutex,
2626
};
2727

28+
2829
#[cfg(feature = "async")]
2930
use tokio::io::{AsyncRead, AsyncWrite};
3031
use tokio::net::unix::pipe::{self, Receiver, Sender};

crates/shim/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ path = "examples/windows_log_reader.rs"
3737
containerd-shim-protos = { path = "../shim-protos", version = "0.7.2" }
3838
go-flag = "0.1.0"
3939
lazy_static = "1.4.0"
40+
sha2 = "0.10.2"
4041
libc.workspace = true
4142
log = { workspace = true, features = ["std", "kv_unstable" ] }
4243
nix = { workspace = true, features = [

crates/shim/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -175,16 +175,16 @@ $ cat log
175175
$env:TTRPC_ADDRESS="\\.\pipe\containerd-containerd.ttrpc"
176176
177177
$ cargo run --example skeleton -- -namespace default -id 1234 -address "\\.\pipe\containerd-containerd" start
178-
\\.\pipe\containerd-shim-17630016127144989388-pipe
178+
\\.\pipe\containerd-shim-bc764c65e177434fcefe8257dc440be8b8acf7c96156320d965938f7e9ae1a35-pipe
179179
180180
# (Optional) Run the log collector in a separate command window
181181
# note: log reader won't work if containerd is connected to the named pipe, this works when running manually to help debug locally
182182
$ cargo run --example windows-log-reader \\.\pipe\containerd-shim-default-1234-log
183183
Reading logs from: \\.\pipe\containerd-shim-default-1234-log
184184
<logs will appear after next command>
185185
186-
$ cargo run --example shim-proto-connect \\.\pipe\containerd-shim-17630016127144989388-pipe
187-
Connecting to \\.\pipe\containerd-shim-17630016127144989388-pipe...
186+
$ cargo run --example shim-proto-connect \\.\pipe\containerd-shim-bc764c65e177434fcefe8257dc440be8b8acf7c96156320d965938f7e9ae1a35-pipe
187+
Connecting to \\.\pipe\containerd-shim-bc764c65e177434fcefe8257dc440be8b8acf7c96156320d965938f7e9ae1a35-pipe...
188188
Sending `Connect` request...
189189
Connect response: version: "example"
190190
Sending `Shutdown` request...

crates/shim/src/asynchronous/monitor.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ impl Monitor {
117117
subject: subject.clone(),
118118
exit_code,
119119
})
120-
.map_err(other_error!(e, "failed to send exit code"));
120+
.map_err(other_error!("failed to send exit code"));
121121
results.push(res);
122122
}
123123
}

0 commit comments

Comments
 (0)