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

feat(core) upstreams #1541

Closed
wants to merge 28 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
1f3d8bd
initial setup new entities
Tieske Aug 8, 2016
cd990b2
next step in data model adjustments for upstreams and targets
Tieske Aug 9, 2016
e994ff2
Merge branch 'next' of https://github.com/Mashape/kong into feature/u…
Tieske Aug 11, 2016
715c81f
added ip verification/normalization functions
Tieske Aug 11, 2016
94b5f06
fix(core) request_host validation too weak with a wildcard
Tieske Aug 11, 2016
3502989
added ip address formatter and postgres 'upstream' migrations, migrat…
Tieske Aug 12, 2016
d5487ce
added initial tests for upstream api-routes
Tieske Aug 12, 2016
77dbfb1
enabled and fixed more upstream tests
Tieske Aug 13, 2016
1153a5f
added the targets, fixed their initial tests
Tieske Aug 15, 2016
d8dd5d5
Merge branch 'next' of https://github.com/Mashape/kong into feature/u…
Tieske Aug 15, 2016
5afc330
added configuration items for dns resolver, removed dnsmasq settings …
Tieske Aug 15, 2016
f988f45
ripped out all dnsmasq related stuff, added initialization code for d…
Tieske Aug 16, 2016
bb224c4
updated rockspec for added/removed files
Tieske Aug 18, 2016
9d3ac18
Merge branch 'next' of https://github.com/Mashape/kong into feature/u…
Tieske Aug 18, 2016
6bf6ec3
change dns initialize error behaviour, fixed unit test
Tieske Aug 18, 2016
9e5e90a
work in progress commit, see comments in 'kong.core.balancer.lua'
Tieske Aug 18, 2016
fc13c53
refactor resolver, simplify simple string functions
Tieske Aug 18, 2016
20139fc
refactor resolver some more, exporting the parsed url table as well
Tieske Aug 19, 2016
5915ef8
Merge branch 'refactor/resolver' into feature/upstreams
Tieske Aug 19, 2016
de0cbb0
Merge branch 'next' of https://github.com/Mashape/kong into feature/u…
Tieske Aug 19, 2016
9851854
wip commit; implemented all the core logic, needs detailing out the `…
Tieske Aug 19, 2016
7815566
fix typo
Tieske Aug 24, 2016
db1c26c
Merge branch 'next' of https://github.com/Mashape/kong into feature/u…
Tieske Aug 24, 2016
33b81a4
all unit tests pass
Tieske Aug 24, 2016
2895c2e
wip commit
Tieske Aug 24, 2016
af10447
implemented workaround to fix the tests
Tieske Aug 26, 2016
193f969
Merge branch 'next' of https://github.com/Mashape/kong into feature/u…
Tieske Aug 26, 2016
aca3c90
drop the to-ip functionality in favour of the dns lib. Simpler, better.
Tieske Aug 26, 2016
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
9 changes: 7 additions & 2 deletions kong-0.9.0-0.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ dependencies = {
"lua-resty-iputils == 0.2.1",
"luacrypto == 0.3.2",
"luasyslog == 1.0.0",
"lua_pack == 1.0.4"
"lua_pack == 1.0.4",
"dns == 0.1",
}
build = {
type = "builtin",
Expand Down Expand Up @@ -63,7 +64,6 @@ build = {
["kong.cmd.utils.serf_signals"] = "kong/cmd/utils/serf_signals.lua",
["kong.cmd.utils.nginx_signals"] = "kong/cmd/utils/nginx_signals.lua",
["kong.cmd.utils.prefix_handler"] = "kong/cmd/utils/prefix_handler.lua",
["kong.cmd.utils.dnsmasq_signals"] = "kong/cmd/utils/dnsmasq_signals.lua",

["kong.api.init"] = "kong/api/init.lua",
["kong.api.api_helpers"] = "kong/api/api_helpers.lua",
Expand All @@ -74,7 +74,9 @@ build = {
["kong.api.routes.plugins"] = "kong/api/routes/plugins.lua",
["kong.api.routes.cache"] = "kong/api/routes/cache.lua",
["kong.api.routes.cluster"] = "kong/api/routes/cluster.lua",
["kong.api.routes.upstreams"] = "kong/api/routes/upstreams.lua",

["kong.tools.dns"] = "kong/tools/dns.lua",
["kong.tools.utils"] = "kong/tools/utils.lua",
["kong.tools.printable"] = "kong/tools/printable.lua",
["kong.tools.responses"] = "kong/tools/responses.lua",
Expand All @@ -90,13 +92,16 @@ build = {
["kong.core.cluster"] = "kong/core/cluster.lua",
["kong.core.events"] = "kong/core/events.lua",
["kong.core.error_handlers"] = "kong/core/error_handlers.lua",
["kong.core.balancer"] = "kong/core/balancer.lua",

["kong.dao.errors"] = "kong/dao/errors.lua",
["kong.dao.schemas_validation"] = "kong/dao/schemas_validation.lua",
["kong.dao.schemas.apis"] = "kong/dao/schemas/apis.lua",
["kong.dao.schemas.nodes"] = "kong/dao/schemas/nodes.lua",
["kong.dao.schemas.consumers"] = "kong/dao/schemas/consumers.lua",
["kong.dao.schemas.plugins"] = "kong/dao/schemas/plugins.lua",
["kong.dao.schemas.upstreams"] = "kong/dao/schemas/upstreams.lua",
["kong.dao.schemas.targets"] = "kong/dao/schemas/targets.lua",
["kong.dao.base_db"] = "kong/dao/base_db.lua",
["kong.dao.cassandra_db"] = "kong/dao/cassandra_db.lua",
["kong.dao.postgres_db"] = "kong/dao/postgres_db.lua",
Expand Down
19 changes: 7 additions & 12 deletions kong.conf.default
Original file line number Diff line number Diff line change
Expand Up @@ -231,18 +231,13 @@
# DNS RESOLVER
#------------------------------------------------------------------------------

#dnsmasq = on # Toggles if Kong should start/stop dnsmasq,
# which can be used as the Nginx DNS resolver.
# Using dnsmasq allows Nginx to resolve
# domains defined in /etc/hosts.
# dnsmasq must be installed and available in
# your $PATH.

#dnsmasq_port = 8053 # The port on which dnsmasq should listen to
# for queries.

#dns_resolver = 8.8.8.8 # Configure a name server to be used by Nginx.
# Only valid when `dnsmasq` is disabled.
#dns_resolver = # Comma separated list of name servers, each
# entry in `ipv4[:port]` format to be used by
# Kong. If not specified the nameservers in
# the local `resolv.conf` file will be used.
# Port defaults to 53 if omitted.

#dns_hostsfile = /etc/hosts # The `hosts` file to use.

#------------------------------------------------------------------------------
# DEVELOPMENT & MISCELLANEOUS
Expand Down
18 changes: 18 additions & 0 deletions kong/api/crud_helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,24 @@ function _M.find_consumer_by_username_or_id(self, dao_factory, helpers)
end
end

function _M.find_upstream_by_name_or_id(self, dao_factory, helpers)
local filter_keys = {
[utils.is_valid_uuid(self.params.name_or_id) and "id" or "name"] = self.params.name_or_id
}
self.params.name_or_id = nil

local rows, err = dao_factory.upstreams:find_all(filter_keys)
if err then
return helpers.yield_error(err)
end

-- We know name and id are unique, so if we have a row, it must be the only one
self.upstream = rows[1]
if not self.upstream then
return helpers.responses.send_HTTP_NOT_FOUND()
end
end

function _M.paginated_set(self, dao_collection)
local size = self.params.size and tonumber(self.params.size) or 100
local offset = self.params.offset and ngx.decode_base64(self.params.offset) or nil
Expand Down
2 changes: 1 addition & 1 deletion kong/api/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ local function insert_405(routes)
end

-- Load core routes
for _, v in ipairs({"kong", "apis", "consumers", "plugins", "cache", "cluster" }) do
for _, v in ipairs({"kong", "apis", "consumers", "plugins", "cache", "cluster", "upstreams" }) do
local routes = require("kong.api.routes."..v)
attach_routes(insert_405(routes))
end
Expand Down
51 changes: 51 additions & 0 deletions kong/api/routes/upstreams.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
local crud = require "kong.api.crud_helpers"
local reports = require "kong.core.reports"

return {
["/upstreams/"] = {
GET = function(self, dao_factory)
crud.paginated_set(self, dao_factory.upstreams)
end,

PUT = function(self, dao_factory)
crud.put(self.params, dao_factory.upstreams)
end,

POST = function(self, dao_factory, helpers)
crud.post(self.params, dao_factory.upstreams)
end
},

["/upstreams/:name_or_id"] = {
before = function(self, dao_factory, helpers)
crud.find_upstream_by_name_or_id(self, dao_factory, helpers)
end,

GET = function(self, dao_factory, helpers)
return helpers.responses.send_HTTP_OK(self.upstream)
end,

PATCH = function(self, dao_factory)
crud.patch(self.params, dao_factory.upstreams, self.upstream)
end,

DELETE = function(self, dao_factory)
crud.delete(self.upstream, dao_factory.upstreams)
end
},

["/upstreams/:name_or_id/targets/"] = {
before = function(self, dao_factory, helpers)
crud.find_upstream_by_name_or_id(self, dao_factory, helpers)
self.params.upstream_id = self.upstream.id
end,

GET = function(self, dao_factory)
crud.paginated_set(self, dao_factory.targets)
end,

POST = function(self, dao_factory)
crud.post(self.params, dao_factory.targets)
end,
},
}
2 changes: 1 addition & 1 deletion kong/cmd/compile.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Example usage:
}

Note:
Third-party services such as Serf and dnsmasq need to be properly configured
Third-party services such as Serf need to be properly configured
and started for Kong to be fully compatible while embedded.

Options:
Expand Down
1 change: 0 additions & 1 deletion kong/cmd/health.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ local function execute(args)
local pids = {
serf = conf.serf_pid,
nginx = conf.nginx_pid,
dnsmasq = conf.dnsmasq and conf.dnsmasq_pid or nil
}

local count = 0
Expand Down
5 changes: 0 additions & 5 deletions kong/cmd/quit.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
local dnsmasq_signals = require "kong.cmd.utils.dnsmasq_signals"
local nginx_signals = require "kong.cmd.utils.nginx_signals"
local serf_signals = require "kong.cmd.utils.serf_signals"
local conf_loader = require "kong.conf_loader"
Expand Down Expand Up @@ -39,10 +38,6 @@ local function execute(args)

assert(serf_signals.stop(conf, DAOFactory(conf)))

if conf.dnsmasq then
assert(dnsmasq_signals.stop(conf))
end

log("Kong stopped (gracefully)")
end

Expand Down
4 changes: 0 additions & 4 deletions kong/cmd/reload.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
local dnsmasq_signals = require "kong.cmd.utils.dnsmasq_signals"
local prefix_handler = require "kong.cmd.utils.prefix_handler"
local nginx_signals = require "kong.cmd.utils.nginx_signals"
local serf_signals = require "kong.cmd.utils.serf_signals"
Expand All @@ -22,9 +21,6 @@ local function execute(args)
prefix = args.prefix
}))
assert(prefix_handler.prepare_prefix(conf, args.nginx_conf))
if conf.dnsmasq then
assert(dnsmasq_signals.start(conf))
end
assert(serf_signals.start(conf, DAOFactory(conf)))
assert(nginx_signals.reload(conf))
log("Kong reloaded")
Expand Down
2 changes: 1 addition & 1 deletion kong/cmd/restart.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ end
local lapp = [[
Usage: kong restart [OPTIONS]

Restart a Kong node (and other configured services like dnsmasq and Serf)
Restart a Kong node (and other configured services like Serf)
in the given prefix directory.

This command is equivalent to doing both 'kong stop' and
Expand Down
4 changes: 0 additions & 4 deletions kong/cmd/start.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
local dnsmasq_signals = require "kong.cmd.utils.dnsmasq_signals"
local prefix_handler = require "kong.cmd.utils.prefix_handler"
local nginx_signals = require "kong.cmd.utils.nginx_signals"
local serf_signals = require "kong.cmd.utils.serf_signals"
Expand All @@ -14,9 +13,6 @@ local function execute(args)
local dao = DAOFactory(conf)
assert(dao:run_migrations())
assert(prefix_handler.prepare_prefix(conf, args.nginx_conf))
if conf.dnsmasq then
assert(dnsmasq_signals.start(conf))
end
assert(serf_signals.start(conf, dao))
assert(nginx_signals.start(conf))
log("Kong started")
Expand Down
4 changes: 0 additions & 4 deletions kong/cmd/stop.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
local dnsmasq_signals = require "kong.cmd.utils.dnsmasq_signals"
local nginx_signals = require "kong.cmd.utils.nginx_signals"
local serf_signals = require "kong.cmd.utils.serf_signals"
local conf_loader = require "kong.conf_loader"
Expand All @@ -20,9 +19,6 @@ local function execute(args)
local conf = assert(conf_loader(default_conf.kong_conf))
assert(nginx_signals.stop(conf))
assert(serf_signals.stop(conf, DAOFactory(conf)))
if conf.dnsmasq then
assert(dnsmasq_signals.stop(conf))
end
log("Kong stopped")
end

Expand Down
75 changes: 0 additions & 75 deletions kong/cmd/utils/dnsmasq_signals.lua

This file was deleted.

3 changes: 0 additions & 3 deletions kong/cmd/utils/prefix_handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,6 @@ local function compile_conf(kong_config, conf_template)
tostring = tostring
}

if kong_config.dnsmasq then
compile_env["dns_resolver"] = "127.0.0.1:"..kong_config.dnsmasq_port
end
if kong_config.anonymous_reports and socket.dns.toip(constants.SYSLOG.ADDRESS) then
compile_env["syslog_reports"] = fmt("error_log syslog:server=%s:%d error;",
constants.SYSLOG.ADDRESS, constants.SYSLOG.PORT)
Expand Down
21 changes: 12 additions & 9 deletions kong/conf_loader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ local pl_config = require "pl.config"
local pl_file = require "pl.file"
local pl_path = require "pl.path"
local tablex = require "pl.tablex"
local utils = require "kong.tools.utils"
local log = require "kong.cmd.utils.log"

local ipv4_port_pattern = "^(%d+)%.(%d+)%.(%d+)%.(%d+):(%d+)$"

local DEFAULT_PATHS = {
"/etc/kong.conf",
"/etc/kong/kong.conf"
}

local PREFIX_PATHS = {
dnsmasq_pid = {"pids", "dnsmasq.pid"}
;
serf_pid = {"pids", "serf.pid"},
serf_log = {"logs", "serf.log"},
serf_event = {"serf", "serf_event.sh"},
Expand Down Expand Up @@ -75,8 +76,7 @@ local CONF_INFERENCES = {
cluster_profile = {enum = {"local", "lan", "wan"}},
cluster_ttl_on_failure = {typ = "number"},

dnsmasq = {typ = "boolean"},
dnsmasq_port = {typ = "number"},
dns_resolver = {typ = "array"},

ssl = {typ = "boolean"},

Expand Down Expand Up @@ -174,13 +174,16 @@ local function check_and_infer(conf)
end
end

if conf.dns_resolver and conf.dnsmasq then
errors[#errors+1] = "must disable dnsmasq when a custom DNS resolver is specified"
elseif not conf.dns_resolver and not conf.dnsmasq then
errors[#errors+1] = "must specify a custom DNS resolver when dnsmasq is turned off"
if conf.dns_resolver then
for _, server in ipairs(conf.dns_resolver) do
local dns = utils.normalize_ip(server)
if (not dns) or (dns.type ~= "ipv4") then
errors[#errors+1] = "dns_resolver must be a comma separated list in the form of IPv4 or IPv4:port"
break -- one error is enough
end
end
end

local ipv4_port_pattern = "^(%d+)%.(%d+)%.(%d+)%.(%d+):(%d+)$"
if not conf.cluster_listen:match(ipv4_port_pattern) then
errors[#errors+1] = "cluster_listen must be in the form of IPv4:port"
end
Expand Down
Loading