Skip to content

Commit

Permalink
initial dns commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Tieske committed Sep 1, 2016
1 parent 2614119 commit 2b9486d
Show file tree
Hide file tree
Showing 33 changed files with 689 additions and 334 deletions.
6 changes: 4 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 @@ -75,6 +75,7 @@ build = {
["kong.api.routes.cache"] = "kong/api/routes/cache.lua",
["kong.api.routes.cluster"] = "kong/api/routes/cluster.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 @@ -91,6 +92,7 @@ build = {
["kong.core.events"] = "kong/core/events.lua",
["kong.core.error_handlers"] = "kong/core/error_handlers.lua",
["kong.core.globalpatches"] = "kong/core/globalpatches.lua",
["kong.core.balancer"] = "kong/core/balancer.lua",

["kong.dao.errors"] = "kong/dao/errors.lua",
["kong.dao.schemas_validation"] = "kong/dao/schemas_validation.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
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 @@ -175,13 +175,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
80 changes: 80 additions & 0 deletions kong/core/balancer.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
local utils = require "kong.tools.utils"
local dns_client = require "dns.client"

local toip = dns_client.toip

-- looks up a balancer for the target.
-- @param target the table with the target details
-- @return balancer if found, or nil if not found, or nil+error on error
local get_balancer = function(target)
return nil -- TODO: place holder, forces dns use to first fix regression
end


local first_try_balancer = function(target)
end

local retry_balancer = function(target)
end

local first_try_dns = function(target)
local ip, port = toip(target.upstream.host, target.upstream.port, false)
if not ip then
return nil, port
end
target.ip = ip
target.port = port
return true
end

local retry_dns = function(target)
local ip, port = toip(target.upstream.host, target.upstream.port, true)
if type(ip) ~= "string" then
return nil, port
end
target.ip = ip
target.port = port
return true
end


-- Resolves the target structure in-place (fields `ip` and `port`).
--
-- If the hostname matches an 'upstream' pool, then it must be balanced in that
-- pool, in this case any port number provided will be ignored, as the pool provides it.
--
-- @param target the data structure as defined in `core.access.before` where it is created
-- @return true on success, nil+error otherwise
local function execute(target)
if target.type ~= "name" then
-- it's an ip address (v4 or v6), so nothing we can do...
target.ip = target.upstream.host
target.port = target.upstream.port or 80
return true
end

-- when tries == 0 it runs before the `balancer` context (in the `access` context),
-- when tries >= 2 then it performs a retry in the `balancer` context
if target.tries == 0 then
local err
-- first try, so try and find a matching balancer/upstream object
target.balancer, err = get_balancer(target)
if err then return nil, err end

if target.balancer then
return first_try_balancer(target)
else
return first_try_dns(target)
end
else
if target.balancer then
return retry_balancer(target)
else
return retry_dns(target)
end
end
end

return {
execute = execute,
}
Loading

0 comments on commit 2b9486d

Please sign in to comment.