ADD: added a general kubenetes API and integrated that in the classes and remodeled the info management

This commit is contained in:
hwinkel
2023-12-22 13:14:40 +01:00
parent 351cf9ec69
commit 4045f21786
13 changed files with 363 additions and 833 deletions

View File

@@ -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;
}