Skip to content
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

Standalone configuration #926

Merged
merged 8 commits into from
Feb 14, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions gateway/src/apicast/management.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
local _M = {}

local cjson = require('cjson')
local context = require('apicast.executor'):context()
local executor = require('apicast.executor')
local router = require('router')
local configuration_parser = require('apicast.configuration_parser')
local configuration_loader = require('apicast.configuration_loader')
local load_configuration = require('apicast.policy.load_configuration')
local inspect = require('inspect')
local resolver_cache = require('resty.resolver.cache')
local env = require('resty.env')
@@ -28,8 +29,12 @@ function _M.live()
json_response(live, ngx.HTTP_OK)
end

local function context_configuration()
return executor:context().configuration or load_configuration.configuration
end

function _M.status(config)
local configuration = config or context.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
@@ -44,7 +49,7 @@ function _M.status(config)
end

function _M.config()
local config = context.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'
@@ -66,7 +71,7 @@ function _M.update_config()
local config, err = configuration_parser.decode(data)

if config then
local configured, error = configuration_loader.configure(context.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)})
@@ -81,7 +86,7 @@ end
function _M.delete_config()
ngx.log(ngx.DEBUG, 'management config delete')

context.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'
@@ -97,7 +102,7 @@ function _M.boot()

ngx.log(ngx.DEBUG, 'management boot config:' .. inspect(data))

configuration_loader.configure(context.configuration, config)
configuration_loader.configure(context_configuration(), config)

ngx.say(response)
end
@@ -163,18 +168,18 @@ function routes.policies(r)
r:get('/policies/', _M.get_all_policy_manifests)
end

function _M.router()
function _M.router(name)
local r = router.new()

local name = env.value('APICAST_MANAGEMENT_API') or 'status'
local api = routes[name]
local mode = name or env.value('APICAST_MANAGEMENT_API') or 'status'
local api = routes[mode]

ngx.log(ngx.DEBUG, 'management api mode: ', name)
ngx.log(ngx.DEBUG, 'management api mode: ', mode)

if api then
api(r)
else
ngx.log(ngx.ERR, 'invalid management api setting: ', name)
ngx.log(ngx.ERR, 'invalid management api setting: ', mode)
end

return r
1 change: 1 addition & 0 deletions gateway/src/apicast/policy/management/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
return require('management')
40 changes: 40 additions & 0 deletions gateway/src/apicast/policy/management/management.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
-- This is a management description.

local policy = require('apicast.policy')
local _M = policy.new('management')

local management = require('apicast.management')

local new = _M.new
--- Initialize a management
-- @tparam[opt] table config Policy configuration.
function _M.new(config)
local self = new(config)

local router = management.router(config and config.mode)

if not router then
return nil, 'invalid management api'
end

self.router = router

return self
end

function _M:content()
local method = ngx.req.get_method()
local uri = ngx.var.uri

local ok, err = self.router:execute(method, uri)

if not ok then
ngx.status = 404
end

if err then
ngx.say(err)
end
end

return _M
13 changes: 13 additions & 0 deletions spec/policy/management/management_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
local _M = require('apicast.policy.management')

describe('management policy', function()
describe('.new', function()
it('works without configuration', function()
assert(_M.new())
end)

it('accepts configuration', function()
assert(_M.new({ }))
end)
end)
end)
26 changes: 26 additions & 0 deletions t/apicast-policy-management.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use lib 't';
use Test::APIcast::Blackbox 'no_plan';

run_tests();

__DATA__

=== TEST 1: management accepts configuration
--- configuration
{
"services": [
{
"proxy": {
"policy_chain": [
{ "name": "apicast.policy.management",
"configuration": { } }
]
}
}
]
}
--- request
GET /status/info
--- error_code: 200
--- no_error_log
[error]