From 5f043ca88e235b3f9d71f157470c63cf8e1b7fa9 Mon Sep 17 00:00:00 2001 From: Henry Winkel Date: Tue, 8 Aug 2023 15:09:18 +0200 Subject: [PATCH] ADD: added a podcontroller and test for the first basic usage --- CMakeLists.txt | 4 ++ config/ca.crt | 27 -------- config/curl_command.txt | 1 + docs/Doxyfile.in | 23 ------- docs/test.json | 99 --------------------------- include/kubecontrol/KubePod.hpp | 6 +- include/kubecontrol/PodController.hpp | 6 +- src/kubecontrol/KubePod.cpp | 45 ++++++------ src/kubecontrol/PodController.cpp | 97 +++++++++++++++++++++++++- src/kubecontrol/kubecontrol.cpp | 36 ++++++++-- tests/test_KubePod.cpp | 8 +-- tests/test_kubecontrol.cpp | 11 ++- tests/test_podcontroller.cpp | 25 ++++++- 13 files changed, 196 insertions(+), 192 deletions(-) delete mode 100644 config/ca.crt create mode 100644 config/curl_command.txt delete mode 100644 docs/Doxyfile.in delete mode 100644 docs/test.json diff --git a/CMakeLists.txt b/CMakeLists.txt index bc4d392..bc90a28 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -89,6 +89,10 @@ target_include_directories(kubecontrol PUBLIC add_custom_command(TARGET kubecontrol POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/config $/config) + + add_custom_command(TARGET kubecontrol POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_directory + ${CMAKE_SOURCE_DIR}/docs $/docs) # # Everything TEST related # diff --git a/config/ca.crt b/config/ca.crt deleted file mode 100644 index 3e79282..0000000 --- a/config/ca.crt +++ /dev/null @@ -1,27 +0,0 @@ ------BEGIN CERTIFICATE----- -MIIEozCCAwugAwIBAgIBATANBgkqhkiG9w0BAQsFADA+MRwwGgYDVQQKDBNUSS5V -TklCVy1IQU1CVVJHLkRFMR4wHAYDVQQDDBVDZXJ0aWZpY2F0ZSBBdXRob3JpdHkw -HhcNMjIwOTA4MTMyNTE2WhcNNDIwOTA4MTMyNTE2WjA+MRwwG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ciYjXY7h -VQrCkjAa8khj9dqPV1Yndx9dUR7qv4KluUNLc6SMEZ4WdGL4xGIvSc/Mp49UwP3f -p7kio88qGTygpvO6rMcfwtkvhz1rb5s1tg+c9OkPBkTSyWIzg/6sNVB2n2I3mSJz -F7fruyS6SfmgK+FWtKBvyD6htFPuBJrS9HFVHSHIgvE36xIOCWC50L1k4ZVOykoq -dhu/v4HRrGZp9AkQjMzm3bqoua62YitF5LOAQb9idz7w2b7KwwqINHti186cxsv3 -9dpMlJVPIrf9Y97YAK2aV4OdEaDjWXVjfuJ2jrqOAV9gWqUCx6AxN4kCaaZ70NIp -PtsUQZvheUOcmH/XWA+3R5iv6bFUfUYKBCagc5f7N9I+0p/OovwH ------END CERTIFICATE----- diff --git a/config/curl_command.txt b/config/curl_command.txt new file mode 100644 index 0000000..449c6f4 --- /dev/null +++ b/config/curl_command.txt @@ -0,0 +1 @@ +curl -k https://10.96.0.1:443/api/v1/namespaces/hwinkel/pods -H "Authorization: Bearer $TOKEN" \ No newline at end of file diff --git a/docs/Doxyfile.in b/docs/Doxyfile.in deleted file mode 100644 index f2ebb78..0000000 --- a/docs/Doxyfile.in +++ /dev/null @@ -1,23 +0,0 @@ -DOXYFILE_ENCODING = UTF-8 -PROJECT_NAME = @PROJECT_NAME@ -PROJECT_NUMBER = "@GIT_BRANCH@ @GIT_COMMIT_HASH@" -PROJECT_BRIEF = "library implementing a floats to libcms gateway" -OUTPUT_DIRECTORY = @CMAKE_CURRENT_BINARY_DIR@/doxygen/ -OUTPUT_LANGUAGE = English -BRIEF_MEMBER_DESC = YES -REPEAT_BRIEF = YES -ALWAYS_DETAILED_SEC = NO -INLINE_INHERITED_MEMB = NO -FULL_PATH_NAMES = YES -MULTILINE_CPP_IS_BRIEF = NO -TAB_SIZE = 4 -MARKDOWN_SUPPORT = YES -GENERATE_TODOLIST = YES -GENERATE_TESTLIST = YES -GENERATE_BUGLIST = YES -GENERATE_DEPRECATEDLIST= YES -INPUT = @CMAKE_CURRENT_SOURCE_DIR@/README.md @CMAKE_CURRENT_SOURCE_DIR@/src/ @CMAKE_CURRENT_SOURCE_DIR@/tests/ @CMAKE_CURRENT_SOURCE_DIR@/docs @CMAKE_CURRENT_SOURCE_DIR@/include -INPUT_ENCODING = UTF-8 -FILE_PATTERNS = *.hpp *.cpp *.md -USE_MDFILE_AS_MAINPAGE = README.md -RECURSIVE = YES diff --git a/docs/test.json b/docs/test.json deleted file mode 100644 index 0428e5b..0000000 --- a/docs/test.json +++ /dev/null @@ -1,99 +0,0 @@ -{"annotations":{"kubectl.kubernetes.io/last-applied-configuration":"{\"apiVersion\":\"v1\",\"kind\":\"Pod\",\"metadata\":{\"annotations\":{},\"labels\":{\"app.kubernetes.io/name\":\"plaindebian\"},\"name\":\"debug-debian\",\"namespace\":\"hwinkel\"},\"spec\":{\"containers\":[{\"args\":[\"-c\",\"while true; do echo hello; sleep 5;done\"],\"command\":[\"/bin/sh\"],\"env\":[{\"name\":\"OWN_SHIP_SERVER\",\"value\":\"127.0.0.1\"}],\"image\":\"kmaster.ti.unibw-hamburg.de:30808/debugdebianhenry:0.1.2\",\"name\":\"debug-debian-container\"}],\"restartPolicy\":\"Never\"}}\n"}, - "creationTimestamp":"2023-08-07T09:01:18Z", - "labels":{"app.kubernetes.io/name":"plaindebian"}, - "managedFields":[ - {"apiVersion":"v1", - "fieldsType":"FieldsV1", - "fieldsV1":{ - "f:metadata":{ - "f:annotations":{ - ".":{}, - "f:kubectl.kubernetes.io/last-applied-configuration":{} - }, - "f:labels":{ - ".":{}, - "f:app.kubernetes.io/name":{} - } - }, - "f:spec":{ - "f:containers":{ - "k:{\"name\":\"debug-debian-container\"}":{ - ".":{}, - "f:args":{}, - "f:command":{}, - "f:env":{ - ".":{}, - "k:{\"name\":\"OWN_SHIP_SERVER\"}":{ - ".":{},"f:name":{},"f:value":{} - } - }, - "f:image":{}, - "f:imagePullPolicy":{}, - "f:name":{}, - "f:resources":{}, - "f:terminationMessagePath":{}, - "f:terminationMessagePolicy":{} - } - }, - "f:dnsPolicy":{}, - "f:enableServiceLinks":{}, - "f:restartPolicy":{}, - "f:schedulerName":{}, - "f:securityContext":{}, - "f:terminationGracePeriodSeconds":{} - } - }, - "manager":"kubectl-client-side-apply", - "operation":"Update", - "time":"2023-08-07T09:01:18Z" - }, - {"apiVersion":"v1", - "fieldsType":"FieldsV1", - "fieldsV1":{ - "f:status":{ - "f:conditions":{ - "k:{\"type\":\"ContainersReady\"}":{ - ".":{}, - "f:lastProbeTime":{}, - "f:lastTransitionTime":{}, - "f:status":{}, - "f:type":{} - }, - "k:{\"type\":\"Initialized\"}":{ - ".":{}, - "f:lastProbeTime":{}, - "f:lastTransitionTime":{}, - "f:status":{}, - "f:type":{} - }, - "k:{\"type\":\"Ready\"}":{ - ".":{}, - "f:lastProbeTime":{}, - "f:lastTransitionTime":{}, - "f:status":{}, - "f:type":{} - } - }, - "f:containerStatuses":{}, - "f:hostIP":{}, - "f:phase":{}, - "f:podIP":{}, - "f:podIPs":{ - ".":{}, - "k:{\"ip\":\"10.0.3.70\"}":{ - ".":{}, - "f:ip":{} - } - }, - "f:startTime":{} - } - }, - "manager":"kubelet", - "operation":"Update", - "subresource":"status", - "time":"2023-08-07T09:01:20Z"} -], -"name":"debug-debian", -"namespace":"hwinkel", -"resourceVersion":"170886884", -"uid":"f9df752e-2404-433f-b377-6e03721a5044"} \ No newline at end of file diff --git a/include/kubecontrol/KubePod.hpp b/include/kubecontrol/KubePod.hpp index 7acbc57..0f008c1 100644 --- a/include/kubecontrol/KubePod.hpp +++ b/include/kubecontrol/KubePod.hpp @@ -28,9 +28,9 @@ namespace kubecontrol std::string* GetEnvironmentVar(std::string key); std::string createYAML(); - nlohmann::json start(std::string apiAddress,std::string token); - nlohmann::json stop(std::string apiAddress,std::string token); - nlohmann::json getInfo(std::string apiAddress,std::string token); + std::string start(std::string apiAddress,std::string token); + std::string stop(std::string apiAddress,std::string token); + std::string getInfo(std::string apiAddress,std::string token); diff --git a/include/kubecontrol/PodController.hpp b/include/kubecontrol/PodController.hpp index dccd467..660916f 100644 --- a/include/kubecontrol/PodController.hpp +++ b/include/kubecontrol/PodController.hpp @@ -11,10 +11,14 @@ namespace kubecontrol std::string getServerAddress(); void startPod(KubePod Pod); + void stopPod(std::string Label); void stopAllPods(); + std::string getPodsInfo(); + std::string getInfoForPod(std::string Label); + private: - std::vector PodList; + std::vector PodList_; std::string BearerToken_; std::string ServerAddress_; diff --git a/src/kubecontrol/KubePod.cpp b/src/kubecontrol/KubePod.cpp index c6c64bd..7c5a367 100644 --- a/src/kubecontrol/KubePod.cpp +++ b/src/kubecontrol/KubePod.cpp @@ -63,11 +63,12 @@ namespace kubecontrol node["spec"]["containers"][0]["name"] = this->Label_+"-container"; node["spec"]["containers"][0]["image"] = this->ContainerRegistry_ + "/" + this->ContainerImage_; + node["spec"]["containers"][0]["imagePullPolicy"] = "Always"; int i = 0; for (auto item :EnvirmonentVars_) { node["spec"]["containers"][0]["env"][i]["name"] = item.first; - node["spec"]["containers"][0]["env"][i]["value"] = R"(item.second)"; + node["spec"]["containers"][0]["env"][i]["value"] = item.second; } @@ -82,7 +83,7 @@ namespace kubecontrol } - nlohmann::json KubePod::start(std::string apiAddress,std::string token) + std::string KubePod::start(std::string apiAddress,std::string token) { std::string curlURL = apiAddress; std::string AuthString = "Authorization: Bearer " + token; @@ -109,28 +110,30 @@ namespace kubecontrol request.setOpt(new curlpp::options::Post(true)); - if(std::filesystem::exists(this->PathToYaml_) != true) - { - this->createYAML(); - } - std::ifstream is; - is.open (this->PathToYaml_, std::ios::binary ); - is.seekg (0, std::ios::end); - long length = is.tellg(); - is.seekg (0, std::ios::beg); - char *buffer = new char [length]; - is.read (buffer,length); - - + + this->createYAML(); + + LOG_S(ERROR)<< this->PathToYaml_; + + std::ifstream is; + is.open (this->PathToYaml_, std::ios::binary ); + is.seekg (0, std::ios::end); + long length = is.tellg(); + is.seekg (0, std::ios::beg); + char *buffer = new char [length]; + is.read(buffer,length); + is.close(); request.setOpt(new curlpp::options::PostFields(buffer)); request.perform(); + + request.reset(); auto response = mWriterChunk.getResponse(); - return nlohmann::json::parse(response); + return nlohmann::json::parse(response).dump(); } - nlohmann::json KubePod::stop(std::string apiAddress,std::string token) + std::string KubePod::stop(std::string apiAddress,std::string token) { std::string curlURL = apiAddress+ this->Uuid_; std::string AuthString = "Authorization: Bearer " + token; @@ -160,12 +163,12 @@ namespace kubecontrol request.perform(); auto response = mWriterChunk.getResponse(); - return nlohmann::json::parse(response); + return nlohmann::json::parse(response).dump(); } - nlohmann::json KubePod::getInfo(std::string apiAddress,std::string token) + std::string KubePod::getInfo(std::string apiAddress,std::string token) { - std::string curlURL = apiAddress; + std::string curlURL = apiAddress+"/"+Uuid_; std::string AuthString = "Authorization: Bearer " + token; std::list headers; @@ -191,7 +194,7 @@ namespace kubecontrol request.perform(); auto response = mWriterChunk.getResponse(); - return nlohmann::json::parse(response); + return nlohmann::json::parse(response).dump(); } } \ No newline at end of file diff --git a/src/kubecontrol/PodController.cpp b/src/kubecontrol/PodController.cpp index 0824628..ec12010 100644 --- a/src/kubecontrol/PodController.cpp +++ b/src/kubecontrol/PodController.cpp @@ -1,5 +1,8 @@ +#include +#include #include +#include namespace kubecontrol @@ -9,7 +12,20 @@ namespace kubecontrol YAML::Node config = YAML::LoadFile(pathToKubectlConfig); BearerToken_ = config["users"][0]["user"]["token"].as(); - ServerAddress_ = config["clusters"][0]["cluster"]["server"].as(); + + char * KUBERNETES_SERVICE_HOST = std::getenv("KUBERNETES_SERVICE_HOST"); + char * KUBERNETES_SERVICE_PORT = std::getenv("KUBERNETES_SERVICE_PORT"); + + if (KUBERNETES_SERVICE_HOST != nullptr && KUBERNETES_SERVICE_PORT!= nullptr) + { + ServerAddress_ = std::string(KUBERNETES_SERVICE_HOST)+":"+std::string(KUBERNETES_SERVICE_PORT); + } else + { + LOG_S(INFO)<<"Taking Serveraddress out of the provided YAML file"; + + ServerAddress_ = config["clusters"][0]["cluster"]["server"].as(); + } + ApiCall_ = "/api/v1/namespaces/hwinkel/pods/"; } @@ -19,4 +35,83 @@ namespace kubecontrol } + + void PodController::startPod(KubePod Pod) + { + PodList_.emplace_back(Pod); + LOG_S(INFO)<< "starting pod: "< headers; + headers.push_back(AuthString); + + 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); + + 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(); + auto response = mWriterChunk.getResponse(); + + auto j = nlohmann::json::parse(response); + return j.dump(); + } + + + std::string PodController::getInfoForPod(std::string Label) + { + for (auto item : PodList_) + { + if (Label == item.getLabel()) + { + return item.getInfo(ServerAddress_+ApiCall_, BearerToken_); + } + } + return nullptr; + } + + } \ No newline at end of file diff --git a/src/kubecontrol/kubecontrol.cpp b/src/kubecontrol/kubecontrol.cpp index 73af23d..8b0d8d8 100644 --- a/src/kubecontrol/kubecontrol.cpp +++ b/src/kubecontrol/kubecontrol.cpp @@ -1,7 +1,12 @@ #include "nlohmann/json_fwd.hpp" +#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 @@ -10,6 +15,7 @@ #include #include +#include #include #include @@ -109,6 +115,12 @@ namespace kubecontrol 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 ); @@ -123,7 +135,7 @@ namespace kubecontrol // read data as a block: is.read (buffer,length); - + std::istringstream myStream((std::string( buffer ))); int size = myStream.str().size(); is.close(); @@ -137,7 +149,7 @@ namespace kubecontrol request.perform(); auto response = mWriterChunk.getResponse(); - // LOG_S(INFO)< #define CATCH_CONFIG_MAIN #include #include - +#include SCENARIO("Testing the SimCore Sensor") { @@ -16,9 +21,9 @@ kubecontrol::kubecontrol kc("../docs/config"); kc.createYAML(); kc.startPod(); // kc.getPods(); - kc.deletePod("debug-debian"); + // kc.deletePod("debug-debian"); kc.getPods(); - + WHEN("constructing Track Object with data") { diff --git a/tests/test_podcontroller.cpp b/tests/test_podcontroller.cpp index 22cc96f..75861fe 100644 --- a/tests/test_podcontroller.cpp +++ b/tests/test_podcontroller.cpp @@ -1,25 +1,44 @@ +#include "kubecontrol/KubePod.hpp" +#include "nlohmann/json_fwd.hpp" #define CATCH_CONFIG_MAIN #include #include +#include SCENARIO("Testing the SimCore Sensor") { -kubecontrol::PodController podc("../docs/config"); - +kubecontrol::PodController podc("docs/config"); + + +kubecontrol::KubePod pod1("pod1",xg::newGuid().str(),"debugdebianhenry:0.1.3"); + +kubecontrol::KubePod pod2("pod2",xg::newGuid().str(),"debugdebianhenry:0.1.3"); + +podc.startPod(pod1); +podc.startPod(pod2); +std::string info1 = ""; +info1 = podc.getInfoForPod("pod1"); +nlohmann::json j1 = nlohmann::json::parse(info1); + +nlohmann::json jall = nlohmann::json::parse(podc.getPodsInfo()); + +// podc.stopAllPods(); + GIVEN("different Attributes for a Track in different forms") { - LOG_S(INFO)<< podc.getServerAddress(); WHEN("constructing Track Object with data") { THEN("check if Track attributes are correct") { REQUIRE(true == true); + + // REQUIRE(info1 != "");