Skip to content

Commit

Permalink
merge k8s_deployments branch
Browse files Browse the repository at this point in the history
  • Loading branch information
aleks-f committed Oct 10, 2016
1 parent 6568d6d commit dbf9b5c
Show file tree
Hide file tree
Showing 34 changed files with 300 additions and 146 deletions.
2 changes: 1 addition & 1 deletion userspace/libsinsp/chisel.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class SINSP_PUBLIC sinsp_chisel
uint64_t m_lua_last_interval_sample_time;
uint64_t m_lua_last_interval_ts;
vector<sinsp_filter_check*> m_allocated_fltchecks;
char m_lua_fld_storage[1024];
char m_lua_fld_storage[16384];
chiselinfo* m_lua_cinfo;
string m_new_chisel_to_exec;
int m_udp_socket;
Expand Down
4 changes: 2 additions & 2 deletions userspace/libsinsp/chisel_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ uint32_t lua_cbacks::rawval_to_lua_stack(lua_State *ls, uint8_t* rawval, const f
case PT_BYTEBUF:
if(rawval[len] == 0)
{
lua_pushstring(ls, (char*)rawval);
lua_pushlstring(ls, (char*)rawval, len);
return 1;
}
else
Expand All @@ -128,7 +128,7 @@ uint32_t lua_cbacks::rawval_to_lua_stack(lua_State *ls, uint8_t* rawval, const f

memcpy(ch->m_lua_fld_storage, rawval, max_len);
ch->m_lua_fld_storage[max_len] = 0;
lua_pushstring(ls, (char*)ch->m_lua_fld_storage);
lua_pushlstring(ls, (char*)ch->m_lua_fld_storage, max_len);
return 1;
}
case PT_SOCKADDR:
Expand Down
72 changes: 72 additions & 0 deletions userspace/libsinsp/filterchecks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6317,6 +6317,10 @@ const filtercheck_field_info sinsp_filter_check_k8s_fields[] =
{PT_CHARBUF, EPF_NONE, PF_NA, "k8s.rs.id", "Kubernetes replica set id."},
{PT_CHARBUF, EPF_NONE, PF_NA, "k8s.rs.label", "Kubernetes replica set label. E.g. 'k8s.rs.label.foo'."},
{PT_CHARBUF, EPF_NONE, PF_NA, "k8s.rs.labels", "Kubernetes replica set comma-separated key/value labels. E.g. 'foo1:bar1,foo2:bar2'."},
{PT_CHARBUF, EPF_NONE, PF_NA, "k8s.deployment.name", "Kubernetes deployment name."},
{PT_CHARBUF, EPF_NONE, PF_NA, "k8s.deployment.id", "Kubernetes deployment id."},
{PT_CHARBUF, EPF_NONE, PF_NA, "k8s.deployment.label", "Kubernetes deployment label. E.g. 'k8s.rs.label.foo'."},
{PT_CHARBUF, EPF_NONE, PF_NA, "k8s.deployment.labels", "Kubernetes deployment comma-separated key/value labels. E.g. 'foo1:bar1,foo2:bar2'."},
};

sinsp_filter_check_k8s::sinsp_filter_check_k8s()
Expand Down Expand Up @@ -6376,6 +6380,14 @@ int32_t sinsp_filter_check_k8s::parse_field_name(const char* str, bool alloc_sta

return extract_arg("k8s.ns.label", val);
}
else if(string(val, 0, sizeof("k8s.deployment.label") - 1) == "k8s.deployment.label" &&
string(val, 0, sizeof("k8s.deployment.labels") - 1) != "k8s.deployment.labels")
{
m_field_id = TYPE_K8S_DEPLOYMENT_LABEL;
m_field = &m_info.m_fields[m_field_id];

return extract_arg("k8s.deployment.label", val);
}
else
{
return sinsp_filter_check::parse_field_name(str, alloc_state);
Expand Down Expand Up @@ -6480,6 +6492,20 @@ vector<const k8s_service_t*> sinsp_filter_check_k8s::find_svc_by_pod(const k8s_p
return services;
}

const k8s_deployment_t* sinsp_filter_check_k8s::find_deployment_by_pod(const k8s_pod_t* pod)
{
const k8s_state_t& k8s_state = m_inspector->m_k8s_client->get_state();

const k8s_state_t::pod_deployment_map& pod_deployments = k8s_state.get_pod_deployment_map();
k8s_state_t::pod_deployment_map::const_iterator it = pod_deployments.find(pod->get_uid());
if(it != pod_deployments.end())
{
return it->second;
}

return NULL;
}

void sinsp_filter_check_k8s::concatenate_labels(const k8s_pair_list& labels, string* s)
{
for(const k8s_pair_t& label_pair : labels)
Expand Down Expand Up @@ -6779,6 +6805,52 @@ uint8_t* sinsp_filter_check_k8s::extract(sinsp_evt *evt, OUT uint32_t* len, bool
}
break;
}
case TYPE_K8S_DEPLOYMENT_NAME:
{
const k8s_deployment_t* deployment = find_deployment_by_pod(pod);
if(deployment != NULL)
{
m_tstr = deployment->get_name();
*len = m_tstr.size();
return (uint8_t*) m_tstr.c_str();
}
break;
}
case TYPE_K8S_DEPLOYMENT_ID:
{
const k8s_deployment_t* deployment = find_deployment_by_pod(pod);
if(deployment != NULL)
{
m_tstr = deployment->get_uid();
*len = m_tstr.size();
return (uint8_t*) m_tstr.c_str();
}
break;
}
case TYPE_K8S_DEPLOYMENT_LABEL:
{
const k8s_deployment_t* deployment = find_deployment_by_pod(pod);
if(deployment != NULL)
{
if(find_label(deployment->get_labels(), m_argname, &m_tstr))
{
*len = m_tstr.size();
return (uint8_t*) m_tstr.c_str();
}
}
break;
}
case TYPE_K8S_DEPLOYMENT_LABELS:
{
const k8s_deployment_t* deployment = find_deployment_by_pod(pod);
if(deployment != NULL)
{
concatenate_labels(deployment->get_labels(), &m_tstr);
*len = m_tstr.size();
return (uint8_t*) m_tstr.c_str();
}
break;
}
default:
ASSERT(false);
return NULL;
Expand Down
5 changes: 5 additions & 0 deletions userspace/libsinsp/filterchecks.h
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,10 @@ class sinsp_filter_check_k8s : public sinsp_filter_check
TYPE_K8S_RS_ID,
TYPE_K8S_RS_LABEL,
TYPE_K8S_RS_LABELS,
TYPE_K8S_DEPLOYMENT_NAME,
TYPE_K8S_DEPLOYMENT_ID,
TYPE_K8S_DEPLOYMENT_LABEL,
TYPE_K8S_DEPLOYMENT_LABELS,
};

sinsp_filter_check_k8s();
Expand All @@ -882,6 +886,7 @@ class sinsp_filter_check_k8s : public sinsp_filter_check
const k8s_rc_t* find_rc_by_pod(const k8s_pod_t* pod);
const k8s_rs_t* find_rs_by_pod(const k8s_pod_t* pod);
vector<const k8s_service_t*> find_svc_by_pod(const k8s_pod_t* pod);
const k8s_deployment_t* find_deployment_by_pod(const k8s_pod_t* pod);
void concatenate_labels(const k8s_pair_list& labels, string* s);
bool find_label(const k8s_pair_list& labels, const string& key, string* value);

Expand Down
4 changes: 2 additions & 2 deletions userspace/libsinsp/k8s.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ void k8s::check_components()
{
if(m_net->has_handler(*it))
{
k8s_net::handler_ptr_t handler = m_net->get_handler(*it);
k8s_net::handler_ptr_t handler = k8s_net::get_handler(m_net->handlers(), *it);
if(handler)
{
k8s_handler::api_error_ptr handler_error = handler->error();
Expand Down Expand Up @@ -229,7 +229,7 @@ void k8s::simulate_watch_event(const std::string& json, int version)
{
if(m_handler_map.find(component_type) == m_handler_map.end())
{
m_handler_map[component_type] = k8s_net::get_handler(m_state, component_type, false);
m_handler_map[component_type] = k8s_net::make_handler(m_state, component_type, false);
}
if(m_handler_map[component_type])
{
Expand Down
4 changes: 2 additions & 2 deletions userspace/libsinsp/k8s_api_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ k8s_api_handler::k8s_api_handler(collector_ptr_t collector,
ssl_ptr_t ssl,
bt_ptr_t bt):
k8s_handler("k8s_api_handler", false, url, path,
filter, ".", collector, http_version,
1000L, ssl, bt, nullptr, false)
filter, ".", std::make_shared<k8s_dummy_handler>(),
collector, http_version, 1000L, ssl, bt, nullptr, false)
{
}

Expand Down
13 changes: 13 additions & 0 deletions userspace/libsinsp/k8s_component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,19 @@ k8s_deployment_t::k8s_deployment_t(const std::string& name, const std::string& u
{
}

std::vector<const k8s_pod_t*> k8s_deployment_t::get_selected_pods(const std::vector<k8s_pod_t>& pods) const
{
std::vector<const k8s_pod_t*> pod_vec;
for(const auto& pod : pods)
{
if(selectors_in_labels(pod.get_labels()) && get_namespace() == pod.get_namespace())
{
pod_vec.push_back(&pod);
}
}
return pod_vec;
}

//
// event
//
Expand Down
2 changes: 2 additions & 0 deletions userspace/libsinsp/k8s_component.h
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,8 @@ class k8s_deployment_t : public k8s_component
void set_replicas(const Json::Value& item);
void set_replicas(int desired, int current);

std::vector<const k8s_pod_t*> get_selected_pods(const std::vector<k8s_pod_t>& pods) const;

private:
k8s_replicas_t m_replicas;
};
Expand Down
3 changes: 2 additions & 1 deletion userspace/libsinsp/k8s_daemonset_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ std::string k8s_daemonset_handler::STATE_FILTER =
"}";

k8s_daemonset_handler::k8s_daemonset_handler(k8s_state_t& state,
ptr_t dependency_handler,
collector_ptr_t collector,
std::string url,
const std::string& http_version,
Expand All @@ -60,7 +61,7 @@ k8s_daemonset_handler::k8s_daemonset_handler(k8s_state_t& state,
bool connect):
k8s_handler("k8s_daemonset_handler", true, url,
"/apis/extensions/v1beta1/daemonsets",
STATE_FILTER, EVENT_FILTER, collector,
STATE_FILTER, EVENT_FILTER, dependency_handler, collector,
http_version, 1000L, ssl, bt, &state, true, connect)
{
}
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 @@ -15,6 +15,7 @@ class k8s_daemonset_handler : public k8s_handler
{
public:
k8s_daemonset_handler(k8s_state_t& state,
ptr_t dependency_handler,
collector_ptr_t collector = nullptr,
std::string url = "",
const std::string& http_version = "1.0",
Expand Down
3 changes: 2 additions & 1 deletion userspace/libsinsp/k8s_deployment_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ std::string k8s_deployment_handler::STATE_FILTER =
"}";

k8s_deployment_handler::k8s_deployment_handler(k8s_state_t& state,
ptr_t dependency_handler,
collector_ptr_t collector,
std::string url,
const std::string& http_version,
Expand All @@ -60,7 +61,7 @@ k8s_deployment_handler::k8s_deployment_handler(k8s_state_t& state,
bool connect):
k8s_handler("k8s_deployment_handler", true, url,
"/apis/extensions/v1beta1/deployments",
STATE_FILTER, EVENT_FILTER, collector,
STATE_FILTER, EVENT_FILTER, dependency_handler, collector,
http_version, 1000L, ssl, bt, &state, true, connect)
{
}
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 @@ -15,6 +15,7 @@ class k8s_deployment_handler : public k8s_handler
{
public:
k8s_deployment_handler(k8s_state_t& state,
ptr_t dependency_handler,
collector_ptr_t collector = nullptr,
std::string url = "",
const std::string& http_version = "1.0",
Expand Down
3 changes: 2 additions & 1 deletion userspace/libsinsp/k8s_event_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ std::string k8s_event_handler::STATE_FILTER =
"}";

k8s_event_handler::k8s_event_handler(k8s_state_t& state,
ptr_t dependency_handler,
collector_ptr_t collector,
std::string url,
const std::string& http_version,
Expand All @@ -61,7 +62,7 @@ k8s_event_handler::k8s_event_handler(k8s_state_t& state,
filter_ptr_t event_filter):
k8s_handler("k8s_event_handler", true,
url, "/api/v1/events",
STATE_FILTER, EVENT_FILTER, collector,
STATE_FILTER, EVENT_FILTER, dependency_handler, collector,
http_version, 1000L, ssl, bt, &state, true, connect),
m_event_filter(event_filter)
{
Expand Down
1 change: 1 addition & 0 deletions userspace/libsinsp/k8s_event_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class k8s_event_handler : public k8s_handler
typedef user_event_filter_t::ptr_t filter_ptr_t;

k8s_event_handler(k8s_state_t& state,
ptr_t dependency_handler,
collector_ptr_t collector = nullptr,
std::string url = "",
const std::string& http_version = "1.0",
Expand Down
41 changes: 24 additions & 17 deletions userspace/libsinsp/k8s_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ k8s_handler::k8s_handler(const std::string& id,
const std::string& path,
const std::string& state_filter,
const std::string& event_filter,
ptr_t dependency_handler,
collector_ptr_t collector,
const std::string& http_version,
int timeout_ms,
Expand All @@ -53,7 +54,8 @@ k8s_handler::k8s_handler(const std::string& id,
m_bt(bt),
m_watch(watch),
m_connect(connect),
m_is_captured(is_captured)
m_is_captured(is_captured),
m_dependency_handler(dependency_handler)
{
g_logger.log("Creating K8s " + name() + " (" + m_id + ") "
"handler object for [" + uri(m_url).to_string(false) + m_path + ']',
Expand All @@ -78,6 +80,7 @@ void k8s_handler::make_http()
m_http->add_json_filter(m_filter);
m_http->add_json_filter(ERROR_FILTER);
m_req_sent = false;
m_resp_recvd = false;
connect();
}
}
Expand Down Expand Up @@ -181,32 +184,35 @@ void k8s_handler::check_collector_status()

void k8s_handler::process_events()
{
for(auto evt : m_events)
if(m_dependency_handler->is_state_built())
{
if(evt && !evt->isNull())
for(auto evt : m_events)
{
g_logger.log("k8s_handler (" + m_id + ") data:\n" + json_as_string(*evt),
sinsp_logger::SEV_TRACE);
if(m_is_captured)
if(evt && !evt->isNull())
{
m_state->enqueue_capture_event(*evt);
g_logger.log("k8s_handler (" + m_id + ") data:\n" + json_as_string(*evt),
sinsp_logger::SEV_TRACE);
if(m_is_captured)
{
m_state->enqueue_capture_event(*evt);
}
handle_json(std::move(*evt));
if(!m_state_built) { m_state_built = true; }
}
else
{
g_logger.log("k8s_handler (" + m_id + ") error (" + uri(m_url).to_string(false) + ") " +
(!evt ? "data is null." : (evt->isNull() ? "JSON is null." : "Unknown")),
sinsp_logger::SEV_ERROR);
}
handle_json(std::move(*evt));
if(!m_state_built) { m_state_built = true; }
}
else
{
g_logger.log("k8s_handler (" + m_id + ") error (" + uri(m_url).to_string(false) + ") " +
(!evt ? "data is null." : (evt->isNull() ? "JSON is null." : "Unknown")),
sinsp_logger::SEV_ERROR);
}
m_events.clear();
}
m_events.clear();
}

void k8s_handler::check_state()
{
if(m_collector && m_state_built && m_watch && !m_watching)
if(m_collector && m_resp_recvd && m_watch && !m_watching)
{
// done with initial state handling, switch to events
m_collector->remove(m_http);
Expand Down Expand Up @@ -509,6 +515,7 @@ void k8s_handler::set_event_json(json_ptr_t json, const std::string&)
if(json)
{
m_events.emplace_back(json);
if(!m_resp_recvd) { m_resp_recvd = true; }
g_logger.log("k8s_handler added event, (" + m_id + ") has " + std::to_string(m_events.size()) +
" events from " + uri(m_url).to_string(false), sinsp_logger::SEV_TRACE);
}
Expand Down
Loading

0 comments on commit dbf9b5c

Please sign in to comment.