ADD: added podinfo with building a tree hierarchy for a pod

This commit is contained in:
Henry Winkel
2023-08-16 14:42:00 +02:00
parent 6c2f8f1eb6
commit 89465f06a5
8 changed files with 474 additions and 48 deletions

111
src/kubecontrol/PodInfo.cpp Normal file
View File

@@ -0,0 +1,111 @@
#include "nlohmann/json_fwd.hpp"
#include <kubecontrol/PodInfo.hpp>
namespace kubecontrol
{
PodInfo::PodInfo()
{
Uuid = "";
Component = "";
Image = "";
Ip = "";
Status = "";
PartOf = "";
}
PodInfo::PodInfo(std::string response)
{
update(response);
}
void PodInfo::update(std::string response)
{
nlohmann::json j;
try
{
j = nlohmann::json::parse(response);
if (j.contains("items"))
{
if (j["items"].size() == 1) {
Uuid = j["items"][0]["metadata"]["labels"]["app.kubernetes.io/name"].get<std::string>();
Component = j["items"][0]["metadata"]["labels"]["app.kubernetes.io/component"].get<std::string>();
Image = j["items"][0]["spec"]["containers"][0]["image"].get<std::string>();
Ip = j["items"][0]["status"]["podIP"].get<std::string>();
Status = j["items"][0]["status"]["phase"].get<std::string>();
PartOf = j["items"][0]["metadata"]["labels"]["app.kubernetes.io/part-of"].get<std::string>();
}
}else
{
Uuid = j["metadata"]["labels"]["app.kubernetes.io/name"].get<std::string>();
Component = j["metadata"]["labels"]["app.kubernetes.io/component"].get<std::string>();
PartOf = j["metadata"]["labels"]["app.kubernetes.io/part-of"].get<std::string>();
Image = j["spec"]["containers"][0]["image"].get<std::string>();
if (j["status"].contains("podIP")) Ip = j["status"]["podIP"].get<std::string>();
Status = j["status"]["phase"].get<std::string>();
}
} catch (std::exception& e)
{
LOG_S(ERROR)<<e.what();
}
}
std::vector<std::string> PodInfo::getRelatedPods()
{
return relatedPods;
}
std::string PodInfo::ToString()
{
return "UUID: "+Uuid + " Compoment: " + Component + " Image: "+ Image+ " IP: " + Ip + " Status: "+Status + " Part Of: " +PartOf;
}
nlohmann::json PodInfo::ToJson()
{
nlohmann::json j;
j["UUID"] = Uuid;
j["Compoment"] = Component;
j["Image"] = Image;
j["Ip"] = Ip;
j["Status"] = Status;
j["Part-Of"] = PartOf;
return j;
}
void PodInfo::addRelatedPods(std::string uuid)
{
if ( std::find(relatedPods.begin(), relatedPods.end(), uuid) == relatedPods.end() )
{
relatedPods.emplace_back(uuid);
}
}
size_t PodInfo::relatedPodsSize()
{
return relatedPods.size();
}
}