From 263aaa6a7112565b83ba0e16a17c33f9ae91c7a4 Mon Sep 17 00:00:00 2001 From: Henry Winkel Date: Tue, 12 Mar 2024 15:23:37 +0100 Subject: [PATCH] ADD: add posibility to start a pod with multiple containers --- include/kubecontrol/KubePod.hpp | 10 +++++- libs/yaml-cpp | 2 +- src/kubecontrol/KubePod.cpp | 60 +++++++++++++++++++++++++++++---- tests/test_KubePod.cpp | 42 +++++++++++++++++++++-- 4 files changed, 102 insertions(+), 12 deletions(-) diff --git a/include/kubecontrol/KubePod.hpp b/include/kubecontrol/KubePod.hpp index 1513385..e8f9a62 100644 --- a/include/kubecontrol/KubePod.hpp +++ b/include/kubecontrol/KubePod.hpp @@ -4,6 +4,7 @@ #include "nlohmann/json_fwd.hpp" #include "yaml-cpp/node/node.h" #include +#include #include #include #include @@ -22,12 +23,15 @@ namespace kubecontrol { + + class KubePod { public: 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"); + std::string getUUID(); std::string getOwner(); std::string getIp(); @@ -46,6 +50,8 @@ namespace kubecontrol void setComponent(std::string component); std::string getComponent(); + void addContainer(std::string name,std::string image, std::shared_ptr> envs = nullptr, std::string PullPolicy = "Always"); + /** * @brief sets the name of the pod * @param string - term to be displayed @@ -78,11 +84,13 @@ namespace kubecontrol static const int MaxWaitTimeInSeconds; + std::string Owner_; std::string Uuid_; std::string Name_; std::string Component_; - std::string ContainerImage_; + std::vector ContainerImages_; + // std::string ContainerImage_; std::string ContainerRegistry_; std::string PathToYaml_; std::string Namespace_; diff --git a/libs/yaml-cpp b/libs/yaml-cpp index c7639e8..76dc671 160000 --- a/libs/yaml-cpp +++ b/libs/yaml-cpp @@ -1 +1 @@ -Subproject commit c7639e81d5f00a5b47a2c9bd668f10d74b949071 +Subproject commit 76dc6715734295ff1866bfc32872ff2278258fc8 diff --git a/src/kubecontrol/KubePod.cpp b/src/kubecontrol/KubePod.cpp index 3237f6b..fcbde05 100644 --- a/src/kubecontrol/KubePod.cpp +++ b/src/kubecontrol/KubePod.cpp @@ -2,15 +2,19 @@ #include "kubecontrol/Utils.hpp" #include "nlohmann/json_fwd.hpp" #include "yaml-cpp/binary.h" +#include #include #include +#include #include #include +#include #include #include #include #include #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(); @@ -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(); @@ -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> 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_) { diff --git a/tests/test_KubePod.cpp b/tests/test_KubePod.cpp index 771b8a6..95ac4ec 100644 --- a/tests/test_KubePod.cpp +++ b/tests/test_KubePod.cpp @@ -3,6 +3,8 @@ #include "kubecontrol/PodController.hpp" #include "nlohmann/json_fwd.hpp" +#include +#include #include #define CATCH_CONFIG_MAIN #include @@ -16,7 +18,8 @@ SCENARIO("Testing the SimCore Sensor") // kubecontrol::PodController podc("docs/config"); kubecontrol::KubernetesAPI api(YAML::LoadFile("docs/config")); - std::string name = "hamburg"; + std::string name = "componenttest1"; + std::string uuid = name; kubecontrol::KubePod ShipPod1("controller",uuid,"ship","ship:latest","simulator"); @@ -45,8 +48,39 @@ LOG_S(INFO)<<"Starting"; ShipPod1.start(api); 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>(); +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") { @@ -55,7 +89,8 @@ std::this_thread::sleep_for(std::chrono::milliseconds(5000)); 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.getComponent() == "ship"); @@ -69,6 +104,7 @@ std::this_thread::sleep_for(std::chrono::milliseconds(5000)); ShipPod1.stop(api); + ShipPod2.stop(api);