ADD: added a podcontroller and test for the first basic usage

This commit is contained in:
Henry Winkel
2023-08-08 15:09:18 +02:00
parent 1c4c883648
commit 5f043ca88e
13 changed files with 196 additions and 192 deletions

View File

@@ -63,11 +63,12 @@ namespace kubecontrol
node["spec"]["containers"][0]["name"] = this->Label_+"-container";
node["spec"]["containers"][0]["image"] = this->ContainerRegistry_ + "/" + this->ContainerImage_;
node["spec"]["containers"][0]["imagePullPolicy"] = "Always";
int i = 0;
for (auto item :EnvirmonentVars_) {
node["spec"]["containers"][0]["env"][i]["name"] = item.first;
node["spec"]["containers"][0]["env"][i]["value"] = R"(item.second)";
node["spec"]["containers"][0]["env"][i]["value"] = item.second;
}
@@ -82,7 +83,7 @@ namespace kubecontrol
}
nlohmann::json KubePod::start(std::string apiAddress,std::string token)
std::string KubePod::start(std::string apiAddress,std::string token)
{
std::string curlURL = apiAddress;
std::string AuthString = "Authorization: Bearer " + token;
@@ -109,28 +110,30 @@ namespace kubecontrol
request.setOpt(new curlpp::options::Post(true));
if(std::filesystem::exists(this->PathToYaml_) != true)
{
this->createYAML();
}
std::ifstream is;
is.open (this->PathToYaml_, std::ios::binary );
is.seekg (0, std::ios::end);
long length = is.tellg();
is.seekg (0, std::ios::beg);
char *buffer = new char [length];
is.read (buffer,length);
this->createYAML();
LOG_S(ERROR)<< this->PathToYaml_;
std::ifstream is;
is.open (this->PathToYaml_, std::ios::binary );
is.seekg (0, std::ios::end);
long length = is.tellg();
is.seekg (0, std::ios::beg);
char *buffer = new char [length];
is.read(buffer,length);
is.close();
request.setOpt(new curlpp::options::PostFields(buffer));
request.perform();
request.reset();
auto response = mWriterChunk.getResponse();
return nlohmann::json::parse(response);
return nlohmann::json::parse(response).dump();
}
nlohmann::json KubePod::stop(std::string apiAddress,std::string token)
std::string KubePod::stop(std::string apiAddress,std::string token)
{
std::string curlURL = apiAddress+ this->Uuid_;
std::string AuthString = "Authorization: Bearer " + token;
@@ -160,12 +163,12 @@ namespace kubecontrol
request.perform();
auto response = mWriterChunk.getResponse();
return nlohmann::json::parse(response);
return nlohmann::json::parse(response).dump();
}
nlohmann::json KubePod::getInfo(std::string apiAddress,std::string token)
std::string KubePod::getInfo(std::string apiAddress,std::string token)
{
std::string curlURL = apiAddress;
std::string curlURL = apiAddress+"/"+Uuid_;
std::string AuthString = "Authorization: Bearer " + token;
std::list<std::string> headers;
@@ -191,7 +194,7 @@ namespace kubecontrol
request.perform();
auto response = mWriterChunk.getResponse();
return nlohmann::json::parse(response);
return nlohmann::json::parse(response).dump();
}
}

View File

@@ -1,5 +1,8 @@
#include <cstddef>
#include <cstdlib>
#include <kubecontrol/PodController.hpp>
#include <string>
namespace kubecontrol
@@ -9,7 +12,20 @@ namespace kubecontrol
YAML::Node config = YAML::LoadFile(pathToKubectlConfig);
BearerToken_ = config["users"][0]["user"]["token"].as<std::string>();
ServerAddress_ = config["clusters"][0]["cluster"]["server"].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_ = 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/hwinkel/pods/";
}
@@ -19,4 +35,83 @@ namespace kubecontrol
}
void PodController::startPod(KubePod Pod)
{
PodList_.emplace_back(Pod);
LOG_S(INFO)<< "starting pod: "<<Pod.getUUID();
Pod.start(ServerAddress_+ApiCall_, BearerToken_);
}
void PodController::stopPod(std::string Label)
{
for (auto item : PodList_)
{
if (Label == item.getLabel())
{
item.stop(ServerAddress_+ApiCall_, BearerToken_);
}
}
}
void PodController::stopAllPods()
{
for (auto item : PodList_)
{
item.stop(ServerAddress_+ApiCall_, BearerToken_);
LOG_S(INFO)<< "stopping pod: "<<item.getUUID();
}
}
std::string PodController::getPodsInfo()
{
std::string curlURL = ServerAddress_+ApiCall_;
std::string AuthString = "Authorization: Bearer " + BearerToken_;
std::list<std::string> headers;
headers.push_back(AuthString);
curlpp::Easy request;
WriterMemoryClass mWriterChunk;
curlpp::types::WriteFunctionFunctor functor = std::bind(&WriterMemoryClass::WriteMemoryCallback, &mWriterChunk, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
curlpp::options::WriteFunction *test = new curlpp::options::WriteFunction(functor);
request.setOpt(test);
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();
auto response = mWriterChunk.getResponse();
auto j = nlohmann::json::parse(response);
return j.dump();
}
std::string PodController::getInfoForPod(std::string Label)
{
for (auto item : PodList_)
{
if (Label == item.getLabel())
{
return item.getInfo(ServerAddress_+ApiCall_, BearerToken_);
}
}
return nullptr;
}
}

View File

@@ -1,7 +1,12 @@
#include "nlohmann/json_fwd.hpp"
#include "yaml-cpp/binary.h"
#include "yaml-cpp/emitter.h"
#include "yaml-cpp/emitterdef.h"
#include "yaml-cpp/emittermanip.h"
#include "yaml-cpp/emitterstyle.h"
#include "yaml-cpp/node/convert.h"
#include "yaml-cpp/node/node.h"
#include <iomanip>
#include <kubecontrol/kubecontrol.hpp>
@@ -10,6 +15,7 @@
#include <curlpp/Options.hpp>
#include <curlpp/Exception.hpp>
#include <sstream>
#include <string>
#include <yaml-cpp/yaml.h>
@@ -109,6 +115,12 @@ namespace kubecontrol
auto yaml = createYAML();
// YAML::Node node = YAML::LoadFile("config/pods_deployment.yaml");
// YAML::Binary node = YAML::LoadFile("config/pods_deployment.yaml");
// LOG_S(INFO)<< node["spec"]["containers"][0]["imagePullPolicy"];
// YAML::Binary binary = node.as<YAML::Binary>();
std::ifstream is;
is.open ("config/pods_deployment.yaml", std::ios::binary );
@@ -123,7 +135,7 @@ namespace kubecontrol
// read data as a block:
is.read (buffer,length);
std::istringstream myStream((std::string( buffer )));
int size = myStream.str().size();
is.close();
@@ -137,7 +149,7 @@ namespace kubecontrol
request.perform();
auto response = mWriterChunk.getResponse();
// LOG_S(INFO)<<response;
LOG_S(INFO)<<response;
}
@@ -168,7 +180,7 @@ namespace kubecontrol
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::Post(true));
// request.setOpt(new curlpp::options::PostFields("DELETE"));
@@ -185,20 +197,27 @@ namespace kubecontrol
std::string kubecontrol::createYAML()
{
YAML::Node node;
node["apiVersion"] = "v1";
node["kind"] = "Pod";
node["metadata"]["name"] = "debug-debian";
node["metadata"]["labels"]["app.kubernetes.io/name"] = "plaindebian";
node["spec"]["containers"][0]["name"] = "debug-debian-container";
node["spec"]["containers"][0]["image"] = "kmaster.ti.unibw-hamburg.de:30808/debugdebianhenry:0.1.2";
node["spec"]["containers"][0]["image"] = "kmaster.ti.unibw-hamburg.de:30808/debugdebianhenry:0.1.3";
// node["spec"]["containers"][0]["imagePullPolicy"] = "\"Always\"";
node["spec"]["containers"][0]["env"][0]["name"] = "OWN_SHIP_SERVER";
node["spec"]["containers"][0]["env"][0]["value"] = "127.0.0.1";
node["spec"]["containers"][0]["env"][1]["name"] = "TOKEN";
node["spec"]["containers"][0]["env"][1]["value"] = BearerToken_;
node["spec"]["containers"][0]["args"].SetStyle(YAML::EmitterStyle::Flow);
node["spec"]["containers"][0]["args"].push_back("-c");
node["spec"]["containers"][0]["args"].push_back("while true; do echo hello; sleep 5;done");
@@ -210,7 +229,10 @@ namespace kubecontrol
std::ofstream fout("config/pods_deployment.yaml");
fout << node;
std::stringstream ss;
ss <<node;
LOG_S(INFO)<< node;
fout << ss.str();
fout.close();
return "config/pods_deployment.yaml";