Skip to content
This repository was archived by the owner on Aug 19, 2019. It is now read-only.
Merged
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
62 changes: 37 additions & 25 deletions src/kubernetes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -163,37 +163,47 @@ json::value KubernetesReader::ComputePodAssociations(const json::Object* pod)
const throw(json::Exception) {
const json::Object* metadata = pod->Get<json::Object>("metadata");
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 = 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");
const std::string top_level_name =
top_level_metadata->Get<json::String>("name");
if (!top_level_controller->Has("kind") &&
top_level_metadata->Get<json::String>("uid") != pod_id) {
LOG(ERROR) << "Internal error; top-level controller without 'kind' "
<< *top_level_controller
<< " not the same as pod " << *pod;
}
const std::string top_level_kind =
top_level_controller->Has("kind")
? top_level_controller->Get<json::String>("kind")
: "Pod";

json::value instance_resource =
InstanceReader::InstanceResource(environment_).ToJSON();

std::unique_ptr<json::Object> raw_associations(new json::Object({
{"infrastructureResource", std::move(instance_resource)},
{"controllers", json::object({
{"topLevelControllerType", json::string(top_level_kind)},
{"topLevelControllerName", json::string(top_level_name)},
})},
}));

try {
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");
const std::string top_level_name =
top_level_metadata->Get<json::String>("name");
const std::string pod_id = metadata->Get<json::String>("uid");
if (!top_level_controller->Has("kind") &&
top_level_metadata->Get<json::String>("uid") != pod_id) {
LOG(ERROR) << "Internal error; top-level controller without 'kind' "
<< *top_level_controller
<< " not the same as pod " << *pod;
}
const std::string top_level_kind =
top_level_controller->Has("kind")
? top_level_controller->Get<json::String>("kind")
: "Pod";

raw_associations->emplace(std::make_pair(
"controllers",
json::object({
{"topLevelControllerType", json::string(top_level_kind)},
{"topLevelControllerName", json::string(top_level_name)},
})
));
} catch (const QueryException& e) {
LOG(ERROR) << "Error while finding top-level controller for "
<< namespace_name << "." << metadata->Get<json::String>("name")
<< ": " << e.what();
}

const json::Object* spec = pod->Get<json::Object>("spec");
if (spec->Has("nodeName")) {
// Pods that have been scheduled will have a nodeName.
Expand Down Expand Up @@ -666,10 +676,12 @@ json::value KubernetesReader::QueryMaster(const std::string& path) const
try {
http::client::response response = client.get(request);
if (status(response) >= 400 && status(response) <= 403) {
throw NonRetriableError(
const std::string what =
format::Substitute("Server responded with '{{message}}' ({{code}})",
{{"message", status_message(response)},
{"code", format::str(status(response))}}));
{"code", format::str(status(response))}});
LOG(ERROR) << "Failed to query " << endpoint << ": " << what;
throw NonRetriableError(what);
} else if (status(response) >= 300) {
throw boost::system::system_error(
boost::system::errc::make_error_code(boost::system::errc::not_connected),
Expand Down