diff --git a/kong/clustering/rpc/manager.lua b/kong/clustering/rpc/manager.lua index 5104fdab723..3b8471e22d8 100644 --- a/kong/clustering/rpc/manager.lua +++ b/kong/clustering/rpc/manager.lua @@ -42,6 +42,7 @@ function _M.new(conf, node_id) -- clients[node_id]: { socket1 => true, socket2 => true, ... } clients = {}, client_capabilities = {}, + client_ips = {}, -- store DP node's ip addr node_id = node_id, conf = conf, cluster_cert = assert(clustering_tls.get_cluster_cert(conf)), @@ -75,16 +76,18 @@ end function _M:_remove_socket(socket) - local sockets = assert(self.clients[socket.node_id]) + local node_id = socket.node_id + local sockets = assert(self.clients[node_id]) assert(sockets[socket]) sockets[socket] = nil if table_isempty(sockets) then - self.clients[socket.node_id] = nil - self.client_capabilities[socket.node_id] = nil - assert(self.concentrator:_enqueue_unsubscribe(socket.node_id)) + self.clients[node_id] = nil + self.client_ips[node_id] = nil + self.client_capabilities[node_id] = nil + assert(self.concentrator:_enqueue_unsubscribe(node_id)) end end @@ -255,6 +258,9 @@ function _M:handle_websocket() local s = socket.new(self, wb, node_id) self:_add_socket(s, rpc_capabilities) + -- store DP's ip addr + self.client_ips[node_id] = ngx_var.remote_addr + s:start() local res, err = s:join() self:_remove_socket(s) diff --git a/kong/clustering/services/sync/rpc.lua b/kong/clustering/services/sync/rpc.lua index 0353b65123f..68b51576cf9 100644 --- a/kong/clustering/services/sync/rpc.lua +++ b/kong/clustering/services/sync/rpc.lua @@ -58,7 +58,7 @@ function _M:init_cp(manager) local ok, err = kong.db.clustering_data_planes:upsert({ id = node_id }, { last_seen = ngx.time(), hostname = node_id, - ip = "127.0.7.1", -- XXX how to get the corret ip? + ip = kong.rpc.client_ips[node_id], -- try to get the corret ip version = "3.8.0.0", sync_status = CLUSTERING_SYNC_STATUS.NORMAL, config_hash = string.format("%032d", default_namespace.version), diff --git a/spec/02-integration/18-hybrid_rpc/01-rpc_spec.lua b/spec/02-integration/18-hybrid_rpc/01-rpc_spec.lua index c69cac3dd20..ad5419062b5 100644 --- a/spec/02-integration/18-hybrid_rpc/01-rpc_spec.lua +++ b/spec/02-integration/18-hybrid_rpc/01-rpc_spec.lua @@ -48,7 +48,7 @@ for _, strategy in helpers.each_strategy() do local json = cjson.decode(body) for _, v in pairs(json.data) do - if v.rpc_capabilities and #v.rpc_capabilities ~= 0 then + if v.ip == "127.0.0.1" and v.rpc_capabilities and #v.rpc_capabilities ~= 0 then table.sort(v.rpc_capabilities) --assert.near(14 * 86400, v.ttl, 3) --assert.same({ "kong.debug.log_level.v1", }, v.rpc_capabilities)