-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an introspection endpoint for container metadata
- Loading branch information
Showing
11 changed files
with
155 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#include "ContainerInfoInspector.h" | ||
|
||
#include <civetweb.h> | ||
#include <string> | ||
#include <string_view> | ||
|
||
namespace collector { | ||
const char* ContainerInfoInspector::kBaseRoute = "/state/containers/"; | ||
|
||
bool ContainerInfoInspector::handleGet(CivetServer* server, struct mg_connection* conn) { | ||
const mg_request_info* req_info = mg_get_request_info(conn); | ||
if (req_info == nullptr) { | ||
return ServerError(conn, "unable to read request"); | ||
} | ||
|
||
std::string_view url = req_info->local_uri; | ||
std::string container_id(url.substr(url.rfind('/') + 1)); | ||
|
||
if (container_id.length() != 12) { | ||
return ClientError(conn, "invalid container ID"); | ||
} | ||
|
||
Json::Value root; | ||
|
||
root["container_id"] = container_id; | ||
root["namespace"] = std::string(k8s_inspector_->GetNamespace(container_id)); | ||
|
||
mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nConnection: close\r\n\r\n"); | ||
mg_printf(conn, "%s\r\n", writer_.write(root).c_str()); | ||
|
||
return true; | ||
} | ||
|
||
} // namespace collector |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#ifndef _CONTAINER_INFO_INSPECTOR_ | ||
#define _CONTAINER_INFO_INSPECTOR_ | ||
|
||
#include <CivetServer.h> | ||
#include <civetweb.h> | ||
#include <memory> | ||
#include <string> | ||
#include <unordered_map> | ||
|
||
#include "IntrospectionEndpoint.h" | ||
#include "K8s.h" | ||
#include "json/writer.h" | ||
|
||
namespace collector { | ||
|
||
using QueryParams = std::unordered_map<std::string, std::string>; | ||
|
||
class ContainerInfoInspector : public IntrospectionEndpoint { | ||
public: | ||
static const char* kBaseRoute; | ||
|
||
ContainerInfoInspector(const std::shared_ptr<K8s>& k8s_inspector) : k8s_inspector_(k8s_inspector) {} | ||
|
||
// implementation of CivetHandler | ||
bool handleGet(CivetServer* server, struct mg_connection* conn) override; | ||
|
||
private: | ||
std::shared_ptr<K8s> k8s_inspector_; | ||
Json::FastWriter writer_; | ||
}; | ||
|
||
} // namespace collector | ||
|
||
#endif //_CONTAINER_INFO_INSPECTOR_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#include "IntrospectionEndpoint.h" | ||
|
||
#include <sstream> | ||
|
||
namespace collector { | ||
|
||
QueryParams IntrospectionEndpoint::ParseParameters(const char* queryString) { | ||
QueryParams params; | ||
|
||
if (queryString == nullptr) { | ||
return params; | ||
} | ||
|
||
std::stringstream query_stringstream(queryString); | ||
while (query_stringstream.good()) { | ||
std::string statement; | ||
|
||
std::getline(query_stringstream, statement, '&'); | ||
|
||
size_t equal = statement.find('='); | ||
|
||
if (equal != std::string::npos) { | ||
params[statement.substr(0, equal)] = statement.substr(equal + 1); | ||
} | ||
} | ||
return params; | ||
} | ||
|
||
std::optional<std::string> IntrospectionEndpoint::GetParameter(const QueryParams& params, const std::string& paramName) { | ||
return params.count(paramName) != 0 ? std::make_optional(params.at(paramName)) : std::nullopt; | ||
} | ||
|
||
} // namespace collector |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#ifndef _INTROSPECTION_ENDPOINT_ | ||
#define _INTROSPECTION_ENDPOINT_ | ||
|
||
#include <CivetServer.h> | ||
#include <optional> | ||
#include <unordered_map> | ||
|
||
namespace collector { | ||
|
||
using QueryParams = std::unordered_map<std::string, std::string>; | ||
|
||
class IntrospectionEndpoint : public CivetHandler { | ||
protected: | ||
static QueryParams ParseParameters(const char* queryString); | ||
static std::optional<std::string> GetParameter(const QueryParams& params, const std::string& paramName); | ||
|
||
static bool ServerError(struct mg_connection* conn, const char* err) { | ||
return mg_send_http_error(conn, 500, err) >= 0; | ||
} | ||
static bool ClientError(struct mg_connection* conn, const char* err) { | ||
return mg_send_http_error(conn, 400, err) >= 0; | ||
} | ||
}; | ||
|
||
} // namespace collector | ||
|
||
#endif // _INTROSPECTION_ENDPOINT_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters