Files
KubeControl/include/kubecontrol/KubePod.hpp

253 lines
7.3 KiB
C++

#pragma once
#include <kubecontrol/KubernetesAPI.hpp>
#include "nlohmann/json_fwd.hpp"
#include "yaml-cpp/node/node.h"
#include <map>
#include <cstdint>
#include <memory>
#include <string>
#include <vector>
#include <yaml-cpp/yaml.h>
#include <fstream>
#include <curlpp/Easy.hpp>
#include <curlpp/Options.hpp>
#define LOGURU_WITH_STREAMS 1
#include <loguru.hpp>
#include <nlohmann/json.hpp>
#include <curlpp/cURLpp.hpp>
#include <filesystem>
#include <kubecontrol/Container.hpp>
namespace kubecontrol
{
struct PodChild
{
std::string UUID;
std::string IP;
std::string Component;
std::string Status;
};
struct PodInfos
{
std::string UUID;
std::string IP;
std::string Status;
std::string Component;
std::string Owner;
std::vector<PodChild> Childs;
};
class KubePod
{
public:
KubePod(const std::string &Owner, const std::string &Uuid, const std::string &ContainerImage, const std::string &Namespace = "simulator") ;
KubePod(const std::string &Owner, const std::string &Uuid, const std::string &Component, const std::string &ContainerImage, const std::string &Namespace = "simulator");
/**
* @brief returns the uuid of the pod
*
* @return std::string
*/
std::string getUUID();
/**
* @brief Get the Owner of the pods
*
* @return std::string
*/
std::string getOwner();
/**
* @brief Get the Ip of the pod
*
* @return std::string
*/
std::string getIp();
/**
* @brief Get the Status
*
* @return std::string
*/
std::string getStatus();
/**
* @brief Set the Environment Vars for pod
*
* @param key
* @param val
*/
void setEnvironmentVar(std::string key, std::string val);
/**
* @brief Get the Environment Vars
*
* @return std::map<std::string, std::string>
*/
std::map<std::string, std::string> GetEnvironmentVars();
/**
* @brief Get the Environment Var for a specific key
*
* @param key std::string
* @return std::string
*/
std::string GetEnvironmentVar(std::string key);
/**
* @brief Set CMD args
*
* @param args
*/
void setArgs(const std::string &args);
/**
* @brief Get the Args
*
* @return std::vector<std::string>
*/
std::vector<std::string> GetArgs();
/**
* @brief Set the Command for a pod
*
* @param command
*/
void setCommand(const std::string &command);
/**
* @brief Get the Command
*
* @return std::string
*/
std::string getCommand();
/**
* @brief Set the Component var for the pod
*
* @param component
*/
void setComponent(const std::string &component);
/**
* @brief Get the Component var
*
* @return std::string
*/
std::string getComponent();
/**
* @brief add a Container to a pod
*
* @param name std::string
* @param image std::string
* @param envs std::map<std::string,std:.string>
* @param PullPolicy std::string (defualt: Always) possible:( IfNotPresent, Never)
*/
void addContainer(std::string name,std::string image, std::map<std::string, std::string> &envs , kubecontrol::PullPolicy pullPolicy = ALWAYS);
/**
* @brief add container object
*
* @param containter
*/
void addContainer(Container containter);
/**
* @brief
*
* @param containerUUID
* @param envs
*/
void addEnvValuesToContainer(std::string containerUUID, std::map<std::string, std::string> envs);
/**
* @brief sets the name of the pod
* @param string - term to be displayed
*/
void setName(std::string &name);
/**
* @brief return the name of the pod
* @return std::string
*/
std::string getName();
int start(KubernetesAPI APIInterface,bool WaitTillRunning = true);
int stop(KubernetesAPI APIInterface);
int stopChilds(KubernetesAPI APIInterface);
/**
*@brief updates the infos for a pod from the give Kubernetes API
*@param KubernetesAPI - object of KubernetesAPI wich holds all necessary to perfom requests
*@return returns 1 on failure
*/
int updateInfoForThisPod(KubernetesAPI APIInterface);
std::vector<PodChild> getPodsChilds(KubernetesAPI APIInterface);
private:
/// @brief maximum time to wait of the response when creating a pod
static const int MaxWaitTimeInSeconds;
/// @brief uuid of the owner
std::string Owner_;
/// @brief uuid of the pod
std::string Uuid_;
/// @brief name of the pod
std::string Name_;
/// @brief component the pod represents
std::string Component_;
/// @brief vector of images the pod contains
std::vector<Container> ContainerImages_;
/// @brief url of the container registry
std::string ContainerRegistry_;
/// @brief path to kube yaml
std::string PathToYaml_;
/// @brief namespace the pod is designated
std::string Namespace_;
/// @brief IP of the pod
std::string Ip_;
/// @brief status of the pod
std::string Status_;
/// @brief uuid of pod this pod is part of
std::string PartOf_;
/// @brief vector of child pods
std::vector<PodChild> ChildPods;
/// @brief command the pod schould execute when created
std::string PodCommand_;
/// @brief yaml node of the pod which is used for creation
YAML::Node YAMLNode_;
/// @brief environment vars of the pod
std::map<std::string, std::string> EnvirmonentVars_;
/// @brief arguments for the pod
std::vector<std::string> Args_;
/**
* @brief function to create the yaml string for the pod creation
* @return std::string - returns a serialized yaml
*/
std::string createYAML();
/**
* @brief extracts the asked inforamtion from the kubernetes response
*
*
*/
int extractInformationFromResopnse(std::string response);
};
}