ADD: added central kubernetes api class

This commit is contained in:
Henry Winkel
2023-12-21 17:15:28 +01:00
parent d1024de907
commit 351cf9ec69
12 changed files with 514 additions and 128 deletions

View File

@@ -1,6 +1,9 @@
#include "curlpp/Options.hpp"
#include "kubecontrol/PodInfo.hpp"
#include "kubecontrol/Utils.hpp"
#include "nlohmann/json_fwd.hpp"
#include "yaml-cpp/binary.h"
#include <exception>
#include <fstream>
#include <iterator>
#include <kubecontrol/KubePod.hpp>
@@ -170,7 +173,7 @@ namespace kubecontrol
node["spec"]["containers"][0]["command"].push_back(PodCommand_);
}
node["spec"]["terminationGracePeriodSeconds"] = 3;
node["spec"]["terminationGracePeriodSeconds"] = 10;
node["spec"]["restartPolicy"] = "Never";
@@ -185,62 +188,43 @@ namespace kubecontrol
}
std::string KubePod::start(std::string apiAddress,std::string token)
std::string KubePod::start(KubernetesAPI APIInterface,bool WaitTillRunning)
{
std::string curlURL = apiAddress;
std::string AuthString = "Authorization: Bearer " + token;
std::list<std::string> headers;
headers.push_back(AuthString);
headers.push_back("Content-Type: application/yaml");
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::Post(true));
request.setOpt(new curlpp::options::CustomRequest("POST"));
// request.setOpt(new curlpp::options::Verbose("POST"));
std::string request = "/api/v1/namespaces/simulator/pods/";
this->createYAML();
LOG_S(INFO)<< this->PathToYaml_;
// LOG_S(INFO)<< this->PathToYaml_;
std::stringstream stream;
stream << YAMLNode_;
// request.setOpt(new curlpp::options::PostFields(buffer));
request.setOpt(new curlpp::options::PostFields(stream.str()));
std::string response = APIInterface.performRequest(request,"POST",stream.str());
request.perform();
request.reset();
std::string response = result.str();
InfoPod.update(response);
while (InfoPod.Status != "Running" && InfoPod.Status != "Succeeded") {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
response = this->getInfo(curlURL, token);
InfoPod.update(response);
}
if (WaitTillRunning == true) {
return response;
while (InfoPod.Status != "Running" && InfoPod.Status != "Succeeded") {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
response = this->getInfoForThisPod(APIInterface);
InfoPod.update(response);
}
}
return response;
}
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;
@@ -267,12 +251,20 @@ namespace kubecontrol
request.perform();
auto response = result.str();
LOG_S(INFO)<<InfoPod.getRelatedPods().size();
// 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]);
StopChilds(apiAddress, token, InfoPod.getRelatedPods()[i]);
}
}
@@ -281,6 +273,19 @@ namespace kubecontrol
return response;
}
std::string KubePod::stop(KubernetesAPI APIInterface)
{
std::string request = "/api/v1/namespaces/simulator/pods/"+Uuid_;
std::string result = APIInterface.performRequest(request,"DELETE");
this->getInfoForRelatedPods(APIInterface);
return result;
}
std::string KubePod::StopChilds(std::string apiAddress,std::string token,std::string uuid)
{
std::string curlURL = apiAddress+ uuid;
@@ -316,34 +321,88 @@ namespace kubecontrol
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);
// 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;
// curlpp::Cleanup cleaner;
// curlpp::Easy request;
std::stringstream result;
request.setOpt(cURLpp::Options::WriteStream(&result));
// std::stringstream result;
// request.setOpt(cURLpp::Options::WriteStream(&result));
request.setOpt(new curlpp::options::HttpHeader(headers));
// request.setOpt(new curlpp::options::HttpHeader(headers));
request.setOpt(new curlpp::options::Url(curlURL));
// request.setOpt(new curlpp::options::Url(curlURL));
request.setOpt(new curlpp::options::SslVerifyPeer(false));
// request.setOpt(new curlpp::options::SslVerifyPeer(false));
request.perform();
return result.str();
// request.perform();
// return result.str();
// }
std::string KubePod::getInfoForThisPod(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 == "")
{
return uuids;
}
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();
}
// LOG_S(INFO)<<rawResponse;
return uuids;
}
}