Skip to content

Commit

Permalink
ConfigLoader should not need code changes if the protobuf definition …
Browse files Browse the repository at this point in the history
…changes
  • Loading branch information
JoukoVirtanen committed Dec 13, 2024
1 parent 6a62b33 commit 7d13d7c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 60 deletions.
69 changes: 41 additions & 28 deletions collector/lib/ConfigLoader.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#include "ConfigLoader.h"

#include <google/protobuf/util/json_util.h>

#include <internalapi/sensor/collector.pb.h>

#include "EnvVar.h"
#include "Logging.h"

Expand Down Expand Up @@ -52,41 +56,50 @@ bool ConfigLoader::LoadConfiguration(CollectorConfig& config) {
return LoadConfiguration(config, node);
}

bool ConfigLoader::LoadConfiguration(CollectorConfig& config, const YAML::Node& node) {
const auto& config_file = CONFIG_FILE.value();
Json::Value yamlNodeToJson(const YAML::Node& yamlNode) {
Json::Value jsonValue;

if (node.IsNull() || !node.IsDefined() || !node.IsMap()) {
CLOG(ERROR) << "Unable to read config from " << config_file;
return false;
switch (yamlNode.Type()) {
case YAML::NodeType::Null:
jsonValue = Json::Value::null;
break;
case YAML::NodeType::Scalar:
jsonValue = yamlNode.as<std::string>();
break;
case YAML::NodeType::Sequence:
for (std::size_t i = 0; i < yamlNode.size(); ++i) {
jsonValue.append(yamlNodeToJson(yamlNode[i]));
}
break;
case YAML::NodeType::Map:
for (const auto& pair : yamlNode) {
jsonValue[pair.first.as<std::string>()] = yamlNodeToJson(pair.second);
}
break;
case YAML::NodeType::Undefined:
default:
break;
}

YAML::Node networking_node = node["networking"];
if (!networking_node || networking_node.IsNull()) {
CLOG(DEBUG) << "No networking in " << config_file;
return true;
}
return jsonValue;
}

YAML::Node external_ips_node = networking_node["externalIps"];
if (!external_ips_node) {
CLOG(DEBUG) << "No external IPs in " << config_file;
return true;
bool ConfigLoader::LoadConfiguration(CollectorConfig& config, const YAML::Node& node) {
const auto jsonConfig = yamlNodeToJson(node);
Json::StreamWriterBuilder writer;
std::string jsonStr = Json::writeString(writer, jsonConfig);

sensor::CollectorConfig runtimeConfig;
google::protobuf::util::JsonParseOptions parseOptions;
parseOptions.ignore_unknown_fields = true;
auto status = google::protobuf::util::JsonStringToMessage(jsonStr, &runtimeConfig, parseOptions);
if (!status.ok()) {
CLOG(ERROR) << "Failed to parse config: " << status.ToString();
return false;
}

bool enable_external_ips = external_ips_node["enable"].as<bool>(false);
int64_t per_container_rate_limit = networking_node["perContainerRateLimit"].as<int64_t>(1024);

sensor::CollectorConfig runtime_config;
auto* networking = runtime_config.mutable_networking();
networking
->mutable_external_ips()
->set_enable(enable_external_ips);
networking
->set_per_container_rate_limit(per_container_rate_limit);

config.SetRuntimeConfig(std::move(runtime_config));
config.SetRuntimeConfig(runtimeConfig);

CLOG(DEBUG) << "Runtime configuration:\n"
<< config.GetRuntimeConfigStr();
return true;
}

Expand Down
39 changes: 7 additions & 32 deletions collector/test/ConfigLoaderTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,34 +42,16 @@ TEST(CollectorConfigTest, TestYamlConfigToConfigMultiple) {
}
}

TEST(CollectorConfigTest, TestYamlConfigToConfigInvalid) {
TEST(CollectorConfigTest, TestYamlConfigToConfigEmptyOrMalformed) {
std::vector<std::string> tests = {
R"(
networking:
asdf
)",
R"(
networking:
unknownFiled: asdf
)",
R"(
unknownField: asdf
)"};

for (const auto& yamlStr : tests) {
YAML::Node yamlNode = YAML::Load(yamlStr);
CollectorConfig config;
ASSERT_TRUE(ConfigLoader::LoadConfiguration(config, yamlNode));

auto runtime_config = config.GetRuntimeConfig();

EXPECT_FALSE(runtime_config.has_value());
}
}

TEST(CollectorConfigTest, TestYamlConfigToConfigEmptyOrMalformed) {
std::vector<std::string> tests = {
R"(
asdf
externalIps:
enable: false
perContainerRateLimit: invalid
)",
R"()"};

Expand All @@ -89,24 +71,17 @@ TEST(CollectorConfigTest, TestPerContainerRateLimit) {
{R"(
networking:
externalIps:
enabled: false
enable: false
perContainerRateLimit: 1234
)",
1234},
{R"(
networking:
externalIps:
enabled: false
enable: false
perContainerRateLimit: 1337
)",
1337},
{R"(
networking:
externalIps:
enabled: false
perContainerRateLimit: invalid
)",
1024},
};

for (const auto& [yamlStr, expected] : tests) {
Expand Down

0 comments on commit 7d13d7c

Please sign in to comment.