Skip to content

Commit 301adf9

Browse files
committed
bugfix:fix exec log grow to burst
Signed-off-by: ningmingxiao <[email protected]>
1 parent ae58ea3 commit 301adf9

File tree

4 files changed

+144
-51
lines changed

4 files changed

+144
-51
lines changed

crates/runc-shim/src/runc.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ use containerd_shim::{
4747
use log::{debug, error};
4848
use nix::{sys::signal::kill, unistd::Pid};
4949
use oci_spec::runtime::{LinuxResources, Process};
50-
use runc::{Command, Runc, Spawner};
50+
use runc::{Command, Runc, RuncGlobalArgs, Spawner};
5151
use tokio::{
5252
fs::{remove_file, File, OpenOptions},
5353
io::{AsyncBufReadExt, AsyncRead, AsyncReadExt, AsyncWrite, BufReader},
@@ -455,7 +455,14 @@ impl ProcessLifecycle<ExecProcess> for RuncExecLifecycle {
455455
async fn start(&self, p: &mut ExecProcess) -> containerd_shim::Result<()> {
456456
let bundle = self.bundle.to_string();
457457
let pid_path = Path::new(&bundle).join(format!("{}.pid", &p.id));
458+
let log_path = Path::new(&bundle).join(format!("{}-exec.log", &p.id));
459+
let custom_global_args = RuncGlobalArgs {
460+
log: Some(log_path.clone()),
461+
..Default::default()
462+
};
463+
458464
let mut exec_opts = runc::options::ExecOpts {
465+
custom_args: custom_global_args,
459466
io: None,
460467
pid_file: Some(pid_path.to_owned()),
461468
console_socket: None,
@@ -475,6 +482,7 @@ impl ProcessLifecycle<ExecProcess> for RuncExecLifecycle {
475482
.runtime
476483
.exec(&self.container_id, &self.spec, Some(&exec_opts))
477484
.await;
485+
let _ = tokio::fs::remove_file(log_path).await;
478486
if let Err(e) = exec_result {
479487
if let Some(s) = socket {
480488
s.clean().await;

crates/runc/src/asynchronous/runc.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use crate::{
2626
events,
2727
options::*,
2828
utils::{self, write_value_to_temp_file},
29-
Command, Response, Result, Runc,
29+
Command, Response, Result, Runc, RuncGlobalArgs,
3030
};
3131

3232
// a macro tool to cleanup the file with name $filename,
@@ -138,11 +138,15 @@ impl Runc {
138138
pub async fn exec(&self, id: &str, spec: &Process, opts: Option<&ExecOpts>) -> Result<()> {
139139
let f = write_value_to_temp_file(spec).await?;
140140
let mut args = vec!["exec".to_string(), "--process".to_string(), f.clone()];
141+
let mut custom_global_args = RuncGlobalArgs {
142+
..Default::default()
143+
};
141144
if let Some(opts) = opts {
142145
args.append(&mut tc!(opts.args(), &f));
146+
custom_global_args = opts.custom_args.clone();
143147
}
144148
args.push(id.to_string());
145-
let mut cmd = self.command(&args)?;
149+
let mut cmd = self.command_with_global_args(&args, custom_global_args)?;
146150
match opts {
147151
Some(ExecOpts { io: Some(io), .. }) => {
148152
tc!(

crates/runc/src/lib.rs

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ pub mod utils;
6565
const JSON: &str = "json";
6666
const TEXT: &str = "text";
6767

68+
const DEBUG: &str = "--debug";
69+
const LOG: &str = "--log";
70+
const LOG_FORMAT: &str = "--log-format";
71+
const ROOT: &str = "--root";
72+
const ROOTLESS: &str = "--rootless";
73+
const SYSTEMD_CGROUP: &str = "--systemd-cgroup";
74+
6875
pub type Result<T> = std::result::Result<T, crate::error::Error>;
6976

7077
/// Response is for (pid, exit status, outputs).
@@ -107,13 +114,88 @@ pub type Command = tokio::process::Command;
107114
#[derive(Debug, Clone)]
108115
pub struct Runc {
109116
command: PathBuf,
110-
args: Vec<String>,
117+
global_args: RuncGlobalArgs,
111118
spawner: Arc<dyn Spawner + Send + Sync>,
112119
}
113120

121+
#[derive(Debug, Clone, Default)]
122+
pub struct RuncGlobalArgs {
123+
pub debug: Option<bool>,
124+
pub log: Option<PathBuf>,
125+
pub log_format: Option<String>,
126+
pub root: Option<PathBuf>,
127+
pub systemd_cgroup: Option<bool>,
128+
pub rootless: Option<bool>,
129+
}
130+
114131
impl Runc {
115132
fn command(&self, args: &[String]) -> Result<Command> {
116-
let args = [&self.args, args].concat();
133+
let custom_global_args = RuncGlobalArgs {
134+
debug: None,
135+
log: None,
136+
log_format: None,
137+
root: None,
138+
systemd_cgroup: None,
139+
rootless: None,
140+
};
141+
self.command_with_global_args(args, custom_global_args)
142+
}
143+
144+
fn command_with_global_args(
145+
&self,
146+
args: &[String],
147+
custom_global_args: RuncGlobalArgs,
148+
) -> Result<Command> {
149+
let mut global_args_vec: Vec<String> = Vec::new();
150+
if let Some(custom_debug) = custom_global_args.debug {
151+
global_args_vec.push(DEBUG.to_string());
152+
global_args_vec.push(custom_debug.to_string());
153+
} else if let Some(debug) = self.global_args.debug {
154+
global_args_vec.push(DEBUG.to_string());
155+
global_args_vec.push(debug.to_string());
156+
}
157+
158+
if let Some(custom_log) = custom_global_args.log {
159+
global_args_vec.push(LOG.to_string());
160+
global_args_vec.push(custom_log.to_string_lossy().to_string());
161+
} else if let Some(log) = &self.global_args.log {
162+
global_args_vec.push(LOG.to_string());
163+
global_args_vec.push(log.to_string_lossy().to_string());
164+
}
165+
166+
if let Some(custom_log_format) = custom_global_args.log_format {
167+
global_args_vec.push(LOG_FORMAT.to_string());
168+
global_args_vec.push(custom_log_format);
169+
} else if let Some(log_format) = &self.global_args.log_format {
170+
global_args_vec.push(LOG_FORMAT.to_string());
171+
global_args_vec.push(log_format.to_string());
172+
}
173+
174+
if let Some(custom_root) = custom_global_args.root {
175+
global_args_vec.push(ROOT.to_string());
176+
global_args_vec.push(custom_root.to_string_lossy().to_string());
177+
} else if let Some(root) = &self.global_args.root {
178+
global_args_vec.push(ROOT.to_string());
179+
global_args_vec.push(root.to_string_lossy().to_string());
180+
}
181+
182+
if let Some(systemd_cgroup) = custom_global_args.systemd_cgroup {
183+
global_args_vec.push(SYSTEMD_CGROUP.to_string());
184+
global_args_vec.push(systemd_cgroup.to_string());
185+
} else if let Some(systemd_cgroup) = self.global_args.systemd_cgroup {
186+
global_args_vec.push(SYSTEMD_CGROUP.to_string());
187+
global_args_vec.push(systemd_cgroup.to_string());
188+
}
189+
190+
if let Some(custom_rootless) = custom_global_args.rootless {
191+
global_args_vec.push(ROOTLESS.to_string());
192+
global_args_vec.push(custom_rootless.to_string());
193+
} else if let Some(rootless) = self.global_args.rootless {
194+
global_args_vec.push(ROOTLESS.to_string());
195+
global_args_vec.push(rootless.to_string());
196+
}
197+
198+
let args = [global_args_vec, args.to_vec()].concat();
117199
let mut cmd = Command::new(&self.command);
118200

119201
// Default to piped stdio, and they may be override by command options.

crates/runc/src/options.rs

Lines changed: 45 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,12 @@ use std::{
3939
time::Duration,
4040
};
4141

42-
use crate::{error::Error, utils, DefaultExecutor, Io, LogFormat, Runc, Spawner};
42+
use crate::{error::Error, utils, DefaultExecutor, Io, LogFormat, Runc, RuncGlobalArgs, Spawner};
4343

4444
// constants for log format
4545
pub const JSON: &str = "json";
4646
pub const TEXT: &str = "text";
4747

48-
// constants for runc global flags
49-
const DEBUG: &str = "--debug";
50-
const LOG: &str = "--log";
51-
const LOG_FORMAT: &str = "--log-format";
52-
const ROOT: &str = "--root";
53-
const ROOTLESS: &str = "--rootless";
54-
const SYSTEMD_CGROUP: &str = "--systemd-cgroup";
55-
5648
// constants for runc-create/runc-exec flags
5749
const CONSOLE_SOCKET: &str = "--console-socket";
5850
const DETACH: &str = "--detach";
@@ -79,7 +71,7 @@ pub trait Args {
7971
///
8072
/// These options will be passed for all subsequent runc calls.
8173
/// See <https://github.com/opencontainers/runc/blob/main/man/runc.8.md#global-options>
82-
#[derive(Debug, Default)]
74+
#[derive(Debug, Default, Clone)]
8375
pub struct GlobalOpts {
8476
/// Override the name of the runc binary. If [`None`], `runc` is used.
8577
command: Option<PathBuf>,
@@ -203,64 +195,65 @@ impl GlobalOpts {
203195
self.args()
204196
}
205197

206-
fn output(&self) -> Result<(PathBuf, Vec<String>), Error> {
198+
fn output(&self) -> Result<(PathBuf, RuncGlobalArgs), Error> {
207199
let path = self
208200
.command
209201
.clone()
210202
.unwrap_or_else(|| PathBuf::from("runc"));
211203

212204
let command = utils::binary_path(path).ok_or(Error::NotFound)?;
213205

214-
let mut args = Vec::new();
215-
206+
let mut global_args = RuncGlobalArgs {
207+
debug: None,
208+
log: None,
209+
log_format: None,
210+
root: None,
211+
systemd_cgroup: None,
212+
rootless: None,
213+
};
216214
// --root path : Set the root directory to store containers' state.
217215
if let Some(root) = &self.root {
218-
args.push(ROOT.into());
219-
args.push(utils::abs_string(root)?);
216+
global_args.root = Some(root.to_path_buf());
220217
}
221218

222219
// --debug : Enable debug logging.
223220
if self.debug {
224-
args.push(DEBUG.into());
221+
global_args.debug = Some(self.debug);
225222
}
226223

227224
// --log path : Set the log destination to path. The default is to log to stderr.
228225
if let Some(log_path) = &self.log {
229-
args.push(LOG.into());
230-
args.push(utils::abs_string(log_path)?);
226+
global_args.log = Some(log_path.to_path_buf());
231227
}
232228

233229
// --log-format text|json : Set the log format (default is text).
234-
args.push(LOG_FORMAT.into());
235-
args.push(self.log_format.to_string());
230+
global_args.log_format = Some(self.log_format.to_string());
236231

237-
// --systemd-cgroup : Enable systemd cgroup support.
238232
if self.systemd_cgroup {
239-
args.push(SYSTEMD_CGROUP.into());
233+
global_args.systemd_cgroup = Some(true);
240234
}
241235

242236
// --rootless true|false|auto : Enable or disable rootless mode.
243237
if let Some(mode) = self.rootless {
244-
let arg = format!("{}={}", ROOTLESS, mode);
245-
args.push(arg);
238+
global_args.rootless = Some(mode);
246239
}
247-
Ok((command, args))
240+
Ok((command, global_args))
248241
}
249242
}
250243

251244
impl Args for GlobalOpts {
252245
type Output = Result<Runc, Error>;
253246

254247
fn args(&self) -> Self::Output {
255-
let (command, args) = self.output()?;
248+
let (command, global_args) = self.output()?;
256249
let executor = if let Some(exec) = self.executor.clone() {
257250
exec
258251
} else {
259252
Arc::new(DefaultExecutor {})
260253
};
261254
Ok(Runc {
262255
command,
263-
args,
256+
global_args,
264257
spawner: executor,
265258
})
266259
}
@@ -352,6 +345,7 @@ impl CreateOpts {
352345
/// Container execution options
353346
#[derive(Clone, Default)]
354347
pub struct ExecOpts {
348+
pub custom_args: RuncGlobalArgs,
355349
pub io: Option<Arc<dyn Io>>,
356350
/// Path to where a pid file should be created.
357351
pub pid_file: Option<PathBuf>,
@@ -596,15 +590,11 @@ mod tests {
596590
fn global_opts_test() {
597591
let cfg = GlobalOpts::default().command("true");
598592
let runc = cfg.build().unwrap();
599-
let args = &runc.args;
600-
assert_eq!(args.len(), 2);
601-
assert!(args.contains(&LOG_FORMAT.to_string()));
602-
assert!(args.contains(&TEXT.to_string()));
603-
604-
let cfg = GlobalOpts::default().command("/bin/true");
605-
let runc = cfg.build().unwrap();
606-
assert_eq!(runc.args.len(), 2);
593+
let global_args = &runc.global_args;
607594

595+
if let Some(log_format) = &global_args.log_format {
596+
assert!(TEXT == log_format);
597+
}
608598
let cfg = GlobalOpts::default()
609599
.command("true")
610600
.root("/tmp")
@@ -614,16 +604,25 @@ mod tests {
614604
.systemd_cgroup(true)
615605
.rootless(true);
616606
let runc = cfg.build().unwrap();
617-
let args = &runc.args;
618-
assert!(args.contains(&ROOT.to_string()));
619-
assert!(args.contains(&DEBUG.to_string()));
620-
assert!(args.contains(&"/tmp".to_string()));
621-
assert!(args.contains(&LOG.to_string()));
622-
assert!(args.contains(&"/tmp/runc.log".to_string()));
623-
assert!(args.contains(&LOG_FORMAT.to_string()));
624-
assert!(args.contains(&JSON.to_string()));
625-
assert!(args.contains(&"--rootless=true".to_string()));
626-
assert!(args.contains(&SYSTEMD_CGROUP.to_string()));
627-
assert_eq!(args.len(), 9);
607+
let global_args = &runc.global_args;
608+
609+
if let Some(root) = &global_args.root {
610+
assert!(root.to_string_lossy() == "/tmp");
611+
}
612+
if let Some(debug) = global_args.debug {
613+
assert!(debug);
614+
}
615+
if let Some(log) = &global_args.log {
616+
assert!(log.to_string_lossy() == "/tmp/runc.log");
617+
}
618+
if let Some(log_format) = &global_args.log_format {
619+
assert!(log_format == "json");
620+
}
621+
if let Some(root_less) = global_args.rootless {
622+
assert!(root_less);
623+
}
624+
if let Some(systemd_cgroup) = global_args.systemd_cgroup {
625+
assert!(systemd_cgroup);
626+
}
628627
}
629628
}

0 commit comments

Comments
 (0)