ADD: added updated drone.yml and changed some function calls

This commit is contained in:
Henry Winkel
2024-03-13 16:41:56 +01:00
parent 48101a673a
commit 0e05106d1e
13 changed files with 264 additions and 231 deletions

View File

@@ -1,5 +1,6 @@
#include <kubecontrol/PodController.hpp>
#include "curlpp/Options.hpp"
#include "kubecontrol/PodController.hpp"
#include "kubecontrol/Utils.hpp"
#include "nlohmann/json_fwd.hpp"
#include "yaml-cpp/binary.h"
@@ -19,27 +20,30 @@
namespace kubecontrol
namespace kubecontrol
{
KubePod::KubePod(std::string Owner, std::string Uuid, std::string ContainerImage,std::string Namespace)
KubePod::KubePod(std::string &Owner, std::string &Uuid, std::string &ContainerImage,std::string Namespace)
:Owner_(Utils::to_lower(Owner)),
Uuid_(Utils::to_lower(Uuid)),
Namespace_(Namespace),
EnvirmonentVars_()
ContainerRegistry_("kmaster.ti.unibw-hamburg.de:30808"),
Namespace_(Namespace)
{
ContainerRegistry_ = "kmaster.ti.unibw-hamburg.de:30808";
EnvirmonentVars_ = std::map<std::string, std::string>();
this->PathToYaml_ = "config/pods/" + this->Uuid_ + ".yaml";
if( std::filesystem::directory_entry("config").exists() != true)
if( !std::filesystem::directory_entry("config").exists())
{
std::filesystem::create_directory("config");
}
std::filesystem::directory_entry entry("config/pods");
if(entry.exists() != true)
if(!entry.exists())
{
std::filesystem::create_directory("config/pods");
}
@@ -48,25 +52,25 @@ namespace kubecontrol
}
KubePod::KubePod(std::string Owner, std::string Uuid, std::string Component, std::string ContainerImage,std::string Namespace):
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)),
Namespace_(Namespace),
EnvirmonentVars_()
ContainerRegistry_("kmaster.ti.unibw-hamburg.de:30808"),
Namespace_(Namespace)
{
ContainerRegistry_ = "kmaster.ti.unibw-hamburg.de:30808";
EnvirmonentVars_ = std::map<std::string, std::string>();
this->PathToYaml_ = "config/pods/" + this->Uuid_ + ".yaml";
if( std::filesystem::directory_entry("config").exists() != true)
if( !std::filesystem::directory_entry("config").exists())
{
std::filesystem::create_directory("config");
}
std::filesystem::directory_entry entry("config/pods");
if(entry.exists() != true)
if(!entry.exists())
{
std::filesystem::create_directory("config/pods");
}
@@ -80,25 +84,25 @@ namespace kubecontrol
void KubePod::addContainer(std::string name,std::string image, std::shared_ptr<std::map<std::string, std::string>> envs, PullPolicy pullPolicy )
{
YAML::Node container;
transform(image.begin(), image.end(), image.begin(), ::tolower);
transform(name.begin(), name.end(), name.begin(), ::tolower);
transform(image.begin(), image.end(), image.begin(), ::tolower);
transform(name.begin(), name.end(), name.begin(), ::tolower);
container["name"] = name+"-container";
container["image"] = this->ContainerRegistry_ + "/" + image;
container["imagePullPolicy"] = toString(pullPolicy);
if (envs != nullptr)
if (envs != nullptr)
{
int i = 0;
int lauf = 0;
for(auto [key,value] : *envs)
{
container["env"][i]["name"] = key;
container["env"][i]["value"] = value;
i++;
container["env"][lauf]["name"] = key;
container["env"][lauf]["value"] = value;
lauf++;
}
}
}
ContainerImages_.push_back(container);
}
@@ -138,12 +142,12 @@ namespace kubecontrol
{
if(EnvirmonentVars_.contains(key)) {
return EnvirmonentVars_[key];
} else {
return nullptr;
}
return "";
}
void KubePod::setArgs(std::string args)
void KubePod::setArgs(std::string &args)
{
Args_.emplace_back(Utils::to_lower(args));
}
@@ -153,7 +157,7 @@ namespace kubecontrol
return Args_;
}
void KubePod::setCommand(std::string command)
void KubePod::setCommand(std::string &command)
{
PodCommand_ = Utils::to_lower(command);
}
@@ -163,7 +167,7 @@ namespace kubecontrol
return PodCommand_;
}
void KubePod::setComponent(std::string component)
void KubePod::setComponent(std::string &component)
{
this->Component_ = Utils::to_lower(component);
}
@@ -173,7 +177,7 @@ namespace kubecontrol
return this->Component_;
}
void KubePod::setName(std::string name)
void KubePod::setName(std::string &name)
{
this->Name_ = name;
}
@@ -200,9 +204,9 @@ namespace kubecontrol
// if (!std::empty(this->Component_)) node["metadata"]["labels"]["app.kubernetes.io/component"] = this->Component_;
node["metadata"]["labels"]["app.kubernetes.io/component"] = this->Component_;
for (int i = 0; i< this->ContainerImages_.size(); i++)
for (int i = 0; i< this->ContainerImages_.size(); i++)
{
node["spec"]["containers"][i] = this->ContainerImages_[i];
// node["spec"]["containers"][i]["name"] = this->Uuid_+"-container";
@@ -212,36 +216,36 @@ namespace kubecontrol
// node["spec"]["containers"][i]["ports"][0]["protocol"] = "UDP";
}
}
int i = 0;
for (auto item :EnvirmonentVars_) {
for (const auto& item :EnvirmonentVars_) {
node["spec"]["containers"][0]["env"][i]["name"] = item.first;
node["spec"]["containers"][0]["env"][i]["value"] = item.second;
i++;
}
if (Args_.size() > 0)
if (!Args_.empty())
{
node["spec"]["containers"][0]["args"].SetStyle(YAML::EmitterStyle::Flow);
for (auto item : Args_) {
for (const auto& item : Args_) {
node["spec"]["containers"][0]["args"].push_back(item);
}
}
if (!PodCommand_.empty())
if (!PodCommand_.empty())
{
node["spec"]["containers"][0]["command"].SetStyle(YAML::EmitterStyle::Flow);
node["spec"]["containers"][0]["command"].push_back(PodCommand_);
}
node["spec"]["terminationGracePeriodSeconds"] = 5;
node["spec"]["restartPolicy"] = "Never";
YAMLNode_ = node;
@@ -250,19 +254,19 @@ namespace kubecontrol
fout << node;
fout.close();
return this->PathToYaml_;
}
int KubePod::start(KubernetesAPI APIInterface,bool WaitTillRunning)
{
std::string request = "/api/v1/namespaces/simulator/pods/";
this->createYAML();
std::stringstream stream;
stream << YAMLNode_;
std::string response = APIInterface.performRequest(request,"POST",stream.str());
@@ -271,10 +275,10 @@ namespace kubecontrol
auto timeoutTime = std::chrono::system_clock::now() + std::chrono::seconds(10);
if (WaitTillRunning == true ) {
if (WaitTillRunning ) {
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));
this->updateInfoForThisPod(APIInterface);
@@ -288,15 +292,15 @@ namespace kubecontrol
return 0;
}
int KubePod::stop(KubernetesAPI APIInterface)
{
std::string request = "/api/v1/namespaces/simulator/pods/"+Uuid_;
std::string result = APIInterface.performRequest(request,"DELETE");
LOG_S(INFO)<<"stopping: " <<Uuid_;
LOG_S(INFO)<<"stopping: " <<Uuid_;
return 0;
}
@@ -305,19 +309,19 @@ namespace kubecontrol
int KubePod::stopChilds(KubernetesAPI APIInterface)
{
auto uuids = this->getPodsChilds(APIInterface);
if (uuids.size() == 0)
if (uuids.empty())
{
return 1;
}
for(auto i: uuids)
for(const auto& uuid: uuids)
{
std::string request = "/api/v1/namespaces/simulator/pods/"+i.UUID;
std::string request = "/api/v1/namespaces/simulator/pods/"+uuid.UUID;
std::string result = APIInterface.performRequest(request,"DELETE");
}
return 0;
}
@@ -326,15 +330,15 @@ namespace kubecontrol
{
std::string request = "/api/v1/namespaces/simulator/pods/"+Uuid_+"/status";
std::string result = APIInterface.performRequest(request,"GET");
if (result == "")
if (result.empty())
{
return 1;
}
if (extractInformationFromResopnse(result)!= 0)
if (extractInformationFromResopnse(result)!= 0)
{
return 1;
}
}
return 0;
}
@@ -344,12 +348,12 @@ namespace kubecontrol
std::string request = "/api/v1/namespaces/simulator/pods?labelSelector=app.kubernetes.io/part-of="+Uuid_;
std::string result = APIInterface.performRequest(request,"GET");
if (result == "")
if (result.empty())
{
return std::vector<PodChild>();
return {};
}
extractInformationFromResopnse(result);
extractInformationFromResopnse(result);
return this->ChildPods;
}
@@ -357,7 +361,7 @@ namespace kubecontrol
int KubePod::extractInformationFromResopnse(std::string response)
{
if (response == "")
if (response.empty())
{
return 1 ;
}
@@ -366,16 +370,16 @@ namespace kubecontrol
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"])
}else if(j.contains("items") && j["items"].is_array())
{
for (auto i : j["items"])
{
// LOG_S(INFO)<<i;
PodChild child;
@@ -389,7 +393,7 @@ namespace kubecontrol
}else{
return 1;
}
} catch (const std::exception e) {
} 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();
@@ -398,17 +402,16 @@ namespace kubecontrol
}
return 0;
}
}