diff --git a/.gitignore b/.gitignore index 9a846af..87a0ee3 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ build compile_commands.json .cache .vscode +config/pods diff --git a/include/kubecontrol/KubePod.hpp b/include/kubecontrol/KubePod.hpp index e8f9a62..f0c28fb 100644 --- a/include/kubecontrol/KubePod.hpp +++ b/include/kubecontrol/KubePod.hpp @@ -4,6 +4,7 @@ #include "nlohmann/json_fwd.hpp" #include "yaml-cpp/node/node.h" #include +#include #include #include #include @@ -24,33 +25,157 @@ 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 Childs; + }; + + + 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"); - + /** + * @brief returns the uuid of the pod + * + * @return std::string + */ std::string getUUID(); + + /** + * @brief Get the Owner of the pods + * + * @return std::string + */ std::string getOwner(); + + /** + * @brief Get the Ip of the pod + * + * @return std::string + */ std::string getIp(); + + /** + * @brief Get the Status + * + * @return std::string + */ std::string getStatus(); + /** + * @brief Set the Environment Vars for pod + * + * @param key + * @param val + */ void setEnvironmentVar(std::string key, std::string val); + + /** + * @brief Get the Environment Vars + * + * @return std::map + */ std::map GetEnvironmentVars(); + + /** + * @brief Get the Environment Var for a specific key + * + * @param key std::string + * @return std::string + */ std::string GetEnvironmentVar(std::string key); + /** + * @brief Set CMD args + * + * @param args + */ void setArgs(std::string args); - std::vector GetArgs(); + /** + * @brief Get the Args + * + * @return std::vector + */ + std::vector GetArgs(); + + /** + * @brief Set the Command for a pod + * + * @param command + */ void setCommand(std::string command); + + /** + * @brief Get the Command + * + * @return std::string + */ std::string getCommand(); + /** + * @brief Set the Component var for the pod + * + * @param component + */ void setComponent(std::string component); + + /** + * @brief Get the Component var + * + * @return std::string + */ std::string getComponent(); - void addContainer(std::string name,std::string image, std::shared_ptr> 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 + * @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); /** * @brief sets the name of the pod @@ -77,7 +202,7 @@ namespace kubecontrol */ int updateInfoForThisPod(KubernetesAPI APIInterface); - std::vector getUUIDsForChildPods(KubernetesAPI APIInterface); + std::vector getPodsChilds(KubernetesAPI APIInterface); private: @@ -98,7 +223,7 @@ namespace kubecontrol std::string Ip_; std::string Status_; std::string PartOf_; - std::vector uuidsOfShildContainers; + std::vector ChildPods; std::string PodCommand_; diff --git a/include/kubecontrol/PodController.hpp b/include/kubecontrol/PodController.hpp index 5578ca9..6a96da4 100644 --- a/include/kubecontrol/PodController.hpp +++ b/include/kubecontrol/PodController.hpp @@ -10,15 +10,7 @@ namespace kubecontrol { - struct PodInfos - { - std::string UUID; - std::string IP; - std::string Status; - std::string Component; - std::string Owner; - std::vector UUIDOfChilds; - }; + class PodController { public: diff --git a/src/kubecontrol/KubePod.cpp b/src/kubecontrol/KubePod.cpp index fcbde05..608259a 100644 --- a/src/kubecontrol/KubePod.cpp +++ b/src/kubecontrol/KubePod.cpp @@ -1,4 +1,5 @@ #include "curlpp/Options.hpp" +#include "kubecontrol/PodController.hpp" #include "kubecontrol/Utils.hpp" #include "nlohmann/json_fwd.hpp" #include "yaml-cpp/binary.h" @@ -76,7 +77,7 @@ namespace kubecontrol - void KubePod::addContainer(std::string name,std::string image, std::shared_ptr> envs, std::string PullPolicy ) + void KubePod::addContainer(std::string name,std::string image, std::shared_ptr> envs, PullPolicy pullPolicy ) { YAML::Node container; transform(image.begin(), image.end(), image.begin(), ::tolower); @@ -84,7 +85,7 @@ namespace kubecontrol container["name"] = name+"-container"; container["image"] = this->ContainerRegistry_ + "/" + image; - container["imagePullPolicy"] = PullPolicy; + container["imagePullPolicy"] = toString(pullPolicy); if (envs != nullptr) { @@ -303,14 +304,14 @@ namespace kubecontrol int KubePod::stopChilds(KubernetesAPI APIInterface) { - auto uuids = this->getUUIDsForChildPods(APIInterface); + auto uuids = this->getPodsChilds(APIInterface); if (uuids.size() == 0) { return 1; } 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"); } @@ -338,18 +339,18 @@ namespace kubecontrol } - std::vector KubePod::getUUIDsForChildPods( KubernetesAPI APIInterface) + std::vector KubePod::getPodsChilds( KubernetesAPI APIInterface) { std::string request = "/api/v1/namespaces/simulator/pods?labelSelector=app.kubernetes.io/part-of="+Uuid_; std::string result = APIInterface.performRequest(request,"GET"); if (result == "") { - return std::vector(); + return std::vector(); } extractInformationFromResopnse(result); - return this->uuidsOfShildContainers; + return this->ChildPods; } @@ -372,10 +373,17 @@ namespace kubecontrol }else if(j.contains("items") && j["items"].is_array()) - { + { + for (auto i : j["items"]) { - this->uuidsOfShildContainers.push_back(i["metadata"]["name"].get()); + // LOG_S(INFO)<(); + child.IP = i["status"]["podIP"].get(); + child.Status = i["status"]["phase"].get(); + child.Component = i["metadata"]["labels"]["app.kubernetes.io/component"].get(); + this->ChildPods.push_back(child); } }else{ diff --git a/src/kubecontrol/PodController.cpp b/src/kubecontrol/PodController.cpp index 3108682..bb86315 100644 --- a/src/kubecontrol/PodController.cpp +++ b/src/kubecontrol/PodController.cpp @@ -141,7 +141,7 @@ namespace kubecontrol info.Component =pod->getComponent(); info.IP = pod->getIp(); info.Status = pod->getStatus(); - info.UUIDOfChilds = pod->getUUIDsForChildPods(APIInterface_); + info.Childs = pod->getPodsChilds(APIInterface_); return info; } diff --git a/tests/test_KubePod.cpp b/tests/test_KubePod.cpp index 95ac4ec..1e226a9 100644 --- a/tests/test_KubePod.cpp +++ b/tests/test_KubePod.cpp @@ -97,7 +97,7 @@ std::this_thread::sleep_for(std::chrono::milliseconds(5000)); REQUIRE(ShipPod1.getOwner() == "controller"); REQUIRE(ShipPod1.getStatus() == "Running"); REQUIRE(ShipPod1.getIp() != ""); - REQUIRE(ShipPod1.getUUIDsForChildPods(api).size() == 2); + REQUIRE(ShipPod1.getPodsChilds(api).size() == 2); // REQUIRE(info1 != ""); diff --git a/tests/test_podcontroller.cpp b/tests/test_podcontroller.cpp index 477645d..6961082 100644 --- a/tests/test_podcontroller.cpp +++ b/tests/test_podcontroller.cpp @@ -16,7 +16,7 @@ SCENARIO("Testing the SimCore Sensor") kubecontrol::PodController podc("docs/config"); -std::string name = "hamburg"; +std::string name = "test1"; std::string uuid = name; kubecontrol::KubePod ShipPod1("controller",uuid,"ship","ship:latest","simulator"); @@ -78,6 +78,15 @@ ShipPod1.updateInfoForThisPod(podc.getKubernetesAPI()); THEN("check if Track attributes are correct") { auto info = podc.getInfoForPod(ShipPod1.getUUID()); + for (auto item: info.Childs) + { + LOG_S(INFO)<