Skip to content

Commit

Permalink
Merge pull request Kong#1008 from Mashape/fix/nodes
Browse files Browse the repository at this point in the history
Adding node on start, and not on hook
  • Loading branch information
subnetmarco committed Feb 19, 2016
2 parents 541a5a8 + 98887ab commit 7095754
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 25 deletions.
4 changes: 1 addition & 3 deletions kong/api/routes/cluster.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
local responses = require "kong.tools.responses"
local cjson = require "cjson"
local Serf = require "kong.cli.services.serf"

local pairs = pairs
Expand All @@ -9,12 +8,11 @@ local string_upper = string.upper
return {
["/cluster/"] = {
GET = function(self, dao_factory, helpers)
local res, err = Serf(configuration):invoke_signal("members", {["-format"] = "json"})
local members, err = Serf(configuration):_members()
if err then
return responses.send_HTTP_INTERNAL_SERVER_ERROR(err)
end

local members = cjson.decode(res).members
local result = {data = {}}
for _, v in pairs(members) do
if not self.params.status or (self.params.status and v.status == self.params.status) then
Expand Down
47 changes: 46 additions & 1 deletion kong/cli/services/serf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ function Serf:_join_node(address)
return true
end

function Serf:_members()
local res, err = self:invoke_signal("members", {["-format"] = "json"})
if err then
return false, err
end

return cjson.decode(res).members
end

function Serf:_autojoin(current_node_name)
if self._configuration.cluster["auto-join"] then
logger:info("Trying to auto-join Kong nodes, please wait..")
Expand Down Expand Up @@ -129,6 +138,36 @@ function Serf:_autojoin(current_node_name)
return true
end

function Serf:_add_node()
local members, err = self:_members()
if err then
return false, err
end

local name = cluster_utils.get_node_name(self._configuration)
local addr
for _, member in ipairs(members) do
if member.name == name then
addr = member.addr
break
end
end

if not addr then
return false, "Can't find current member address"
end

local _, err = self._dao_factory.nodes:insert({
name = name,
cluster_listening_address = stringy.strip(addr)
})
if err then
return false, err
end

return true
end

function Serf:start()
if self:is_running() then
return nil, SERVICE_NAME.." is already running"
Expand Down Expand Up @@ -168,7 +207,13 @@ function Serf:start()
logger:info(string.format([[serf ..............%s]], str_cmd_args))

-- Auto-Join nodes
return self:_autojoin(node_name)
local ok, err = self:_autojoin(node_name)
if not ok then
return nil, err
end

-- Adding node to nodes table
return self:_add_node()
else
-- Get last error message
local parts = stringy.split(IO.read_file(self._log_path), "\n")
Expand Down
9 changes: 5 additions & 4 deletions kong/cli/utils/services.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ _M.STATUSES = {
-- Services ordered by priority
local services = {
require "kong.cli.services.dnsmasq",
require "kong.cli.services.nginx",
require "kong.cli.services.serf"
require "kong.cli.services.serf",
require "kong.cli.services.nginx"
}

local function prepare_database(configuration)
Expand Down Expand Up @@ -116,8 +116,9 @@ function _M.check_status(configuration, configuration_path)
end

function _M.stop_all(configuration, configuration_path)
for _, service in ipairs(services) do
service(configuration, configuration_path):stop()
-- Backwards
for i=#services, 1, -1 do
services[i](configuration, configuration_path):stop()
end
end

Expand Down
4 changes: 1 addition & 3 deletions kong/core/cluster.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
local cluster_utils = require "kong.tools.cluster"
local Serf = require "kong.cli.services.serf"
local cache = require "kong.tools.database_cache"
local cjson = require "cjson"

local resty_lock
local status, res = pcall(require, "resty.lock")
Expand Down Expand Up @@ -40,12 +39,11 @@ local function async_autojoin(premature)
ngx.log(ngx.ERR, tostring(err))
elseif count > 1 then
local serf = Serf(configuration)
local res, err = serf:invoke_signal("members", {["-format"] = "json"})
local members, err = serf:_members()
if err then
ngx.log(ngx.ERR, tostring(err))
end

local members = cjson.decode(res).members
if #members < 2 then
-- Trigger auto-join
local _, err = serf:_autojoin(cluster_utils.get_node_name(configuration))
Expand Down
18 changes: 4 additions & 14 deletions kong/core/hooks.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
local events = require "kong.core.events"
local cache = require "kong.tools.database_cache"
local stringy = require "stringy"
local cjson = require "cjson"
local Serf = require "kong.cli.services.serf"

local function invalidate_plugin(entity)
Expand All @@ -24,11 +23,11 @@ end

local function get_cluster_members()
local serf = require("kong.cli.services.serf")(configuration)
local res, err = serf:invoke_signal("members", { ["-format"] = "json" })
local members, err = serf:_members()
if err then
ngx.log(ngx.ERR, err)
else
return cjson.decode(res).members
return members
end
end

Expand Down Expand Up @@ -106,18 +105,9 @@ local function member_join(message_t)
return
end

if #nodes == 0 then -- Insert
local _, err = dao.nodes:insert({
name = stringy.strip(member.name),
cluster_listening_address = stringy.strip(member.cluster_listening_address)
})
if err then
ngx.log(ngx.ERR, tostring(err))
return
end
elseif #nodes == 1 then -- Update
if #nodes == 1 then -- Update
member_update(message_t)
else
elseif #nodes > 1 then
error("Inconsistency error. More than one node found with name "..member.name)
end

Expand Down

0 comments on commit 7095754

Please sign in to comment.