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 improvements #712

Merged
merged 15 commits into from
Feb 21, 2017
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 1 addition & 1 deletion userspace/libsinsp/k8s_api_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ k8s_api_handler::k8s_api_handler(collector_ptr_t collector,
):
k8s_handler("k8s_api_handler", false,
#ifdef HAS_CAPTURE
url, path, filter, ".", collector, http_version, 1000L, ssl, bt,
url, path, filter, ".", "", collector, http_version, 1000L, ssl, bt,
false, true, std::make_shared<k8s_dummy_handler>(), blocking_socket,
#endif // HAS_CAPTURE
~0, nullptr)
Expand Down
12 changes: 7 additions & 5 deletions userspace/libsinsp/k8s_component.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ class k8s_component
COMPONENT_MODIFIED,
COMPONENT_DELETED,
COMPONENT_ERROR,
COMPONENT_NONEXISTENT,
COMPONENT_UNKNOWN // only to mark bad event messages
};

Expand All @@ -132,12 +133,13 @@ class k8s_component
{
switch(m_reason)
{
case COMPONENT_ADDED: return "ADDED";
case COMPONENT_MODIFIED: return "MODIFIED";
case COMPONENT_DELETED: return "DELETED";
case COMPONENT_ERROR: return "ERROR";
case COMPONENT_ADDED: return "ADDED";
case COMPONENT_MODIFIED: return "MODIFIED";
case COMPONENT_DELETED: return "DELETED";
case COMPONENT_ERROR: return "ERROR";
case COMPONENT_NONEXISTENT: return "NONEXISTENT";
case COMPONENT_UNKNOWN:
default: return "UNKNOWN";
default: return "UNKNOWN";
}
return "UNKNOWN";
}
Expand Down
10 changes: 9 additions & 1 deletion userspace/libsinsp/k8s_daemonset_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ std::string k8s_daemonset_handler::STATE_FILTER =
" ]"
"}";

std::string k8s_daemonset_handler::NULL_FILTER =
"{"
" type: \"NONEXISTENT\","
" apiVersion: .apiVersion,"
" kind: \"DaemonSet\", "
" items: [ null ]"
"}";

k8s_daemonset_handler::k8s_daemonset_handler(k8s_state_t& state
#ifdef HAS_CAPTURE
,ptr_t dependency_handler
Expand All @@ -67,7 +75,7 @@ k8s_daemonset_handler::k8s_daemonset_handler(k8s_state_t& state
#ifdef HAS_CAPTURE
url,
"/apis/extensions/v1beta1/daemonsets",
STATE_FILTER, EVENT_FILTER, collector,
STATE_FILTER, EVENT_FILTER, NULL_FILTER, collector,
http_version, 1000L, ssl, bt, true,
connect, dependency_handler, blocking_socket,
#endif // HAS_CAPTURE
Expand Down
1 change: 1 addition & 0 deletions userspace/libsinsp/k8s_daemonset_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class k8s_daemonset_handler : public k8s_handler
private:
static std::string EVENT_FILTER;
static std::string STATE_FILTER;
static std::string NULL_FILTER;

virtual bool handle_component(const Json::Value& json, const msg_data* data = 0);
};
10 changes: 9 additions & 1 deletion userspace/libsinsp/k8s_deployment_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ std::string k8s_deployment_handler::STATE_FILTER =
" ]"
"}";

std::string k8s_deployment_handler::NULL_FILTER =
"{"
" type: \"NONEXISTENT\","
" apiVersion: .apiVersion,"
" kind: \"Deployment\", "
" items: [ null ]"
"}";

k8s_deployment_handler::k8s_deployment_handler(k8s_state_t& state
#ifdef HAS_CAPTURE
,ptr_t dependency_handler
Expand All @@ -66,7 +74,7 @@ k8s_deployment_handler::k8s_deployment_handler(k8s_state_t& state
k8s_handler("k8s_deployment_handler", true,
#ifdef HAS_CAPTURE
url, "/apis/extensions/v1beta1/deployments",
STATE_FILTER, EVENT_FILTER, collector,
STATE_FILTER, EVENT_FILTER, NULL_FILTER, collector,
http_version, 1000L, ssl, bt, true,
connect, dependency_handler, blocking_socket,
#endif // HAS_CAPTURE
Expand Down
1 change: 1 addition & 0 deletions userspace/libsinsp/k8s_deployment_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class k8s_deployment_handler : public k8s_handler
private:
static std::string EVENT_FILTER;
static std::string STATE_FILTER;
static std::string NULL_FILTER;

virtual bool handle_component(const Json::Value& json, const msg_data* data = 0);
};
3 changes: 2 additions & 1 deletion userspace/libsinsp/k8s_event_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ k8s_event_handler::k8s_event_handler(k8s_state_t& state
k8s_handler("k8s_event_handler", true,
#ifdef HAS_CAPTURE
url, "/api/v1/events",
STATE_FILTER, EVENT_FILTER, collector,
STATE_FILTER, EVENT_FILTER, "", collector,
http_version, 1000L, ssl, bt, true,
connect, dependency_handler, blocking_socket,
#endif // HAS_CAPTURE
Expand Down Expand Up @@ -242,6 +242,7 @@ void k8s_event_handler::handle_json(Json::Value&& root)
if(data.m_reason != k8s_component::COMPONENT_ADDED &&
data.m_reason != k8s_component::COMPONENT_MODIFIED &&
data.m_reason != k8s_component::COMPONENT_DELETED &&
data.m_reason != k8s_component::COMPONENT_NONEXISTENT &&
data.m_reason != k8s_component::COMPONENT_ERROR)
{
g_logger.log(std::string("Unsupported K8S " + name() + " event reason: ") +
Expand Down
14 changes: 12 additions & 2 deletions userspace/libsinsp/k8s_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ k8s_handler::k8s_handler(const std::string& id,
const std::string& path,
const std::string& state_filter,
const std::string& event_filter,
const std::string& null_filter,
collector_ptr_t collector,
const std::string& http_version,
int timeout_ms,
Expand All @@ -52,6 +53,7 @@ k8s_handler::k8s_handler(const std::string& id,
m_path(path),
m_state_filter(state_filter),
m_event_filter(event_filter),
m_null_filter(null_filter),
m_filter(&m_state_filter),
m_timeout_ms(timeout_ms),
m_url(url),
Expand All @@ -78,6 +80,10 @@ k8s_handler::k8s_handler(const std::string& id,
m_timeout_ms, m_ssl, m_bt, !m_blocking_socket, m_blocking_socket);
m_handler->set_json_callback(&k8s_handler::set_event_json);
m_handler->add_json_filter(*m_filter);
if(!m_null_filter.empty())
{
m_handler->add_json_filter(m_null_filter);
}
m_handler->add_json_filter(ERROR_FILTER);
m_handler->close_on_chunked_end(false);
m_handler->set_check_chunked(false);
Expand Down Expand Up @@ -417,6 +423,7 @@ k8s_handler::msg_data k8s_handler::get_msg_data(const std::string& type, const s
if(type[0] == 'A') { data.m_reason = k8s_component::COMPONENT_ADDED; }
else if(type[0] == 'M') { data.m_reason = k8s_component::COMPONENT_MODIFIED; }
else if(type[0] == 'D') { data.m_reason = k8s_component::COMPONENT_DELETED; }
else if(type[0] == 'N') { data.m_reason = k8s_component::COMPONENT_NONEXISTENT; }
else if(type[0] == 'E') { data.m_reason = k8s_component::COMPONENT_ERROR; }
}
else
Expand Down Expand Up @@ -545,8 +552,11 @@ void k8s_handler::handle_json(Json::Value&& root)
}
else
{
g_logger.log(std::string("Unsupported K8S " + name() + " event reason: ") +
std::to_string(data.m_reason), sinsp_logger::SEV_ERROR);
if(data.m_reason != k8s_component::COMPONENT_NONEXISTENT)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should log at debug for COMPONENT_NONEXISTENT and keep error for the rest (should only be COMPONENT_UNKNOWN. If we're trying to debug a customer issue where something is empty but shouldn't be, e.g replicasets not showing up, we're going to silently swallow events.

{
g_logger.log(std::string("Unsupported K8S " + name() + " event reason: ") +
std::to_string(data.m_reason), sinsp_logger::SEV_ERROR);
}
continue;
}
/*if(g_logger.get_severity() >= sinsp_logger::SEV_TRACE)
Expand Down
4 changes: 3 additions & 1 deletion userspace/libsinsp/k8s_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class k8s_handler
const std::string& path,
const std::string& state_filter,
const std::string& event_filter,
const std::string& null_filter,
collector_ptr_t collector = nullptr,
const std::string& http_version = "1.1",
int timeout_ms = default_timeout_ms,
Expand Down Expand Up @@ -141,6 +142,7 @@ class k8s_handler
std::string m_path;
std::string m_state_filter;
std::string m_event_filter;
std::string m_null_filter;
std::string* m_filter;
std::string m_event_uri;
long m_timeout_ms;
Expand Down Expand Up @@ -279,7 +281,7 @@ class k8s_dummy_handler : public k8s_handler
public:
k8s_dummy_handler(): k8s_handler("k8s_dummy_handler", false,
#ifdef HAS_CAPTURE
"", "", "", "", nullptr,
"", "", "", "", "", nullptr,
"", 0, nullptr, nullptr,
false, false, nullptr, false,
#endif // HAS_CAPTURE
Expand Down
2 changes: 1 addition & 1 deletion userspace/libsinsp/k8s_namespace_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ k8s_namespace_handler::k8s_namespace_handler(k8s_state_t& state
k8s_handler("k8s_namespace_handler", true,
#ifdef HAS_CAPTURE
url, "/api/v1/namespaces",
STATE_FILTER, EVENT_FILTER, collector,
STATE_FILTER, EVENT_FILTER, "", collector,
http_version, 1000L, ssl, bt, true,
connect, dependency_handler, blocking_socket,
#endif // HAS_CAPTURE
Expand Down
2 changes: 1 addition & 1 deletion userspace/libsinsp/k8s_node_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ k8s_node_handler::k8s_node_handler(k8s_state_t& state
k8s_handler("k8s_node_handler", true,
#ifdef HAS_CAPTURE
url, "/api/v1/nodes",
STATE_FILTER, EVENT_FILTER, collector,
STATE_FILTER, EVENT_FILTER, "", collector,
http_version, 1000L, ssl, bt, true,
connect, dependency_handler, blocking_socket,
#endif // HAS_CAPTURE
Expand Down
2 changes: 1 addition & 1 deletion userspace/libsinsp/k8s_pod_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ k8s_pod_handler::k8s_pod_handler(k8s_state_t& state
k8s_handler("k8s_pod_handler", true,
#ifdef HAS_CAPTURE
url, "/api/v1/pods?fieldSelector=status.phase%3DRunning",
STATE_FILTER, EVENT_FILTER, collector,
STATE_FILTER, EVENT_FILTER, "", collector,
http_version, 1000L, ssl, bt, true,
connect, dependency_handler, blocking_socket,
#endif // HAS_CAPTURE
Expand Down
10 changes: 9 additions & 1 deletion userspace/libsinsp/k8s_replicaset_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ std::string k8s_replicaset_handler::STATE_FILTER =
" ]"
"}";

std::string k8s_replicaset_handler::NULL_FILTER =
"{"
" type: \"NONEXISTENT\","
" apiVersion: .apiVersion,"
" kind: \"ReplicaSet\", "
" items: [ null ]"
"}";

k8s_replicaset_handler::k8s_replicaset_handler(k8s_state_t& state
#ifdef HAS_CAPTURE
,ptr_t dependency_handler
Expand All @@ -66,7 +74,7 @@ k8s_replicaset_handler::k8s_replicaset_handler(k8s_state_t& state
k8s_handler("k8s_replicaset_handler", true,
#ifdef HAS_CAPTURE
url, "/apis/extensions/v1beta1/replicasets",
STATE_FILTER, EVENT_FILTER, collector,
STATE_FILTER, EVENT_FILTER, NULL_FILTER, collector,
http_version, 1000L, ssl, bt, true,
connect, dependency_handler, blocking_socket,
#endif // HAS_CAPTURE
Expand Down
1 change: 1 addition & 0 deletions userspace/libsinsp/k8s_replicaset_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class k8s_replicaset_handler : public k8s_handler
private:
static std::string EVENT_FILTER;
static std::string STATE_FILTER;
static std::string NULL_FILTER;

virtual bool handle_component(const Json::Value& json, const msg_data* data = 0);
};
10 changes: 9 additions & 1 deletion userspace/libsinsp/k8s_replicationcontroller_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ std::string k8s_replicationcontroller_handler::STATE_FILTER =
" ]"
"}";

std::string k8s_replicationcontroller_handler::NULL_FILTER =
"{"
" type: \"NONEXISTENT\","
" apiVersion: .apiVersion,"
" kind: \"ReplicationController\", "
" items: [ null ]"
"}";

k8s_replicationcontroller_handler::k8s_replicationcontroller_handler(k8s_state_t& state
#ifdef HAS_CAPTURE
,ptr_t dependency_handler
Expand All @@ -66,7 +74,7 @@ k8s_replicationcontroller_handler::k8s_replicationcontroller_handler(k8s_state_t
k8s_handler("k8s_replicationcontroller_handler", true,
#ifdef HAS_CAPTURE
url, "/api/v1/replicationcontrollers",
STATE_FILTER, EVENT_FILTER, collector,
STATE_FILTER, EVENT_FILTER, NULL_FILTER, collector,
http_version, 1000L, ssl, bt, true,
connect, dependency_handler, blocking_socket,
#endif // HAS_CAPTURE
Expand Down
1 change: 1 addition & 0 deletions userspace/libsinsp/k8s_replicationcontroller_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class k8s_replicationcontroller_handler : public k8s_handler
private:
static std::string EVENT_FILTER;
static std::string STATE_FILTER;
static std::string NULL_FILTER;

virtual bool handle_component(const Json::Value& json, const msg_data* data = 0);
};
10 changes: 9 additions & 1 deletion userspace/libsinsp/k8s_service_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ std::string k8s_service_handler::STATE_FILTER =
" ]"
"}";

std::string k8s_service_handler::NULL_FILTER =
"{"
" type: \"NONEXISTENT\","
" apiVersion: .apiVersion,"
" kind: \"Service\", "
" items: [ null ]"
"}";

k8s_service_handler::k8s_service_handler(k8s_state_t& state
#ifdef HAS_CAPTURE
,ptr_t dependency_handler
Expand All @@ -66,7 +74,7 @@ k8s_service_handler::k8s_service_handler(k8s_state_t& state
k8s_handler("k8s_service_handler", true,
#ifdef HAS_CAPTURE
url, "/api/v1/services",
STATE_FILTER, EVENT_FILTER, collector,
STATE_FILTER, EVENT_FILTER, NULL_FILTER, collector,
http_version, 1000L, ssl, bt, true,
connect, dependency_handler, blocking_socket,
#endif // HAS_CAPTURE
Expand Down
1 change: 1 addition & 0 deletions userspace/libsinsp/k8s_service_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class k8s_service_handler : public k8s_handler
private:
static std::string EVENT_FILTER;
static std::string STATE_FILTER;
static std::string NULL_FILTER;

virtual bool handle_component(const Json::Value& json, const msg_data* data = 0);
};
16 changes: 8 additions & 8 deletions userspace/libsinsp/socket_collector.h
Original file line number Diff line number Diff line change
Expand Up @@ -358,14 +358,14 @@ class socket_collector
return it;
}

socket_map_t m_sockets;
fd_set m_infd;
fd_set m_errfd;
int m_nfds = 0;
bool m_loop = false;
long m_timeout_ms;
bool m_stopped = false;
bool m_steady_state = false;
socket_map_t m_sockets;
fd_set m_infd;
fd_set m_errfd;
int m_nfds = 0;
bool m_loop = false;
long m_timeout_ms;
bool m_stopped = false;
bool m_steady_state = false;
};

#endif // HAS_CAPTURE
Loading