Skip to content

Commit

Permalink
refactor(config) remove unnecessary dao config nesting
Browse files Browse the repository at this point in the history
DAO properties are no nested in `properties` property anymore.
  • Loading branch information
thibaultcha committed Oct 16, 2015
1 parent 121a949 commit ccac50b
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 188 deletions.
125 changes: 64 additions & 61 deletions kong.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<<<<<<< HEAD
######
## Kong configuration file. All commented values are default values.
## Uncomment and update a value to configure Kong to your needs.
Expand Down Expand Up @@ -34,12 +33,17 @@
# proxy_ssl_port: 8443

######
## DNS resolver configuration. Specify how Kong should resolve DNS addresses either by
## specifying a DNS resolver address, or by using dnsmasq (default) that will use the system settings
# dns_resolver:
# address: "8.8.8.8:53"
## Specify how Kong performs DNS resolution (in the `dns_resolvers_available` property) you want to use.
## Options are: "dnsmasq" (You will need dnsmasq to be installed) or "server".
# dns_resolver: dnsmasq

######
## DNS resolvers configuration. Specify a DNS server or the port on which you want
## dnsmasq to run.
# dns_resolvers_available:
# server:
# address: "8.8.8.8:53"
# dnsmasq:
# enabled: true
# port: 8053

######
Expand All @@ -59,61 +63,60 @@
## Databases configuration.
# databases_available:
# cassandra:
# properties:
######
## Contact points to your Cassandra cluster.
# contact_points:
# - "localhost:9042"

# timeout: 1000

# keyspace: kong

######
## Time (in milliseconds) for which sockets will be keep alive.
## for being eventually re-used before being closed.
# keepalive: 60000

######
## Keyspace options. Set those before running Kong or any migration.
## Those settings will be used to create a keyspace with the desired options
## when first running the migrations.
## See http://docs.datastax.com/en/cql/3.1/cql/cql_reference/create_keyspace_r.html

######
## The name of the replica placement strategy class for the new keyspace.
## Can be "SimpleStrategy" or "NetworkTopologyStrategy".
# replication_strategy: SimpleStrategy

######
## For SimpleStrategy only.
## The number of replicas of data on multiple nodes.
# replication_factor: 1

######
## For NetworkTopologyStrategy only.
## The number of replicas of data on multiple nodes in each data center.
# data_centers:
# dc1: 2
# dc2: 3

######
## If true, will enable client-to-node encryption.
# ssl: false

######
## If true, will verify the SSL certificate in use.
## `ssl_certificate` must be provided.
# ssl_verify: false

######
## **Absolute path** to the certificate authority file for your cluster.
# ssl_certificate: "/path/to/cluster-ca-certificate.pem"

######
## If the cluster as authentication enabled, provide a user and a password here.
# user: cassandra
# password: cassandra
######
## Contact points to your Cassandra cluster.
# contact_points:
# - "localhost:9042"

# timeout: 1000

# keyspace: kong

######
## Time (in milliseconds) for which sockets will be keep alive.
## for being eventually re-used before being closed.
# keepalive: 60000

######
## Keyspace options. Set those before running Kong or any migration.
## Those settings will be used to create a keyspace with the desired options
## when first running the migrations.
## See http://docs.datastax.com/en/cql/3.1/cql/cql_reference/create_keyspace_r.html

######
## The name of the replica placement strategy class for the new keyspace.
## Can be "SimpleStrategy" or "NetworkTopologyStrategy".
# replication_strategy: SimpleStrategy

######
## For SimpleStrategy only.
## The number of replicas of data on multiple nodes.
# replication_factor: 1

######
## For NetworkTopologyStrategy only.
## The number of replicas of data on multiple nodes in each data center.
# data_centers:
# dc1: 2
# dc2: 3

######
## If true, will enable client-to-node encryption.
# ssl: false

######
## If true, will verify the SSL certificate in use.
## `ssl_certificate` must be provided.
# ssl_verify: false

######
## **Absolute path** to the certificate authority file for your cluster.
# ssl_certificate: "/path/to/cluster-ca-certificate.pem"

######
## If the cluster as authentication enabled, provide a user and a password here.
# user: cassandra
# password: cassandra

######
## Time (in seconds) for which entities from the database (APIs, plugins configurations...)
Expand Down
28 changes: 7 additions & 21 deletions kong/cli/utils/signal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -95,29 +95,15 @@ local function prepare_nginx_working_dir(args_config)

ssl.prepare_ssl(kong_config)
local ssl_cert_path, ssl_key_path = ssl.get_ssl_cert_and_key(kong_config)
local trusted_ssl_cert_path = kong_config.dao_config.properties.ssl_certificate -- DAO ssl cert

-- Check dns_resolver
local dns_resolver
if kong_config.dns_resolver.address and kong_config.dns_resolver.dnsmasq.enabled then
cutils.logger:error_exit("Invalid \"dns_resolver\" setting: you cannot set both an address and enable dnsmasq")
elseif not kong_config.dns_resolver.address and not kong_config.dns_resolver.dnsmasq.enabled then
cutils.logger:error_exit("Invalid \"dns_resolver\" setting: you must set at least an address or enable dnsmasq")
elseif kong_config.dns_resolver.address then
dns_resolver = kong_config.dns_resolver.address
else
dns_resolver = "127.0.0.1:"..kong_config.dns_resolver.dnsmasq.port
end

cutils.logger:info("DNS resolver set to: "..dns_resolver)
local trusted_ssl_cert_path = kong_config.dao_config.ssl_certificate -- DAO ssl cert

-- Extract nginx config from kong config, replace any needed value
local nginx_config = kong_config.nginx
local nginx_inject = {
proxy_port = kong_config.proxy_port,
proxy_ssl_port = kong_config.proxy_ssl_port,
admin_api_port = kong_config.admin_api_port,
dns_resolver = dns_resolver,
dns_resolver = kong_config.dns_resolver.address,
memory_cache_size = kong_config.memory_cache_size,
ssl_cert = ssl_cert_path,
ssl_key = ssl_key_path,
Expand Down Expand Up @@ -207,7 +193,7 @@ _M.QUIT = QUIT

function _M.prepare_kong(args_config, signal)
local kong_config = get_kong_config(args_config)
local dao_config = kong_config.databases_available[kong_config.database].properties
local dao_config = kong_config.dao_config

local printable_mt = require "kong.tools.printable"
setmetatable(dao_config, printable_mt)
Expand All @@ -217,14 +203,14 @@ function _M.prepare_kong(args_config, signal)
Proxy HTTP port....%s
Proxy HTTPS port...%s
Admin API port.....%s
dnsmasq port.......%s
DNS resolver.......%s
Database...........%s %s
]],
constants.VERSION,
kong_config.proxy_port,
kong_config.proxy_ssl_port,
kong_config.admin_api_port,
kong_config.dns_resolver.dnsmasq.enabled and kong_config.dns_resolver.dnsmasq.port or "DISABLED",
kong_config.dns_resolver.address,
kong_config.database,
tostring(dao_config)))

Expand Down Expand Up @@ -274,8 +260,8 @@ function _M.send_signal(args_config, signal)
-- dnsmasq start/stop
if signal == START then
dnsmasq.stop(kong_config)
if kong_config.dns_resolver.dnsmasq.enabled then
local dnsmasq_port = kong_config.dns_resolver.dnsmasq.port
if kong_config.dns_resolver.dnsmasq then
local dnsmasq_port = kong_config.dns_resolver.port
check_port(dnsmasq_port)
dnsmasq.start(kong_config.nginx_working_dir, dnsmasq_port)
end
Expand Down
24 changes: 1 addition & 23 deletions kong/dao/cassandra/schema/migrations.lua
Original file line number Diff line number Diff line change
@@ -1,32 +1,10 @@
local DEFAULTS = {
["SimpleStrategy"] = {
replication_factor = 1
},
["NetworkTopologyStrategy"] = {
data_centers = {}
}
}

local Migrations = {
{
init = true,
name = "2015-01-12-175310_skeleton",
up = function(options)
if not options.replication_strategy then options.replication_strategy = "SimpleStrategy" end
local keyspace_name = options.keyspace
local strategy, strategy_properties = "", ""

-- Find strategy and retrieve default options
for strategy_name, strategy_defaults in pairs(DEFAULTS) do
if options.replication_strategy == strategy_name then
strategy = strategy_name
for opt_name, opt_value in pairs(strategy_defaults) do
if not options[opt_name] then
options[opt_name] = opt_value
end
end
end
end
local strategy, strategy_properties = options.replication_strategy, ""

-- Format strategy options
if strategy == "SimpleStrategy" then
Expand Down
45 changes: 30 additions & 15 deletions kong/tools/config_defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,43 @@ return {
["proxy_port"] = {type = "number", default = 8000},
["proxy_ssl_port"] = {type = "number", default = 8443},
["admin_api_port"] = {type = "number", default = 8001},
["dnsmasq_port"] = {type = "number", default = 8053},
["dns_resolver"] = {type = "string", default = "dnsmasq"},
["dns_resolvers_available"] = {
type = "table",
content = {
["server"] = {
type = "table",
content = {
["address"] = {type = "string", default = "8.8.8.8:53"}
}
},
["dnsmasq"] = {
type = "table",
content = {
["port"] = {type = "number", default = 8053}
}
}
}
},
["database"] = {type = "string", default = "cassandra"},
["databases_available"] = {
type = "table",
content = {
["cassandra"] = {
type = "table",
content = {
["properties"] = {
type = "table",
content = {
["contact_points"] = {type = "array", default = {"localhost:9042"}},
["timeout"] = {type = "number", default = 1000},
["keyspace"] = {type = "string", default = "kong"},
["keepalive"] = {type = "number", default = 60000},
["ssl"] = {type = "boolean", default = false},
["ssl_verify"] = {type = "boolean", default = false},
["ssl_certificate"] = {type = "string", nullable = true},
["user"] = {type = "string", nullable = true},
["password"] = {type = "string", nullable = true}
}
}
["contact_points"] = {type = "array", default = {"localhost:9042"}},
["timeout"] = {type = "number", default = 1000},
["keyspace"] = {type = "string", default = "kong"},
["keepalive"] = {type = "number", default = 60000},
["replication_strategy"] = {type = "string", default = "SimpleStrategy"},
["replication_factor"] = {type = "number", default = 1},
["data_centers"] = {type = "table", default = {}},
["ssl"] = {type = "boolean", default = false},
["ssl_verify"] = {type = "boolean", default = false},
["ssl_certificate"] = {type = "string", nullable = true},
["user"] = {type = "string", nullable = true},
["password"] = {type = "string", nullable = true}
}
}
}
Expand Down
12 changes: 11 additions & 1 deletion kong/tools/config_loader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ local function validate_config_schema(config, config_schema)
property = config[config_key] or key_infos.default

-- Recursion on table values
if key_infos.type == "table" then
if key_infos.type == "table" and key_infos.content ~= nil then
if property == nil then
property = {}
end
Expand Down Expand Up @@ -94,6 +94,16 @@ function _M.load(config_path)
-- Adding computed properties
config.pid_file = IO.path:join(config.nginx_working_dir, constants.CLI.NGINX_PID)
config.dao_config = config.databases_available[config.database]
if config.dns_resolver == "dnsmasq" then
config.dns_resolver = {
address = "127.0.0.1:"..config.dns_resolvers_available.dnsmasq.port,
port = config.dns_resolvers_available.dnsmasq.port,
dnsmasq = true
}
else
config.dns_resolver = {address = config.dns_resolver.server.address}
end


-- Load absolute path for the nginx working directory
if not stringy.startswith(config.nginx_working_dir, "/") then
Expand Down
2 changes: 1 addition & 1 deletion kong/tools/dao_loader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ local _M = {}

function _M.load(config)
local DaoFactory = require("kong.dao."..config.database..".factory")
return DaoFactory(config.dao_config.properties, config.plugins_available)
return DaoFactory(config.dao_config, config.plugins_available)
end

return _M
38 changes: 0 additions & 38 deletions spec/integration/cli/start_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,44 +29,6 @@ describe("CLI", function()
pcall(spec_helper.stop_kong, SERVER_CONF)
end)

describe("dnsmasq check", function()

it("should start dnsmasq with the default settings", function()
local _, exit_code = spec_helper.start_kong(SERVER_CONF, true)
assert.are.same(0, exit_code)
end)

it("should not start with both dnsmasq and an address set", function()
replace_conf_property("dns_resolver", {
address = "8.8.8.8:53",
dnsmasq = {
enabled = true,
port = 8053
}
})

assert.error_matches(function()
spec_helper.start_kong(SERVER_CONF, true)
end, "Invalid \"dns_resolver\" setting: you cannot set both an address and enable dnsmasq", nil, true)
end)

it("should not start with none of dnsmasq and an address set", function()
replace_conf_property("dns_resolver", { dnsmasq = {}})

assert.error_matches(function()
spec_helper.start_kong(SERVER_CONF, true)
end, "Invalid \"dns_resolver\" setting: you must set at least an address or enable dnsmasq", nil, true)
end)

it("should start dnsmasq with a custom address", function()
replace_conf_property("dns_resolver", { address = "8.8.8.8:53", dnsmasq = { enabled = false, port = 8053}})

local _, exit_code = spec_helper.start_kong(SERVER_CONF, true)
assert.are.same(0, exit_code)
end)

end)

describe("Startup plugins check", function()

it("should start with the default configuration", function()
Expand Down
Loading

0 comments on commit ccac50b

Please sign in to comment.