Skip to content

Commit

Permalink
fix code
Browse files Browse the repository at this point in the history
  • Loading branch information
NameHaibinZhang committed Sep 25, 2024
1 parent 360d089 commit c42c78b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
15 changes: 10 additions & 5 deletions core/metadata/k8sMetadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include <ctime>
#include <chrono>
#include <thread>
#include "k8sMetadata.h"
#include "common/MachineInfoUtil.h"
#include "logger/Logger.h"
Expand Down Expand Up @@ -83,7 +84,7 @@ namespace logtail {
}
}

bool K8sMetadata::SendRequestToOperator(const std::string& urlHost, const std::string& output, containerInfoType infoType) {
int K8sMetadata::SendRequestToOperator(const std::string& urlHost, const std::string& output, containerInfoType infoType) {
std::unique_ptr<HttpRequest> request;
HttpResponse res;
std::string path = "/metadata/containerid";
Expand All @@ -94,7 +95,7 @@ namespace logtail {
bool success = SendHttpRequest(std::move(request), res);
if (res.mStatusCode != 200) {
LOG_DEBUG(sLogger, ("fetch k8s meta from one operator fail, code is ", res.mStatusCode));
return false;
return res.mStatusCode;
}
if (success) {
Json::CharReaderBuilder readerBuilder;
Expand All @@ -113,14 +114,14 @@ namespace logtail {
}
}
} else {
return true;
return 200;
}

delete reader;
} else {
LOG_DEBUG(sLogger, ("fetch k8s meta from one operator fail", urlHost));
}
return true;
return 200;
}

void K8sMetadata::GetByContainerIdsFromServer(std::vector<std::string> containerIds) {
Expand All @@ -143,7 +144,11 @@ namespace logtail {
Json::StreamWriterBuilder writer;
std::string output = Json::writeString(writer, jsonObj);
std::string urlHost = mServiceHost;
SendRequestToOperator(urlHost, output, containerInfoType::ContainerIdInfo);
int code = SendRequestToOperator(urlHost, output, containerInfoType::ContainerIdInfo);
if (code == 503) {
std::this_thread::sleep_for(std::chrono::seconds(1));
SendRequestToOperator(urlHost, output, containerInfoType::ContainerIdInfo);
}
}

void K8sMetadata::SetContainerCache(const Json::Value& root) {
Expand Down
2 changes: 1 addition & 1 deletion core/metadata/k8sMetadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ namespace logtail {
std::shared_ptr<k8sContainerInfo> GetInfoByContainerIdFromCache(const std::string& containerId);
// get info by ip from cache
std::shared_ptr<k8sContainerInfo> GetInfoByIpFromCache(const std::string& ip);
bool SendRequestToOperator(const std::string& urlHost, const std::string& output, containerInfoType infoType);
int SendRequestToOperator(const std::string& urlHost, const std::string& output, containerInfoType infoType);
#ifdef APSARA_UNIT_TEST_MAIN
friend class k8sMetadataUnittest;
#endif
Expand Down

0 comments on commit c42c78b

Please sign in to comment.