Skip to content

Commit 9add291

Browse files
committed
fixed service version configuration via environment variable
1 parent bf8b6e8 commit 9add291

File tree

3 files changed

+83
-13
lines changed

3 files changed

+83
-13
lines changed

gateway/src/apicast/configuration_loader/remote_v2.lua

+27-13
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,19 @@ local function parse_resp_body(self, resp_body)
137137
return cjson.encode(config)
138138
end
139139

140+
local function use_service_version(portal_endpoint_has_path)
141+
local vars = resty_env.list()
142+
for n, v in pairs(vars) do
143+
if match(n, "APICAST_SERVICE_\\d+_CONFIGURATION_VERSION") and v and v ~= ''
144+
and portal_endpoint_has_path then
145+
ngx.log(ngx.INFO, "APICAST_SERVICE_${ID}_CONFIGURATION_VERSION is configured: ",
146+
"all services will be loaded")
147+
return true
148+
end
149+
end
150+
return false
151+
end
152+
140153
-- When the APICAST_LOAD_SERVICES_WHEN_NEEDED is enabled, but the config loader
141154
-- is boot, APICAST_LOAD_SERVICES_WHEN_NEEDED is going to be ignored. But in
142155
-- that case, env refers to a host and we need to reset it to pick the env
@@ -150,12 +163,8 @@ end
150163
-- http://${THREESCALE_PORTAL_ENDPOINT}/<env>.json?host=host
151164
-- http://${THREESCALE_PORTAL_ENDPOINT}/admin/api/account/proxy_configs/<env>.json?host=host&version=version
152165
local function configuration_endpoint_params(env, host, portal_endpoint_path)
153-
local version = resty_env.get(
154-
format('APICAST_SERVICE_%s_CONFIGURATION_VERSION', service_id)
155-
) or "latest"
156-
157166
return portal_endpoint_path and {path = env, args = {host = host}}
158-
or {path = '/admin/api/account/proxy_configs/' .. env, args = {host = host, version = version} }
167+
or {path = '/admin/api/account/proxy_configs/' .. env, args = {host = host, version = "latest"} }
159168
end
160169

161170
function _M:index(host)
@@ -172,6 +181,13 @@ function _M:index(host)
172181
return nil, 'missing environment'
173182
end
174183

184+
-- Exit from index if a service version is configured because
185+
-- in that case we have to retrieve the services individually
186+
-- through the "old" services endpoint.
187+
if use_service_version(not self.path and true) then
188+
return nil, 'wrong endpoint url'
189+
end
190+
175191
local endpoint_params = configuration_endpoint_params(env, host, self.path)
176192
local base_url = resty_url.join(self.endpoint, endpoint_params.path .. '.json')
177193
local query_args = encode_args(endpoint_params.args) ~= '' and '?'..encode_args(endpoint_params.args)
@@ -211,16 +227,14 @@ function _M:call(environment)
211227
return ret, err
212228
end
213229

214-
if load_just_for_host then
215-
return m:call(host)
216-
else
217-
return m:call()
218-
end
230+
return m:call()
231+
219232
end
220233

221-
-- Everything past this point in `call()` is deprecated because
222-
-- now the configuration load is handled completely by `index()`.
223-
-- Service filtering is done in configuration.lua
234+
-- Unless APICAST_SERVICE_${ID}_CONFIGURATION_VERSION is set,
235+
-- everything past this point in `call()` is skipped because
236+
-- otherwise the configuration load is handled completely by
237+
-- `index()`. Service filtering is done in configuration.lua
224238
local http_client = self.http_client
225239

226240
if not http_client then

spec/configuration_loader/remote_v2_spec.lua

+8
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,14 @@ UwIDAQAB
454454
end)
455455

456456
describe(':index', function()
457+
458+
it('returns "wrong endpoint url" when service version is configured', function()
459+
loader = _M.new('http://example.com/', { client = test_backend })
460+
env.set('THREESCALE_DEPLOYMENT_ENV', 'production')
461+
env.set('APICAST_SERVICE_2_CONFIGURATION_VERSION', 42)
462+
env.set('APICAST_CONFIGURATION_LOADER', 'lazy')
463+
assert.same({ nil, 'wrong endpoint url' }, { loader:index() })
464+
end)
457465

458466
it('returns configuration for all services (no path on endpoint)', function()
459467
loader = _M.new('http://example.com/', { client = test_backend })

t/configuration-loading-lazy.t

+48
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,51 @@ using lazy configuration loader
155155
OIDC url is not valid, uri:
156156
--- no_error_log
157157
[error]
158+
159+
=== TEST 6: load configuration with APICAST_SERVICE_${ID}_CONFIGURATION_VERSION
160+
--- env eval
161+
(
162+
'APICAST_CONFIGURATION_LOADER' => 'lazy',
163+
'THREESCALE_PORTAL_ENDPOINT' => "http://test:$ENV{TEST_NGINX_SERVER_PORT}",
164+
'APICAST_SERVICE_2_CONFIGURATION_VERSION' => 42
165+
)
166+
--- upstream env
167+
location = /admin/api/services.json {
168+
echo '{ "services": [ { "service": { "id": 2 } } ] }';
169+
}
170+
location = /admin/api/services/2/proxy/configs/production/42.json {
171+
echo '
172+
{
173+
"proxy_config": {
174+
"content": {
175+
"id": 2,
176+
"backend_version": 1,
177+
"proxy": {
178+
"hosts": [ "localhost" ],
179+
"api_backend": "http://test:$TEST_NGINX_SERVER_PORT/",
180+
"proxy_rules": [
181+
{ "pattern": "/t", "http_method": "GET", "metric_system_name": "test","delta": 1 }
182+
]
183+
}
184+
}
185+
}
186+
}';
187+
}
188+
189+
location /t {
190+
echo "all ok";
191+
}
192+
193+
--- backend
194+
location /transactions/authrep.xml {
195+
content_by_lua_block {
196+
ngx.exit(200)
197+
}
198+
}
199+
--- request
200+
GET /t?user_key=fake
201+
--- error_code: 200
202+
--- error_log
203+
using lazy configuration loader
204+
--- no_error_log
205+
[error]

0 commit comments

Comments
 (0)