ADD: added a general kubenetes API and integrated that in the classes and remodeled the info management

This commit is contained in:
hwinkel
2023-12-22 13:14:40 +01:00
parent 351cf9ec69
commit 4045f21786
13 changed files with 363 additions and 833 deletions

View File

@@ -1,7 +1,6 @@
#pragma once
#include "kubecontrol/KubernetesAPI.hpp"
#include "kubecontrol/PodInfo.hpp"
#include "nlohmann/json_fwd.hpp"
#include "yaml-cpp/node/node.h"
#include <map>
@@ -26,11 +25,13 @@ namespace kubecontrol
class KubePod
{
public:
KubePod(std::string Owner, std::string Uuid, std::string ContainerImage,std::string Namespace = "simulator");
KubePod(std::string Owner, std::string Uuid, std::string ContainerImage,std::string Namespace = "simulator") ;
KubePod(std::string Owner, std::string Uuid, std::string Component, std::string ContainerImage,std::string Namespace = "simulator");
std::string getUUID();
std::string getOwner();
std::string getIp();
std::string getStatus();
void setEnvironmentVar(std::string key, std::string val);
std::map<std::string, std::string> GetEnvironmentVars();
@@ -44,37 +45,20 @@ namespace kubecontrol
void setComponent(std::string component);
std::string getComponent();
// [[deprecated("replace with start(KubernetesAPI APIInterface)")]]
// std::string start(std::string apiAddress,std::string token,bool WaitTillRunning = true);
std::string start(KubernetesAPI APIInterface,bool WaitTillRunning = true);
[[deprecated("replace with stop(KubernetesAPI APIInterface)")]]
std::string stop(std::string apiAddress,std::string token);
std::string stop(KubernetesAPI APIInterface);
// [[deprecated("replace with getInfo(KubernetesAPI APIInterface)")]]
// std::string getInfo(std::string apiAddress,std::string token);
std::string getInfoForThisPod(KubernetesAPI APIInterface);
std::string getInfoForRelatedPods(KubernetesAPI APIInterface);
int start(KubernetesAPI APIInterface,bool WaitTillRunning = true);
int stop(KubernetesAPI APIInterface);
int stopChilds(KubernetesAPI APIInterface);
int updateInfoForThisPod(KubernetesAPI APIInterface);
std::vector<std::string> getUUIDForRelatedPods(KubernetesAPI APIInterface);
std::vector<std::string> getUUIDsForChildPods(KubernetesAPI APIInterface);
PodInfo InfoPod;
private:
static const int MaxWaitTimeInSeconds;
std::string Owner_;
std::string Uuid_;
@@ -98,10 +82,14 @@ namespace kubecontrol
std::string createYAML();
// std::string read;
/**
* @brief extracts the asked inforamtion from the kubernetes response
*
*
*/
int extractInformationFromResopnse(std::string response);
std::string StopChilds(std::string apiAddress,std::string token,std::string uuid);
};
}

View File

@@ -19,8 +19,11 @@ class KubernetesAPI
std::string performRequest(std::string request,std::string Methode,std::string PostFields);
void addAddress(std::string address);
std::string getAddress();
void addToken(std::string Token);
void addYaml(YAML::Node config);
std::string getNamespace();
private:
std::string APIAddress_;
std::string Token_;

View File

@@ -1,6 +1,5 @@
#pragma once
#include "kubecontrol/KubernetesAPI.hpp"
#include "kubecontrol/PodInfo.hpp"
#include <cstddef>
#include <kubecontrol/KubePod.hpp>
#include <memory>
@@ -10,6 +9,16 @@
namespace kubecontrol
{
struct PodInfos
{
std::string UUID;
std::string IP;
std::string Status;
std::string Component;
std::string Owner;
std::vector<std::string> UUIDOfChilds;
};
class PodController
{
public:
@@ -21,33 +30,21 @@ namespace kubecontrol
void stopPod(std::string uuid);
void stopAllPods();
std::string getInfoForAllPods();
// std::string getInfoForOwnPod();
std::string getInfoForPod(std::string uuid);
size_t getListOfChildPods();
std::vector<PodInfos> getInfoForAllPods();
PodInfos getInfoForPod(std::string uuid);
void checkPodsHierarchy();
KubernetesAPI getKubernetesAPI();
std::string getPodsInfoForAll();
std::shared_ptr<PodInfo> getPodsInfo(std::string uuid);
private:
KubernetesAPI APIInterface_;
std::vector<KubePod> PodList_;
std::vector<std::unique_ptr<KubePod>> PodList_;
std::string performRequest(std::string url);
void performForceStopRequest(std::string uuid);
std::string BearerToken_;
std::string ServerAddress_;
std::string Namespace_;
std::string ApiCall_;
// void addPodInfoToInfoList(std::shared_ptr<PodInfo> podinfo);
std::shared_ptr<PodInfo> getPodInfo(std::string uuid);
PodInfos extractInfosFromKubePod(KubePod *pod);
std::vector<std::shared_ptr<PodInfo>> podsInfoList_;
mutable std::mutex mx_;
};

View File

@@ -1,66 +0,0 @@
#pragma once
#include "nlohmann/json_fwd.hpp"
#include <memory>
#include <string>
#include <vector>
#include <nlohmann/json.hpp>
#include <string>
#include <loguru.hpp>
namespace kubecontrol {
class PodInfo
{
public:
PodInfo();
PodInfo(std::string response);
void update(std::string response);
std::string ToString();
nlohmann::json ToJson();
std::vector<std::string> getRelatedPods();
std::string Uuid;
std::string Image;
std::string Ip;
std::string Component;
std::string Status;
std::string PartOf;
void addRelatedPods(std::string uuid);
size_t relatedPodsSize();
private:
std::vector<std::string> relatedPods;
};
// struct PodInfo
// {
// std::string Uuid;
// std::string Image;
// std::string Ip;
// std::string Component;
// std::string Status;
// std::string PartOf;
// std::vector<std::pair<std::string,std::shared_ptr<KubePod>>> relatedPods;
// bool operator==(PodInfo const &rhs )
// {
// if (Uuid == rhs.Uuid) {
// return true;
// }
// return false;
// }
// };
}