From c3c6eba6d26c9ab2f18aa7caa55ede03c5910c9e Mon Sep 17 00:00:00 2001 From: Robert Paprocki Date: Thu, 30 Mar 2017 19:44:58 -0700 Subject: [PATCH] hotfix(upstream) display active targets based on proper definition Previously we defined active upstream targets as any target with a nonzero weight. This definition was properly implemented, but caused confusion. As such, we redfine 'active targets' to be the most recent entry of each nonzero target. This presents a more accurate picture of the targets currently in use by the balancer. --- kong/api/routes/upstreams.lua | 12 +++++++++ .../03-admin_api/09-targets_routes_spec.lua | 26 ++++++++++++++----- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/kong/api/routes/upstreams.lua b/kong/api/routes/upstreams.lua index 09b4a8c818de..437430eab9d2 100644 --- a/kong/api/routes/upstreams.lua +++ b/kong/api/routes/upstreams.lua @@ -148,15 +148,27 @@ return { if not found[entry.target] and not ignored[entry.target] then if entry.weight ~= 0 then entry.order = nil -- dont show our order key to the client + + -- add what we want to send to the client in our array found_n = found_n + 1 found[found_n] = entry + -- track that we found this host:port so we only show + -- the most recent one (kinda) + found[entry.target] = true + else ignored[entry.target] = true end end end + -- get rid of the hash elements we tracked + -- so we can send this to the client + for i = 1, found_n do + found[found[i].target] = nil + end + -- for now lets not worry about rolling our own pagination -- we also end up returning a "backwards" list of targets because -- of how we sorted- do we care? diff --git a/spec/02-integration/03-admin_api/09-targets_routes_spec.lua b/spec/02-integration/03-admin_api/09-targets_routes_spec.lua index 18806d8aa28c..e4f6034a737f 100644 --- a/spec/02-integration/03-admin_api/09-targets_routes_spec.lua +++ b/spec/02-integration/03-admin_api/09-targets_routes_spec.lua @@ -261,8 +261,9 @@ describe("Admin API", function() end) describe("/upstreams/{upstream}/targets/active/", function() - describe("only shows active targets", function() + describe("GET", function() local upstream_name3 = "example.com" + local api4 before_each(function() local upstream3 = assert(helpers.dao.upstreams:insert { @@ -298,12 +299,24 @@ describe("Admin API", function() upstream_id = upstream3.id, }) - -- and an insert of a separate active target + -- an insert of a separate active target assert(helpers.dao.targets:insert { target = "api-3:80", weight = 10, upstream_id = upstream3.id, }) + + -- two target inserts (we should only see the last one) + assert(helpers.dao.targets:insert { + target = "api-4:80", + weight = 10, + upstream_id = upstream3.id, + }) + api4 = assert(helpers.dao.targets:insert { + target = "api-4:80", + weight = 10, + upstream_id = upstream3.id, + }) end) it("only shows active targets", function() @@ -313,10 +326,11 @@ describe("Admin API", function() }) assert.response(res).has.status(200) local json = assert.response(res).has.jsonbody() - assert.equal(2, #json.data) - assert.equal(2, json.total) - assert.equal("api-3:80", json.data[1].target) - assert.equal("api-2:80", json.data[2].target) + assert.equal(3, #json.data) + assert.equal(3, json.total) + assert.equal(api4.id, json.data[1].id) + assert.equal("api-3:80", json.data[2].target) + assert.equal("api-2:80", json.data[3].target) end) end) end)