ADD: added container class and adapted test

This commit is contained in:
Henry Winkel
2024-03-14 09:46:37 +01:00
parent 37627a16ca
commit b645005aeb
11 changed files with 219 additions and 70 deletions

View File

@@ -1,6 +1,7 @@
#include <kubecontrol/PodController.hpp>
#include "curlpp/Options.hpp"
#include "kubecontrol/Container.hpp"
#include "kubecontrol/Utils.hpp"
#include "nlohmann/json_fwd.hpp"
#include "yaml-cpp/binary.h"
@@ -10,6 +11,7 @@
#include <future>
#include <iterator>
#include <kubecontrol/KubePod.hpp>
#include <map>
#include <memory>
#include <sstream>
#include <string>
@@ -26,7 +28,7 @@ namespace kubecontrol
KubePod::KubePod(std::string &Owner, std::string &Uuid, std::string &ContainerImage,std::string Namespace)
KubePod::KubePod(const std::string &Owner, const std::string &Uuid, const std::string &ContainerImage, const std::string &Namespace)
:Owner_(Utils::to_lower(Owner)),
Uuid_(Utils::to_lower(Uuid)),
ContainerRegistry_("kmaster.ti.unibw-hamburg.de:30808"),
@@ -34,7 +36,7 @@ namespace kubecontrol
{
EnvirmonentVars_ = std::map<std::string, std::string>();
// EnvirmonentVars_ = std::map<std::string, std::string>();
this->PathToYaml_ = "config/pods/" + this->Uuid_ + ".yaml";
@@ -48,11 +50,12 @@ namespace kubecontrol
std::filesystem::create_directory("config/pods");
}
addContainer(Uuid, ContainerImage);
auto map = std::map<std::string, std::string>();
addContainer(Uuid, ContainerImage,map);
}
KubePod::KubePod(std::string &Owner, std::string &Uuid, std::string &Component, std::string &ContainerImage,std::string Namespace):
KubePod::KubePod(const std::string &Owner, const std::string &Uuid, const std::string &Component, const std::string &ContainerImage, const std::string &Namespace):
Owner_(Utils::to_lower(Owner)),
Uuid_(Utils::to_lower(Uuid)),
Component_(Utils::to_lower(Component)),
@@ -61,7 +64,7 @@ namespace kubecontrol
{
EnvirmonentVars_ = std::map<std::string, std::string>();
// EnvirmonentVars_ = std::map<std::string, std::string>();
this->PathToYaml_ = "config/pods/" + this->Uuid_ + ".yaml";
@@ -74,38 +77,41 @@ namespace kubecontrol
{
std::filesystem::create_directory("config/pods");
}
auto map = std::map<std::string, std::string>();
addContainer(Uuid, ContainerImage);
addContainer(Uuid, ContainerImage,map);
}
void KubePod::addContainer(std::string name,std::string image, std::shared_ptr<std::map<std::string, std::string>> envs, PullPolicy pullPolicy )
void KubePod::addContainer(std::string name,std::string image, 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);
container["name"] = name+"-container";
container["image"] = this->ContainerRegistry_ + "/" + image;
container["imagePullPolicy"] = toString(pullPolicy);
if (envs != nullptr)
{
int lauf = 0;
for(auto [key,value] : *envs)
{
container["env"][lauf]["name"] = key;
container["env"][lauf]["value"] = value;
lauf++;
}
}
Container container(Owner_,name,image,pullPolicy);
container.addEnv(envs);
ContainerImages_.push_back(container);
}
void KubePod::addContainer(Container containter)
{
ContainerImages_.push_back(containter);
}
void KubePod::addEnvValuesToContainer(std::string containerUUID, std::map<std::string, std::string> envs)
{
for (auto container: ContainerImages_)
{
if (container.getUUID() == containerUUID +"-container")
{
container.addEnv(envs);
}
}
}
@@ -208,7 +214,7 @@ namespace kubecontrol
for (int i = 0; i< this->ContainerImages_.size(); i++)
{
node["spec"]["containers"][i] = this->ContainerImages_[i];
node["spec"]["containers"][i] = this->ContainerImages_[i].toYAML();
// node["spec"]["containers"][i]["name"] = this->Uuid_+"-container";
// node["spec"]["containers"][i]["image"] = this->ContainerRegistry_ + "/" + this->ContainerImages_[i];
// node["spec"]["containers"][i]["imagePullPolicy"] = imagePullPolicy;