Skip to content

Commit

Permalink
update(userspace/libsinsp): sinsp_container_manager can now handle mu…
Browse files Browse the repository at this point in the history
…ltiple CRI engines simultaneously

Signed-off-by: Leonardo Grasso <[email protected]>
  • Loading branch information
leogr committed Nov 5, 2024
1 parent 044fc33 commit 202feef
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
44 changes: 30 additions & 14 deletions userspace/libsinsp/container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
*/

#include <algorithm>
#include <vector>

#if !defined(MINIMAL_BUILD) && !defined(__EMSCRIPTEN__)
#include <libsinsp/container_engine/cri.h>
Expand Down Expand Up @@ -551,53 +552,66 @@ 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__)
#ifndef _WIN32
if(m_container_engine_mask & (1 << CT_PODMAN)) {
auto podman_engine = std::make_shared<container_engine::podman>(*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<container_engine::docker_linux>(*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<container_engine::cri>(*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<container_engine::cri>(*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<container_engine::lxc>(*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<container_engine::libvirt_lxc>(*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<container_engine::mesos>(*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<container_engine::rkt>(*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<container_engine::bpm>(*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
Expand All @@ -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() {
Expand Down
5 changes: 4 additions & 1 deletion userspace/libsinsp/container.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ limitations under the License.
#include <functional>
#include <memory>
#include <unordered_map>
#include <vector>

#include <libscap/scap.h>

Expand Down Expand Up @@ -254,8 +255,10 @@ class sinsp_container_manager : public libsinsp::container_engine::container_cac

std::list<std::shared_ptr<libsinsp::container_engine::container_engine_base>>
m_container_engines;

// Map container types to vectors of engines
std::map<sinsp_container_type,
std::shared_ptr<libsinsp::container_engine::container_engine_base>>
std::vector<std::shared_ptr<libsinsp::container_engine::container_engine_base>>>
m_container_engine_by_type;

sinsp* m_inspector;
Expand Down

0 comments on commit 202feef

Please sign in to comment.