ADD: added more labels to identifiy pods

This commit is contained in:
Henry Winkel
2023-08-14 17:09:55 +02:00
parent f767307987
commit 6c2f8f1eb6
3 changed files with 92 additions and 17 deletions

View File

@@ -1,6 +1,7 @@
#pragma once #pragma once
#include "nlohmann/json_fwd.hpp" #include "nlohmann/json_fwd.hpp"
#include "yaml-cpp/node/node.h"
#include <map> #include <map>
#include <string> #include <string>
#include <vector> #include <vector>
@@ -23,9 +24,11 @@ namespace kubecontrol
class KubePod class KubePod
{ {
public: 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 getUUID();
std::string getLabel(); std::string getOwner();
void setEnvironmentVar(std::string key, std::string val); void setEnvironmentVar(std::string key, std::string val);
std::map<std::string, std::string> GetEnvironmentVars(); std::map<std::string, std::string> GetEnvironmentVars();
@@ -37,6 +40,9 @@ namespace kubecontrol
void setCommand(std::string command); void setCommand(std::string command);
std::string getCommand(); std::string getCommand();
void setComponent(std::string component);
std::string getComponent();
std::string createYAML(); std::string createYAML();
std::string start(std::string apiAddress,std::string token); std::string start(std::string apiAddress,std::string token);
@@ -46,13 +52,17 @@ namespace kubecontrol
private: private:
std::string Label_; std::string Owner_;
std::string Uuid_; std::string Uuid_;
std::string Component_;
std::string ContainerImage_; std::string ContainerImage_;
std::string ContainerRegistry_; std::string ContainerRegistry_;
std::string PathToYaml_; std::string PathToYaml_;
std::string PodCommand_; std::string PodCommand_;
YAML::Node YAMLNode_;
std::map<std::string, std::string> EnvirmonentVars_; std::map<std::string, std::string> EnvirmonentVars_;
std::vector<std::string> Args_; std::vector<std::string> Args_;

View File

@@ -1,18 +1,33 @@
#include "curlpp/Options.hpp" #include "curlpp/Options.hpp"
#include "kubecontrol/Utils.hpp" #include "kubecontrol/Utils.hpp"
#include "yaml-cpp/binary.h"
#include <fstream>
#include <iterator>
#include <kubecontrol/KubePod.hpp> #include <kubecontrol/KubePod.hpp>
#include <sstream>
#include <string>
namespace kubecontrol 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_() ContainerImage_(ContainerImage),EnvirmonentVars_()
{ {
ContainerRegistry_ = "kmaster.ti.unibw-hamburg.de:30808"; ContainerRegistry_ = "kmaster.ti.unibw-hamburg.de:30808";
EnvirmonentVars_ = std::map<std::string, std::string>(); EnvirmonentVars_ = std::map<std::string, std::string>();
this->PathToYaml_ = "config/pods/" + this->Label_ + ".yaml"; this->PathToYaml_ = "config/pods/" + this->Uuid_ + ".yaml";
if( std::filesystem::directory_entry("config").exists() != true) if( std::filesystem::directory_entry("config").exists() != true)
{ {
@@ -23,18 +38,39 @@ namespace kubecontrol
{ {
std::filesystem::create_directory("config/pods"); 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<std::string, std::string>();
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() std::string KubePod::getUUID()
{ {
return this->Uuid_; 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) void KubePod::setEnvironmentVar(std::string key, std::string val)
@@ -76,6 +112,17 @@ namespace kubecontrol
return PodCommand_; 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() std::string KubePod::createYAML()
@@ -85,9 +132,14 @@ namespace kubecontrol
node["apiVersion"] = "v1"; node["apiVersion"] = "v1";
node["kind"] = "Pod"; node["kind"] = "Pod";
node["metadata"]["name"] = this->Uuid_; 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]["image"] = this->ContainerRegistry_ + "/" + this->ContainerImage_;
node["spec"]["containers"][0]["imagePullPolicy"] = "Always"; node["spec"]["containers"][0]["imagePullPolicy"] = "Always";
@@ -118,8 +170,11 @@ namespace kubecontrol
node["spec"]["restartPolicy"] = "Never"; node["spec"]["restartPolicy"] = "Never";
YAMLNode_ = node;
std::ofstream fout(this->PathToYaml_); std::ofstream fout(this->PathToYaml_);
fout << node; fout << node;
fout.close(); fout.close();
return this->PathToYaml_; return this->PathToYaml_;
@@ -161,15 +216,21 @@ namespace kubecontrol
LOG_S(ERROR)<< this->PathToYaml_; LOG_S(ERROR)<< this->PathToYaml_;
std::ifstream is; 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); is.seekg (0, std::ios::end);
long length = is.tellg(); long length = is.tellg();
is.seekg (0, std::ios::beg); is.seekg (0, std::ios::beg);
char *buffer = new char [length]; char *buffer = new char [length];
is.read(buffer,length); is.read(buffer,length);
is.close(); 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(); request.perform();
@@ -242,4 +303,8 @@ namespace kubecontrol
return nlohmann::json::parse(response).dump(); return nlohmann::json::parse(response).dump();
} }
}
}

View File

@@ -41,7 +41,7 @@ namespace kubecontrol
PodList_.emplace_back(Pod); PodList_.emplace_back(Pod);
LOG_S(INFO)<< "starting pod: "<<Pod.getUUID(); LOG_S(INFO)<< "starting pod: "<<Pod.getUUID();
Pod.start(ServerAddress_+ApiCall_, BearerToken_); LOG_S(INFO)<<Pod.start(ServerAddress_+ApiCall_, BearerToken_);
} }
@@ -49,7 +49,7 @@ namespace kubecontrol
{ {
for (auto item : PodList_) for (auto item : PodList_)
{ {
if (Label == item.getLabel()) if (Label == item.getUUID())
{ {
item.stop(ServerAddress_+ApiCall_, BearerToken_); item.stop(ServerAddress_+ApiCall_, BearerToken_);
} }
@@ -105,7 +105,7 @@ namespace kubecontrol
{ {
for (auto item : PodList_) for (auto item : PodList_)
{ {
if (Label == item.getLabel()) if (Label == item.getUUID())
{ {
return item.getInfo(ServerAddress_+ApiCall_, BearerToken_); return item.getInfo(ServerAddress_+ApiCall_, BearerToken_);
} }