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

[nginx] define resolver directive in the config #508

Merged
merged 1 commit into from
Nov 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

- New policy chains system. This allows users to write custom policies to configure what Apicast can do on each of the Nginx phases [PR #450](https://github.com/3scale/apicast/pull/450)
- Resolver can resolve nginx upstreams [PR #478](https://github.com/3scale/apicast/pull/478)
- Add `resolver` directive in the nginx configuration [PR #508](https://github.com/3scale/apicast/pull/508)
- Calls 3scale backend with the 'no_body' option enabled. This reduces network traffic in cases where APIcast does not need to parse the response body [PR #483](https://github.com/3scale/apicast/pull/483)
- Methods to modify policy chains [PR #505](https://github.com/3scale/apicast/pull/505)
- Ability to load several environment configurations [PR #504](https://github.com/3scale/apicast/pull/504)
Expand Down
5 changes: 5 additions & 0 deletions gateway/conf/nginx.conf.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ http {

lua_package_path ";;{{prefix}}/?.lua;{{prefix}}/src/?.lua";

{% if nameservers %}
resolver {{ nameservers | join: " " }};
{% endif %}


{% for file in "http.d/*.conf" | filesystem %}
{% include file %}
{% endfor %}
Expand Down
24 changes: 24 additions & 0 deletions gateway/src/apicast/cli/environment.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,32 @@ local assert = assert
local error = error
local print = print
local pairs = pairs
local ipairs = ipairs
local tostring = tostring
local insert = table.insert
local concat = table.concat
local re = require('ngx.re')

local function parse_nameservers()
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this method be exposed in resty.resolver?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

resty.resolver exposes the nameservers as tables with some metamethods. Those objects can be joined with table.join so they have to be serialized first into ip:port. I guess it is worth a comment in the code. Will fix it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

Copy link
Contributor

Choose a reason for hiding this comment

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

That helps 👍

local resolver = require('resty.resolver')
local nameservers = {}

for _,nameserver in ipairs(resolver.init_nameservers()) do
-- resty.resolver returns nameservers as tables with __tostring metamethod
-- unfortunately those objects can't be joined with table.concat
-- and have to be converted to strings first
insert(nameservers, tostring(nameserver))
end

-- return the table only if there are some nameservers
-- because it is way easier to check in liquid and `resolver` directive
-- has to contain at least one server, so we can skip it when there are none
if #nameservers > 0 then
return nameservers
end
end


local _M = {}
---
-- @field default_environment Default environment name.
Expand All @@ -26,9 +48,11 @@ _M.default_environment = 'production'

--- Default configuration.
-- @tfield ?string ca_bundle path to CA store file
-- @tfield ?{string,...} nameservers list of nameservers
-- @table environment.default_config default configuration
_M.default_config = {
ca_bundle = resty_env.value('SSL_CERT_FILE'),
nameservers = parse_nameservers(),
}

local mt = { __index = _M }
Expand Down
2 changes: 2 additions & 0 deletions gateway/src/resty/resolver.lua
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ function _M.init_nameservers(path)
ngx.log(ngx.INFO, 'adding ', search[i], ' as search domain')
insert(_M.search, search[i])
end

return nameservers
end

function _M.nameservers()
Expand Down