Skip to content

Commit

Permalink
Make one function that works for both platforms
Browse files Browse the repository at this point in the history
Signed-off-by: James Sturtevant <jstur@microsoft.com>
  • Loading branch information
jsturtevant committed Apr 27, 2023
1 parent 6a28278 commit 0d5591d
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 202 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,5 @@ jobs:
cargo run --example skeleton -- -namespace default -id 1234 -address "\\.\pipe\containerd-containerd" -publish-binary ./bin/containerd start
ps skeleton
cargo run --example shim-proto-connect \\.\pipe\containerd-shim-17630016127144989388-pipe
$skeleton = get-process skeleton -ErrorAction SilentlyContinue
if ($skeleton) { exit 1 }
2 changes: 1 addition & 1 deletion crates/shim/src/asynchronous/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ where
}
_ => {
if !config.no_setup_logger {
logger::init(flags.debug)?;
logger::init(flags.debug, &flags.namespace, &flags.id)?;
}

let publisher = RemotePublisher::new(&ttrpc_address).await?;
Expand Down
18 changes: 8 additions & 10 deletions crates/shim/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,17 +181,15 @@ pub fn socket_address(socket_path: &str, namespace: &str, id: &str) -> String {
hasher.finish()
};

format_address(hash)
}

#[cfg(windows)]
fn format_address(hash: u64) -> String {
format!(r"\\.\pipe\containerd-shim-{}-pipe", hash)
}
#[cfg(unix)]
{
format!("unix://{}/{:x}.sock", SOCKET_ROOT, hash)
}

#[cfg(unix)]
fn format_address(hash: u64) -> String {
format!("unix://{}/{:x}.sock", SOCKET_ROOT, hash)
#[cfg(windows)]
{
format!(r"\\.\pipe\containerd-shim-{}-pipe", hash)
}
}

#[cfg(unix)]
Expand Down
32 changes: 11 additions & 21 deletions crates/shim/src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,37 +78,27 @@ impl log::Log for FifoLogger {
}
}

#[cfg(unix)]
pub fn init(debug: bool) -> Result<(), Error> {
pub fn init(debug: bool, _namespace: &str, _id: &str) -> Result<(), Error> {
#[cfg(unix)]
let logger = FifoLogger::new().map_err(io_error!(e, "failed to init logger"))?;

let boxed_logger = Box::new(logger);
configure_logger(boxed_logger, debug)
}

#[cfg(windows)]
// Containerd on windows expects the log to be a named pipe in the format of \\.\pipe\containerd-<namespace>-<id>-log
// There is an assumption that there is always only one client connected which is containerd.
// If there is a restart of containerd then logs during that time period will be lost.
//
// https://github.com/containerd/containerd/blob/v1.7.0/runtime/v2/shim_windows.go#L77
// https://github.com/microsoft/hcsshim/blob/5871d0c4436f131c377655a3eb09fc9b5065f11d/cmd/containerd-shim-runhcs-v1/serve.go#L132-L137
pub fn init(debug: bool, namespace: &String, id: &String) -> Result<(), Error> {
// Containerd on windows expects the log to be a named pipe in the format of \\.\pipe\containerd-<namespace>-<id>-log
// There is an assumption that there is always only one client connected which is containerd.
// If there is a restart of containerd then logs during that time period will be lost.
//
// https://github.com/containerd/containerd/blob/v1.7.0/runtime/v2/shim_windows.go#L77
// https://github.com/microsoft/hcsshim/blob/5871d0c4436f131c377655a3eb09fc9b5065f11d/cmd/containerd-shim-runhcs-v1/serve.go#L132-L137
#[cfg(windows)]
let logger =
NamedPipeLogger::new(namespace, id).map_err(io_error!(e, "failed to init logger"))?;

let boxed_logger = Box::new(logger);
configure_logger(boxed_logger, debug)
}
NamedPipeLogger::new(_namespace, _id).map_err(io_error!(e, "failed to init logger"))?;

fn configure_logger(logger: Box<dyn log::Log>, debug: bool) -> Result<(), Error> {
let level = if debug {
log::LevelFilter::Debug
} else {
log::LevelFilter::Info
};

log::set_boxed_logger(logger)?;
log::set_boxed_logger(Box::new(logger))?;
log::set_max_level(level);
Ok(())
}
Expand Down
Loading

0 comments on commit 0d5591d

Please sign in to comment.