saluki_env/features/
containerd.rs

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