Skip to content

Commit

Permalink
fix(libsinsp/container_engine/containerd): avoid cache confusion betw…
Browse files Browse the repository at this point in the history
…een containerd sockets

Signed-off-by: Roberto Scolaro <[email protected]>
  • Loading branch information
therealbobo committed Jan 15, 2025
1 parent 810b2d6 commit 46d0c28
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
6 changes: 4 additions & 2 deletions userspace/libsinsp/container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,7 @@ void sinsp_container_manager::create_engines() {
m_container_engine_by_type[CT_DOCKER].push_back(docker_engine);
}

size_t engine_index = 0;
if(m_container_engine_mask & ((1 << CT_CRI) | (1 << CT_CRIO) | (1 << CT_CONTAINERD))) {
// Get CRI socket paths from settings
libsinsp::cri::cri_settings& cri_settings = libsinsp::cri::cri_settings::get();
Expand All @@ -582,13 +583,13 @@ void sinsp_container_manager::create_engines() {

const auto& cri_socket_paths = cri_settings.get_cri_unix_socket_paths();

size_t engine_index = 0;
for(auto socket_path : cri_socket_paths) {
auto cri_engine =
std::make_shared<container_engine::cri>(*this, socket_path, engine_index);
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);
engine_index++;
}
}
Expand Down Expand Up @@ -618,7 +619,8 @@ void sinsp_container_manager::create_engines() {
m_container_engine_by_type[CT_BPM].push_back(bpm_engine);
}
if(m_container_engine_mask & (1 << CT_CONTAINERD)) {
auto containerd_engine = std::make_shared<container_engine::containerd>(*this);
auto containerd_engine =
std::make_shared<container_engine::containerd>(*this, engine_index);
m_container_engines.push_back(containerd_engine);
m_container_engine_by_type[CT_CONTAINERD].push_back(containerd_engine);
}
Expand Down
29 changes: 17 additions & 12 deletions userspace/libsinsp/container_engine/containerd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ constexpr const std::string_view CONTAINERD_SOCKETS[] = {
};

bool containerd_async_source::is_ok() {
return m_container_stub != nullptr && m_image_stub != nullptr;
return m_container_stub && m_image_stub;
}

static inline void setup_grpc_client_context(grpc::ClientContext &context) {
Expand Down Expand Up @@ -144,8 +144,11 @@ grpc::Status containerd_async_source::get_image_resp(
return m_image_stub->Get(&context, req, &resp);
}

libsinsp::container_engine::containerd::containerd(container_cache_interface &cache):
libsinsp::container_engine::containerd::containerd(container_cache_interface &cache,
size_t engine_index):
container_engine_base(cache) {
m_engine_index = engine_index;

for(const auto &p : CONTAINERD_SOCKETS) {
if(p.empty()) {
continue;
Expand All @@ -158,11 +161,8 @@ libsinsp::container_engine::containerd::containerd(container_cache_interface &ca
}

container_cache_interface *cache_interface = &container_cache();
auto src = new containerd_async_source(socket_path,
containerd_async_source::NO_WAIT_LOOKUP,
10000,
cache_interface);
m_containerd_info_source.reset(src);
m_containerd_info_source =

Check warning on line 164 in userspace/libsinsp/container_engine/containerd.cpp

View check run for this annotation

Codecov / codecov/patch

userspace/libsinsp/container_engine/containerd.cpp#L164

Added line #L164 was not covered by tests
std::make_unique<containerd_async_source>(socket_path, 0, 10000, cache_interface);
if(!m_containerd_info_source->is_ok()) {
m_containerd_info_source.reset(nullptr);
continue;
Expand All @@ -172,6 +172,10 @@ libsinsp::container_engine::containerd::containerd(container_cache_interface &ca

bool containerd_async_source::parse(const containerd_lookup_request &request,
sinsp_container_info &container) {
if(!is_ok()) {
return false;
}

auto container_id = request.container_id;

libsinsp_logger()->format(sinsp_logger::SEV_DEBUG,
Expand Down Expand Up @@ -294,12 +298,13 @@ void libsinsp::container_engine::containerd::parse_containerd(
libsinsp_logger()->format(sinsp_logger::SEV_DEBUG,
"containerd_async (%s): Starting asynchronous lookup",
request.container_id.c_str());
done = m_containerd_info_source->lookup(request, result);
done = m_containerd_info_source && m_containerd_info_source->lookup(request, result);
} else {
libsinsp_logger()->format(sinsp_logger::SEV_DEBUG,
"containerd_async (%s): Starting synchronous lookup",
request.container_id.c_str());
done = m_containerd_info_source->lookup_sync(request, result);

done = m_containerd_info_source && m_containerd_info_source->lookup_sync(request, result);
}
if(done) {
// if a previous lookup call already found the metadata, process it now
Expand Down Expand Up @@ -339,7 +344,7 @@ bool libsinsp::container_engine::containerd::resolve(sinsp_threadinfo *tinfo,
return true;
}

if(cache->should_lookup(request.container_id, request.container_type)) {
if(cache->should_lookup(request.container_id, request.container_type, m_engine_index)) {
libsinsp_logger()->format(sinsp_logger::SEV_DEBUG,
"containerd_async (%s): No existing container info",
request.container_id.c_str());
Expand All @@ -348,7 +353,7 @@ bool libsinsp::container_engine::containerd::resolve(sinsp_threadinfo *tinfo,
cache->set_lookup_status(request.container_id,
request.container_type,
sinsp_container_lookup::state::STARTED,
0);
m_engine_index);
parse_containerd(request, cache);
}
return false;
Expand All @@ -375,7 +380,7 @@ bool libsinsp::container_engine::containerd::resolve(sinsp_threadinfo *tinfo,
container.m_cpu_period = limits.m_cpu_period;
container.m_cpuset_cpu_count = limits.m_cpuset_cpu_count;

if(container_cache().should_lookup(container.m_id, CT_CONTAINERD)) {
if(container_cache().should_lookup(container.m_id, CT_CONTAINERD, m_engine_index)) {
container.m_name = container.m_id;
container.set_lookup_status(sinsp_container_lookup::state::SUCCESSFUL);
container_cache().add_container(std::make_shared<sinsp_container_info>(container), tinfo);
Expand Down
3 changes: 2 additions & 1 deletion userspace/libsinsp/container_engine/containerd.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,15 @@ class containerd_async_source : public container_async_source<containerd_lookup_

class containerd : public container_engine_base {
public:
containerd(container_cache_interface& cache);
containerd(container_cache_interface& cache, size_t engine_index);

void parse_containerd(const containerd_lookup_request& request,
container_cache_interface* cache);
bool resolve(sinsp_threadinfo* tinfo, bool query_os_for_missing_info) override;

private:
std::unique_ptr<containerd_async_source> m_containerd_info_source;
size_t m_engine_index;
};

} // namespace container_engine
Expand Down

0 comments on commit 46d0c28

Please sign in to comment.