ADD: add posibility to start a pod with multiple containers

This commit is contained in:
Henry Winkel
2024-03-12 15:23:37 +01:00
parent e38214f4d0
commit 263aaa6a71
4 changed files with 102 additions and 12 deletions

View File

@@ -2,15 +2,19 @@
#include "kubecontrol/Utils.hpp"
#include "nlohmann/json_fwd.hpp"
#include "yaml-cpp/binary.h"
#include <cctype>
#include <exception>
#include <fstream>
#include <future>
#include <iterator>
#include <kubecontrol/KubePod.hpp>
#include <memory>
#include <sstream>
#include <string>
#include <thread>
#include <filesystem>
#include "loguru.hpp"
#include "yaml-cpp/node/node.h"
@@ -20,10 +24,10 @@ namespace kubecontrol
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>();
@@ -38,6 +42,8 @@ namespace kubecontrol
{
std::filesystem::create_directory("config/pods");
}
addContainer(Uuid, ContainerImage);
}
@@ -45,10 +51,10 @@ namespace kubecontrol
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>();
@@ -63,10 +69,40 @@ namespace kubecontrol
{
std::filesystem::create_directory("config/pods");
}
addContainer(Uuid, ContainerImage);
}
void KubePod::addContainer(std::string name,std::string image, std::shared_ptr<std::map<std::string, std::string>> envs, std::string PullPolicy )
{
YAML::Node container;
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"] = PullPolicy;
if (envs != nullptr)
{
int i = 0;
for(auto [key,value] : *envs)
{
container["env"][i]["name"] = key;
container["env"][i]["value"] = value;
i++;
}
}
ContainerImages_.push_back(container);
}
std::string KubePod::getUUID()
{
@@ -163,12 +199,22 @@ 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++)
{
node["spec"]["containers"][i] = this->ContainerImages_[i];
// node["spec"]["containers"][i]["name"] = this->Uuid_+"-container";
// node["spec"]["containers"][i]["image"] = this->ContainerRegistry_ + "/" + this->ContainerImages_[i];
// node["spec"]["containers"][i]["imagePullPolicy"] = imagePullPolicy;
// node["spec"]["containers"][i]["ports"][0]["containerPort"] = 10000;
// node["spec"]["containers"][i]["ports"][0]["protocol"] = "UDP";
}
node["spec"]["containers"][0]["name"] = this->Uuid_+"-container";
node["spec"]["containers"][0]["image"] = this->ContainerRegistry_ + "/" + this->ContainerImage_;
node["spec"]["containers"][0]["imagePullPolicy"] = "Always";
node["spec"]["containers"][0]["ports"][0]["containerPort"] = 10000;
node["spec"]["containers"][0]["ports"][0]["protocol"] = "UDP";
int i = 0;
for (auto item :EnvirmonentVars_) {