From 654cd67a95b64ac89b25e78267eb43f1298c4b35 Mon Sep 17 00:00:00 2001 From: Daniel Hochman Date: Tue, 7 Nov 2017 17:38:59 -0800 Subject: [PATCH 1/4] add comment capability to runtime Signed-off-by: Daniel Hochman --- docs/configuration/overview/runtime.rst | 10 ++++++++++ source/common/runtime/runtime_impl.cc | 17 +++++++++++++++++ test/common/runtime/runtime_impl_test.cc | 5 +++++ test/common/runtime/test_data/root/envoy/file5 | 2 ++ test/common/runtime/test_data/root/envoy/file6 | 2 ++ test/common/runtime/test_data/root/envoy/file7 | 2 ++ 6 files changed, 38 insertions(+) create mode 100644 test/common/runtime/test_data/root/envoy/file5 create mode 100644 test/common/runtime/test_data/root/envoy/file6 create mode 100644 test/common/runtime/test_data/root/envoy/file7 diff --git a/docs/configuration/overview/runtime.rst b/docs/configuration/overview/runtime.rst index 47c00367b34a3..b4fd709e011b9 100644 --- a/docs/configuration/overview/runtime.rst +++ b/docs/configuration/overview/runtime.rst @@ -70,6 +70,16 @@ 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 the ``#`` character at the beginning of a file are treated as comments. +Multiple lines are allowed as long as they are all at the beginning of the file. + +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..5465f83a03284 100644 --- a/source/common/runtime/runtime_impl.cc +++ b/source/common/runtime/runtime_impl.cc @@ -225,8 +225,25 @@ void SnapshotImpl::walkDirectory(const std::string& path, const std::string& pre ENVOY_LOG(debug, "reading file: {}", full_path); Entry entry; entry.string_value_ = Filesystem::fileReadToEnd(full_path); + + // Trim whitespace. StringUtil::rtrim(entry.string_value_); + // Remove any comments. Comments are only allowed at the beginning of the file. A comment + // is a line starting with a '#' character. Comments can span multiple lines if each line + // starts with a '#' character. Comments are useful for placeholder files with no value. + while (!entry.string_value_.empty()) { + if (entry.string_value_.at(0) == '#') { + size_t index_end = entry.string_value_.find('\n'); + if (index_end == std::string::npos) { + index_end = entry.string_value_.size() - 1; + } + entry.string_value_.erase(0, index_end + 1); + } else { + break; + } + } + // As a perf optimization, attempt to convert the string into an integer. If we don't // succeed that's fine. uint64_t converted; 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. From b7cb0b5580e49a68363ff092ed76a102f3a42b16 Mon Sep 17 00:00:00 2001 From: Daniel Hochman Date: Thu, 9 Nov 2017 11:54:37 -0800 Subject: [PATCH 2/4] use split Signed-off-by: Daniel Hochman --- source/common/runtime/runtime_impl.cc | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/source/common/runtime/runtime_impl.cc b/source/common/runtime/runtime_impl.cc index 5465f83a03284..278aa155a4095 100644 --- a/source/common/runtime/runtime_impl.cc +++ b/source/common/runtime/runtime_impl.cc @@ -224,25 +224,18 @@ 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); - // Trim whitespace. - StringUtil::rtrim(entry.string_value_); - - // Remove any comments. Comments are only allowed at the beginning of the file. A comment - // is a line starting with a '#' character. Comments can span multiple lines if each line - // starts with a '#' character. Comments are useful for placeholder files with no value. - while (!entry.string_value_.empty()) { - if (entry.string_value_.at(0) == '#') { - size_t index_end = entry.string_value_.find('\n'); - if (index_end == std::string::npos) { - index_end = entry.string_value_.size() - 1; - } - entry.string_value_.erase(0, index_end + 1); - } else { - break; + // 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 // succeed that's fine. From 6a8e6bafaf289ae1c6abb47528db9318be9c3d40 Mon Sep 17 00:00:00 2001 From: Daniel Hochman Date: Thu, 9 Nov 2017 12:02:08 -0800 Subject: [PATCH 3/4] update docs Signed-off-by: Daniel Hochman --- docs/configuration/overview/runtime.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/configuration/overview/runtime.rst b/docs/configuration/overview/runtime.rst index b4fd709e011b9..383b21ca1c46e 100644 --- a/docs/configuration/overview/runtime.rst +++ b/docs/configuration/overview/runtime.rst @@ -73,8 +73,7 @@ to customize the runtime values for individual clusters on top of global default Comments -------- -Lines starting with the ``#`` character at the beginning of a file are treated as comments. -Multiple lines are allowed as long as they are all at the beginning of the file. +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 From b34759bc03aa9beecfcf628da4c284c54970cbf0 Mon Sep 17 00:00:00 2001 From: Daniel Hochman Date: Thu, 9 Nov 2017 14:01:08 -0800 Subject: [PATCH 4/4] reflow docs Signed-off-by: Daniel Hochman --- docs/configuration/overview/runtime.rst | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/docs/configuration/overview/runtime.rst b/docs/configuration/overview/runtime.rst index 383b21ca1c46e..8b623ee2ec5ec 100644 --- a/docs/configuration/overview/runtime.rst +++ b/docs/configuration/overview/runtime.rst @@ -75,9 +75,8 @@ 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. +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 ----------------------------------------------