From b645005aebe3088b2a123d12dfc9736bf6e316fe Mon Sep 17 00:00:00 2001 From: Henry Winkel Date: Thu, 14 Mar 2024 09:46:37 +0100 Subject: [PATCH] ADD: added container class and adapted test --- .drone.yml | 2 +- CMakeLists.txt | 5 +-- include/kubecontrol/Container.hpp | 75 +++++++++++++++++++++++++++++++ include/kubecontrol/KubePod.hpp | 45 +++++++++---------- include/kubecontrol/Utils.hpp | 32 ++++++++++--- src/kubecontrol/Container.cpp | 55 +++++++++++++++++++++++ src/kubecontrol/KubePod.cpp | 58 +++++++++++++----------- tests/test_KubePod.cpp | 8 ++-- tests/test_kubecontrol.cpp | 2 +- tests/test_massivPodHandling.cpp | 2 +- tests/test_podcontroller.cpp | 5 +-- 11 files changed, 219 insertions(+), 70 deletions(-) create mode 100644 include/kubecontrol/Container.hpp create mode 100644 src/kubecontrol/Container.cpp diff --git a/.drone.yml b/.drone.yml index 69f6dbc..db2617d 100644 --- a/.drone.yml +++ b/.drone.yml @@ -16,7 +16,7 @@ steps: pull: always settings: CODECHECKER_URL: "http://codechecker:8001" - CODECHECKER_PRODUCT: "Kubecontrol" + CODECHECKER_PRODUCT: "kubecontrol" CODECHECKER_USER: from_secret: CODECHECKER_USER_SECRET CODECHECKER_PASS: diff --git a/CMakeLists.txt b/CMakeLists.txt index 80d9b9a..a4ef909 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -62,9 +62,8 @@ ENDIF() include/kubecontrol/Utils.hpp src/kubecontrol/Utils.cpp - # include/kubecontrol/PodInfo.hpp - # src/kubecontrol/PodInfo.cpp - + include/kubecontrol/Container.hpp + src/kubecontrol/Container.cpp include/kubecontrol/KubernetesAPI.hpp src/kubecontrol/KubernetesAPI.cpp diff --git a/include/kubecontrol/Container.hpp b/include/kubecontrol/Container.hpp new file mode 100644 index 0000000..8178edc --- /dev/null +++ b/include/kubecontrol/Container.hpp @@ -0,0 +1,75 @@ +#pragma once +#include +#include +#include +#include +#include + + +namespace kubecontrol +{ + + + + + + class Container + { + public: + /** + * @brief Construct a new Container object + * + * @param Owner + * @param Uuid + * @param ContainerImage + */ + Container(const std::string &Owner, const std::string &Uuid,const std::string &Image, const PullPolicy &policy, const std::string ®istry = "kmaster.ti.unibw-hamburg.de:30808"); + + /** + * @brief adds env to container + * + * @param env + */ + void addEnv(const std::pair &env); + + /** + * @brief add map of envs + * + * @param envs + */ + void addEnv(const std::map &envs); + + /** + * @brief Get the Owner + * + * @return + */ + std::string getOwner() { return owner_;}; + + /** + * @brief get the uuid + * + * @return + */ + std::string getUUID() { return uuid_;}; + + /** + * @brief Get the Image + * + * @return + */ + std::string getImage() { return image_;}; + + + YAML::Node toYAML(); + private: + std::string owner_; + std::string uuid_; + std::string image_; + std::string registry_; + PullPolicy pullPolicy_; + std::map envs_; + + }; + +} diff --git a/include/kubecontrol/KubePod.hpp b/include/kubecontrol/KubePod.hpp index aa886e9..caffe49 100644 --- a/include/kubecontrol/KubePod.hpp +++ b/include/kubecontrol/KubePod.hpp @@ -17,33 +17,13 @@ #include #include +#include namespace kubecontrol { - enum PullPolicy: uint32_t - { - ALWAYS, - IFNOTPRESENT, - NEVER - }; - - - inline std::string toString(const PullPolicy &kind) - { - switch (kind) - { - case PullPolicy::ALWAYS: return "Always"; - case PullPolicy::IFNOTPRESENT: return "IfNotPresent"; - case PullPolicy::NEVER: return "Never"; - default: return "Always"; - } - } - - - struct PodChild { std::string UUID; @@ -68,8 +48,8 @@ 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"); + KubePod(const std::string &Owner, const std::string &Uuid, const std::string &ContainerImage, const std::string &Namespace = "simulator") ; + KubePod(const std::string &Owner, const std::string &Uuid, const std::string &Component, const std::string &ContainerImage, const std::string &Namespace = "simulator"); /** * @brief returns the uuid of the pod @@ -172,7 +152,22 @@ namespace kubecontrol * @param envs std::map * @param PullPolicy std::string (defualt: Always) possible:( IfNotPresent, Never) */ - void addContainer(std::string name,std::string image, std::shared_ptr> envs = nullptr, PullPolicy pullPolicy = ALWAYS); + void addContainer(std::string name,std::string image, std::map &envs , kubecontrol::PullPolicy pullPolicy = ALWAYS); + + /** + * @brief add container object + * + * @param containter + */ + void addContainer(Container containter); + + /** + * @brief + * + * @param containerUUID + * @param envs + */ + void addEnvValuesToContainer(std::string containerUUID, std::map envs); /** * @brief sets the name of the pod @@ -211,7 +206,7 @@ namespace kubecontrol std::string Uuid_; std::string Name_; std::string Component_; - std::vector ContainerImages_; + std::vector ContainerImages_; // std::string ContainerImage_; std::string ContainerRegistry_; std::string PathToYaml_; diff --git a/include/kubecontrol/Utils.hpp b/include/kubecontrol/Utils.hpp index 1103ba8..17d308b 100644 --- a/include/kubecontrol/Utils.hpp +++ b/include/kubecontrol/Utils.hpp @@ -1,22 +1,42 @@ #pragma once -#include #include #include -namespace kubecontrol +namespace kubecontrol { + + + enum PullPolicy: uint32_t + { + ALWAYS, + IFNOTPRESENT, + NEVER + }; + + + inline std::string toString(const PullPolicy &kind) + { + switch (kind) + { + case PullPolicy::ALWAYS: return "Always"; + case PullPolicy::IFNOTPRESENT: return "IfNotPresent"; + case PullPolicy::NEVER: return "Never"; + default: return "Always"; + } + } + class Utils { public: static std::string to_lower(std::string); }; - - - -} \ No newline at end of file + + + +} diff --git a/src/kubecontrol/Container.cpp b/src/kubecontrol/Container.cpp new file mode 100644 index 0000000..adaa1e5 --- /dev/null +++ b/src/kubecontrol/Container.cpp @@ -0,0 +1,55 @@ +#include + + +namespace kubecontrol +{ + Container::Container(const std::string &Owner, const std::string &Uuid, const std::string &Image, const PullPolicy &policy, const std::string ®istry ): + owner_(Utils::to_lower(Owner)), + uuid_(Utils::to_lower(Uuid)), + image_(Utils::to_lower(Image)), + pullPolicy_(policy), + registry_(registry) + { + + } + + + void Container::addEnv(const std::pair &env) + { + this->envs_.emplace(env); + } + + void Container::addEnv(const std::map &envs) + { + + this->envs_ = envs; + } + + + + YAML::Node Container::toYAML() + { + YAML::Node container; + + container["name"] = uuid_+"-container"; + container["image"] = this->registry_ + "/"+image_; + container["imagePullPolicy"] = toString(pullPolicy_); + + if (!envs_.empty()) + { + int lauf = 0; + for(auto [key,value] : envs_) + { + container["env"][lauf]["name"] = key; + container["env"][lauf]["value"] = value; + lauf++; + } + + } + return container; + } + + + + +} diff --git a/src/kubecontrol/KubePod.cpp b/src/kubecontrol/KubePod.cpp index 93775a8..896d54b 100644 --- a/src/kubecontrol/KubePod.cpp +++ b/src/kubecontrol/KubePod.cpp @@ -1,6 +1,7 @@ #include #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 #include #include +#include #include #include #include @@ -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(); + // EnvirmonentVars_ = std::map(); 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(); + 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(); + // EnvirmonentVars_ = std::map(); this->PathToYaml_ = "config/pods/" + this->Uuid_ + ".yaml"; @@ -74,38 +77,41 @@ namespace kubecontrol { std::filesystem::create_directory("config/pods"); } + auto map = std::map(); - addContainer(Uuid, ContainerImage); + addContainer(Uuid, ContainerImage,map); } - void KubePod::addContainer(std::string name,std::string image, std::shared_ptr> envs, PullPolicy pullPolicy ) + void KubePod::addContainer(std::string name,std::string image, std::map &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 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; diff --git a/tests/test_KubePod.cpp b/tests/test_KubePod.cpp index 4dcb7db..beb5768 100644 --- a/tests/test_KubePod.cpp +++ b/tests/test_KubePod.cpp @@ -11,7 +11,7 @@ #include "loguru.hpp" -SCENARIO("Testing the SimCore Sensor") +SCENARIO("Testing KubePod") { @@ -25,7 +25,7 @@ SCENARIO("Testing the SimCore Sensor") std::string controller = "controller"; std::string type = "ship"; std::string image = "ship:latest"; - kubecontrol::KubePod ShipPod1(controller,uuid,type,image,"simulator"); + kubecontrol::KubePod ShipPod1("controller",uuid,type,image,"simulator"); ShipPod1.setName(name); nlohmann::json vars; @@ -74,8 +74,8 @@ nlohmann::json vars1; vars["ENTITY_SENSORS"].push_back("radar:latest"); ShipPod2.setEnvironmentVar("CONFIG", vars1.dump()); -auto envmaps2 = std::make_shared>(); -envmaps2->emplace("CONFIG", vars1.dump()); +auto envmaps2 = std::map(); +envmaps2.emplace("CONFIG", vars1.dump()); ShipPod2.addContainer("CMS", "systemprototype:latest",envmaps2); diff --git a/tests/test_kubecontrol.cpp b/tests/test_kubecontrol.cpp index 98882ce..7c8e75f 100644 --- a/tests/test_kubecontrol.cpp +++ b/tests/test_kubecontrol.cpp @@ -12,7 +12,7 @@ #include #include -SCENARIO("Testing the SimCore Sensor") +SCENARIO("Testing the kubecontrol") { std::string file= "../docs/config"; kubecontrol::kubecontrol kubecontroler(file); diff --git a/tests/test_massivPodHandling.cpp b/tests/test_massivPodHandling.cpp index 1c0bf61..deeb288 100644 --- a/tests/test_massivPodHandling.cpp +++ b/tests/test_massivPodHandling.cpp @@ -95,7 +95,7 @@ void createScenario(kubecontrol::PodController* podc,std::vector *u -SCENARIO("Testing the SimCore Sensor") +SCENARIO("Testing massiv pod handling") { diff --git a/tests/test_podcontroller.cpp b/tests/test_podcontroller.cpp index dcc8536..2bee35b 100644 --- a/tests/test_podcontroller.cpp +++ b/tests/test_podcontroller.cpp @@ -12,7 +12,7 @@ #include -SCENARIO("Testing the SimCore Sensor") +SCENARIO("Testing podcontroler ") { @@ -20,10 +20,9 @@ kubecontrol::PodController podc("docs/config"); std::string name = "test1"; std::string uuid = name; -std::string owner = "controller"; std::string type = "ship"; std::string image = "ship:latest"; - kubecontrol::KubePod ShipPod1(owner,uuid,type,image,"simulator"); + kubecontrol::KubePod ShipPod1("controller",uuid,type,image,"simulator"); nlohmann::json vars; vars["ENTITY_ID"] = uuid;