-
Notifications
You must be signed in to change notification settings - Fork 170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Policy chain - first version #450
Merged
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
e658d90
prototype basic policy chain
mikz b1905a4
prototype of executor and context for sharing data
mikz 9d8c59d
[policy] don't return metatable and let policy to figure it out
mikz db804e2
first version of truly local policy chain for every request
mikz 80f5582
extract finding a service to a policy
mikz 4cb932f
policies are loaded and initialized with service config
mikz c21b163
move phases definition to policy
mikz 97b2056
spec: add tests for Policy
davidor 7a40112
spec: add tests for PolicyChain
davidor d9c3bc6
spec: add tests for Executor
davidor d7db0df
spec: add tests for find_service policy
davidor c100bb4
spec: add tests for the load_configuration policy
davidor d5f1e6a
Add phase logger policy
davidor 5f89a97
t: add basic integration test for policy chains
davidor 83ed56d
spec: add tests for linked_list
davidor 9dbb2a8
policy/local_chain: make explicit that the default chain contains onl…
davidor 8075f69
linked_list: raise error when trying to modify read-only list
davidor 34d5a94
policy: set version to 0.0 when not specified
davidor e1a63cb
policy: avoid exposing PHASES
davidor a6b9c94
executor: extract global_chain() and DEFAULT_POLICIES
davidor 58b686f
spec/proxy: delete test that belongs to configuration_store
davidor 5dc8c54
Document policy-related modules
davidor 69ce941
policy_chain: delete unused add()
davidor a6b047a
policy_chain: simplify freeze of the chain
davidor 95af30e
local_chain: keep compatibility with current module system
davidor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
--- Executor module | ||
-- The executor has a policy chain and will simply forward the calls it | ||
-- receives to that policy chain. It also manages the 'context' that is passed | ||
-- when calling the policy chain methods. This 'context' contains information | ||
-- shared among policies. | ||
|
||
local policy_chain = require('policy_chain') | ||
local policy = require('policy') | ||
local linked_list = require('linked_list') | ||
|
||
local setmetatable = setmetatable | ||
|
||
local _M = { } | ||
|
||
local DEFAULT_POLICIES = { | ||
'policy.load_configuration', | ||
'policy.find_service', | ||
'policy.local_chain' | ||
} | ||
|
||
local mt = { __index = _M } | ||
|
||
-- forward all policy methods to the policy chain | ||
for _,phase in policy.phases() do | ||
_M[phase] = function(self, ...) | ||
return self.policy_chain[phase](self.policy_chain, self:context(phase), ...) | ||
end | ||
end | ||
|
||
local function global_chain() | ||
return policy_chain.build(DEFAULT_POLICIES) | ||
end | ||
|
||
function _M.new() | ||
return setmetatable({ policy_chain = global_chain() }, mt) | ||
end | ||
|
||
local function build_context(executor) | ||
local config = executor.policy_chain:export() | ||
|
||
return linked_list.readwrite({}, config) | ||
end | ||
|
||
local function shared_build_context(executor) | ||
local ctx = ngx.ctx or {} | ||
local context = ctx.context | ||
|
||
if not context then | ||
context = build_context(executor) | ||
|
||
ctx.context = context | ||
end | ||
|
||
return context | ||
end | ||
|
||
--- Shared context among policies | ||
-- @tparam string phase Nginx phase | ||
-- @treturn linked_list The context. Note: The list returned is 'read-write'. | ||
function _M:context(phase) | ||
if phase == 'init' then | ||
return build_context(self) | ||
end | ||
|
||
return shared_build_context(self) | ||
end | ||
|
||
return _M.new() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
local setmetatable = setmetatable | ||
local error = error | ||
|
||
local _M = { | ||
|
||
} | ||
|
||
local noop = function() end | ||
|
||
|
||
local empty_t = setmetatable({}, { __newindex = noop }) | ||
local __index = function(t,k) | ||
return t.current[k] or t.next[k] | ||
end | ||
|
||
local ro_mt = { | ||
__index = __index, | ||
__newindex = function() | ||
error("readonly list") | ||
end, | ||
} | ||
|
||
local rw_mt = { | ||
__index = __index, | ||
__newindex = function(t, k, v) | ||
t.current[k] = v | ||
end | ||
} | ||
|
||
local function linked_list(item, next, mt) | ||
return setmetatable({ | ||
current = item or empty_t, | ||
next = next or empty_t | ||
}, mt) | ||
end | ||
|
||
local function readonly_linked_list(item, next) | ||
return linked_list(item, next, ro_mt) | ||
end | ||
|
||
local function readwrite_linked_list(item, next) | ||
return linked_list(item, next, rw_mt) | ||
end | ||
|
||
_M.readonly = readonly_linked_list | ||
_M.readwrite = readwrite_linked_list | ||
|
||
return _M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
local _M = {} | ||
|
||
local cjson = require('cjson') | ||
local module = require('module') | ||
local context = require('executor'):context() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This won't work when |
||
local router = require('router') | ||
local configuration_parser = require('configuration_parser') | ||
local configuration_loader = require('configuration_loader') | ||
|
@@ -28,7 +28,7 @@ function _M.live() | |
end | ||
|
||
function _M.status(config) | ||
local configuration = config or module.configuration | ||
local configuration = config or context.configuration | ||
-- TODO: this should be fixed for multi-tenant deployment | ||
local has_configuration = configuration.configured | ||
local has_services = #(configuration:all()) > 0 | ||
|
@@ -43,7 +43,7 @@ function _M.status(config) | |
end | ||
|
||
function _M.config() | ||
local config = module.configuration | ||
local config = context.configuration | ||
local contents = cjson.encode(config.configured and { services = config:all() } or nil) | ||
|
||
ngx.header.content_type = 'application/json; charset=utf-8' | ||
|
@@ -65,7 +65,7 @@ function _M.update_config() | |
local config, err = configuration_parser.decode(data) | ||
|
||
if config then | ||
local configured, error = configuration_loader.configure(module.configuration, config) | ||
local configured, error = configuration_loader.configure(context.configuration, config) | ||
-- TODO: respond with proper 304 Not Modified when config is the same | ||
if configured and #(configured.services) > 0 then | ||
json_response({ status = 'ok', config = config, services = #(configured.services)}) | ||
|
@@ -80,7 +80,7 @@ end | |
function _M.delete_config() | ||
ngx.log(ngx.DEBUG, 'management config delete') | ||
|
||
module.configuration:reset() | ||
context.configuration:reset() | ||
-- TODO: respond with proper 304 Not Modified when config is the same | ||
local response = cjson.encode({ status = 'ok', config = cjson.null }) | ||
ngx.header.content_type = 'application/json; charset=utf-8' | ||
|
@@ -96,7 +96,7 @@ function _M.boot() | |
|
||
ngx.log(ngx.DEBUG, 'management boot config:' .. inspect(data)) | ||
|
||
configuration_loader.configure(module.configuration, config) | ||
configuration_loader.configure(context.configuration, config) | ||
|
||
ngx.say(response) | ||
end | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will not work when
APICAST_MODULE
is set.