From d0db414c16ab82714eefaa337378816cbcd6c489 Mon Sep 17 00:00:00 2001 From: Henry Winkel Date: Thu, 26 Oct 2023 15:57:37 +0200 Subject: [PATCH] FIX: fixed issue where the response of the kube api was not written correct --- include/kubecontrol/WriterMemoryClass.hpp | 55 +++++------------------ src/kubecontrol/KubePod.cpp | 12 ++--- src/kubecontrol/PodInfo.cpp | 4 +- tests/test_KubePod.cpp | 2 +- 4 files changed, 20 insertions(+), 53 deletions(-) diff --git a/include/kubecontrol/WriterMemoryClass.hpp b/include/kubecontrol/WriterMemoryClass.hpp index 43a67b6..9ce957f 100644 --- a/include/kubecontrol/WriterMemoryClass.hpp +++ b/include/kubecontrol/WriterMemoryClass.hpp @@ -10,62 +10,29 @@ class WriterMemoryClass { public: - // Helper Class for reading result from remote host - WriterMemoryClass() - { - this->m_pBuffer = NULL; - this->m_pBuffer = (char*) malloc(MAX_FILE_LENGTH * sizeof(char)); - this->m_Size = 0; - }; - - ~WriterMemoryClass() - { - if (this->m_pBuffer) - free(this->m_pBuffer); - }; - - void* Realloc(void* ptr, size_t size) - { - if(ptr) - return realloc(ptr, size); - else - return malloc(size); - }; - + // Callback must be declared static, otherwise it won't link... size_t WriteMemoryCallback(char* ptr, size_t size, size_t nmemb) { - // Calculate the real size of the incoming buffer - size_t realsize = size * nmemb; - - // (Re)Allocate memory for the buffer - m_pBuffer = (char*) Realloc(m_pBuffer, m_Size + realsize); - - // Test if Buffer is initialized correctly & copy memory - if (m_pBuffer == NULL) { - realsize = 0; - } - - memcpy(&(m_pBuffer[m_Size]), ptr, realsize); - m_Size += realsize; - - - // return the real size of the buffer... - return realsize; + str_pBuffer.erase(); + str_pBuffer = std::string(ptr); + + return str_pBuffer.size(); }; std::string getResponse() { - return std::string(m_pBuffer); + // return std::string(m_pBuffer); + return str_pBuffer; } void print() { - std::cout << "Size: " << m_Size << std::endl; - std::cout << "Content: " << std::endl << m_pBuffer << std::endl; + std::cout << "Size: " << str_pBuffer.size() << std::endl; + std::cout << "Content: " << std::endl << str_pBuffer << std::endl; } // Public member vars - char* m_pBuffer; - size_t m_Size; + private: + std::string str_pBuffer; }; \ No newline at end of file diff --git a/src/kubecontrol/KubePod.cpp b/src/kubecontrol/KubePod.cpp index e159d6e..b0bddf9 100644 --- a/src/kubecontrol/KubePod.cpp +++ b/src/kubecontrol/KubePod.cpp @@ -226,12 +226,13 @@ namespace kubecontrol request.perform(); request.reset(); - auto response = mWriterChunk.getResponse(); + std::string response = mWriterChunk.getResponse(); InfoPod.update(response); - - while (InfoPod.Status != "Running") { + while (InfoPod.Status != "Running" && InfoPod.Status != "Succeeded") { + std::this_thread::sleep_for(std::chrono::milliseconds(200)); - response = this->getInfo(curlURL, token); + response = this->getInfo(curlURL, token); + InfoPod.update(response); } @@ -348,8 +349,7 @@ namespace kubecontrol request.perform(); - auto response = mWriterChunk.getResponse(); - return response; + return mWriterChunk.getResponse(); } diff --git a/src/kubecontrol/PodInfo.cpp b/src/kubecontrol/PodInfo.cpp index 9b1ea09..c320edf 100644 --- a/src/kubecontrol/PodInfo.cpp +++ b/src/kubecontrol/PodInfo.cpp @@ -47,7 +47,6 @@ namespace kubecontrol Component = j["metadata"]["labels"]["app.kubernetes.io/component"].get(); PartOf = j["metadata"]["labels"]["app.kubernetes.io/part-of"].get(); Image = j["spec"]["containers"][0]["image"].get(); - if (j["status"].contains("podIP")) Ip = j["status"]["podIP"].get(); Status = j["status"]["phase"].get(); @@ -57,7 +56,8 @@ namespace kubecontrol } catch (std::exception& e) - { + { + // LOG_S(WARNING)<< response; LOG_S(ERROR)<