Skip to content

Commit

Permalink
dockershim fallback for containerd (#630)
Browse files Browse the repository at this point in the history
Co-authored-by: Aviram Hassan <[email protected]>
  • Loading branch information
aviramha and aviramha committed Oct 25, 2022
1 parent 73086e7 commit 737b482
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 14 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how
- Added timeout for "waiting for pod to be ready..." in mirrord-layer to prevent unresponsive behavior. See [#579](https://github.com/metalbear-co/mirrord/issues/579)
- IntelliJ Extension: Default log level to `ERROR` from `DEBUG`

### Fixed
- Issue with [bottlerocket](https://github.com/bottlerocket-os/bottlerocket) where they use `/run/dockershim.sock` instead of the default containerd path. Add new path as fallback.

## 3.2.0

### Changed
Expand Down
7 changes: 6 additions & 1 deletion mirrord-agent/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use tracing::debug;
use crate::error::AgentError;

const CONTAINERD_SOCK_PATH: &str = "/run/containerd/containerd.sock";
const CONTAINERD_ALTERNATIVE_SOCK_PATH: &str = "/run/dockershim.sock";

const DEFAULT_CONTAINERD_NAMESPACE: &str = "k8s.io";

pub async fn get_container_pid(
Expand Down Expand Up @@ -49,7 +51,10 @@ async fn get_docker_container_pid(container_id: String) -> Result<u64, AgentErro
}

async fn get_containerd_container_pid(container_id: String) -> Result<u64, AgentError> {
let channel = connect(CONTAINERD_SOCK_PATH).await?;
let channel = match connect(CONTAINERD_SOCK_PATH).await {
Ok(channel) => channel,
Err(_) => connect(CONTAINERD_ALTERNATIVE_SOCK_PATH).await?,
};
let mut client = TasksClient::new(channel);
let request = GetRequest {
container_id,
Expand Down
44 changes: 31 additions & 13 deletions mirrord-layer/src/pod_api.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::HashSet, str::FromStr};
use std::{collections::HashSet, fmt::Display, str::FromStr};

use async_trait::async_trait;
use futures::{StreamExt, TryStreamExt};
Expand Down Expand Up @@ -265,7 +265,7 @@ impl KubernetesAPI {
"--container-id".to_string(),
runtime_data.container_id,
"--container-runtime".to_string(),
runtime_data.container_runtime,
runtime_data.container_runtime.to_string(),
"-l".to_string(),
connection_port.to_string(),
];
Expand Down Expand Up @@ -307,7 +307,7 @@ impl KubernetesAPI {
{
"name": "sockpath",
"hostPath": {
"path": runtime_data.socket_path
"path": runtime_data.container_runtime.mount_path()
}
}
],
Expand All @@ -321,7 +321,7 @@ impl KubernetesAPI {
},
"volumeMounts": [
{
"mountPath": runtime_data.socket_path,
"mountPath": runtime_data.container_runtime.mount_path(),
"name": "sockpath"
}
],
Expand Down Expand Up @@ -476,13 +476,35 @@ async fn wait_for_agent_startup(
Ok(())
}

pub(crate) enum ContainerRuntime {
Docker,
Containerd,
}

impl ContainerRuntime {
pub(crate) fn mount_path(&self) -> String {
match self {
ContainerRuntime::Docker => "/var/run/docker.sock".to_string(),
ContainerRuntime::Containerd => "/run/".to_string(),
}
}
}

impl Display for ContainerRuntime {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ContainerRuntime::Docker => write!(f, "docker"),
ContainerRuntime::Containerd => write!(f, "containerd"),
}
}
}

pub(crate) struct RuntimeData {
pod_name: String,
node_name: String,
container_id: String,
container_runtime: String,
container_runtime: ContainerRuntime,
container_name: String,
socket_path: String,
}

impl RuntimeData {
Expand Down Expand Up @@ -524,9 +546,9 @@ impl RuntimeData {

let mut split = container_id_full.split("://");

let (container_runtime, socket_path) = match split.next() {
Some("docker") => Ok(("docker", "/var/run/docker.sock")),
Some("containerd") => Ok(("containerd", "/run/containerd/containerd.sock")),
let container_runtime = match split.next() {
Some("docker") => Ok(ContainerRuntime::Docker),
Some("containerd") => Ok(ContainerRuntime::Containerd),
_ => Err(LayerError::ContainerRuntimeParseError(
container_id_full.to_string(),
)),
Expand All @@ -537,16 +559,12 @@ impl RuntimeData {
.ok_or_else(|| LayerError::ContainerRuntimeParseError(container_id_full.to_string()))?
.to_owned();

let container_runtime = container_runtime.to_string();
let socket_path = socket_path.to_string();

Ok(RuntimeData {
pod_name,
node_name,
container_id,
container_runtime,
container_name,
socket_path,
})
}
}
Expand Down

0 comments on commit 737b482

Please sign in to comment.