Skip to content

Commit

Permalink
refactor(userspace/libsinsp): make container_engine::cri handle only …
Browse files Browse the repository at this point in the history
…one CRI path

Signed-off-by: Leonardo Grasso <[email protected]>
  • Loading branch information
leogr authored and poiana committed Dec 20, 2024
1 parent 5b95bc8 commit 3c4a820
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 42 deletions.
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

0 comments on commit 3c4a820

Please sign in to comment.