Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
NameHaibinZhang committed Dec 19, 2024
1 parent e096805 commit f68b24a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
5 changes: 5 additions & 0 deletions core/metadata/K8sMetadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ bool K8sMetadata::FromContainerJson(const Json::Value& json, std::shared_ptr<Con
return true;
}

void K8sMetadata::ClearCache() {
containerCache.clear();
ipCache.clear();
}

bool K8sMetadata::SendRequestToOperator(const std::string& urlHost,
const std::string& output,
containerInfoType infoType) {
Expand Down
10 changes: 7 additions & 3 deletions core/metadata/K8sMetadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,14 @@ namespace logtail {
void SetContainerCache(const Json::Value& root);
bool FromInfoJson(const Json::Value& json, k8sContainerInfo& info);
bool FromContainerJson(const Json::Value& json, std::shared_ptr<ContainerData> data);

public:
static K8sMetadata& GetInstance() {
// Destructor
~K8sMetadata() {
ClearCache();
}
static K8sMetadata* GetInstance() {
static K8sMetadata instance(500);
return instance;
return &instance;
}
// 公共方法
//if cache not have,get from server
Expand All @@ -87,6 +90,7 @@ namespace logtail {
// 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);
void ClearCache();

#ifdef APSARA_UNIT_TEST_MAIN
friend class k8sMetadataUnittest;
Expand Down
12 changes: 6 additions & 6 deletions core/metadata/LabelingK8sMetadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ void LabelingK8sMetadata::AddLabelToLogGroup(PipelineEventGroup& logGroup) {
cotainerNotTag.push_back(rIdx);
}
}
auto& k8sMetadata = K8sMetadata::GetInstance();
if (containerVec.empty() || (!k8sMetadata.GetByContainerIdsFromServer(containerVec))) {
K8sMetadata* k8sMetadata = K8sMetadata::GetInstance();
if (containerVec.empty() || (!k8sMetadata->GetByContainerIdsFromServer(containerVec))) {
return;
}
if (remoteIpVec.empty() || (!k8sMetadata.GetByIpsFromServer(remoteIpVec))) {
if (remoteIpVec.empty() || (!k8sMetadata->GetByIpsFromServer(remoteIpVec))) {
return;
}
for (size_t i = 0; i < cotainerNotTag.size(); ++i) {
Expand All @@ -74,12 +74,12 @@ template <typename Event>
bool LabelingK8sMetadata::AddLabels(Event& e, std::vector<std::string>& containerVec, std::vector<std::string>& remoteIpVec) {
bool res = true;

auto& k8sMetadata = K8sMetadata::GetInstance();
K8sMetadata* k8sMetadata = K8sMetadata::GetInstance();
StringView containerIdViewKey(containerIdKey);
StringView containerIdView = e.HasTag(containerIdViewKey) ? e.GetTag(containerIdViewKey) : StringView{};
if (!containerIdView.empty()) {
std::string containerId(containerIdView);
std::shared_ptr<k8sContainerInfo> containerInfo = k8sMetadata.GetInfoByContainerIdFromCache(containerId);
std::shared_ptr<k8sContainerInfo> containerInfo = k8sMetadata->GetInfoByContainerIdFromCache(containerId);
if (containerInfo == nullptr) {
containerVec.push_back(containerId);
res = false;
Expand All @@ -95,7 +95,7 @@ bool LabelingK8sMetadata::AddLabels(Event& e, std::vector<std::string>& containe
StringView remoteIpView = e.HasTag(ipView) ? e.GetTag(ipView) : StringView{};
if (!remoteIpView.empty()) {
std::string remoteIp(remoteIpView);
std::shared_ptr<k8sContainerInfo> ipInfo = k8sMetadata.GetInfoByIpFromCache(remoteIp);
std::shared_ptr<k8sContainerInfo> ipInfo = k8sMetadata->GetInfoByIpFromCache(remoteIp);
if (ipInfo == nullptr) {
remoteIpVec.push_back(remoteIp);
res = false;
Expand Down
6 changes: 3 additions & 3 deletions core/unittest/metadata/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
cmake_minimum_required(VERSION 3.22)
project(metadata_unittest)

# add_executable(metadata_unittest K8sMetadataUnittest.cpp)
# target_link_libraries(metadata_unittest ${UT_BASE_TARGET})
add_executable(metadata_unittest K8sMetadataUnittest.cpp)
target_link_libraries(metadata_unittest ${UT_BASE_TARGET})

include(GoogleTest)
# gtest_discover_tests(metadata_unittest)
gtest_discover_tests(metadata_unittest)

0 comments on commit f68b24a

Please sign in to comment.