ADD: added more extracted details for a child pod and changed the data type for the child information

This commit is contained in:
Henry Winkel
2024-03-12 18:08:55 +01:00
parent 263aaa6a71
commit 48101a673a
7 changed files with 161 additions and 26 deletions

1
.gitignore vendored
View File

@@ -3,3 +3,4 @@ build
compile_commands.json compile_commands.json
.cache .cache
.vscode .vscode
config/pods

View File

@@ -4,6 +4,7 @@
#include "nlohmann/json_fwd.hpp" #include "nlohmann/json_fwd.hpp"
#include "yaml-cpp/node/node.h" #include "yaml-cpp/node/node.h"
#include <map> #include <map>
#include <cstdint>
#include <memory> #include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
@@ -24,33 +25,157 @@
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";
}
}
struct PodChild
{
std::string UUID;
std::string IP;
std::string Component;
std::string Status;
};
struct PodInfos
{
std::string UUID;
std::string IP;
std::string Status;
std::string Component;
std::string Owner;
std::vector<PodChild> Childs;
};
class KubePod class KubePod
{ {
public: public:
KubePod(std::string Owner, std::string Uuid, std::string ContainerImage,std::string Namespace = "simulator") ; 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(std::string Owner, std::string Uuid, std::string Component, std::string ContainerImage,std::string Namespace = "simulator");
/**
* @brief returns the uuid of the pod
*
* @return std::string
*/
std::string getUUID(); std::string getUUID();
/**
* @brief Get the Owner of the pods
*
* @return std::string
*/
std::string getOwner(); std::string getOwner();
/**
* @brief Get the Ip of the pod
*
* @return std::string
*/
std::string getIp(); std::string getIp();
/**
* @brief Get the Status
*
* @return std::string
*/
std::string getStatus(); std::string getStatus();
/**
* @brief Set the Environment Vars for pod
*
* @param key
* @param val
*/
void setEnvironmentVar(std::string key, std::string val); void setEnvironmentVar(std::string key, std::string val);
/**
* @brief Get the Environment Vars
*
* @return std::map<std::string, std::string>
*/
std::map<std::string, std::string> GetEnvironmentVars(); std::map<std::string, std::string> GetEnvironmentVars();
/**
* @brief Get the Environment Var for a specific key
*
* @param key std::string
* @return std::string
*/
std::string GetEnvironmentVar(std::string key); std::string GetEnvironmentVar(std::string key);
/**
* @brief Set CMD args
*
* @param args
*/
void setArgs(std::string args); void setArgs(std::string args);
std::vector<std::string> GetArgs();
/**
* @brief Get the Args
*
* @return std::vector<std::string>
*/
std::vector<std::string> GetArgs();
/**
* @brief Set the Command for a pod
*
* @param command
*/
void setCommand(std::string command); void setCommand(std::string command);
/**
* @brief Get the Command
*
* @return std::string
*/
std::string getCommand(); std::string getCommand();
/**
* @brief Set the Component var for the pod
*
* @param component
*/
void setComponent(std::string component); void setComponent(std::string component);
/**
* @brief Get the Component var
*
* @return std::string
*/
std::string getComponent(); std::string getComponent();
void addContainer(std::string name,std::string image, std::shared_ptr<std::map<std::string, std::string>> envs = nullptr, std::string PullPolicy = "Always"); /**
* @brief add a Container to a pod
*
* @param name std::string
* @param image std::string
* @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);
/** /**
* @brief sets the name of the pod * @brief sets the name of the pod
@@ -77,7 +202,7 @@ namespace kubecontrol
*/ */
int updateInfoForThisPod(KubernetesAPI APIInterface); int updateInfoForThisPod(KubernetesAPI APIInterface);
std::vector<std::string> getUUIDsForChildPods(KubernetesAPI APIInterface); std::vector<PodChild> getPodsChilds(KubernetesAPI APIInterface);
private: private:
@@ -98,7 +223,7 @@ namespace kubecontrol
std::string Ip_; std::string Ip_;
std::string Status_; std::string Status_;
std::string PartOf_; std::string PartOf_;
std::vector<std::string> uuidsOfShildContainers; std::vector<PodChild> ChildPods;
std::string PodCommand_; std::string PodCommand_;

View File

@@ -10,15 +10,7 @@
namespace kubecontrol namespace kubecontrol
{ {
struct PodInfos
{
std::string UUID;
std::string IP;
std::string Status;
std::string Component;
std::string Owner;
std::vector<std::string> UUIDOfChilds;
};
class PodController class PodController
{ {
public: public:

View File

@@ -1,4 +1,5 @@
#include "curlpp/Options.hpp" #include "curlpp/Options.hpp"
#include "kubecontrol/PodController.hpp"
#include "kubecontrol/Utils.hpp" #include "kubecontrol/Utils.hpp"
#include "nlohmann/json_fwd.hpp" #include "nlohmann/json_fwd.hpp"
#include "yaml-cpp/binary.h" #include "yaml-cpp/binary.h"
@@ -76,7 +77,7 @@ namespace kubecontrol
void KubePod::addContainer(std::string name,std::string image, std::shared_ptr<std::map<std::string, std::string>> envs, std::string PullPolicy ) void KubePod::addContainer(std::string name,std::string image, std::shared_ptr<std::map<std::string, std::string>> envs, PullPolicy pullPolicy )
{ {
YAML::Node container; YAML::Node container;
transform(image.begin(), image.end(), image.begin(), ::tolower); transform(image.begin(), image.end(), image.begin(), ::tolower);
@@ -84,7 +85,7 @@ namespace kubecontrol
container["name"] = name+"-container"; container["name"] = name+"-container";
container["image"] = this->ContainerRegistry_ + "/" + image; container["image"] = this->ContainerRegistry_ + "/" + image;
container["imagePullPolicy"] = PullPolicy; container["imagePullPolicy"] = toString(pullPolicy);
if (envs != nullptr) if (envs != nullptr)
{ {
@@ -303,14 +304,14 @@ namespace kubecontrol
int KubePod::stopChilds(KubernetesAPI APIInterface) int KubePod::stopChilds(KubernetesAPI APIInterface)
{ {
auto uuids = this->getUUIDsForChildPods(APIInterface); auto uuids = this->getPodsChilds(APIInterface);
if (uuids.size() == 0) if (uuids.size() == 0)
{ {
return 1; return 1;
} }
for(auto i: uuids) for(auto i: uuids)
{ {
std::string request = "/api/v1/namespaces/simulator/pods/"+i; std::string request = "/api/v1/namespaces/simulator/pods/"+i.UUID;
std::string result = APIInterface.performRequest(request,"DELETE"); std::string result = APIInterface.performRequest(request,"DELETE");
} }
@@ -338,18 +339,18 @@ namespace kubecontrol
} }
std::vector<std::string> KubePod::getUUIDsForChildPods( KubernetesAPI APIInterface) std::vector<PodChild> KubePod::getPodsChilds( KubernetesAPI APIInterface)
{ {
std::string request = "/api/v1/namespaces/simulator/pods?labelSelector=app.kubernetes.io/part-of="+Uuid_; std::string request = "/api/v1/namespaces/simulator/pods?labelSelector=app.kubernetes.io/part-of="+Uuid_;
std::string result = APIInterface.performRequest(request,"GET"); std::string result = APIInterface.performRequest(request,"GET");
if (result == "") if (result == "")
{ {
return std::vector<std::string>(); return std::vector<PodChild>();
} }
extractInformationFromResopnse(result); extractInformationFromResopnse(result);
return this->uuidsOfShildContainers; return this->ChildPods;
} }
@@ -372,10 +373,17 @@ namespace kubecontrol
}else if(j.contains("items") && j["items"].is_array()) }else if(j.contains("items") && j["items"].is_array())
{ {
for (auto i : j["items"]) for (auto i : j["items"])
{ {
this->uuidsOfShildContainers.push_back(i["metadata"]["name"].get<std::string>()); // LOG_S(INFO)<<i;
PodChild child;
child.UUID =i["metadata"]["name"].get<std::string>();
child.IP = i["status"]["podIP"].get<std::string>();
child.Status = i["status"]["phase"].get<std::string>();
child.Component = i["metadata"]["labels"]["app.kubernetes.io/component"].get<std::string>();
this->ChildPods.push_back(child);
} }
}else{ }else{

View File

@@ -141,7 +141,7 @@ namespace kubecontrol
info.Component =pod->getComponent(); info.Component =pod->getComponent();
info.IP = pod->getIp(); info.IP = pod->getIp();
info.Status = pod->getStatus(); info.Status = pod->getStatus();
info.UUIDOfChilds = pod->getUUIDsForChildPods(APIInterface_); info.Childs = pod->getPodsChilds(APIInterface_);
return info; return info;
} }

View File

@@ -97,7 +97,7 @@ std::this_thread::sleep_for(std::chrono::milliseconds(5000));
REQUIRE(ShipPod1.getOwner() == "controller"); REQUIRE(ShipPod1.getOwner() == "controller");
REQUIRE(ShipPod1.getStatus() == "Running"); REQUIRE(ShipPod1.getStatus() == "Running");
REQUIRE(ShipPod1.getIp() != ""); REQUIRE(ShipPod1.getIp() != "");
REQUIRE(ShipPod1.getUUIDsForChildPods(api).size() == 2); REQUIRE(ShipPod1.getPodsChilds(api).size() == 2);
// REQUIRE(info1 != ""); // REQUIRE(info1 != "");

View File

@@ -16,7 +16,7 @@ SCENARIO("Testing the SimCore Sensor")
kubecontrol::PodController podc("docs/config"); kubecontrol::PodController podc("docs/config");
std::string name = "hamburg"; std::string name = "test1";
std::string uuid = name; std::string uuid = name;
kubecontrol::KubePod ShipPod1("controller",uuid,"ship","ship:latest","simulator"); kubecontrol::KubePod ShipPod1("controller",uuid,"ship","ship:latest","simulator");
@@ -78,6 +78,15 @@ ShipPod1.updateInfoForThisPod(podc.getKubernetesAPI());
THEN("check if Track attributes are correct") THEN("check if Track attributes are correct")
{ {
auto info = podc.getInfoForPod(ShipPod1.getUUID()); auto info = podc.getInfoForPod(ShipPod1.getUUID());
for (auto item: info.Childs)
{
LOG_S(INFO)<<item.UUID;
LOG_S(INFO)<<item.IP;
LOG_S(INFO)<<item.Component;
LOG_S(INFO)<<item.Status;
}
REQUIRE(info.UUID == ShipPod1.getUUID()); REQUIRE(info.UUID == ShipPod1.getUUID());
REQUIRE(info.IP == ShipPod1.getIp()); REQUIRE(info.IP == ShipPod1.getIp());
REQUIRE(info.Owner == ShipPod1.getOwner()); REQUIRE(info.Owner == ShipPod1.getOwner());