Skip to content

Commit 771ffd3

Browse files
authored
Merge pull request #213 from 3scale/warn-host-clashes
[config] print warning when multiple services share host
2 parents cee5f97 + 958972a commit 771ffd3

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

apicast/src/configuration_store.lua

+11-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@ local ipairs = ipairs
33
local pairs = pairs
44
local tostring = tostring
55
local insert = table.insert
6+
local next = next
7+
8+
local util = require 'util'
69

710
local _M = {
8-
_VERSION = '0.1'
11+
_VERSION = '0.1',
12+
path_routing = util.env_enabled('APICAST_PATH_ROUTING_ENABLED')
913
}
1014

1115
local mt = { __index = _M }
@@ -81,9 +85,15 @@ function _M.add(self, service)
8185
local index = hosts[host] or {}
8286
local id = tostring(service.id)
8387

88+
local exists = not _M.path_routing and next(index)
89+
8490
index[id] = service
8591
all[id] = service
8692
hosts[host] = index
93+
94+
if exists then
95+
ngx.log(ngx.WARN, 'host ', host, ' for service ', id, ' already defined by service ', exists)
96+
end
8797
end
8898
end
8999

apicast/src/provider.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ local function find_service_cascade(host)
151151
return find_service_strict(host)
152152
end
153153

154-
if util.env_enabled('APICAST_PATH_ROUTING_ENABLED') then
154+
if configuration_store.path_routing then
155155
ngx.log(ngx.WARN, 'apicast experimental path routing enabled')
156156
_M.find_service = find_service_cascade
157157
else

t/003-apicast.t

+25
Original file line numberDiff line numberDiff line change
@@ -491,3 +491,28 @@ GET /test?user_key=foo
491491
--- response_body
492492
api response
493493
--- error_code: 200
494+
495+
=== TEST 11: print warning on duplicate service hosts
496+
So when booting it can be immediately known that some of them won't work.
497+
--- http_config
498+
lua_package_path "$TEST_NGINX_LUA_PATH";
499+
--- config
500+
location /t {
501+
content_by_lua_block {
502+
require('provider').configure({
503+
services = {
504+
{ id = 1, proxy = { hosts = { 'foo', 'bar' } } },
505+
{ id = 2, proxy = { hosts = { 'foo', 'daz' } } },
506+
}
507+
})
508+
ngx.say('all ok')
509+
}
510+
}
511+
--- request
512+
GET /t
513+
--- response_body
514+
all ok
515+
--- log_level: warn
516+
--- error_code: 200
517+
--- error_log
518+
host foo for service 2 already defined by service 1

0 commit comments

Comments
 (0)