ADD: added Util class with the function to lowercase the vals for yaml and added support of setting args and command

This commit is contained in:
Henry Winkel
2023-08-09 10:23:50 +02:00
parent 39f0f2b4eb
commit bda06c285b
6 changed files with 96 additions and 8 deletions

View File

@@ -61,6 +61,8 @@ ENDIF()
include/kubecontrol/WriterMemoryClass.hpp
include/kubecontrol/Utils.hpp
src/kubecontrol/Utils.cpp
)

View File

@@ -29,7 +29,14 @@ namespace kubecontrol
void setEnvironmentVar(std::string key, std::string val);
std::map<std::string, std::string> GetEnvironmentVars();
std::string* GetEnvironmentVar(std::string key);
std::string GetEnvironmentVar(std::string key);
void setArgs(std::string args);
std::vector<std::string> 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<std::string, std::string> EnvirmonentVars_;
std::vector<std::string> Args_;
};
}

View File

@@ -0,0 +1,15 @@
#pragma once
#include <string>
#include <algorithm>
namespace kubecontrol
{
class Utils
{
public:
static std::string to_lower(std::string);
};
}

View File

@@ -1,10 +1,11 @@
#include "kubecontrol/Utils.hpp"
#include <kubecontrol/KubePod.hpp>
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<std::string> 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";

21
src/kubecontrol/Utils.cpp Normal file
View File

@@ -0,0 +1,21 @@
#include <kubecontrol/Utils.hpp>
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;
}
}

View File

@@ -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")
{