Skip to main content

saluki_env/features/
containerd.rs

1use std::path::PathBuf;
2
3use saluki_config::GenericConfiguration;
4#[cfg(unix)]
5use tracing::{debug, error};
6
7#[cfg(unix)]
8use super::{
9    find_first_available_unix_socket, is_running_inside_docker, path_contains, path_empty, with_host_mount_prefixes,
10};
11
12#[cfg(unix)]
13const DEFAULT_CONTAINERD_SOCKET_PATH_LINUX: &str = "/var/run/containerd/containerd.sock";
14
15/// Helper type for detecting if containerd is available.
16pub struct ContainerdDetector;
17
18impl ContainerdDetector {
19    /// Tries to detect the containerd gRPC socket path.
20    ///
21    /// The socket path can be specified in the configuration, or if it's not present, default paths will be checked for
22    /// the presence of the socket path.
23    ///
24    /// If the socket path is configured or detected, and is a valid Unix domain socket, `Some` is returned with the
25    /// socket path. Otherwise, `None` is returned.
26    #[cfg(unix)]
27    pub fn detect_grpc_socket_path(config: &GenericConfiguration) -> Option<PathBuf> {
28        // Try and read the socket path from either the configuration, or if it's not present there, from the possible
29        // default paths we would expect it to be listening at.
30        let detected_socket_path = match config.try_get_typed::<PathBuf>("cri_socket_path") {
31            Ok(Some(cri_socket_path)) => Some(cri_socket_path),
32            Ok(None) => {
33                if is_running_inside_docker() {
34                    None
35                } else {
36                    debug!("Containerd socket path (`cri_socket_path`) not present in configuration. Trying to detect at default paths...");
37
38                    let default_socket_paths = with_host_mount_prefixes([DEFAULT_CONTAINERD_SOCKET_PATH_LINUX]);
39                    find_first_available_unix_socket(default_socket_paths)
40                }
41            }
42            Err(e) => {
43                error!(error = %e, "Value for `cri_socket_path` could not be parsed as a valid path.");
44                None
45            }
46        }?;
47
48        // If the path isn't empty, and it contains "containerd", we can assume it's the containerd socket.
49        if !path_empty(&detected_socket_path) && path_contains(&detected_socket_path, "containerd") {
50            debug!(socket_path = %detected_socket_path.to_string_lossy(), "Detected containerd socket path.");
51            Some(detected_socket_path)
52        } else {
53            None
54        }
55    }
56
57    /// Returns `None` because containerd Unix socket detection isn't supported on this platform.
58    #[cfg(not(unix))]
59    pub fn detect_grpc_socket_path(_: &GenericConfiguration) -> Option<PathBuf> {
60        None
61    }
62}