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,18 +1,33 @@
#include "curlpp/Options.hpp"
#include "kubecontrol/Utils.hpp"
#include "yaml-cpp/binary.h"
#include <fstream>
#include <iterator>
#include <kubecontrol/KubePod.hpp>
#include <sstream>
#include <string>
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<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)
{
@@ -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<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()
{
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();
}
}
}

View File

@@ -41,7 +41,7 @@ namespace kubecontrol
PodList_.emplace_back(Pod);
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_)
{
if (Label == item.getLabel())
if (Label == item.getUUID())
{
item.stop(ServerAddress_+ApiCall_, BearerToken_);
}
@@ -105,7 +105,7 @@ namespace kubecontrol
{
for (auto item : PodList_)
{
if (Label == item.getLabel())
if (Label == item.getUUID())
{
return item.getInfo(ServerAddress_+ApiCall_, BearerToken_);
}