diff --git a/userspace/libsinsp/container.cpp b/userspace/libsinsp/container.cpp index f6f74a3c09..692f3ac495 100644 --- a/userspace/libsinsp/container.cpp +++ b/userspace/libsinsp/container.cpp @@ -17,6 +17,7 @@ limitations under the License. */ #include +#include #if !defined(MINIMAL_BUILD) && !defined(__EMSCRIPTEN__) #include @@ -551,7 +552,7 @@ void sinsp_container_manager::create_engines() { m_static_name, m_static_image); m_container_engines.push_back(engine); - m_container_engine_by_type[CT_STATIC] = engine; + m_container_engine_by_type[CT_STATIC].push_back(engine); return; } #if !defined(MINIMAL_BUILD) && !defined(__EMSCRIPTEN__) @@ -559,45 +560,58 @@ void sinsp_container_manager::create_engines() { if(m_container_engine_mask & (1 << CT_PODMAN)) { auto podman_engine = std::make_shared(*this); m_container_engines.push_back(podman_engine); - m_container_engine_by_type[CT_PODMAN] = podman_engine; + m_container_engine_by_type[CT_PODMAN].push_back(podman_engine); } if(m_container_engine_mask & (1 << CT_DOCKER)) { auto docker_engine = std::make_shared(*this); m_container_engines.push_back(docker_engine); - m_container_engine_by_type[CT_DOCKER] = docker_engine; + m_container_engine_by_type[CT_DOCKER].push_back(docker_engine); } if(m_container_engine_mask & ((1 << CT_CRI) | (1 << CT_CRIO) | (1 << CT_CONTAINERD))) { - auto cri_engine = std::make_shared(*this); - m_container_engines.push_back(cri_engine); - m_container_engine_by_type[CT_CRI] = cri_engine; - m_container_engine_by_type[CT_CRIO] = cri_engine; - m_container_engine_by_type[CT_CONTAINERD] = cri_engine; + // Get CRI socket paths from settings + libsinsp::cri::cri_settings& cri_settings = libsinsp::cri::cri_settings::get(); + if(cri_settings.get_cri_unix_socket_paths().empty()) { + // Add default paths + cri_settings.add_cri_unix_socket_path("/run/containerd/containerd.sock"); + cri_settings.add_cri_unix_socket_path("/run/crio/crio.sock"); + cri_settings.add_cri_unix_socket_path("/run/k3s/containerd/containerd.sock"); + } + + const auto& cri_socket_paths = cri_settings.get_cri_unix_socket_paths(); + + for(const auto& socket_path : cri_socket_paths) { + auto cri_engine = std::make_shared(*this, socket_path); + m_container_engines.push_back(cri_engine); + m_container_engine_by_type[CT_CRI].push_back(cri_engine); + m_container_engine_by_type[CT_CRIO].push_back(cri_engine); + m_container_engine_by_type[CT_CONTAINERD].push_back(cri_engine); + } } if(m_container_engine_mask & (1 << CT_LXC)) { auto lxc_engine = std::make_shared(*this); m_container_engines.push_back(lxc_engine); - m_container_engine_by_type[CT_LXC] = lxc_engine; + m_container_engine_by_type[CT_LXC].push_back(lxc_engine); } if(m_container_engine_mask & (1 << CT_LIBVIRT_LXC)) { auto libvirt_lxc_engine = std::make_shared(*this); m_container_engines.push_back(libvirt_lxc_engine); - m_container_engine_by_type[CT_LIBVIRT_LXC] = libvirt_lxc_engine; + m_container_engine_by_type[CT_LIBVIRT_LXC].push_back(libvirt_lxc_engine); } if(m_container_engine_mask & (1 << CT_MESOS)) { auto mesos_engine = std::make_shared(*this); m_container_engines.push_back(mesos_engine); - m_container_engine_by_type[CT_MESOS] = mesos_engine; + m_container_engine_by_type[CT_MESOS].push_back(mesos_engine); } if(m_container_engine_mask & (1 << CT_RKT)) { auto rkt_engine = std::make_shared(*this); m_container_engines.push_back(rkt_engine); - m_container_engine_by_type[CT_RKT] = rkt_engine; + m_container_engine_by_type[CT_RKT].push_back(rkt_engine); } if(m_container_engine_mask & (1 << CT_BPM)) { auto bpm_engine = std::make_shared(*this); m_container_engines.push_back(bpm_engine); - m_container_engine_by_type[CT_BPM] = bpm_engine; + m_container_engine_by_type[CT_BPM].push_back(bpm_engine); } #endif // _WIN32 #endif // MINIMAL_BUILD @@ -615,7 +629,9 @@ void sinsp_container_manager::update_container_with_size(sinsp_container_type ty } libsinsp_logger()->format(sinsp_logger::SEV_DEBUG, "Request size for %s", container_id.c_str()); - found->second->update_with_size(container_id); + for(const auto& engine : found->second) { + engine->update_with_size(container_id); + } } void sinsp_container_manager::cleanup() { diff --git a/userspace/libsinsp/container.h b/userspace/libsinsp/container.h index 17305c96d3..4d3e34cd03 100644 --- a/userspace/libsinsp/container.h +++ b/userspace/libsinsp/container.h @@ -21,6 +21,7 @@ limitations under the License. #include #include #include +#include #include @@ -254,8 +255,10 @@ class sinsp_container_manager : public libsinsp::container_engine::container_cac std::list> m_container_engines; + + // Map container types to vectors of engines std::map> + std::vector>> m_container_engine_by_type; sinsp* m_inspector; diff --git a/userspace/libsinsp/container_engine/cri.cpp b/userspace/libsinsp/container_engine/cri.cpp index 6a85d2d398..5d3b49d908 100644 --- a/userspace/libsinsp/container_engine/cri.cpp +++ b/userspace/libsinsp/container_engine/cri.cpp @@ -53,50 +53,24 @@ constexpr const cgroup_layout CRI_CGROUP_LAYOUT[] = { {nullptr, nullptr}}; } // namespace -cri::cri(container_cache_interface &cache): container_engine_base(cache) { - libsinsp::cri::cri_settings &cri_settings = libsinsp::cri::cri_settings::get(); - if(cri_settings.get_cri_unix_socket_paths().empty()) { - // containerd as primary default value when empty - cri_settings.add_cri_unix_socket_path("/run/containerd/containerd.sock"); - // crio-o as secondary default value when empty - cri_settings.add_cri_unix_socket_path("/run/crio/crio.sock"); - // k3s containerd as third option when empty - cri_settings.add_cri_unix_socket_path("/run/k3s/containerd/containerd.sock"); +cri::cri(container_cache_interface &cache, const std::string &cri_path): + container_engine_base(cache) { + auto unix_socket_path = scap_get_host_root() + cri_path; + struct stat s = {}; + if(stat(unix_socket_path.c_str(), &s) != 0 || (s.st_mode & S_IFMT) != S_IFSOCK) { + return; } - // Try all specified unix socket paths - // NOTE: having multiple container runtimes on the same host is a sporadic case, - // so we wouldn't make things complex to support that. - // On the other hand, specifying multiple unix socket paths (and using only the first match) - // will solve the "same config, multiple hosts" use case. - for(auto &p : cri_settings.get_cri_unix_socket_paths()) { - if(p.empty()) { - continue; - } - - auto cri_path = scap_get_host_root() + p; - struct stat s = {}; - if(stat(cri_path.c_str(), &s) != 0 || (s.st_mode & S_IFMT) != S_IFSOCK) { - continue; - } - - m_cri_v1 = std::make_unique(cri_path); - if(!m_cri_v1->is_ok()) { - m_cri_v1.reset(nullptr); - } else { - // Store used unix_socket_path - cri_settings.set_cri_unix_socket_path(p); - break; - } + m_cri_v1 = std::make_unique(unix_socket_path); + if(!m_cri_v1->is_ok()) { + m_cri_v1.reset(nullptr); + } else { + return; + } - m_cri_v1alpha2 = std::make_unique(cri_path); - if(!m_cri_v1alpha2->is_ok()) { - m_cri_v1alpha2.reset(nullptr); - } else { - // Store used unix_socket_path - cri_settings.set_cri_unix_socket_path(p); - break; - } + m_cri_v1alpha2 = std::make_unique(unix_socket_path); + if(!m_cri_v1alpha2->is_ok()) { + m_cri_v1alpha2.reset(nullptr); } } diff --git a/userspace/libsinsp/container_engine/cri.h b/userspace/libsinsp/container_engine/cri.h index e00af883ad..6c37911eb0 100644 --- a/userspace/libsinsp/container_engine/cri.h +++ b/userspace/libsinsp/container_engine/cri.h @@ -78,7 +78,7 @@ class cri_async_source : public container_async_source