Skip to content
This repository was archived by the owner on Aug 19, 2019. It is now read-only.
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
38 changes: 27 additions & 11 deletions src/kubernetes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ json::value KubernetesReader::ComputePodAssociations(const json::Object* pod)
const std::string namespace_name = metadata->Get<json::String>("namespace");
const std::string pod_id = metadata->Get<json::String>("uid");

const json::value top_level = FindTopLevelOwner(namespace_name, pod->Clone());
const json::value top_level = FindTopLevelController(
namespace_name, pod->Clone());
const json::Object* top_level_controller = top_level->As<json::Object>();
const json::Object* top_level_metadata =
top_level_controller->Get<json::Object>("metadata");
Expand Down Expand Up @@ -1033,7 +1034,7 @@ json::value KubernetesReader::GetOwner(
return owner->Clone();
}

json::value KubernetesReader::FindTopLevelOwner(
json::value KubernetesReader::FindTopLevelController(
const std::string& ns, json::value object) const
throw(QueryException, json::Exception) {
const json::Object* obj = object->As<json::Object>();
Expand All @@ -1042,27 +1043,42 @@ json::value KubernetesReader::FindTopLevelOwner(
#endif
const json::Object* metadata = obj->Get<json::Object>("metadata");
#ifdef VERBOSE
LOG(DEBUG) << "FindTopLevelOwner: metadata is " << *metadata;
LOG(DEBUG) << "FindTopLevelController: metadata is " << *metadata;
#endif
if (!metadata->Has("ownerReferences")) {
#ifdef VERBOSE
LOG(DEBUG) << "FindTopLevelOwner: no owner references in " << *metadata;
LOG(DEBUG) << "FindTopLevelController: no owner references in "
<< *metadata;
#endif
return object;
}
const json::Array* refs = metadata->Get<json::Array>("ownerReferences");
#ifdef VERBOSE
LOG(DEBUG) << "FindTopLevelOwner: refs is " << *refs;
LOG(DEBUG) << "FindTopLevelController: refs is " << *refs;
#endif
if (refs->size() > 1) {
LOG(WARNING) << "Found multiple owner references for " << *obj
<< " picking the first one arbitrarily.";

// Kubernetes objects are supposed to have at most one controller:
// https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.10/#objectmeta-v1-meta.
const json::Object* controller_ref = nullptr;
for (const json::value& ref : *refs) {
const json::Object* ref_obj = ref->As<json::Object>();
if (ref_obj->Has("controller") &&
ref_obj->Get<json::Boolean>("controller")) {
controller_ref = ref_obj;
break;
}
}
if (!controller_ref) {
#ifdef VERBOSE
LOG(DEBUG) << "FindTopLevelController: no controller references in "
<< *refs;
#endif
return object;
}
const json::value& ref = (*refs)[0];
#ifdef VERBOSE
LOG(DEBUG) << "FindTopLevelOwner: ref is " << *ref;
LOG(DEBUG) << "FindTopLevelController: controller_ref is " << *controller_ref;
#endif
return FindTopLevelOwner(ns, GetOwner(ns, ref->As<json::Object>()));
return FindTopLevelController(ns, GetOwner(ns, controller_ref));
}

bool KubernetesReader::ValidateConfiguration() const {
Expand Down
7 changes: 3 additions & 4 deletions src/kubernetes.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,9 @@ class KubernetesReader {
json::value GetOwner(const std::string& ns, const json::Object* owner_ref)
const throw(QueryException, json::Exception);

// For a given object, returns the top-level owner object.
// When there are multiple owner references, follows the first one.
json::value FindTopLevelOwner(const std::string& ns, json::value object) const
throw(QueryException, json::Exception);
// For a given object, returns the top-level controller object.
json::value FindTopLevelController(const std::string& ns, json::value object)
const throw(QueryException, json::Exception);

// Cached data.
mutable std::recursive_mutex mutex_;
Expand Down