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

@@ -4,6 +4,7 @@
#include "nlohmann/json_fwd.hpp" #include "nlohmann/json_fwd.hpp"
#include "yaml-cpp/node/node.h" #include "yaml-cpp/node/node.h"
#include <map> #include <map>
#include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
#include <yaml-cpp/yaml.h> #include <yaml-cpp/yaml.h>
@@ -22,12 +23,15 @@
namespace kubecontrol namespace kubecontrol
{ {
class KubePod class KubePod
{ {
public: public:
KubePod(std::string Owner, std::string Uuid, std::string ContainerImage,std::string Namespace = "simulator") ; KubePod(std::string Owner, std::string Uuid, std::string ContainerImage,std::string Namespace = "simulator") ;
KubePod(std::string Owner, std::string Uuid, std::string Component, std::string ContainerImage,std::string Namespace = "simulator"); KubePod(std::string Owner, std::string Uuid, std::string Component, std::string ContainerImage,std::string Namespace = "simulator");
std::string getUUID(); std::string getUUID();
std::string getOwner(); std::string getOwner();
std::string getIp(); std::string getIp();
@@ -46,6 +50,8 @@ namespace kubecontrol
void setComponent(std::string component); void setComponent(std::string component);
std::string getComponent(); std::string getComponent();
void addContainer(std::string name,std::string image, std::shared_ptr<std::map<std::string, std::string>> envs = nullptr, std::string PullPolicy = "Always");
/** /**
* @brief sets the name of the pod * @brief sets the name of the pod
* @param string - term to be displayed * @param string - term to be displayed
@@ -78,11 +84,13 @@ namespace kubecontrol
static const int MaxWaitTimeInSeconds; static const int MaxWaitTimeInSeconds;
std::string Owner_; std::string Owner_;
std::string Uuid_; std::string Uuid_;
std::string Name_; std::string Name_;
std::string Component_; std::string Component_;
std::string ContainerImage_; std::vector<YAML::Node> ContainerImages_;
// std::string ContainerImage_;
std::string ContainerRegistry_; std::string ContainerRegistry_;
std::string PathToYaml_; std::string PathToYaml_;
std::string Namespace_; std::string Namespace_;

View File

@@ -2,15 +2,19 @@
#include "kubecontrol/Utils.hpp" #include "kubecontrol/Utils.hpp"
#include "nlohmann/json_fwd.hpp" #include "nlohmann/json_fwd.hpp"
#include "yaml-cpp/binary.h" #include "yaml-cpp/binary.h"
#include <cctype>
#include <exception> #include <exception>
#include <fstream> #include <fstream>
#include <future>
#include <iterator> #include <iterator>
#include <kubecontrol/KubePod.hpp> #include <kubecontrol/KubePod.hpp>
#include <memory>
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <thread> #include <thread>
#include <filesystem> #include <filesystem>
#include "loguru.hpp" #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) KubePod::KubePod(std::string Owner, std::string Uuid, std::string ContainerImage,std::string Namespace)
:Owner_(Utils::to_lower(Owner)), :Owner_(Utils::to_lower(Owner)),
Uuid_(Utils::to_lower(Uuid)), Uuid_(Utils::to_lower(Uuid)),
ContainerImage_(ContainerImage),
Namespace_(Namespace), Namespace_(Namespace),
EnvirmonentVars_() EnvirmonentVars_()
{ {
ContainerRegistry_ = "kmaster.ti.unibw-hamburg.de:30808"; ContainerRegistry_ = "kmaster.ti.unibw-hamburg.de:30808";
EnvirmonentVars_ = std::map<std::string, std::string>(); EnvirmonentVars_ = std::map<std::string, std::string>();
@@ -38,6 +42,8 @@ namespace kubecontrol
{ {
std::filesystem::create_directory("config/pods"); std::filesystem::create_directory("config/pods");
} }
addContainer(Uuid, ContainerImage);
} }
@@ -45,10 +51,10 @@ namespace kubecontrol
Owner_(Utils::to_lower(Owner)), Owner_(Utils::to_lower(Owner)),
Uuid_(Utils::to_lower(Uuid)), Uuid_(Utils::to_lower(Uuid)),
Component_(Utils::to_lower(Component)), Component_(Utils::to_lower(Component)),
ContainerImage_(ContainerImage),
Namespace_(Namespace), Namespace_(Namespace),
EnvirmonentVars_() EnvirmonentVars_()
{ {
ContainerRegistry_ = "kmaster.ti.unibw-hamburg.de:30808"; ContainerRegistry_ = "kmaster.ti.unibw-hamburg.de:30808";
EnvirmonentVars_ = std::map<std::string, std::string>(); EnvirmonentVars_ = std::map<std::string, std::string>();
@@ -63,6 +69,36 @@ namespace kubecontrol
{ {
std::filesystem::create_directory("config/pods"); 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);
} }
@@ -164,11 +200,21 @@ namespace kubecontrol
// if (!std::empty(this->Component_)) node["metadata"]["labels"]["app.kubernetes.io/component"] = this->Component_; // if (!std::empty(this->Component_)) node["metadata"]["labels"]["app.kubernetes.io/component"] = this->Component_;
node["metadata"]["labels"]["app.kubernetes.io/component"] = this->Component_; node["metadata"]["labels"]["app.kubernetes.io/component"] = this->Component_;
node["spec"]["containers"][0]["name"] = this->Uuid_+"-container";
node["spec"]["containers"][0]["image"] = this->ContainerRegistry_ + "/" + this->ContainerImage_; for (int i = 0; i< this->ContainerImages_.size(); i++)
node["spec"]["containers"][0]["imagePullPolicy"] = "Always"; {
node["spec"]["containers"][0]["ports"][0]["containerPort"] = 10000; node["spec"]["containers"][i] = this->ContainerImages_[i];
node["spec"]["containers"][0]["ports"][0]["protocol"] = "UDP"; // 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";
}
int i = 0; int i = 0;
for (auto item :EnvirmonentVars_) { for (auto item :EnvirmonentVars_) {

View File

@@ -3,6 +3,8 @@
#include "kubecontrol/PodController.hpp" #include "kubecontrol/PodController.hpp"
#include "nlohmann/json_fwd.hpp" #include "nlohmann/json_fwd.hpp"
#include <memory>
#include <string>
#include <thread> #include <thread>
#define CATCH_CONFIG_MAIN #define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp> #include <catch2/catch.hpp>
@@ -16,7 +18,8 @@ SCENARIO("Testing the SimCore Sensor")
// kubecontrol::PodController podc("docs/config"); // kubecontrol::PodController podc("docs/config");
kubecontrol::KubernetesAPI api(YAML::LoadFile("docs/config")); kubecontrol::KubernetesAPI api(YAML::LoadFile("docs/config"));
std::string name = "hamburg"; std::string name = "componenttest1";
std::string uuid = name; std::string uuid = name;
kubecontrol::KubePod ShipPod1("controller",uuid,"ship","ship:latest","simulator"); kubecontrol::KubePod ShipPod1("controller",uuid,"ship","ship:latest","simulator");
@@ -45,8 +48,39 @@ LOG_S(INFO)<<"Starting";
ShipPod1.start(api); ShipPod1.start(api);
LOG_S(INFO)<<"started"; LOG_S(INFO)<<"started";
std::string name2 = "componenttest2";
kubecontrol::KubePod ShipPod2("controller",name2,"CMS","ship:latest","simulator");
nlohmann::json vars1;
vars1["ENTITY_ID"] = name2;
vars1["ENTITY_NAME"] = "FGS Hamburg";
vars1["ENTITY_SIDE"] = "Neutral";
vars1["POSITION"]["LAT"] = "55";
vars1["POSITION"]["LON"] = "8";
vars1["POSITION"]["Height"] = "0";
vars1["COURSE"] = "0";
vars1["SPEED"] = "0";
vars1["GROUNDTRUTH_PORT"] = std::to_string(10000);
vars1["GROUNDTRUTH_ADDR"] = "239.0.0.1";
vars1["COMMAND_PORT"] = "5555";
vars1["ENTITY_RCS"] = std::to_string(850);
vars["ENTITY_SENSORS"].push_back("radar:latest");
ShipPod2.setEnvironmentVar("CONFIG", vars1.dump());
auto envmaps2 = std::make_shared<std::map<std::string, std::string>>();
envmaps2->emplace("CONFIG", vars1.dump());
ShipPod2.addContainer("CMS", "systemprototype:latest",envmaps2);
ShipPod2.setEnvironmentVar("CONFIG", vars.dump());
LOG_S(INFO)<<"Starting multi container pod";
ShipPod2.start(api);
LOG_S(INFO)<<"started multi container pod";
std::this_thread::sleep_for(std::chrono::milliseconds(5000));
GIVEN("different Attributes for a Track in different forms") GIVEN("different Attributes for a Track in different forms")
{ {
@@ -55,7 +89,8 @@ std::this_thread::sleep_for(std::chrono::milliseconds(5000));
THEN("check if Track attributes are correct") THEN("check if Track attributes are correct")
{ {
REQUIRE(ShipPod1.getUUID() == "hamburg"); std::this_thread::sleep_for(std::chrono::milliseconds(5000));
REQUIRE(ShipPod1.getUUID() == name);
REQUIRE(ShipPod1.getName() == name); REQUIRE(ShipPod1.getName() == name);
REQUIRE(ShipPod1.getComponent() == "ship"); REQUIRE(ShipPod1.getComponent() == "ship");
@@ -69,6 +104,7 @@ std::this_thread::sleep_for(std::chrono::milliseconds(5000));
ShipPod1.stop(api); ShipPod1.stop(api);
ShipPod2.stop(api);