diff --git a/docs/configuration/overview/runtime.rst b/docs/configuration/overview/runtime.rst index 47c00367b34a3..8b623ee2ec5ec 100644 --- a/docs/configuration/overview/runtime.rst +++ b/docs/configuration/overview/runtime.rst @@ -70,6 +70,14 @@ that :option:`--service-cluster` has been set to ``my-cluster``. Envoy will firs If found, the value will override any value found in the primary lookup path. This allows the user to customize the runtime values for individual clusters on top of global defaults. +Comments +-------- + +Lines starting with ``#`` as the first character are treated as comments. + +Comments can be used to provide context on an existing value. Comments are also useful in an +otherwise empty file to keep a placeholder for deployment in a time of need. + Updating runtime values via symbolic link swap ---------------------------------------------- diff --git a/source/common/runtime/runtime_impl.cc b/source/common/runtime/runtime_impl.cc index bb633674bd57d..278aa155a4095 100644 --- a/source/common/runtime/runtime_impl.cc +++ b/source/common/runtime/runtime_impl.cc @@ -224,7 +224,17 @@ void SnapshotImpl::walkDirectory(const std::string& path, const std::string& pre // theoretically lead to issues. ENVOY_LOG(debug, "reading file: {}", full_path); Entry entry; - entry.string_value_ = Filesystem::fileReadToEnd(full_path); + + // Read the file and remove any comments. A comment is a line starting with a '#' character. + // Comments are useful for placeholder files with no value. + const std::vector lines = + StringUtil::split(Filesystem::fileReadToEnd(full_path), "\n"); + for (const std::string& line : lines) { + if (!line.empty() && line.at(0) == '#') { + continue; + } + entry.string_value_ += line + "\n"; + } StringUtil::rtrim(entry.string_value_); // As a perf optimization, attempt to convert the string into an integer. If we don't diff --git a/test/common/runtime/runtime_impl_test.cc b/test/common/runtime/runtime_impl_test.cc index abcf97750eecb..7f839f40cb837 100644 --- a/test/common/runtime/runtime_impl_test.cc +++ b/test/common/runtime/runtime_impl_test.cc @@ -110,6 +110,11 @@ TEST_F(RuntimeImplTest, All) { EXPECT_EQ(2UL, loader->snapshot().getInteger("file3", 1)); EXPECT_EQ(123UL, loader->snapshot().getInteger("file4", 1)); + // Files with comments. + EXPECT_EQ(123UL, loader->snapshot().getInteger("file5", 1)); + EXPECT_EQ("/home#about-us", loader->snapshot().get("file6")); + EXPECT_EQ("", loader->snapshot().get("file7")); + // Feature enablement. EXPECT_CALL(generator, random()).WillOnce(Return(1)); EXPECT_TRUE(loader->snapshot().featureEnabled("file3", 1)); diff --git a/test/common/runtime/test_data/root/envoy/file5 b/test/common/runtime/test_data/root/envoy/file5 new file mode 100644 index 0000000000000..5426e4c9e4116 --- /dev/null +++ b/test/common/runtime/test_data/root/envoy/file5 @@ -0,0 +1,2 @@ +# This is a comment in a file with an integer. +123 diff --git a/test/common/runtime/test_data/root/envoy/file6 b/test/common/runtime/test_data/root/envoy/file6 new file mode 100644 index 0000000000000..030ad397da4ac --- /dev/null +++ b/test/common/runtime/test_data/root/envoy/file6 @@ -0,0 +1,2 @@ +# This is a comment in a file with a string. +/home#about-us diff --git a/test/common/runtime/test_data/root/envoy/file7 b/test/common/runtime/test_data/root/envoy/file7 new file mode 100644 index 0000000000000..4235d74a7f1f2 --- /dev/null +++ b/test/common/runtime/test_data/root/envoy/file7 @@ -0,0 +1,2 @@ +# This is a comment in an empty file. +# This file was intentionally left blank.