From 6c2f8f1eb6d1bd44709e23c292ed1fad15aedf90 Mon Sep 17 00:00:00 2001 From: Henry Winkel Date: Mon, 14 Aug 2023 17:09:55 +0200 Subject: [PATCH] ADD: added more labels to identifiy pods --- include/kubecontrol/KubePod.hpp | 16 ++++-- src/kubecontrol/KubePod.cpp | 87 +++++++++++++++++++++++++++---- src/kubecontrol/PodController.cpp | 6 +-- 3 files changed, 92 insertions(+), 17 deletions(-) diff --git a/include/kubecontrol/KubePod.hpp b/include/kubecontrol/KubePod.hpp index a55984a..6301b81 100644 --- a/include/kubecontrol/KubePod.hpp +++ b/include/kubecontrol/KubePod.hpp @@ -1,6 +1,7 @@ #pragma once #include "nlohmann/json_fwd.hpp" +#include "yaml-cpp/node/node.h" #include #include #include @@ -23,9 +24,11 @@ namespace kubecontrol class KubePod { public: - KubePod(std::string Label, std::string Uuid, std::string ContainerImage); + KubePod(std::string Owner, std::string Uuid, std::string ContainerImage); + KubePod(std::string Owner, std::string Uuid, std::string Component, std::string ContainerImage); + std::string getUUID(); - std::string getLabel(); + std::string getOwner(); void setEnvironmentVar(std::string key, std::string val); std::map GetEnvironmentVars(); @@ -37,6 +40,9 @@ namespace kubecontrol void setCommand(std::string command); std::string getCommand(); + void setComponent(std::string component); + std::string getComponent(); + std::string createYAML(); std::string start(std::string apiAddress,std::string token); @@ -46,13 +52,17 @@ namespace kubecontrol private: - std::string Label_; + std::string Owner_; std::string Uuid_; + std::string Component_; std::string ContainerImage_; std::string ContainerRegistry_; std::string PathToYaml_; + std::string PodCommand_; + + YAML::Node YAMLNode_; std::map EnvirmonentVars_; std::vector Args_; diff --git a/src/kubecontrol/KubePod.cpp b/src/kubecontrol/KubePod.cpp index 63992a6..17d0f50 100644 --- a/src/kubecontrol/KubePod.cpp +++ b/src/kubecontrol/KubePod.cpp @@ -1,18 +1,33 @@ #include "curlpp/Options.hpp" #include "kubecontrol/Utils.hpp" +#include "yaml-cpp/binary.h" +#include +#include #include +#include +#include namespace kubecontrol { + int getFileSize(std::string filename) { // path to file + FILE *p_file = NULL; + p_file = fopen(filename.c_str(),"rb"); + fseek(p_file,0,SEEK_END); + int size = ftell(p_file); + fclose(p_file); + return size; + } - KubePod::KubePod(std::string Label, std::string Uuid, std::string ContainerImage):Label_(Utils::to_lower(Label)),Uuid_(Uuid), + + + KubePod::KubePod(std::string Owner, std::string Uuid, std::string ContainerImage):Owner_(Utils::to_lower(Owner)),Uuid_(Utils::to_lower(Uuid)), ContainerImage_(ContainerImage),EnvirmonentVars_() { ContainerRegistry_ = "kmaster.ti.unibw-hamburg.de:30808"; EnvirmonentVars_ = std::map(); - this->PathToYaml_ = "config/pods/" + this->Label_ + ".yaml"; + this->PathToYaml_ = "config/pods/" + this->Uuid_ + ".yaml"; if( std::filesystem::directory_entry("config").exists() != true) { @@ -23,18 +38,39 @@ namespace kubecontrol { std::filesystem::create_directory("config/pods"); } - - } + + KubePod::KubePod(std::string Owner, std::string Uuid, std::string Component, std::string ContainerImage):Owner_(Utils::to_lower(Owner)), + Uuid_(Utils::to_lower(Uuid)),Component_(Utils::to_lower(Component)), ContainerImage_(ContainerImage),EnvirmonentVars_() + { + ContainerRegistry_ = "kmaster.ti.unibw-hamburg.de:30808"; + EnvirmonentVars_ = std::map(); + + this->PathToYaml_ = "config/pods/" + this->Uuid_ + ".yaml"; + + if( std::filesystem::directory_entry("config").exists() != true) + { + std::filesystem::create_directory("config"); + } + std::filesystem::directory_entry entry("config/pods"); + if(entry.exists() != true) + { + std::filesystem::create_directory("config/pods"); + } + } + + + + std::string KubePod::getUUID() { return this->Uuid_; } - std::string KubePod::getLabel() + std::string KubePod::getOwner() { - return this->Label_; + return this->Owner_; } void KubePod::setEnvironmentVar(std::string key, std::string val) @@ -76,6 +112,17 @@ namespace kubecontrol return PodCommand_; } + void KubePod::setComponent(std::string component) + { + this->Component_ = Utils::to_lower(component); + } + + std::string KubePod::getComponent() + { + return this->Component_; + } + + std::string KubePod::createYAML() @@ -85,9 +132,14 @@ namespace kubecontrol node["apiVersion"] = "v1"; node["kind"] = "Pod"; node["metadata"]["name"] = this->Uuid_; - node["metadata"]["labels"]["app.kubernetes.io/name"] = this->Label_; + node["metadata"]["labels"]["app.kubernetes.io/name"] = this->Uuid_; - node["spec"]["containers"][0]["name"] = this->Label_+"-container"; + node["metadata"]["labels"]["app.kubernetes.io/part-of"] = this->Owner_; + + // if (!std::empty(this->Component_)) node["metadata"]["labels"]["app.kubernetes.io/component"] = this->Component_; + node["metadata"]["labels"]["app.kubernetes.io/component"] = this->Component_; + + node["spec"]["containers"][0]["name"] = this->Uuid_+"-container"; node["spec"]["containers"][0]["image"] = this->ContainerRegistry_ + "/" + this->ContainerImage_; node["spec"]["containers"][0]["imagePullPolicy"] = "Always"; @@ -118,8 +170,11 @@ namespace kubecontrol node["spec"]["restartPolicy"] = "Never"; + YAMLNode_ = node; + std::ofstream fout(this->PathToYaml_); fout << node; + fout.close(); return this->PathToYaml_; @@ -161,15 +216,21 @@ namespace kubecontrol LOG_S(ERROR)<< this->PathToYaml_; std::ifstream is; - is.open (this->PathToYaml_, std::ios::binary ); + is.open (this->PathToYaml_, std::ios::binary | std::ios::ate); 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)); + std::stringstream stream; + stream << YAMLNode_; + // request.setOpt(new curlpp::options::PostFields(buffer)); + request.setOpt(new curlpp::options::PostFields(stream.str())); + request.perform(); @@ -242,4 +303,8 @@ namespace kubecontrol 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 044dbee..9970470 100644 --- a/src/kubecontrol/PodController.cpp +++ b/src/kubecontrol/PodController.cpp @@ -41,7 +41,7 @@ namespace kubecontrol PodList_.emplace_back(Pod); LOG_S(INFO)<< "starting pod: "<