Skip to content

Commit

Permalink
Switch to classes to hold health probes
Browse files Browse the repository at this point in the history
  • Loading branch information
mstemm committed Feb 27, 2019
1 parent d54f403 commit db912c3
Show file tree
Hide file tree
Showing 7 changed files with 359 additions and 188 deletions.
49 changes: 20 additions & 29 deletions userspace/libsinsp/container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
*/

#include <algorithm>

#include "container_engine/cri.h"
#include "container_engine/docker.h"
#include "container_engine/rkt.h"
Expand Down Expand Up @@ -165,19 +167,9 @@ string sinsp_container_manager::container_to_json(const sinsp_container_info& co

container["Mounts"] = mounts;

if(!container_info.m_healthcheck_obj.isNull())
{
container["Healthcheck"] = container_info.m_healthcheck_obj;
}

if(!container_info.m_liveness_probe_obj.isNull())
for(auto &probe : container_info.m_health_probes)
{
container["livenessProbe"] = container_info.m_liveness_probe_obj;
}

if(!container_info.m_readiness_probe_obj.isNull())
{
container["readinessProbe"] = container_info.m_readiness_probe_obj;
probe.add_to_json(container);
}

char addrbuff[100];
Expand Down Expand Up @@ -312,15 +304,15 @@ void sinsp_container_manager::identify_category(sinsp_threadinfo *tinfo)

// Otherwise, the thread is a part of a container health probe if:
//
// 1. the comm and args match the container's health probe
// 1. the comm and args match one of the container's health probes
// 2. we traverse the parent state and do *not* find vpid=1,
// or find a process not in a container
//
// This indicates the initial process of the health probe
// This indicates the initial process of the health probe.

sinsp_container_info::container_health_probe::probe_type ptype;

if(!cinfo->m_has_health_probe ||
cinfo->m_health_probe_exe != tinfo->m_exe ||
cinfo->m_health_probe_args != tinfo->m_args)
if((ptype = cinfo->match_health_probe(tinfo)) == sinsp_container_info::container_health_probe::PT_NONE)
{
return;
}
Expand Down Expand Up @@ -348,21 +340,20 @@ void sinsp_container_manager::identify_category(sinsp_threadinfo *tinfo)

if(!found_container_init)
{
if (!cinfo->m_liveness_probe_obj.isNull())
// Each health probe type maps to a command category
switch(ptype)
{
case sinsp_container_info::container_health_probe::PT_NONE:
break;
case sinsp_container_info::container_health_probe::PT_HEALTHCHECK:
tinfo->m_category = sinsp_threadinfo::CAT_HEALTHCHECK;
break;
case sinsp_container_info::container_health_probe::PT_LIVENESS_PROBE:
tinfo->m_category = sinsp_threadinfo::CAT_LIVENESS_PROBE;
}
else if (!cinfo->m_readiness_probe_obj.isNull())
{
break;
case sinsp_container_info::container_health_probe::PT_READINESS_PROBE:
tinfo->m_category = sinsp_threadinfo::CAT_READINESS_PROBE;
}
else if(!cinfo->m_healthcheck_obj.isNull())
{
tinfo->m_category = sinsp_threadinfo::CAT_HEALTHCHECK;
}
else
{
g_logger.format(sinsp_logger::SEV_ERROR, "Thread had liveness probe but no associated config object?");
break;
}
}
}
Expand Down
1 change: 0 additions & 1 deletion userspace/libsinsp/container_engine/docker.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ class docker
docker_response get_docker(sinsp_container_manager* manager, const std::string& url, std::string &json);
std::string build_request(const std::string& url);
bool parse_docker(sinsp_container_manager* manager, sinsp_container_info *container, sinsp_threadinfo* tinfo);
Json::Value get_k8s_pod_spec(const Json::Value &config_obj);

static std::string m_api_version;
static bool m_query_image_info;
Expand Down
69 changes: 2 additions & 67 deletions userspace/libsinsp/container_engine/docker_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,22 +87,8 @@ bool docker::parse_docker(sinsp_container_manager* manager, sinsp_container_info
container->m_imageid = imgstr.substr(cpos + 1);
}

container->parse_healthcheck(config_obj["Healthcheck"]);

// Saving full object for container event parsing/writing
container->m_healthcheck_obj = config_obj["Healthcheck"];

// For k8s liveness/readiness probes, we need to fetch a
// specific label that contains the pod spec, which is
// stringified json
Json::Value pod_spec_obj = get_k8s_pod_spec(config_obj);

container->parse_liveness_readiness_probe(pod_spec_obj["livenessProbe"]);
container->parse_liveness_readiness_probe(pod_spec_obj["readinessProbe"]);

// Saving full object for container event parsing/writing
container->m_liveness_probe_obj = pod_spec_obj["livenessProbe"];
container->m_readiness_probe_obj = pod_spec_obj["readinessProbe"];
// Add any health checks described in the container config/labels.
sinsp_container_info::container_health_probe::add_health_probes(config_obj, container->m_health_probes);

// containers can be spawned using just the imageID as image name,
// with or without the hash prefix (e.g. sha256:)
Expand Down Expand Up @@ -301,54 +287,3 @@ bool docker::parse_docker(sinsp_container_manager* manager, sinsp_container_info
return true;
}

Json::Value docker::get_k8s_pod_spec(const Json::Value &config_obj)
{
std::string cfg_str;
Json::Value spec;
Json::Reader reader;

if(config_obj.isNull())
{
return spec;
}

const Json::Value &labels = config_obj["Labels"];

if(labels.isNull())
{
return spec;
}

// The pod spec is stored as a stringified json label on the container
cfg_str = labels["annotation.kubectl.kubernetes.io/last-applied-configuration"].asString();

if(cfg_str == "")
{
return spec;
}

Json::Value cfg;
if(!reader.parse(cfg_str.c_str(), cfg))
{
g_logger.format(sinsp_logger::SEV_WARNING, "Could not parse pod config '%s'", cfg_str.c_str());
return spec;
}

Json::Value &sobj = cfg["spec"];

if(sobj.isNull())
{
return spec;
}

Json::Value &containers = sobj["containers"];

if(containers.isNull())
{
return spec;
}

spec = containers[0];

return spec;
}
Loading

0 comments on commit db912c3

Please sign in to comment.