From bfc12c48b4114815de19cd115d9f463ae16ffd43 Mon Sep 17 00:00:00 2001 From: Anirudha Singh Date: Tue, 1 Dec 2020 19:41:38 +0530 Subject: [PATCH 1/2] Added watcher code in LUA filter config to setup inotify watchers for LUA path --- source/extensions/filters/http/lua/config.cc | 4 ++++ source/extensions/filters/http/lua/lua_filter.cc | 13 +++++++++++++ source/extensions/filters/http/lua/lua_filter.h | 4 ++++ 3 files changed, 21 insertions(+) diff --git a/source/extensions/filters/http/lua/config.cc b/source/extensions/filters/http/lua/config.cc index dbe71ad944b4e..33f58bd13aef9 100644 --- a/source/extensions/filters/http/lua/config.cc +++ b/source/extensions/filters/http/lua/config.cc @@ -16,6 +16,10 @@ Http::FilterFactoryCb LuaFilterConfig::createFilterFactoryFromProtoTyped( Server::Configuration::FactoryContext& context) { FilterConfigConstSharedPtr filter_config(new FilterConfig{ proto_config, context.threadLocal(), context.clusterManager(), context.api()}); + + // Setting up a watcher for LUA_PATHs + filter_config->addLuaPathWatchers(context); + return [filter_config](Http::FilterChainFactoryCallbacks& callbacks) -> void { callbacks.addStreamFilter(std::make_shared(filter_config)); }; diff --git a/source/extensions/filters/http/lua/lua_filter.cc b/source/extensions/filters/http/lua/lua_filter.cc index 99d9618b13a0c..5814f2822f06c 100644 --- a/source/extensions/filters/http/lua/lua_filter.cc +++ b/source/extensions/filters/http/lua/lua_filter.cc @@ -626,6 +626,19 @@ int StreamHandleWrapper::luaBase64Escape(lua_State* state) { return 1; } +void FilterConfig::addLuaPathWatchers(Server::Configuration::FactoryContext& context) { + // hardcoding path for the moment + std::string path = "/usr/local/share/lua/5.1/"; + watcher_ = context.dispatcher().createFilesystemWatcher(); + watcher_->addWatch(path, + Filesystem::Watcher::Events::MovedTo | + Filesystem::Watcher::Events::Modified, + [path](uint32_t) { + ENVOY_LOG(debug, "LUA path: {} is being watched", path); + //TODO: Update this callback to perform PerLuaCodeSetup update + }); +} + FilterConfig::FilterConfig(const envoy::extensions::filters::http::lua::v3::Lua& proto_config, ThreadLocal::SlotAllocator& tls, Upstream::ClusterManager& cluster_manager, Api::Api& api) diff --git a/source/extensions/filters/http/lua/lua_filter.h b/source/extensions/filters/http/lua/lua_filter.h index 95c42c066be6b..a4a9fb86ba99c 100644 --- a/source/extensions/filters/http/lua/lua_filter.h +++ b/source/extensions/filters/http/lua/lua_filter.h @@ -352,8 +352,12 @@ class FilterConfig : Logger::Loggable { Upstream::ClusterManager& cluster_manager_; + // Adds filesystem watchers for LUA paths such as /usr/local/share/lua/5.1/ + void addLuaPathWatchers(Server::Configuration::FactoryContext& context); + private: absl::flat_hash_map per_lua_code_setups_map_; + std::unique_ptr watcher_; }; using FilterConfigConstSharedPtr = std::shared_ptr; From 0eb2a25bdb960dca7b2a57331060c35965e57188 Mon Sep 17 00:00:00 2001 From: Anirudha Singh Date: Wed, 2 Dec 2020 00:48:28 +0530 Subject: [PATCH 2/2] Added watcher callback. This callback will update PerLuaCodeSetup whenever events related to LUA path occur --- .../extensions/filters/http/lua/lua_filter.cc | 17 ++++++++++++----- source/extensions/filters/http/lua/lua_filter.h | 1 + 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/source/extensions/filters/http/lua/lua_filter.cc b/source/extensions/filters/http/lua/lua_filter.cc index 5814f2822f06c..fce9b6fd1bc91 100644 --- a/source/extensions/filters/http/lua/lua_filter.cc +++ b/source/extensions/filters/http/lua/lua_filter.cc @@ -628,21 +628,28 @@ int StreamHandleWrapper::luaBase64Escape(lua_State* state) { void FilterConfig::addLuaPathWatchers(Server::Configuration::FactoryContext& context) { // hardcoding path for the moment - std::string path = "/usr/local/share/lua/5.1/"; + std::string path = "/usr/local/share/lua/5.1/lib/"; watcher_ = context.dispatcher().createFilesystemWatcher(); watcher_->addWatch(path, Filesystem::Watcher::Events::MovedTo | Filesystem::Watcher::Events::Modified, - [path](uint32_t) { - ENVOY_LOG(debug, "LUA path: {} is being watched", path); - //TODO: Update this callback to perform PerLuaCodeSetup update + [path, &context, this](uint32_t) { + ENVOY_LOG(debug, "LUA path: {} is updated. Updating PerLuaCodeSetup", path); + if (per_lua_code_setups_map_[GLOBAL_SCRIPT_NAME]) { + auto global_setup_ptr = std::make_unique( + proto_config_.inline_code(), context.threadLocal()); + if (global_setup_ptr) { + per_lua_code_setups_map_[GLOBAL_SCRIPT_NAME].reset(); + per_lua_code_setups_map_[GLOBAL_SCRIPT_NAME] = std::move(global_setup_ptr); + } + } }); } FilterConfig::FilterConfig(const envoy::extensions::filters::http::lua::v3::Lua& proto_config, ThreadLocal::SlotAllocator& tls, Upstream::ClusterManager& cluster_manager, Api::Api& api) - : cluster_manager_(cluster_manager) { + : cluster_manager_(cluster_manager), proto_config_(proto_config) { auto global_setup_ptr = std::make_unique(proto_config.inline_code(), tls); if (global_setup_ptr) { per_lua_code_setups_map_[GLOBAL_SCRIPT_NAME] = std::move(global_setup_ptr); diff --git a/source/extensions/filters/http/lua/lua_filter.h b/source/extensions/filters/http/lua/lua_filter.h index a4a9fb86ba99c..2637a8aa0e27b 100644 --- a/source/extensions/filters/http/lua/lua_filter.h +++ b/source/extensions/filters/http/lua/lua_filter.h @@ -358,6 +358,7 @@ class FilterConfig : Logger::Loggable { private: absl::flat_hash_map per_lua_code_setups_map_; std::unique_ptr watcher_; + const envoy::extensions::filters::http::lua::v3::Lua proto_config_; }; using FilterConfigConstSharedPtr = std::shared_ptr;