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

K8s pods fix #781

Merged
merged 2 commits into from
Mar 8, 2017
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
27 changes: 3 additions & 24 deletions userspace/libsinsp/k8s_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ std::string k8s_handler::ERROR_FILTER =
" ]"
"}";

const std::string k8s_handler::POD_STATE_FILTER = "fieldSelector=status.phase%3DRunning";

k8s_handler::k8s_handler(const std::string& id,
bool is_captured,
#ifdef HAS_CAPTURE
Expand Down Expand Up @@ -337,26 +335,7 @@ void k8s_handler::check_state()
}
else
{
throw sinsp_exception("k8s_handler (" + m_id + "), invalid URL path "
"encountered while rewriting for watch: " + m_path);
}
}
// get rid of pod state filter, not needed for watch
std::string to_remove(POD_STATE_FILTER);
to_remove.append(1, '&');
pos = m_path.find(to_remove);
if(pos != std::string::npos)
{
m_path.erase(pos, to_remove.length());
}
else // currently never, but to shield against bugs in future
{
to_remove = "?";
to_remove.append(POD_STATE_FILTER);
pos = m_path.find(to_remove);
if(pos != std::string::npos)
{
m_path.erase(pos, to_remove.length());
throw sinsp_exception("k8s_handler (" + m_id + "), invalid URL path: " + m_path);
}
}
m_handler->set_socket_option(SOCK_NONBLOCK);
Expand Down Expand Up @@ -575,15 +554,15 @@ void k8s_handler::handle_json(Json::Value&& root)
}
else if(data.m_reason == k8s_component::COMPONENT_DELETED)
{
if(!m_state->has(data.m_uid) && (g_logger.get_severity() >= sinsp_logger::SEV_DEBUG))
if(!m_state->has(data.m_uid))
{
std::ostringstream os;
os << "K8s " + reason_type + " message received by " << m_id <<
#ifdef HAS_CAPTURE
" [" << uri(m_url).to_string(false) << "]"
#endif // HAS_CAPTURE
" for non-existing " << data.m_kind << " [" << data.m_uid << "], giving up.";
g_logger.log(os.str(), sinsp_logger::SEV_DEBUG);
g_logger.log(os.str(), sinsp_logger::SEV_WARNING);
continue;
}
}
Expand Down
2 changes: 0 additions & 2 deletions userspace/libsinsp/k8s_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ class sinsp;
class k8s_handler
{
public:
static const std::string POD_STATE_FILTER;

typedef k8s_component::msg_reason msg_reason;
typedef k8s_component::msg_data msg_data;

Expand Down
78 changes: 28 additions & 50 deletions userspace/libsinsp/k8s_pod_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ k8s_pod_handler::k8s_pod_handler(k8s_state_t& state
):
k8s_handler("k8s_pod_handler", true,
#ifdef HAS_CAPTURE
url, "/api/v1/pods?" + POD_STATE_FILTER,
url, "/api/v1/pods?fieldSelector=status.phase%3DRunning",
STATE_FILTER, EVENT_FILTER, "", collector,
http_version, 1000L, ssl, bt, true,
connect, dependency_handler, blocking_socket,
Expand Down Expand Up @@ -201,71 +201,49 @@ size_t k8s_pod_handler::extract_pod_restart_count(const Json::Value& item)
return restart_count;
}

bool k8s_pod_handler::is_pod_active(const Json::Value& item)
{
const Json::Value& phase = item["phase"];
if(!phase.isNull() && phase.isString())
{
if(phase.asString() == "Running")
{
return true;
}
}
return false;
}

bool k8s_pod_handler::handle_component(const Json::Value& json, const msg_data* data)
{
if(is_pod_active(json))
if(data)
{
if(data)
if(m_state)
{
if(m_state)
if((data->m_reason == k8s_component::COMPONENT_ADDED) ||
(data->m_reason == k8s_component::COMPONENT_MODIFIED))
{
if((data->m_reason == k8s_component::COMPONENT_ADDED) ||
(data->m_reason == k8s_component::COMPONENT_MODIFIED))
{
k8s_pod_t& pod =
m_state->get_component<k8s_pods, k8s_pod_t>(m_state->get_pods(),
data->m_name, data->m_uid, data->m_namespace);
k8s_pair_list entries = k8s_component::extract_object(json, "labels");
if(entries.size() > 0)
{
pod.set_labels(std::move(entries));
}
k8s_pod_t::container_id_list container_ids = extract_pod_container_ids(json);
k8s_container::list containers = extract_pod_containers(json);
extract_pod_data(json, pod);
pod.set_restart_count(extract_pod_restart_count(json));
pod.set_container_ids(std::move(container_ids));
pod.set_containers(std::move(containers));
}
else if(data->m_reason == k8s_component::COMPONENT_DELETED)
k8s_pod_t& pod =
m_state->get_component<k8s_pods, k8s_pod_t>(m_state->get_pods(),
data->m_name, data->m_uid, data->m_namespace);
k8s_pair_list entries = k8s_component::extract_object(json, "labels");
if(entries.size() > 0)
{
if(!m_state->delete_component(m_state->get_pods(), data->m_uid))
{
log_not_found(*data);
return false;
}
pod.set_labels(std::move(entries));
}
k8s_pod_t::container_id_list container_ids = extract_pod_container_ids(json);
k8s_container::list containers = extract_pod_containers(json);
extract_pod_data(json, pod);
pod.set_restart_count(extract_pod_restart_count(json));
pod.set_container_ids(std::move(container_ids));
pod.set_containers(std::move(containers));
}
else if(data->m_reason != k8s_component::COMPONENT_ERROR)
else if(data->m_reason == k8s_component::COMPONENT_DELETED)
{
g_logger.log(std::string("Unsupported K8S " + name() + " event reason: ") +
std::to_string(data->m_reason), sinsp_logger::SEV_ERROR);
return false;
if(!m_state->delete_component(m_state->get_pods(), data->m_uid))
{
log_not_found(*data);
return false;
}
}
}
else
else if(data->m_reason != k8s_component::COMPONENT_ERROR)
{
throw sinsp_exception("K8s node handler: data is null.");
g_logger.log(std::string("Unsupported K8S " + name() + " event reason: ") +
std::to_string(data->m_reason), sinsp_logger::SEV_ERROR);
return false;
}
}
else
{
g_logger.log("Received handling request for non-running pod: " + (data ? data->m_name : std::string()),
sinsp_logger::SEV_WARNING);
return false;
throw sinsp_exception("K8s node handler: data is null.");
}
return true;
}
1 change: 0 additions & 1 deletion userspace/libsinsp/k8s_pod_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class k8s_pod_handler : public k8s_handler

~k8s_pod_handler();

static bool is_pod_active(const Json::Value& item);
static std::vector<std::string> extract_pod_container_ids(const Json::Value& item);
static k8s_container::list extract_pod_containers(const Json::Value& item);
static void extract_pod_data(const Json::Value& item, k8s_pod_t& pod);
Expand Down