ADD: added a general kubenetes API and integrated that in the classes and remodeled the info management
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
#include "curlpp/Options.hpp"
|
||||
#include "kubecontrol/PodInfo.hpp"
|
||||
#include "kubecontrol/Utils.hpp"
|
||||
#include "nlohmann/json_fwd.hpp"
|
||||
#include "yaml-cpp/binary.h"
|
||||
@@ -10,23 +9,20 @@
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <filesystem>
|
||||
#include "loguru.hpp"
|
||||
|
||||
|
||||
|
||||
namespace kubecontrol
|
||||
{
|
||||
int getFileSize(std::string filename) { // path to file
|
||||
FILE *p_file = NULL;
|
||||
p_file = fopen(filename.c_str(),"rb");
|
||||
fseek(p_file,0,SEEK_END);
|
||||
int size = ftell(p_file);
|
||||
fclose(p_file);
|
||||
return size;
|
||||
}
|
||||
|
||||
|
||||
|
||||
KubePod::KubePod(std::string Owner, std::string Uuid, std::string ContainerImage,std::string Namespace):Owner_(Utils::to_lower(Owner)),Uuid_(Utils::to_lower(Uuid)),
|
||||
ContainerImage_(ContainerImage),Namespace_(Namespace),EnvirmonentVars_()
|
||||
|
||||
KubePod::KubePod(std::string Owner, std::string Uuid, std::string ContainerImage,std::string Namespace)
|
||||
:Owner_(Utils::to_lower(Owner)),
|
||||
Uuid_(Utils::to_lower(Uuid)),
|
||||
ContainerImage_(ContainerImage),
|
||||
Namespace_(Namespace),
|
||||
EnvirmonentVars_()
|
||||
{
|
||||
ContainerRegistry_ = "kmaster.ti.unibw-hamburg.de:30808";
|
||||
EnvirmonentVars_ = std::map<std::string, std::string>();
|
||||
@@ -45,8 +41,13 @@ namespace kubecontrol
|
||||
}
|
||||
|
||||
|
||||
KubePod::KubePod(std::string Owner, std::string Uuid, std::string Component, std::string ContainerImage,std::string Namespace):Owner_(Utils::to_lower(Owner)),
|
||||
Uuid_(Utils::to_lower(Uuid)),Component_(Utils::to_lower(Component)), ContainerImage_(ContainerImage),Namespace_(Namespace),EnvirmonentVars_()
|
||||
KubePod::KubePod(std::string Owner, std::string Uuid, std::string Component, std::string ContainerImage,std::string Namespace):
|
||||
Owner_(Utils::to_lower(Owner)),
|
||||
Uuid_(Utils::to_lower(Uuid)),
|
||||
Component_(Utils::to_lower(Component)),
|
||||
ContainerImage_(ContainerImage),
|
||||
Namespace_(Namespace),
|
||||
EnvirmonentVars_()
|
||||
{
|
||||
ContainerRegistry_ = "kmaster.ti.unibw-hamburg.de:30808";
|
||||
EnvirmonentVars_ = std::map<std::string, std::string>();
|
||||
@@ -77,6 +78,15 @@ namespace kubecontrol
|
||||
return this->Owner_;
|
||||
}
|
||||
|
||||
std::string KubePod::getIp()
|
||||
{
|
||||
return this->Ip_;
|
||||
}
|
||||
std::string KubePod::getStatus()
|
||||
{
|
||||
return this->Status_;
|
||||
}
|
||||
|
||||
void KubePod::setEnvironmentVar(std::string key, std::string val)
|
||||
{
|
||||
EnvirmonentVars_.emplace(key,val);
|
||||
@@ -190,215 +200,142 @@ namespace kubecontrol
|
||||
|
||||
|
||||
|
||||
std::string KubePod::start(KubernetesAPI APIInterface,bool WaitTillRunning)
|
||||
int KubePod::start(KubernetesAPI APIInterface,bool WaitTillRunning)
|
||||
{
|
||||
std::string request = "/api/v1/namespaces/simulator/pods/";
|
||||
|
||||
this->createYAML();
|
||||
|
||||
// LOG_S(INFO)<< this->PathToYaml_;
|
||||
|
||||
|
||||
std::stringstream stream;
|
||||
stream << YAMLNode_;
|
||||
std::string response = APIInterface.performRequest(request,"POST",stream.str());
|
||||
extractInformationFromResopnse(response);
|
||||
|
||||
InfoPod.update(response);
|
||||
if (WaitTillRunning == true) {
|
||||
|
||||
while (InfoPod.Status != "Running" && InfoPod.Status != "Succeeded") {
|
||||
auto timeoutTime = std::chrono::system_clock::now() + std::chrono::seconds(10);
|
||||
|
||||
|
||||
if (WaitTillRunning == true ) {
|
||||
while ((this->Status_ != "Running" && this->Status_ != "Succeeded") || std::chrono::system_clock::now() >= timeoutTime) {
|
||||
// LOG_S(INFO)<<"wainting till running " << this->Status_;
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
response = this->getInfoForThisPod(APIInterface);
|
||||
|
||||
InfoPod.update(response);
|
||||
this->updateInfoForThisPod(APIInterface);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return response;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
std::string KubePod::stop(std::string apiAddress,std::string token)
|
||||
{
|
||||
std::string curlURL = apiAddress+ this->Uuid_;
|
||||
LOG_S(INFO)<<curlURL;
|
||||
|
||||
std::string AuthString = "Authorization: Bearer " + token;
|
||||
|
||||
std::list<std::string> headers;
|
||||
headers.push_back(AuthString);
|
||||
|
||||
|
||||
curlpp::Cleanup cleaner;
|
||||
curlpp::Easy request;
|
||||
|
||||
std::stringstream result;
|
||||
request.setOpt(cURLpp::Options::WriteStream(&result));
|
||||
|
||||
request.setOpt(new curlpp::options::HttpHeader(headers));
|
||||
request.setOpt(new curlpp::options::Url(curlURL));
|
||||
|
||||
|
||||
request.setOpt(new curlpp::options::SslVerifyPeer(false));
|
||||
|
||||
|
||||
request.setOpt(new curlpp::options::CustomRequest("DELETE"));
|
||||
// request.setOpt(new curlpp::options::Verbose(true));
|
||||
|
||||
|
||||
request.perform();
|
||||
auto response = result.str();
|
||||
|
||||
|
||||
// std::this_thread::sleep_for(std::chrono::milliseconds(5000));
|
||||
|
||||
|
||||
// InfoPod.update(this->getInfo(apiAddress, token));
|
||||
|
||||
LOG_S(INFO)<<"Related pods: " <<InfoPod.getRelatedPods().size();
|
||||
|
||||
|
||||
if (InfoPod.getRelatedPods().size() > 0)
|
||||
{
|
||||
for(int i = 0; i<InfoPod.getRelatedPods().size(); i++)
|
||||
{
|
||||
StopChilds(apiAddress, token, InfoPod.getRelatedPods()[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
std::string KubePod::stop(KubernetesAPI APIInterface)
|
||||
int KubePod::stop(KubernetesAPI APIInterface)
|
||||
{
|
||||
std::string request = "/api/v1/namespaces/simulator/pods/"+Uuid_;
|
||||
std::string result = APIInterface.performRequest(request,"DELETE");
|
||||
|
||||
|
||||
this->getInfoForRelatedPods(APIInterface);
|
||||
LOG_S(INFO)<<"stopping: " <<Uuid_;
|
||||
|
||||
return result;
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
|
||||
std::string KubePod::StopChilds(std::string apiAddress,std::string token,std::string uuid)
|
||||
int KubePod::stopChilds(KubernetesAPI APIInterface)
|
||||
{
|
||||
std::string curlURL = apiAddress+ uuid;
|
||||
std::string AuthString = "Authorization: Bearer " + token;
|
||||
auto uuids = this->getUUIDsForChildPods(APIInterface);
|
||||
if (uuids.size() == 0)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
for(auto i: uuids)
|
||||
{
|
||||
std::string request = "/api/v1/namespaces/simulator/pods/"+i;
|
||||
std::string result = APIInterface.performRequest(request,"DELETE");
|
||||
|
||||
std::list<std::string> headers;
|
||||
headers.push_back(AuthString);
|
||||
|
||||
|
||||
curlpp::Cleanup cleaner;
|
||||
curlpp::Easy request;
|
||||
|
||||
std::stringstream result;
|
||||
request.setOpt(cURLpp::Options::WriteStream(&result));
|
||||
|
||||
request.setOpt(new curlpp::options::HttpHeader(headers));
|
||||
request.setOpt(new curlpp::options::Url(curlURL));
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
request.setOpt(new curlpp::options::SslVerifyPeer(false));
|
||||
|
||||
|
||||
request.setOpt(new curlpp::options::CustomRequest("DELETE"));
|
||||
// request.setOpt(new curlpp::options::Verbose(true));
|
||||
|
||||
|
||||
request.perform();
|
||||
auto response = result.str();
|
||||
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// std::string KubePod::getInfo(std::string apiAddress,std::string token)
|
||||
// {
|
||||
// std::string curlURL = apiAddress+Uuid_+"/"+"status";
|
||||
// std::string AuthString = "Authorization: Bearer " + token;
|
||||
|
||||
// std::list<std::string> headers;
|
||||
// headers.push_back(AuthString);
|
||||
|
||||
|
||||
// curlpp::Cleanup cleaner;
|
||||
// curlpp::Easy request;
|
||||
|
||||
// std::stringstream result;
|
||||
// request.setOpt(cURLpp::Options::WriteStream(&result));
|
||||
|
||||
|
||||
// request.setOpt(new curlpp::options::HttpHeader(headers));
|
||||
|
||||
// request.setOpt(new curlpp::options::Url(curlURL));
|
||||
|
||||
// request.setOpt(new curlpp::options::SslVerifyPeer(false));
|
||||
|
||||
|
||||
// request.perform();
|
||||
// return result.str();
|
||||
// }
|
||||
|
||||
std::string KubePod::getInfoForThisPod(KubernetesAPI APIInterface)
|
||||
int KubePod::updateInfoForThisPod(KubernetesAPI APIInterface)
|
||||
{
|
||||
std::string request = "/api/v1/namespaces/simulator/pods/"+Uuid_+"/status";
|
||||
std::string result = APIInterface.performRequest(request,"GET");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
std::string KubePod::getInfoForRelatedPods( KubernetesAPI APIInterface)
|
||||
{
|
||||
// std::string curlURL = apiAddress+"?labelSelector=app.kubernetes.io/part-of="+Uuid_;
|
||||
std::string request = "/api/v1/namespaces/simulator/pods?labelSelector=app.kubernetes.io/part-of="+Uuid_;
|
||||
std::string result = APIInterface.performRequest(request,"GET");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
std::vector<std::string> KubePod::getUUIDForRelatedPods(KubernetesAPI APIInterface)
|
||||
{
|
||||
std::vector<std::string> uuids;
|
||||
|
||||
auto rawResponse = getInfoForRelatedPods(APIInterface);
|
||||
|
||||
if (rawResponse == "")
|
||||
if (result == "")
|
||||
{
|
||||
return uuids;
|
||||
return 1;
|
||||
}
|
||||
|
||||
try {
|
||||
nlohmann::json j = nlohmann::json::parse(rawResponse);
|
||||
if (j.contains("items") && j["items"].is_array())
|
||||
{
|
||||
for (auto i : j["items"])
|
||||
{
|
||||
uuids.push_back(i["metadata"]["name"].get<std::string>());
|
||||
}
|
||||
}
|
||||
} catch (const std::exception e) {
|
||||
LOG_S(ERROR)<< "Exeption in getUUIDForRelatedPods() :" << e.what();
|
||||
if (extractInformationFromResopnse(result)!= 0)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
std::vector<std::string> KubePod::getUUIDsForChildPods( KubernetesAPI APIInterface)
|
||||
{
|
||||
std::string request = "/api/v1/namespaces/simulator/pods?labelSelector=app.kubernetes.io/part-of="+Uuid_;
|
||||
std::string result = APIInterface.performRequest(request,"GET");
|
||||
|
||||
if (result == "")
|
||||
{
|
||||
return std::vector<std::string>();
|
||||
}
|
||||
|
||||
// LOG_S(INFO)<<rawResponse;
|
||||
|
||||
return uuids;
|
||||
|
||||
extractInformationFromResopnse(result);
|
||||
return this->uuidsOfShildContainers;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int KubePod::extractInformationFromResopnse(std::string response)
|
||||
{
|
||||
if (response == "")
|
||||
{
|
||||
return 1 ;
|
||||
}
|
||||
// LOG_S(INFO)<< response;
|
||||
nlohmann::json j;
|
||||
try {
|
||||
j = nlohmann::json::parse(response);
|
||||
if(j.contains("kind") && j["kind"] == "Pod")
|
||||
{
|
||||
if( j["status"].contains("podIP")) this->Ip_ = j["status"]["podIP"].get<std::string>();
|
||||
this->Status_ = j["status"]["phase"].get<std::string>();
|
||||
this->PartOf_ = j["metadata"]["labels"]["app.kubernetes.io/part-of"];
|
||||
|
||||
|
||||
}else if(j.contains("items") && j["items"].is_array())
|
||||
{
|
||||
for (auto i : j["items"])
|
||||
{
|
||||
this->uuidsOfShildContainers.push_back(i["metadata"]["name"].get<std::string>());
|
||||
}
|
||||
|
||||
}else{
|
||||
return 1;
|
||||
}
|
||||
} catch (const std::exception e) {
|
||||
LOG_S(ERROR)<< "Exeption in extractInformationFromResopnse() :" << e.what();
|
||||
std::exception_ptr p = std::current_exception();
|
||||
LOG_S(ERROR)<< "Exeption :" << p.__cxa_exception_type()->name();
|
||||
LOG_S(INFO)<<j;
|
||||
return 1;
|
||||
|
||||
}
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -36,10 +36,23 @@ namespace kubecontrol {
|
||||
{
|
||||
APIAddress_ = address;
|
||||
}
|
||||
|
||||
std::string KubernetesAPI::getAddress()
|
||||
{
|
||||
return APIAddress_;
|
||||
}
|
||||
|
||||
void KubernetesAPI::addToken(std::string token)
|
||||
{
|
||||
Token_ = token;
|
||||
}
|
||||
|
||||
|
||||
std::string KubernetesAPI::getNamespace()
|
||||
{
|
||||
return Namespace_;
|
||||
}
|
||||
|
||||
void KubernetesAPI::addYaml(YAML::Node config)
|
||||
{
|
||||
try {
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
#include "curlpp/Options.hpp"
|
||||
#include "kubecontrol/KubePod.hpp"
|
||||
#include "kubecontrol/KubernetesAPI.hpp"
|
||||
#include "kubecontrol/PodInfo.hpp"
|
||||
#include "nlohmann/json_fwd.hpp"
|
||||
#include <cstddef>
|
||||
#include <cstdlib>
|
||||
@@ -13,6 +12,7 @@
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "loguru.hpp"
|
||||
|
||||
|
||||
namespace kubecontrol
|
||||
@@ -20,40 +20,22 @@ namespace kubecontrol
|
||||
PodController::PodController(std::string pathToKubectlConfig):
|
||||
APIInterface_( YAML::LoadFile(pathToKubectlConfig))
|
||||
{
|
||||
LOG_S(INFO)<< "Path To Yaml: " <<pathToKubectlConfig;
|
||||
|
||||
try {
|
||||
YAML::Node config = YAML::LoadFile(pathToKubectlConfig);
|
||||
|
||||
BearerToken_ = config["users"][0]["user"]["token"].as<std::string>();
|
||||
Namespace_ = config["contexts"][0]["context"]["namespace"].as<std::string>();
|
||||
|
||||
char * KUBERNETES_SERVICE_HOST = std::getenv("KUBERNETES_SERVICE_HOST");
|
||||
char * KUBERNETES_SERVICE_PORT = std::getenv("KUBERNETES_SERVICE_PORT");
|
||||
|
||||
if (KUBERNETES_SERVICE_HOST != nullptr && KUBERNETES_SERVICE_PORT!= nullptr)
|
||||
{
|
||||
ServerAddress_ = "https://" +std::string(KUBERNETES_SERVICE_HOST)+":"+std::string(KUBERNETES_SERVICE_PORT);
|
||||
} else
|
||||
{
|
||||
LOG_S(INFO)<<"Taking Serveraddress out of the provided YAML file";
|
||||
|
||||
ServerAddress_ = config["clusters"][0]["cluster"]["server"].as<std::string>();
|
||||
}
|
||||
|
||||
|
||||
ApiCall_ = "/api/v1/namespaces/"+Namespace_+"/pods/";
|
||||
} catch (std::exception& e) {
|
||||
LOG_S(ERROR)<< e.what();
|
||||
throw e.what();
|
||||
}
|
||||
ApiCall_ = "/api/v1/namespaces/"+APIInterface_.getNamespace()+"/pods/";
|
||||
|
||||
}
|
||||
|
||||
std::string PodController::getServerAddress()
|
||||
{
|
||||
return this->ServerAddress_;
|
||||
return APIInterface_.getAddress();
|
||||
}
|
||||
|
||||
KubernetesAPI PodController::getKubernetesAPI()
|
||||
{
|
||||
return APIInterface_;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void PodController::startPod(KubePod Pod,bool WaitTillRunning )
|
||||
@@ -64,21 +46,22 @@ namespace kubecontrol
|
||||
auto response = Pod.start(APIInterface_,WaitTillRunning);
|
||||
|
||||
std::lock_guard<std::mutex>lock(mx_);
|
||||
PodList_.emplace_back(Pod);
|
||||
PodList_.emplace_back(std::make_unique<KubePod>(Pod));
|
||||
// LOG_S(INFO)<<response;
|
||||
}
|
||||
|
||||
void PodController::stopPod(std::string Label)
|
||||
void PodController::stopPod(std::string uuid)
|
||||
{
|
||||
std::lock_guard<std::mutex>lock(mx_);
|
||||
// PodList_
|
||||
for (std::vector<KubePod>::iterator it = PodList_.begin(); it != PodList_.end();)
|
||||
{
|
||||
if (Label == it->getUUID())
|
||||
{
|
||||
it->stop(APIInterface_);
|
||||
it = PodList_.erase(it);
|
||||
}
|
||||
for (std::vector<std::unique_ptr<KubePod>>::iterator it = PodList_.begin(); it != PodList_.end();)
|
||||
{
|
||||
|
||||
if (uuid == it->get()->getUUID())
|
||||
{
|
||||
it->get()->stop(APIInterface_);
|
||||
it = PodList_.erase(it);
|
||||
}
|
||||
else
|
||||
{
|
||||
it++;
|
||||
@@ -91,263 +74,108 @@ namespace kubecontrol
|
||||
{
|
||||
std::lock_guard<std::mutex>lock(mx_);
|
||||
|
||||
checkPodsHierarchy();
|
||||
|
||||
for (auto item : PodList_)
|
||||
for (auto &item : PodList_)
|
||||
{
|
||||
LOG_S(INFO)<<item.InfoPod.Uuid;
|
||||
item.InfoPod = *getPodInfo(item.getUUID()).get();
|
||||
item->stop(APIInterface_);
|
||||
|
||||
item.stop(APIInterface_);
|
||||
|
||||
LOG_S(INFO)<< "stopping pod: "<<item.getUUID();
|
||||
LOG_S(INFO)<< "stopping pod: "<<item->getUUID();
|
||||
}
|
||||
|
||||
PodList_.clear();
|
||||
}
|
||||
|
||||
// std::string PodController::getInfoForOwnPod()
|
||||
// {
|
||||
// std::string response = "";
|
||||
// for (auto item : PodList_)
|
||||
// {
|
||||
|
||||
// response = item.getInfoForThisPod(APIInterface_);
|
||||
// }
|
||||
|
||||
std::vector<PodInfos> PodController::getInfoForAllPods()
|
||||
{
|
||||
std::vector<PodInfos> infosOfAllPods;
|
||||
for (auto &item : PodList_)
|
||||
{
|
||||
if(item->updateInfoForThisPod(APIInterface_) == 0 )
|
||||
{
|
||||
infosOfAllPods.emplace_back(extractInfosFromKubePod(item.get()));
|
||||
}
|
||||
|
||||
// return response;
|
||||
}
|
||||
|
||||
return infosOfAllPods;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
PodInfos PodController::getInfoForPod(std::string uuid)
|
||||
{
|
||||
PodInfos info;
|
||||
for (auto &item : PodList_)
|
||||
{
|
||||
if(item->getUUID() == uuid )
|
||||
{
|
||||
LOG_S(INFO)<<"pod found: "<< item->getUUID();
|
||||
item->updateInfoForThisPod(APIInterface_);
|
||||
return extractInfosFromKubePod(item.get());
|
||||
}
|
||||
|
||||
}
|
||||
return info;
|
||||
|
||||
}
|
||||
|
||||
|
||||
PodInfos PodController::extractInfosFromKubePod(KubePod * pod)
|
||||
{
|
||||
PodInfos info;
|
||||
info.UUID = pod->getUUID();
|
||||
info.Owner = pod->getOwner();
|
||||
info.Component =pod->getComponent();
|
||||
info.IP = pod->getIp();
|
||||
info.Status = pod->getStatus();
|
||||
info.UUIDOfChilds = pod->getUUIDsForChildPods(APIInterface_);
|
||||
|
||||
return info;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// std::string PodController::performRequest(std::string curlURL)
|
||||
// {
|
||||
|
||||
// std::string AuthString = "Authorization: Bearer " + BearerToken_;
|
||||
|
||||
// std::list<std::string> headers;
|
||||
// headers.push_back(AuthString);
|
||||
|
||||
// auto request = std::make_unique<curlpp::Easy>();
|
||||
|
||||
|
||||
// // request->setOpt(test);
|
||||
// std::stringstream result;
|
||||
// request->setOpt(cURLpp::Options::WriteStream(&result));
|
||||
|
||||
// request->setOpt(new curlpp::options::HttpHeader(headers));
|
||||
|
||||
// request->setOpt(new curlpp::options::Url(curlURL));
|
||||
// // request.setOpt(new curlpp::options::SslEngineDefault());
|
||||
// // request.setOpt(new curlpp::options::CaPath("config/ca.crt"));
|
||||
// request->setOpt(new curlpp::options::SslVerifyPeer(false));
|
||||
// // request->setOpt(new curlpp::options::Verbose(true));
|
||||
|
||||
|
||||
// request->perform();
|
||||
|
||||
// // std::string response = mWriterChunk.getResponse();
|
||||
// std::string response = result.str();
|
||||
// return response;
|
||||
// }
|
||||
|
||||
std::string PodController::getInfoForAllPods()
|
||||
{
|
||||
|
||||
std::string curlURL = ServerAddress_+ApiCall_;
|
||||
|
||||
std::string response = this->performRequest(curlURL);
|
||||
|
||||
|
||||
std::vector<std::string> podsNames;
|
||||
nlohmann::json j;
|
||||
try
|
||||
{
|
||||
|
||||
j = nlohmann::json::parse(response);
|
||||
|
||||
if (j.contains("items"))
|
||||
{
|
||||
int i = j["items"].size();
|
||||
for (int a = 0; a<i; a++)
|
||||
{
|
||||
if (!j["items"][a]["metadata"]["name"].empty())
|
||||
{
|
||||
podsNames.emplace_back(j["items"][a]["metadata"]["name"]);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
j.clear();
|
||||
for (auto item: podsNames)
|
||||
{
|
||||
std::string curlURL = ServerAddress_+ApiCall_+item+"/status";
|
||||
|
||||
auto response = this->performRequest(curlURL);
|
||||
|
||||
j["items"].emplace_back(nlohmann::json::parse(response));
|
||||
}
|
||||
|
||||
|
||||
return j.dump();
|
||||
|
||||
}catch(const std::exception e)
|
||||
{
|
||||
LOG_S(ERROR)<< e.what()<< " IN PodController::getPodsInfo() Function";
|
||||
}
|
||||
return j.dump();
|
||||
}
|
||||
|
||||
|
||||
std::string PodController::getInfoForPod(std::string uid)
|
||||
{
|
||||
|
||||
bool found = false;
|
||||
for (auto item : PodList_)
|
||||
{
|
||||
if (uid == item.getUUID())
|
||||
{
|
||||
found = true;
|
||||
// checkPodsHierarchy();
|
||||
item.InfoPod = *getPodInfo(item.getUUID()).get();
|
||||
// LOG_S(INFO)<<item.InfoPod.getRelatedPods().size();
|
||||
item.getInfoForRelatedPods(APIInterface_);
|
||||
|
||||
// return item.getInfo(ServerAddress_+ApiCall_, BearerToken_);
|
||||
return item.getInfoForThisPod(APIInterface_);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if (found == false)
|
||||
{
|
||||
std::string curlURL = ServerAddress_+ApiCall_+uid;
|
||||
auto response = this->performRequest(curlURL);
|
||||
return response;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void PodController::checkPodsHierarchy()
|
||||
{
|
||||
auto response = this->getInfoForAllPods();
|
||||
|
||||
nlohmann::json j;
|
||||
try
|
||||
{
|
||||
j = nlohmann::json::parse(response);
|
||||
if (j.contains("items"))
|
||||
{
|
||||
LOG_S(INFO)<<j["items"].size();
|
||||
int i = j["items"].size();
|
||||
for (int a = 0; a<i; a++)
|
||||
{
|
||||
auto item = std::make_unique<PodInfo>(j["items"][a].dump());
|
||||
// LOG_S(INFO)<<item->ToString();
|
||||
// item->Uuid = j["items"][a]["metadata"]["labels"]["app.kubernetes.io/name"].get<std::string>();
|
||||
// item->Component = j["items"][a]["metadata"]["labels"]["app.kubernetes.io/component"].get<std::string>();
|
||||
// item->Image = j["items"][a]["spec"]["containers"][0]["image"].get<std::string>();
|
||||
// item->Ip = j["items"][a]["status"]["podIP"].get<std::string>();
|
||||
// item->Status = j["items"][a]["status"]["phase"].get<std::string>();
|
||||
// item->PartOf = j["items"][a]["metadata"]["labels"]["app.kubernetes.io/part-of"].get<std::string>();
|
||||
// // LOG_S(INFO)<<j["items"][a]["status"]["podIP"];
|
||||
|
||||
// LOG_S(INFO)<<j["items"][a]["status"]["phase"];
|
||||
this->podsInfoList_.push_back(std::move(item));
|
||||
}
|
||||
|
||||
|
||||
i = j["items"].size();
|
||||
for (int a = 0; a<i; a++)
|
||||
{
|
||||
auto item = std::make_shared<PodInfo>();
|
||||
std::string uuid = j["items"][a]["metadata"]["labels"]["app.kubernetes.io/name"].get<std::string>();
|
||||
std::string parentName= j["items"][a]["metadata"]["labels"]["app.kubernetes.io/part-of"].get<std::string>();
|
||||
|
||||
auto self = this->getPodInfo(uuid);
|
||||
auto parent = this->getPodInfo(parentName);
|
||||
if (parent != nullptr && self != nullptr) {
|
||||
parent->addRelatedPods(self->Uuid);
|
||||
}else if (parent== nullptr)
|
||||
{
|
||||
self->PartOf = parentName;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} catch (std::exception& e)
|
||||
{
|
||||
LOG_S(ERROR)<<e.what() << " In checkPodsHierarchy() Function";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
std::string PodController::getPodsInfoForAll()
|
||||
{
|
||||
checkPodsHierarchy();
|
||||
std::string item = "";
|
||||
nlohmann::json j;
|
||||
for (int i = 0; i < podsInfoList_.size(); i++) {
|
||||
if(podsInfoList_[i]->relatedPodsSize() != 0 )
|
||||
{
|
||||
item = podsInfoList_[i]->ToJson().dump() +" Childs: ";
|
||||
nlohmann::json tmp = podsInfoList_[i]->ToJson();
|
||||
auto vec = podsInfoList_[i]->getRelatedPods();
|
||||
for(int a = 0; a < vec.size();a++)
|
||||
{
|
||||
tmp["Childs"].push_back(getPodInfo(vec[a])->ToJson());
|
||||
item += getPodInfo(vec[a])->ToString() +" // ";
|
||||
}
|
||||
j.push_back(tmp);
|
||||
}
|
||||
}
|
||||
|
||||
return j.dump();
|
||||
|
||||
}
|
||||
|
||||
std::shared_ptr<PodInfo> PodController::getPodsInfo(std::string uuid)
|
||||
{
|
||||
for (int i = 0; i < podsInfoList_.size(); i++) {
|
||||
|
||||
if (podsInfoList_[i]->Uuid == uuid)
|
||||
{
|
||||
return podsInfoList_[i];
|
||||
}
|
||||
}
|
||||
|
||||
checkPodsHierarchy();
|
||||
LOG_S(INFO)<< "check Hierarchy";
|
||||
return getPodsInfo(uuid);;
|
||||
}
|
||||
|
||||
|
||||
// void PodController::addPodInfoToInfoList(std::unique_ptr<PodInfo> podinfo)
|
||||
// {
|
||||
|
||||
// }
|
||||
std::shared_ptr<PodInfo> PodController::getPodInfo(std::string uuid)
|
||||
{
|
||||
|
||||
for (int i = 0; i < podsInfoList_.size(); i++) {
|
||||
if (podsInfoList_[i]->Uuid == uuid)
|
||||
{
|
||||
return podsInfoList_[i];
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
size_t PodController::getListOfChildPods()
|
||||
{
|
||||
return PodList_.size();
|
||||
}
|
||||
|
||||
|
||||
|
||||
std::string PodController::performRequest(std::string curlURL)
|
||||
{
|
||||
|
||||
std::string AuthString = "Authorization: Bearer " + BearerToken_;
|
||||
|
||||
std::list<std::string> headers;
|
||||
headers.push_back(AuthString);
|
||||
|
||||
auto request = std::make_unique<curlpp::Easy>();
|
||||
|
||||
|
||||
// request->setOpt(test);
|
||||
std::stringstream result;
|
||||
request->setOpt(cURLpp::Options::WriteStream(&result));
|
||||
|
||||
request->setOpt(new curlpp::options::HttpHeader(headers));
|
||||
|
||||
request->setOpt(new curlpp::options::Url(curlURL));
|
||||
// request.setOpt(new curlpp::options::SslEngineDefault());
|
||||
// request.setOpt(new curlpp::options::CaPath("config/ca.crt"));
|
||||
request->setOpt(new curlpp::options::SslVerifyPeer(false));
|
||||
// request->setOpt(new curlpp::options::Verbose(true));
|
||||
|
||||
|
||||
request->perform();
|
||||
|
||||
// std::string response = mWriterChunk.getResponse();
|
||||
std::string response = result.str();
|
||||
return response;
|
||||
}
|
||||
|
||||
void PodController::performForceStopRequest(std::string uuid)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,114 +0,0 @@
|
||||
|
||||
|
||||
#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)
|
||||
{
|
||||
|
||||
try
|
||||
{
|
||||
nlohmann::json 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(WARNING)<< response;
|
||||
LOG_S(ERROR)<<e.what() << " IN PodInfo Constructor";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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;
|
||||
j["Related-Pods"] ;
|
||||
for (auto i : this->relatedPods ) {
|
||||
j["Related-Pods"].push_back(i);
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user