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

Add upstream policy #562

Merged
merged 6 commits into from Jan 25, 2018
Merged

Add upstream policy #562

merged 6 commits into from Jan 25, 2018

Conversation

davidor
Copy link
Contributor

@davidor davidor commented Jan 23, 2018

This is a first version of the upstream policy. For now, it only allows to modify the host of a request based on a set of rules that act on its path, but we can add more functionality later.

local new = _M.new

local function change_upstream(url)
ngx.ctx.upstream = resty_resolver:instance():get_servers(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is very similar to what we already have in Proxy.get_upstream() https://github.com/3scale/apicast/blob/6049ab33581483b0e69fc1ff6ab2fea38aa4a696/gateway/src/apicast/proxy.lua#L180

If we think that there might be other policies that need the same functionality, we should refactor what we have there to avoid duplicating code in the policies.

Copy link
Contributor Author

@davidor davidor Jan 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather address this on a separate PR as this is an internal change that does not affect the schema of the policy.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally 👍


local function change_upstream(url)
ngx.ctx.upstream = resty_resolver:instance():get_servers(
url.host, { port = tonumber(url.port) })
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to check if tonumber() here is needed or if the resty_url.parse() call guarantees that we'll get a number here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@davidor davidor force-pushed the upstream-policy branch 3 times, most recently from a20434f to 95eb0d2 Compare January 24, 2018 10:12
--- upstream
location / {
content_by_lua_block {
ngx.say('yay, api backend');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could print here the ngx.var.uri to verify what was received?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The path in the location already checks schema, host, and port.
For the path we'll need to use a location different from / to be sure. I think that would be enough.
However, we could also use luassert inside the content_by_lua block to make these checks more explicit.
What do you think?

--- upstream
location / {
content_by_lua_block {
ngx.say('yay, api backend');
Copy link
Contributor

@mikz mikz Jan 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same comment as https://github.com/3scale/apicast/pull/562/files#r163527794. Would be nice to verify the upstream uri+host+ whatever is important.

{
"rules": [
{ "regex": "/i_dont_match", "url": "http://example.com" },
{ "regex": "/", "url": "http://test:$TEST_NGINX_SERVER_PORT" },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should test a case when the url has a path to see what is forwarded upstream.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Added in new commit.

]
}
--- upstream
location /a_path {
Copy link
Contributor

@mikz mikz Jan 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can match /a_path/whatever. Would be good to at least define it as location = /a_path. But I'd also go the extra step and print ngx.var.uri in the response. So we can for example verify the args (which would not be in the uri anyway, but still something we should check).

Copy link
Contributor Author

@davidor davidor Jan 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a luassert test that checks the value of ngx.var.request. It turns out that args are not sent when the url of the rule contains a path. Working on a fix.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 I knew this was going to be a bit tricky to send everything correctly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I solved this, but it looks like I have a similar problem with POST bodies.

@davidor davidor force-pushed the upstream-policy branch 3 times, most recently from 3cfaf8c to 0a0b7bf Compare January 24, 2018 15:24
url.host, { port = url.port })

if url.path then
ngx.var.proxy_pass = format('%s://upstream%s?%s', url.scheme, url.path, ngx.var.args)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is going to always append ?, right? Even though there are no args.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haved extracted this part of the code to its own method to make it clearer.

@davidor davidor force-pushed the upstream-policy branch 2 times, most recently from 4474007 to 5711d3f Compare January 24, 2018 18:16
@davidor
Copy link
Contributor Author

davidor commented Jan 25, 2018

@mikz This is ready. I added asserts in the location blocks to make sure that we are receiving the request as we expect it.

@davidor davidor merged commit 09c317b into master Jan 25, 2018
@davidor davidor deleted the upstream-policy branch January 25, 2018 11:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants