Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new(userspace/libsinsp): support multiple CRI engines simultaneously #2141

Merged
merged 3 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
*/

#include <algorithm>
#include <vector>

#if !defined(MINIMAL_BUILD) && !defined(__EMSCRIPTEN__)
#include <libsinsp/container_engine/cri.h>
Expand Down Expand Up @@ -551,53 +552,66 @@
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();
leogr marked this conversation as resolved.
Show resolved Hide resolved

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 @@
}

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);

Check warning on line 633 in userspace/libsinsp/container.cpp

View check run for this annotation

Codecov / codecov/patch

userspace/libsinsp/container.cpp#L633

Added line #L633 was not covered by tests
}
}

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
56 changes: 15 additions & 41 deletions userspace/libsinsp/container_engine/cri.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<libsinsp::cri::cri_interface_v1>(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<libsinsp::cri::cri_interface_v1>(unix_socket_path);
if(!m_cri_v1->is_ok()) {
m_cri_v1.reset(nullptr);
} else {
return;
}

m_cri_v1alpha2 = std::make_unique<libsinsp::cri::cri_interface_v1alpha2>(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<libsinsp::cri::cri_interface_v1alpha2>(unix_socket_path);
if(!m_cri_v1alpha2->is_ok()) {
m_cri_v1alpha2.reset(nullptr);
}
}

Expand Down
2 changes: 1 addition & 1 deletion userspace/libsinsp/container_engine/cri.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class cri_async_source : public container_async_source<libsinsp::cgroup_limits::

class cri : public container_engine_base {
public:
cri(container_cache_interface& cache);
cri(container_cache_interface& cache, const std::string& cri_path);
bool resolve(sinsp_threadinfo* tinfo, bool query_os_for_missing_info) override;
void update_with_size(const std::string& container_id) override;
void cleanup() override;
Expand Down
5 changes: 0 additions & 5 deletions userspace/libsinsp/cri.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,6 @@ class cri_settings {
get().m_cri_runtime_type = v;
}

static const std::string &get_cri_unix_socket_path() { return get().m_cri_unix_socket_path; }

static void set_cri_unix_socket_path(const std::string &v) { get().m_cri_unix_socket_path = v; }

static const bool &get_cri_extra_queries() { return get().m_cri_extra_queries; }

static void set_cri_extra_queries(const bool &v) { get().m_cri_extra_queries = v; }
Expand All @@ -93,7 +89,6 @@ class cri_settings {
int64_t m_cri_timeout;
int64_t m_cri_size_timeout;
sinsp_container_type m_cri_runtime_type;
std::string m_cri_unix_socket_path;
bool m_cri_extra_queries;
};

Expand Down
1 change: 0 additions & 1 deletion userspace/libsinsp/cri_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ cri_settings::cri_settings():
m_cri_timeout(1000),
m_cri_size_timeout(10000),
m_cri_runtime_type(CT_CRI),
m_cri_unix_socket_path(),
m_cri_extra_queries(true) {}

cri_settings::~cri_settings() {}
Expand Down
Loading