From bda06c285b20fda6a6ff1b23ffdb81c9e1de7969 Mon Sep 17 00:00:00 2001 From: Henry Winkel Date: Wed, 9 Aug 2023 10:23:50 +0200 Subject: [PATCH] ADD: added Util class with the function to lowercase the vals for yaml and added support of setting args and command --- CMakeLists.txt | 2 ++ include/kubecontrol/KubePod.hpp | 14 +++++++++++- include/kubecontrol/Utils.hpp | 15 +++++++++++++ src/kubecontrol/KubePod.cpp | 40 +++++++++++++++++++++++++++++---- src/kubecontrol/Utils.cpp | 21 +++++++++++++++++ tests/test_podcontroller.cpp | 12 +++++++--- 6 files changed, 96 insertions(+), 8 deletions(-) create mode 100644 include/kubecontrol/Utils.hpp create mode 100644 src/kubecontrol/Utils.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 70f8dbf..e0223e9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -61,6 +61,8 @@ ENDIF() include/kubecontrol/WriterMemoryClass.hpp + include/kubecontrol/Utils.hpp + src/kubecontrol/Utils.cpp ) diff --git a/include/kubecontrol/KubePod.hpp b/include/kubecontrol/KubePod.hpp index 23c9e03..a55984a 100644 --- a/include/kubecontrol/KubePod.hpp +++ b/include/kubecontrol/KubePod.hpp @@ -29,7 +29,14 @@ namespace kubecontrol void setEnvironmentVar(std::string key, std::string val); std::map GetEnvironmentVars(); - std::string* GetEnvironmentVar(std::string key); + std::string GetEnvironmentVar(std::string key); + + void setArgs(std::string args); + std::vector GetArgs(); + + void setCommand(std::string command); + std::string getCommand(); + std::string createYAML(); std::string start(std::string apiAddress,std::string token); @@ -45,7 +52,12 @@ namespace kubecontrol std::string ContainerRegistry_; std::string PathToYaml_; + std::string PodCommand_; + std::map EnvirmonentVars_; + std::vector Args_; + + }; } \ No newline at end of file diff --git a/include/kubecontrol/Utils.hpp b/include/kubecontrol/Utils.hpp new file mode 100644 index 0000000..04920d2 --- /dev/null +++ b/include/kubecontrol/Utils.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include +#include + + +namespace kubecontrol +{ + class Utils + { + public: + static std::string to_lower(std::string); + + }; +} \ No newline at end of file diff --git a/src/kubecontrol/KubePod.cpp b/src/kubecontrol/KubePod.cpp index 570fa89..c17c205 100644 --- a/src/kubecontrol/KubePod.cpp +++ b/src/kubecontrol/KubePod.cpp @@ -1,10 +1,11 @@ +#include "kubecontrol/Utils.hpp" #include namespace kubecontrol { - KubePod::KubePod(std::string Label, std::string Uuid, std::string ContainerImage):Label_(Label),Uuid_(Uuid), + KubePod::KubePod(std::string Label, std::string Uuid, std::string ContainerImage):Label_(Utils::to_lower(Label)),Uuid_(Uuid), ContainerImage_(ContainerImage),EnvirmonentVars_() { ContainerRegistry_ = "kmaster.ti.unibw-hamburg.de:30808"; @@ -45,15 +46,37 @@ namespace kubecontrol return EnvirmonentVars_; } - std::string* KubePod::GetEnvironmentVar(std::string key) + std::string KubePod::GetEnvironmentVar(std::string key) { if(EnvirmonentVars_.contains(key)) { - return &EnvirmonentVars_[key]; + return EnvirmonentVars_[key]; } else { return nullptr; } } + void KubePod::setArgs(std::string args) + { + Args_.emplace_back(Utils::to_lower(args)); + } + + std::vector KubePod::GetArgs() + { + return Args_; + } + + void KubePod::setCommand(std::string command) + { + PodCommand_ = Utils::to_lower(command); + } + + std::string KubePod::getCommand() + { + return PodCommand_; + } + + + std::string KubePod::createYAML() { YAML::Node node; @@ -71,9 +94,18 @@ namespace kubecontrol for (auto item :EnvirmonentVars_) { node["spec"]["containers"][0]["env"][i]["name"] = item.first; node["spec"]["containers"][0]["env"][i]["value"] = item.second; - } + node["spec"]["containers"][0]["args"].SetStyle(YAML::EmitterStyle::Flow); + + for (auto item : Args_) { + node["spec"]["containers"][0]["args"].push_back(item); + } + + node["spec"]["containers"][0]["command"].SetStyle(YAML::EmitterStyle::Flow); + node["spec"]["containers"][0]["command"].push_back(PodCommand_); + + node["spec"]["restartPolicy"] = "Never"; diff --git a/src/kubecontrol/Utils.cpp b/src/kubecontrol/Utils.cpp new file mode 100644 index 0000000..76cf973 --- /dev/null +++ b/src/kubecontrol/Utils.cpp @@ -0,0 +1,21 @@ +#include + + + + +namespace kubecontrol +{ + std::string Utils::to_lower(std::string str) + { + std::string data = str; + std::for_each( + data.begin(), + data.end(), + [](char & c) { + c = ::tolower(c); + }); + return data; + } + + +} \ No newline at end of file diff --git a/tests/test_podcontroller.cpp b/tests/test_podcontroller.cpp index 75861fe..d397c20 100644 --- a/tests/test_podcontroller.cpp +++ b/tests/test_podcontroller.cpp @@ -15,9 +15,15 @@ SCENARIO("Testing the SimCore Sensor") kubecontrol::PodController podc("docs/config"); -kubecontrol::KubePod pod1("pod1",xg::newGuid().str(),"debugdebianhenry:0.1.3"); +kubecontrol::KubePod pod1("Pod1",xg::newGuid().str(),"debugdebianhenry:0.1.3"); + +kubecontrol::KubePod pod2("Pod2",xg::newGuid().str(),"debugdebianhenry:0.1.3"); + +pod1.setArgs("-c"); +pod1.setArgs("while true; do echo hello; sleep 5;done"); + +pod1.setCommand("/bin/sh"); -kubecontrol::KubePod pod2("pod2",xg::newGuid().str(),"debugdebianhenry:0.1.3"); podc.startPod(pod1); podc.startPod(pod2); @@ -27,7 +33,7 @@ nlohmann::json j1 = nlohmann::json::parse(info1); nlohmann::json jall = nlohmann::json::parse(podc.getPodsInfo()); -// podc.stopAllPods(); +podc.stopAllPods(); GIVEN("different Attributes for a Track in different forms") {