-
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
[balancer] Don't force 80 to be the default if port is not provided #662
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d1a0937
[balancer] resty.balancer doesn't fall back to port 80, apicast.balan…
mayorova 28d5721
[t] call actual apicast.balancer public method in the tests
mayorova 497acbe
[t] Integration test for incorrect URI scheme
mayorova 904a6ee
luacheck fixes
mayorova 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,52 @@ | ||
local round_robin = require 'resty.balancer.round_robin' | ||
local resty_url = require 'resty.url' | ||
local empty = {} | ||
|
||
local _M = { default_balancer = round_robin.new() } | ||
|
||
local function get_default_port(upstream_url) | ||
local url = resty_url.split(upstream_url) or empty | ||
local scheme = url[1] or 'http' | ||
return resty_url.default_port(scheme) | ||
end | ||
|
||
local function exit_service_unavailable() | ||
ngx.status = ngx.HTTP_SERVICE_UNAVAILABLE | ||
ngx.exit(ngx.status) | ||
end | ||
|
||
function _M.call(_, _, balancer) | ||
balancer = balancer or _M.default_balancer | ||
local host = ngx.var.proxy_host -- NYI: return to lower frame | ||
local peers = balancer:peers(ngx.ctx[host]) | ||
|
||
local peer, err = balancer:set_peer(peers) | ||
local peer, err = balancer:select_peer(peers) | ||
|
||
if not peer then | ||
ngx.status = ngx.HTTP_SERVICE_UNAVAILABLE | ||
ngx.log(ngx.ERR, "failed to set current backend peer: ", err) | ||
ngx.exit(ngx.status) | ||
ngx.log(ngx.ERR, 'could not select peer: ', err) | ||
return exit_service_unavailable() | ||
end | ||
|
||
local address, port = peer[1], peer[2] | ||
|
||
if not address then | ||
ngx.log(ngx.ERR, 'peer missing address') | ||
return exit_service_unavailable() | ||
end | ||
|
||
if not port then | ||
port = get_default_port(ngx.var.proxy_pass) | ||
end | ||
|
||
local ok | ||
ok, err = balancer.balancer.set_current_peer(address, port) | ||
|
||
if not ok then | ||
ngx.log(ngx.ERR, 'failed to set current backend peer: ', err) | ||
return exit_service_unavailable() | ||
end | ||
|
||
ngx.log(ngx.INFO, 'balancer set peer ', address, ':', port) | ||
end | ||
|
||
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
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,23 @@ | ||
local apicast_balancer = require 'apicast.balancer' | ||
local resty_balancer = require 'resty.balancer' | ||
|
||
describe('apicast.balancer', function() | ||
|
||
describe('.call', function() | ||
local b = resty_balancer.new(function(peers) return peers[1] end) | ||
b.balancer = { } | ||
|
||
ngx.var = { | ||
proxy_host = 'upstream' | ||
} | ||
|
||
it('sets default port from scheme if no port is specified for peers', function() | ||
b.peers = function() return { { '127.0.0.2' } } end | ||
ngx.var.proxy_pass = 'https://example.com' | ||
|
||
b.balancer.set_current_peer = spy.new(function() return true end) | ||
apicast_balancer:call({},b) | ||
assert.spy(b.balancer.set_current_peer).was.called_with('127.0.0.2', 443) | ||
end) | ||
end) | ||
end) |
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
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.
Just to clarify, I think this only works with http and https: https://github.com/3scale/lua-resty-url#default_port
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.
Right. Do you think it needs to be documented somewhere in this PR?
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.
Well spotted. I think it does not need to be documented (because we don't really support other protocols), but handled with a proper error message.
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.
btw,
set_current_peer
will set port to0
if not provided: https://github.com/openresty/lua-resty-core/blob/a39b8d68b3b1c93f22259f95925c9dc854d69c1c/lib/ngx/balancer.lua#L113There 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.
I guess then we can just leave it to that and just have a test to verify what it does when no port is provided.
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.
If the balancer has a default and would return an error then there is no need to handle this case by us. And to verify that it works it is necessary to have a test. IMO it is better to have a test to see how it works than code to maintain.
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.
I've tried it, but turns out that:
setting
unsupported://example.com
in the upstream policy config will fail here, because trying to parse a URL with unsupported scheme returns anil
.If I force upstream to push the invalid URL forward, APIcast throws an error:
[error] 4373#0: *5 invalid URL prefix in "unsupported://upstream"
So, I am not sure how I can test it in either integration or unit test :/
@mikz @davidor thoughts?
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.
@mayorova fail how? throw an exception? then it should be handled and we can test it is being handled by asserting an entry in the error log and some status.
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.
@mikz 1. will throw an exception...
My point is – I don't see a way to test how balancer will act if the incorrect scheme is passed, because the execution will not even reach the balancer point, and will fail before that.
Or should I add a failing test for the upstream policy instead? (I would consider it as a different PR - handle exception in upstream policy and add test)
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.
@mayorova does https://github.com/3scale/apicast/blob/dab3c183144fa4dc74e765e147ac56297cdcfa05/t/apicast-upstream-balancer.t help?