From d265cb67f03d8f33d1df1a8a4f2e74fdbf3549c8 Mon Sep 17 00:00:00 2001 From: hwinkel Date: Fri, 15 Mar 2024 10:58:56 +0100 Subject: [PATCH] ADD: beautified the class and removed unnecessary class --- .drone.yml | 30 ++-- CMakeLists.txt | 12 -- include/kubecontrol/Container.hpp | 6 + include/kubecontrol/KubePod.hpp | 31 +++- include/kubecontrol/KubernetesAPI.hpp | 48 ++++++ include/kubecontrol/PodController.hpp | 81 +++++++-- include/kubecontrol/Utils.hpp | 13 +- include/kubecontrol/kubecontrol.hpp | 25 --- src/kubecontrol/KubePod.cpp | 8 +- src/kubecontrol/KubernetesAPI.cpp | 4 +- src/kubecontrol/PodController.cpp | 87 +++------- src/kubecontrol/kubecontrol.cpp | 239 -------------------------- tests/test_kubecontrol.cpp | 42 ----- 13 files changed, 186 insertions(+), 440 deletions(-) delete mode 100644 include/kubecontrol/kubecontrol.hpp delete mode 100644 src/kubecontrol/kubecontrol.cpp delete mode 100644 tests/test_kubecontrol.cpp diff --git a/.drone.yml b/.drone.yml index db2617d..cef01f5 100644 --- a/.drone.yml +++ b/.drone.yml @@ -11,21 +11,21 @@ steps: - CC=clang-11 CXX=clang++-11 cmake -DCMAKE_BUILD_TYPE=DEBUG .. - make -j - - name: CodeChecker - image: kmaster.ti.unibw-hamburg.de:30808/drone-ftewa-codechecker - pull: always - settings: - CODECHECKER_URL: "http://codechecker:8001" - CODECHECKER_PRODUCT: "kubecontrol" - CODECHECKER_USER: - from_secret: CODECHECKER_USER_SECRET - CODECHECKER_PASS: - from_secret: CODECHECKER_PASS_SECRET - when: - event: - include: - - push - - pull_request + # - name: CodeChecker + # image: kmaster.ti.unibw-hamburg.de:30808/drone-ftewa-codechecker + # pull: always + # settings: + # CODECHECKER_URL: "http://codechecker:8001" + # CODECHECKER_PRODUCT: "kubecontrol" + # CODECHECKER_USER: + # from_secret: CODECHECKER_USER_SECRET + # CODECHECKER_PASS: + # from_secret: CODECHECKER_PASS_SECRET + # when: + # event: + # include: + # - push + # - pull_request --- kind: secret diff --git a/CMakeLists.txt b/CMakeLists.txt index a4ef909..fee9711 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -45,14 +45,8 @@ ENDIF() ENDIF() - # build with CC=clang-15 CXX=clang++-15 cmake -D CMAKE_BUILD_TYPE=DEBUG .. - - add_library(kubecontrol STATIC - include/kubecontrol/kubecontrol.hpp - src/kubecontrol/kubecontrol.cpp - include/kubecontrol/KubePod.hpp src/kubecontrol/KubePod.cpp @@ -99,12 +93,6 @@ option(TEST_KUBECONTROL_LIBRARY "Turn running of application template specific t IF (${TEST_KUBECONTROL_LIBRARY}) - - - # add_executable(test_kubecontrol tests/test_kubecontrol.cpp) - # target_link_libraries(test_kubecontrol Catch2::Catch2 kubecontrol ) - # catch_discover_tests(test_kubecontrol) - add_executable(test_KubePod tests/test_KubePod.cpp) target_link_libraries(test_KubePod Catch2::Catch2 kubecontrol ) catch_discover_tests(test_KubePod) diff --git a/include/kubecontrol/Container.hpp b/include/kubecontrol/Container.hpp index 8178edc..18db66b 100644 --- a/include/kubecontrol/Container.hpp +++ b/include/kubecontrol/Container.hpp @@ -63,11 +63,17 @@ namespace kubecontrol YAML::Node toYAML(); private: + /// @brief uuid of the container owner std::string owner_; + /// @brief uuid of the container std::string uuid_; + /// @brief image of the container std::string image_; + /// @brief registry of the container std::string registry_; + /// @brief pull Policy is a enum PullPolicy pullPolicy_; + /// @brief environment variables std::map envs_; }; diff --git a/include/kubecontrol/KubePod.hpp b/include/kubecontrol/KubePod.hpp index 1fbdbaa..ecb005f 100644 --- a/include/kubecontrol/KubePod.hpp +++ b/include/kubecontrol/KubePod.hpp @@ -12,9 +12,9 @@ #include #include #include +#define LOGURU_WITH_STREAMS 1 #include #include - #include #include #include @@ -198,32 +198,45 @@ namespace kubecontrol private: - + /// @brief maximum time to wait of the response when creating a pod static const int MaxWaitTimeInSeconds; - - + /// @brief uuid of the owner std::string Owner_; + /// @brief uuid of the pod std::string Uuid_; + /// @brief name of the pod std::string Name_; + /// @brief component the pod represents std::string Component_; + /// @brief vector of images the pod contains std::vector ContainerImages_; - // std::string ContainerImage_; + /// @brief url of the container registry std::string ContainerRegistry_; + /// @brief path to kube yaml std::string PathToYaml_; + /// @brief namespace the pod is designated std::string Namespace_; - + /// @brief IP of the pod std::string Ip_; + /// @brief status of the pod std::string Status_; + /// @brief uuid of pod this pod is part of std::string PartOf_; + /// @brief vector of child pods std::vector ChildPods; - + /// @brief command the pod schould execute when created std::string PodCommand_; - + /// @brief yaml node of the pod which is used for creation YAML::Node YAMLNode_; - + /// @brief environment vars of the pod std::map EnvirmonentVars_; + /// @brief arguments for the pod std::vector Args_; + /** + * @brief function to create the yaml string for the pod creation + * @return std::string - returns a serialized yaml + */ std::string createYAML(); /** diff --git a/include/kubecontrol/KubernetesAPI.hpp b/include/kubecontrol/KubernetesAPI.hpp index 5046290..c68fe16 100644 --- a/include/kubecontrol/KubernetesAPI.hpp +++ b/include/kubecontrol/KubernetesAPI.hpp @@ -1,6 +1,8 @@ #pragma once #include #include +#define LOGURU_WITH_STREAMS 1 +#include namespace kubecontrol @@ -10,23 +12,69 @@ class KubernetesAPI { public: KubernetesAPI(); + /** + * constructor of the Kubernetes API + * @param config - yaml node of the designated users kubectl yaml + */ explicit KubernetesAPI(YAML::Node config); + /** + * @brief costructor of Kuernetes API object + */ KubernetesAPI(std::string APIAddress, std::string Token); + /** + * @brief performs a request to the kubernetes API + * @param request - string of the request + * @param Methode - string (eg. GET, POST) + * @result string - respone of the requestS + */ std::string performRequest(std::string request,std::string Methode = "GET"); + /** + * @brief performs a request to the kubernetes API + * @param request - string of the request + * @param Methode - string (eg. GET, POST) + * @param PostFields - string of the post fields which are sended in the request + * @return string - respone of the requestS + */ std::string performRequest(std::string request,std::string Methode,std::string PostFields); + /** + * @brief adds address of the kubernetes api + * @param address - string + */ void addAddress(std::string address); + + /** + * @brief returns the address of the kubrnetes api stored in the object + * @return std::string returns the address + */ std::string getAddress(); + + /** + * @brief add the bearer token which is necessary to access the api + * @param Token - string + */ void addToken(std::string Token); + + /** + * @brief adds the yaml node which is required to hold all necessary data to access the api + * @param conifg - YAML::Node object of the parsed file + */ void addYaml(YAML::Node config); + /** + * @brief returns the namespace the object uses + * @return string + */ std::string getNamespace(); private: + /// @brief api adress std::string APIAddress_; + /// @brief bearer token std::string Token_; + /// @brief namespace std::string Namespace_; }; diff --git a/include/kubecontrol/PodController.hpp b/include/kubecontrol/PodController.hpp index 83ba3d0..274da5e 100644 --- a/include/kubecontrol/PodController.hpp +++ b/include/kubecontrol/PodController.hpp @@ -14,32 +14,77 @@ namespace kubecontrol class PodController { public: - explicit PodController(std::string pathToKubectlConfig); + /** + * @brief constructs the podcontroller + * @param pathToKubectlConfig - string path to the kubectl config file + */ + explicit PodController(std::string pathToKubectlConfig); + + /** + * @brief returns the Server Address of the given kubernetes api + * @return string + */ + std::string getServerAddress(); - std::string getServerAddress(); + /** + * @brief starts a pod + * @param Pod - pod objet + * @param WaitTillRunning - bool (default: true) indicates if the controller should wait till the pod is startet + */ + void startPod(KubePod Pod,bool WaitTillRunning = true ); + + /** + * @brief starts a pod + * @param Pod - std::shared_ptr objet + * @param WaitTillRunning - bool (default: true) indicates if the controller should wait till the pod is startet + */ + void startPod(std::shared_ptr Pod,bool WaitTillRunning = true ); - void startPod(KubePod Pod,bool WaitTillRunning = true ); - void startPod(std::shared_ptr Pod,bool WaitTillRunning = true ); + /** + * @brief stops a pod referenced by its uuid + * @param uuid - string + */ + void stopPod(std::string uuid); - void stopPod(std::string uuid); - void stopAllPods(); + /** + * @brief stops all pods the controller knows + */ + void stopAllPods(); - std::vector getInfoForAllPods(); - PodInfos getInfoForPod(std::string uuid); + /** + * @brief returns a vector of PodInfos of all pod + * @return std::vector + */ + std::vector getInfoForAllPods(); - KubernetesAPI getKubernetesAPI(); + /** + * @brief returns a PodInfo object of a specific pod + * @param uuid - string + * @return PodInfo + */ + PodInfos getInfoForPod(std::string uuid); + + /** + * @brief retursn a copy of ne current used kubernetes API + */ + KubernetesAPI getKubernetesAPI(); private: - KubernetesAPI APIInterface_; - std::vector> PodList_; + /// @brief object of the kubernetes api object + KubernetesAPI APIInterface_; + /// @brief vector of unique poitners of owned KubePod objects + std::vector> PodList_; + /// @brief current namespace + std::string Namespace_; + /// @brief base apiCall (default: "/api/v1/namespaces/Simulator/pods/") + std::string ApiCall_; + /** + * @brief gets infos for specific pod + */ + PodInfos extractInfosFromKubePod(KubePod *pod); - std::string Namespace_; - std::string ApiCall_; - - PodInfos extractInfosFromKubePod(KubePod *pod); - - - mutable std::mutex mx_; + /// @brief mutex + mutable std::mutex mx_; }; } // namespace ku diff --git a/include/kubecontrol/Utils.hpp b/include/kubecontrol/Utils.hpp index 17d308b..38ce7b9 100644 --- a/include/kubecontrol/Utils.hpp +++ b/include/kubecontrol/Utils.hpp @@ -8,15 +8,17 @@ namespace kubecontrol { - enum PullPolicy: uint32_t + enum PullPolicy: uint32_t { ALWAYS, IFNOTPRESENT, NEVER }; - - inline std::string toString(const PullPolicy &kind) + /** + * @brief translate PullPolicy enum to string + */ + inline std::string toString(const PullPolicy &kind) { switch (kind) { @@ -29,7 +31,10 @@ namespace kubecontrol class Utils { - public: + public: + /** + * @brief lowercase a string + */ static std::string to_lower(std::string); }; diff --git a/include/kubecontrol/kubecontrol.hpp b/include/kubecontrol/kubecontrol.hpp deleted file mode 100644 index 54d9a85..0000000 --- a/include/kubecontrol/kubecontrol.hpp +++ /dev/null @@ -1,25 +0,0 @@ -#pragma once -#include -#include - -namespace kubecontrol -{ - class kubecontrol - { - - public: - explicit kubecontrol(std::string &pathToKubectlConfig); - void getPods(); - void startPod(); - void deletePod(std::string uid); - std::string createYAML(); - - - private: - std::string BearerToken_; - std::string ServerAddress_; - std::string NameSpace_; - - - }; -} diff --git a/src/kubecontrol/KubePod.cpp b/src/kubecontrol/KubePod.cpp index 0126ead..f54e4d8 100644 --- a/src/kubecontrol/KubePod.cpp +++ b/src/kubecontrol/KubePod.cpp @@ -1,5 +1,5 @@ -#include +#include #include "curlpp/Options.hpp" #include "kubecontrol/Container.hpp" #include "kubecontrol/Utils.hpp" @@ -10,15 +10,11 @@ #include #include #include -#include #include #include #include #include #include -#include -#include "loguru.hpp" -#include "yaml-cpp/node/node.h" @@ -37,7 +33,7 @@ namespace kubecontrol // EnvirmonentVars_ = std::map(); - + this->PathToYaml_ = "config/pods/" + this->Uuid_ + ".yaml"; if( !std::filesystem::directory_entry("config").exists()) diff --git a/src/kubecontrol/KubernetesAPI.cpp b/src/kubecontrol/KubernetesAPI.cpp index 7214ec9..a2d4752 100644 --- a/src/kubecontrol/KubernetesAPI.cpp +++ b/src/kubecontrol/KubernetesAPI.cpp @@ -7,8 +7,6 @@ #include #include -#include - @@ -108,7 +106,7 @@ namespace kubecontrol { try { curlRequest.perform(); - } catch (const std::exception e) { + } catch (const std::exception &e) { LOG_S(ERROR)<lock(mx_); PodList_.emplace_back(std::make_unique(Pod)); // LOG_S(INFO)<getUUID(); // auto response = Pod.start(ServerAddress_+ApiCall_, BearerToken_,WaitTillRunning); - auto response = Pod->start(APIInterface_,WaitTillRunning); - + int response = Pod->start(APIInterface_,WaitTillRunning); + if (response != 0) + { + throw std::invalid_argument( "could not start pod" ); + } std::lock_guardlock(mx_); PodList_.emplace_back(std::make_unique(*Pod)); } @@ -65,18 +68,15 @@ namespace kubecontrol void PodController::stopPod(std::string uuid) { std::lock_guardlock(mx_); - // PodList_ for (std::vector>::iterator it = PodList_.begin(); it != PodList_.end();) - { - - if (uuid == it->get()->getUUID()) - { - it->get()->stop(APIInterface_); - it = PodList_.erase(it); -} - else + { + if (uuid == it->get()->getUUID()) { - it++; + it->get()->stop(APIInterface_); + it = PodList_.erase(it); + }else + { + it++; } } @@ -111,7 +111,6 @@ namespace kubecontrol return infosOfAllPods; - } @@ -146,50 +145,4 @@ namespace kubecontrol return info; } - - - - - - - - - - - // std::string PodController::performRequest(std::string curlURL) - // { - - // std::string AuthString = "Authorization: Bearer " + BearerToken_; - - // std::list headers; - // headers.push_back(AuthString); - - // auto request = std::make_unique(); - - - // // request->setOpt(test); - // std::stringstream result; - // request->setOpt(cURLpp::Options::WriteStream(&result)); - - // request->setOpt(new curlpp::options::HttpHeader(headers)); - - // request->setOpt(new curlpp::options::Url(curlURL)); - // // request.setOpt(new curlpp::options::SslEngineDefault()); - // // request.setOpt(new curlpp::options::CaPath("config/ca.crt")); - // request->setOpt(new curlpp::options::SslVerifyPeer(false)); - // // request->setOpt(new curlpp::options::Verbose(true)); - - - // request->perform(); - - // // std::string response = mWriterChunk.getResponse(); - // std::string response = result.str(); - // return response; - // } - - - - - - } \ No newline at end of file diff --git a/src/kubecontrol/kubecontrol.cpp b/src/kubecontrol/kubecontrol.cpp deleted file mode 100644 index 4ea108d..0000000 --- a/src/kubecontrol/kubecontrol.cpp +++ /dev/null @@ -1,239 +0,0 @@ -#include -#include "yaml-cpp/binary.h" -#include "yaml-cpp/emitter.h" -#include "yaml-cpp/emitterdef.h" -#include "yaml-cpp/emittermanip.h" -#include "yaml-cpp/emitterstyle.h" -#include "yaml-cpp/node/convert.h" -#include "yaml-cpp/node/node.h" -#include -#include - - -#include -#include -#include -#include - -#include -#include - -#include - -#include - - - -#include -#include - - -namespace kubecontrol -{ - - kubecontrol::kubecontrol(std::string &pathToKubectlConfig) - { - YAML::Node config = YAML::LoadFile(pathToKubectlConfig); - - BearerToken_ = config["users"][0]["user"]["token"].as(); - ServerAddress_ = config["clusters"][0]["cluster"]["server"].as(); - - } - - void kubecontrol::getPods() - { - std::string curlURL = ServerAddress_+"/api/v1/namespaces/simulator/pods/"; - std::string AuthString = "Authorization: Bearer " + BearerToken_; - - std::list headers; - headers.push_back(AuthString); - - - curlpp::Cleanup cleaner; - curlpp::Easy request; - - - std::stringstream result; - - request.setOpt(cURLpp::Options::WriteStream(&result)); - request.setOpt(curlpp::options::HttpHeader(headers)); - - request.setOpt( curlpp::options::Url(curlURL)); - request.setOpt( curlpp::options::SslEngineDefault()); - request.setOpt( curlpp::options::CaPath("config/ca.crt")); - request.setOpt( curlpp::options::SslVerifyPeer(false)); - - - // request.setOpt(new curlpp::options::Verbose(true)); - - - request.perform(); - auto response = result.str(); - - - - auto j = nlohmann::json::parse(response); - LOG_S(INFO)< headers; - headers.push_back(AuthString); - headers.emplace_back("Content-Type: application/yaml"); - - - curlpp::Cleanup cleaner; - curlpp::Easy request; - - // WriterMemoryClass mWriterChunk; - // curlpp::types::WriteFunctionFunctor functor = std::bind(&WriterMemoryClass::WriteMemoryCallback, &mWriterChunk, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3); - // curlpp::options::WriteFunction *test = new curlpp::options::WriteFunction(functor); - // request.setOpt(test); - std::stringstream result; - request.setOpt(cURLpp::Options::WriteStream(&result)); - - request.setOpt( curlpp::options::HttpHeader(headers)); - - request.setOpt( curlpp::options::Url(curlURL)); - - - - request.setOpt( curlpp::options::SslEngineDefault()); - request.setOpt( curlpp::options::CaPath("config/ca.crt")); - request.setOpt( curlpp::options::SslVerifyPeer(false)); - - request.setOpt( curlpp::options::Post(true)); - - - auto yaml = createYAML(); - - // YAML::Node node = YAML::LoadFile("config/pods_deployment.yaml"); - // YAML::Binary node = YAML::LoadFile("config/pods_deployment.yaml"); - // LOG_S(INFO)<< node["spec"]["containers"][0]["imagePullPolicy"]; - // YAML::Binary binary = node.as(); - - std::ifstream is; - is.open ("config/pods_deployment.yaml", std::ios::binary ); - - // get length of file: - is.seekg (0, std::ios::end); - long length = is.tellg(); - is.seekg (0, std::ios::beg); - - // allocate memory: - char *buffer = new char [length]; - - // read data as a block: - is.read (buffer,length); - - - std::istringstream myStream((std::string( buffer ))); - int size = myStream.str().size(); - is.close(); - - - - request.setOpt( curlpp::options::PostFields(buffer)); - - // request.setOpt(new curlpp::options::Verbose(true)); - - - request.perform(); - auto response = result.str(); - LOG_S(INFO)< headers; - headers.push_back(AuthString); - // headers.push_back("Content-Type: application/yaml"); - - - curlpp::Cleanup cleaner; - curlpp::Easy request; - - std::stringstream result; - request.setOpt(cURLpp::Options::WriteStream(&result)); - - - request.setOpt( curlpp::options::HttpHeader(headers)); - - request.setOpt( curlpp::options::Url(curlURL)); - - - - request.setOpt( curlpp::options::SslEngineDefault()); - request.setOpt( curlpp::options::CaPath("config/ca.crt")); - request.setOpt( curlpp::options::SslVerifyPeer(false)); - - // request.setOpt(new curlpp::options::Post(true)); - - // request.setOpt(new curlpp::options::PostFields("DELETE")); - request.setOpt( curlpp::options::CustomRequest("DELETE")); - // request.setOpt(new curlpp::options::Verbose(true)); - - - request.perform(); - auto response = result.str(); - // LOG_S(INFO)< -#define CATCH_CONFIG_MAIN -#include - -#include -#include - -SCENARIO("Testing the kubecontrol") -{ - std::string file= "../docs/config"; -kubecontrol::kubecontrol kubecontroler(file); - - GIVEN("different Attributes for a Track in different forms") - { - kubecontroler.createYAML(); - kubecontroler.startPod(); - // kc.getPods(); - // kc.deletePod("debug-debian"); - kubecontroler.getPods(); - - WHEN("constructing Track Object with data") - { - - THEN("check if Track attributes are correct") - { - REQUIRE(true == true); - - - - - - } //THEN - } // WHEN - } // GIVEN -} //SCENARIO