Skip to content

Commit

Permalink
Add changes missing after splitting the branch
Browse files Browse the repository at this point in the history
  • Loading branch information
Molter73 committed Apr 1, 2024
1 parent b623e54 commit 604e236
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 25 deletions.
35 changes: 13 additions & 22 deletions collector/lib/K8s.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef _K8S_H_
#define _K8S_H_
#ifndef _CONTAINER_METADATA_H_
#define _CONTAINER_METADATA_H_

#include <sstream>
#include <string_view>
Expand All @@ -8,41 +8,32 @@

namespace collector {

class K8s {
class ContainerMetadata {
public:
K8s(sinsp* inspector) : inspector_(inspector) {
ContainerMetadata(sinsp* inspector) : inspector_(inspector) {
event_extractor_.Init(inspector);
}

inline std::string_view GetNamespace(sinsp_evt* event) {
inline std::string GetNamespace(sinsp_evt* event) {
const char* ns = event_extractor_.get_k8s_namespace(event);
return ns != nullptr ? ns : "";
}

inline std::string_view GetNamespace(const std::string& container_id) {
inline std::string GetNamespace(const std::string& container_id) {
return GetContainerLabel(container_id, "io.kubernetes.pod.namespace");
}

std::string GetContainerLabels(const std::string& container_id) {
const auto container = inspector_->m_container_manager.get_container(container_id);
if (container == nullptr) {
inline std::string GetContainerLabel(const std::string& container_id, const std::string& label) {
const auto& containers = *inspector_->m_container_manager.get_containers();
if (containers.count(container_id) == 0) {
return "";
}

std::stringstream ss;

for (const auto& [key, value] : container->m_labels) {
ss << key << ":" << value << ",";
}

return ss.str();
}

inline std::string_view GetContainerLabel(const std::string& container_id, const std::string& label) {
const auto container = inspector_->m_container_manager.get_container(container_id);
if (container == nullptr || container->m_labels.count(label) == 0) {
const auto& container = containers.at(container_id);
if (container->m_labels.count(label) == 0) {
return "";
}

return container->m_labels.at(label);
}

Expand All @@ -53,4 +44,4 @@ class K8s {

} // namespace collector

#endif // _K8S_H_
#endif // _CONTAINER_METADATA_H_
2 changes: 1 addition & 1 deletion collector/lib/ProcessSignalFormatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ ProcessSignal* ProcessSignalFormatter::CreateProcessSignal(sinsp_evt* event) {
}

CLOG(DEBUG) << "Process (" << signal->container_id() << ": " << signal->pid() << "): "
<< signal->name() << "[" << k8s_.GetNamespace(event) << "] "
<< signal->name() << "[" << container_metadata_.GetNamespace(event) << "] "
<< " (" << signal->exec_file_path() << ")"
<< " " << signal->args();

Expand Down
4 changes: 2 additions & 2 deletions collector/lib/ProcessSignalFormatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace collector {

class ProcessSignalFormatter : public ProtoSignalFormatter<sensor::SignalStreamMessage> {
public:
ProcessSignalFormatter(sinsp* inspector) : event_names_(EventNames::GetInstance()), k8s_(inspector) {
ProcessSignalFormatter(sinsp* inspector) : event_names_(EventNames::GetInstance()), container_metadata_(inspector) {
event_extractor_.Init(inspector);
}

Expand All @@ -42,7 +42,7 @@ class ProcessSignalFormatter : public ProtoSignalFormatter<sensor::SignalStreamM

const EventNames& event_names_;
system_inspector::EventExtractor event_extractor_;
K8s k8s_;
ContainerMetadata container_metadata_;
};

} // namespace collector
Expand Down

0 comments on commit 604e236

Please sign in to comment.