ADD: added container class and adapted test
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -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
|
||||
|
||||
75
include/kubecontrol/Container.hpp
Normal file
75
include/kubecontrol/Container.hpp
Normal file
@@ -0,0 +1,75 @@
|
||||
#pragma once
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <kubecontrol/Utils.hpp>
|
||||
#include <yaml-cpp/yaml.h>
|
||||
|
||||
|
||||
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<std::string, std::string> &env);
|
||||
|
||||
/**
|
||||
* @brief add map of envs
|
||||
*
|
||||
* @param envs
|
||||
*/
|
||||
void addEnv(const std::map<std::string, std::string> &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<std::string, std::string> envs_;
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
@@ -17,33 +17,13 @@
|
||||
|
||||
#include <curlpp/cURLpp.hpp>
|
||||
#include <filesystem>
|
||||
#include <kubecontrol/Container.hpp>
|
||||
|
||||
|
||||
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<std::string,std:.string>
|
||||
* @param PullPolicy std::string (defualt: Always) possible:( IfNotPresent, Never)
|
||||
*/
|
||||
void addContainer(std::string name,std::string image, std::shared_ptr<std::map<std::string, std::string>> envs = nullptr, PullPolicy pullPolicy = ALWAYS);
|
||||
void addContainer(std::string name,std::string image, std::map<std::string, std::string> &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<std::string, std::string> 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<YAML::Node> ContainerImages_;
|
||||
std::vector<Container> ContainerImages_;
|
||||
// std::string ContainerImage_;
|
||||
std::string ContainerRegistry_;
|
||||
std::string PathToYaml_;
|
||||
|
||||
@@ -1,22 +1,42 @@
|
||||
#pragma once
|
||||
|
||||
#include <kubecontrol/KubePod.hpp>
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
|
||||
|
||||
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);
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
55
src/kubecontrol/Container.cpp
Normal file
55
src/kubecontrol/Container.cpp
Normal file
@@ -0,0 +1,55 @@
|
||||
#include <kubecontrol/Container.hpp>
|
||||
|
||||
|
||||
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<std::string, std::string> &env)
|
||||
{
|
||||
this->envs_.emplace(env);
|
||||
}
|
||||
|
||||
void Container::addEnv(const std::map<std::string, std::string> &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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
|
||||
#include <kubecontrol/PodController.hpp>
|
||||
#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 <future>
|
||||
#include <iterator>
|
||||
#include <kubecontrol/KubePod.hpp>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
@@ -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<std::string, std::string>();
|
||||
// EnvirmonentVars_ = std::map<std::string, std::string>();
|
||||
|
||||
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<std::string, std::string>();
|
||||
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<std::string, std::string>();
|
||||
// EnvirmonentVars_ = std::map<std::string, std::string>();
|
||||
|
||||
this->PathToYaml_ = "config/pods/" + this->Uuid_ + ".yaml";
|
||||
|
||||
@@ -74,38 +77,41 @@ namespace kubecontrol
|
||||
{
|
||||
std::filesystem::create_directory("config/pods");
|
||||
}
|
||||
auto map = std::map<std::string, std::string>();
|
||||
|
||||
addContainer(Uuid, ContainerImage);
|
||||
addContainer(Uuid, ContainerImage,map);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
void KubePod::addContainer(std::string name,std::string image, std::shared_ptr<std::map<std::string, std::string>> envs, PullPolicy pullPolicy )
|
||||
void KubePod::addContainer(std::string name,std::string image, std::map<std::string, std::string> &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<std::string, std::string> 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;
|
||||
|
||||
@@ -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<std::map<std::string, std::string>>();
|
||||
envmaps2->emplace("CONFIG", vars1.dump());
|
||||
auto envmaps2 = std::map<std::string, std::string>();
|
||||
envmaps2.emplace("CONFIG", vars1.dump());
|
||||
|
||||
ShipPod2.addContainer("CMS", "systemprototype:latest",envmaps2);
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
#include <kubecontrol/kubecontrol.hpp>
|
||||
#include <loguru.hpp>
|
||||
|
||||
SCENARIO("Testing the SimCore Sensor")
|
||||
SCENARIO("Testing the kubecontrol")
|
||||
{
|
||||
std::string file= "../docs/config";
|
||||
kubecontrol::kubecontrol kubecontroler(file);
|
||||
|
||||
@@ -95,7 +95,7 @@ void createScenario(kubecontrol::PodController* podc,std::vector<std::string> *u
|
||||
|
||||
|
||||
|
||||
SCENARIO("Testing the SimCore Sensor")
|
||||
SCENARIO("Testing massiv pod handling")
|
||||
{
|
||||
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
#include <crossguid/guid.hpp>
|
||||
|
||||
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user