#include "curlpp/Options.hpp" #include "kubecontrol/PodInfo.hpp" #include "nlohmann/json_fwd.hpp" #include #include #include #include #include #include #include #include namespace kubecontrol { PodController::PodController(std::string pathToKubectlConfig) { LOG_S(INFO)<< "Path To Yaml: " <(); Namespace_ = config["contexts"][0]["context"]["namespace"].as(); 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(); } ApiCall_ = "/api/v1/namespaces/"+Namespace_+"/pods/"; } catch (std::exception& e) { LOG_S(ERROR)<< e.what(); throw e.what(); } } std::string PodController::getServerAddress() { return this->ServerAddress_; } void PodController::startPod(KubePod Pod) { PodList_.emplace_back(Pod); LOG_S(INFO)<< "starting pod: "<performRequest(curlURL); std::vector podsNames; nlohmann::json j; try { j = nlohmann::json::parse(response); if (j.contains("items")) { int i = j["items"].size(); for (int a = 0; aperformRequest(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)<performRequest(curlURL); return response; } return nullptr; } void PodController::checkPodsHierarchy() { auto response = this->getPodsInfo(); nlohmann::json j; try { j = nlohmann::json::parse(response); if (j.contains("items")) { int i = j["items"].size(); for (int a = 0; a(j["items"][a].dump()); // item->Uuid = j["items"][a]["metadata"]["labels"]["app.kubernetes.io/name"].get(); // item->Component = j["items"][a]["metadata"]["labels"]["app.kubernetes.io/component"].get(); // item->Image = j["items"][a]["spec"]["containers"][0]["image"].get(); // item->Ip = j["items"][a]["status"]["podIP"].get(); // item->Status = j["items"][a]["status"]["phase"].get(); // item->PartOf = j["items"][a]["metadata"]["labels"]["app.kubernetes.io/part-of"].get(); // // LOG_S(INFO)<podsInfoList_.push_back(std::move(item)); } i = j["items"].size(); for (int a = 0; a(); std::string uuid = j["items"][a]["metadata"]["labels"]["app.kubernetes.io/name"].get(); std::string parentName= j["items"][a]["metadata"]["labels"]["app.kubernetes.io/part-of"].get(); 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)<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 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) // { // } std::shared_ptr PodController::getPodInfo(std::string uuid) { for (int i = 0; i < podsInfoList_.size(); i++) { if (podsInfoList_[i]->Uuid == uuid) { return podsInfoList_[i]; } } return nullptr; } std::string PodController::performRequest(std::string curlURL) { std::string AuthString = "Authorization: Bearer " + BearerToken_; std::list headers; headers.push_back(AuthString); auto request = std::make_unique(); // 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) { } }