Skip to content

Commit

Permalink
fix(endpoints) remove healthy apis in hybrid mode
Browse files Browse the repository at this point in the history
Update 08-targets_routes_spec.lua

Co-authored-by: Aapo Talvensaari <[email protected]>
  • Loading branch information
2 people authored and guanlan committed Aug 25, 2022
1 parent e57df7d commit 62d113d
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 25 deletions.
8 changes: 8 additions & 0 deletions autodoc/admin-api/data/admin-api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1844,6 +1844,8 @@ return {
This resets the health counters of the health checkers running in all workers
of the Kong node, and broadcasts a cluster-wide message so that the "healthy"
status is propagated to the whole Kong cluster.
Note: This API is not available when Kong is running in Hybrid mode.
]],
endpoint = [[
<div class="endpoint post indent">/upstreams/{upstream name or id}/targets/{target or id}/healthy</div>
Expand Down Expand Up @@ -1882,6 +1884,8 @@ return {
that the target is actually healthy, it will automatically re-enable it again.
To permanently remove a target from the balancer, you should [delete a
target](#delete-target) instead.
Note: This API is not available when Kong is running in Hybrid mode.
]],
endpoint = [[
<div class="endpoint post indent">/upstreams/{upstream name or id}/targets/{target or id}/unhealthy</div>
Expand Down Expand Up @@ -1914,6 +1918,8 @@ return {
This resets the health counters of the health checkers running in all workers
of the Kong node, and broadcasts a cluster-wide message so that the "healthy"
status is propagated to the whole Kong cluster.
Note: This API is not available when Kong is running in Hybrid mode.
]],
endpoint = [[
<div class="endpoint post indent">/upstreams/{upstream name or id}/targets/{target or id}/{address}/healthy</div>
Expand Down Expand Up @@ -1952,6 +1958,8 @@ return {
that the address is actually healthy, it will automatically re-enable it again.
To permanently remove a target from the balancer, you should [delete a
target](#delete-target) instead.
Note: This API is not available when Kong is running in Hybrid mode.
]],
endpoint = [[
<div class="endpoint post indent">/upstreams/{upstream name or id}/targets/{target or id}/unhealthy</div>
Expand Down
54 changes: 30 additions & 24 deletions kong/api/routes/upstreams.lua
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ local function target_endpoint(self, db, callback)
end


return {
local api_routes = {
["/upstreams/:upstreams/health"] = {
GET = function(self, db)
local upstream, _, err_t = endpoints.select_entity(self, db, db.upstreams.schema)
Expand Down Expand Up @@ -206,42 +206,48 @@ return {
end
},

["/upstreams/:upstreams/targets/:targets/healthy"] = {
PUT = function(self, db)
return set_target_health(self, db, true)
["/upstreams/:upstreams/targets/:targets"] = {
DELETE = function(self, db)
return target_endpoint(self, db, delete_target_cb)
end,
GET = function(self, db)
return target_endpoint(self, db, select_target_cb)
end,
PATCH = function(self, db)
return target_endpoint(self, db, update_target_cb)
end,
},

["/upstreams/:upstreams/targets/:targets/unhealthy"] = {
PUT = function(self, db)
return set_target_health(self, db, false)
return target_endpoint(self, db, update_target_cb)
end,
},
}

["/upstreams/:upstreams/targets/:targets/:address/healthy"] = {
-- upstream targets' healthcheck management is not available in the hybrid mode
if kong.configuration.role ~= "control_plane" then
api_routes["/upstreams/:upstreams/targets/:targets/healthy"] = {
PUT = function(self, db)
return set_target_health(self, db, true)
end,
},
}

["/upstreams/:upstreams/targets/:targets/:address/unhealthy"] = {
api_routes["/upstreams/:upstreams/targets/:targets/unhealthy"] = {
PUT = function(self, db)
return set_target_health(self, db, false)
end,
},
}

["/upstreams/:upstreams/targets/:targets"] = {
DELETE = function(self, db)
return target_endpoint(self, db, delete_target_cb)
end,
GET = function(self, db)
return target_endpoint(self, db, select_target_cb)
end,
PATCH = function(self, db)
return target_endpoint(self, db, update_target_cb)
api_routes["/upstreams/:upstreams/targets/:targets/:address/healthy"] = {
PUT = function(self, db)
return set_target_health(self, db, true)
end,
}

api_routes["/upstreams/:upstreams/targets/:targets/:address/unhealthy"] = {
PUT = function(self, db)
return target_endpoint(self, db, update_target_cb)
return set_target_health(self, db, false)
end,
},
}
}

end

return api_routes
31 changes: 30 additions & 1 deletion spec/02-integration/04-admin_api/08-targets_routes_spec.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local helpers = require "spec.helpers"
local cjson = require "cjson"
local utils = require "kong.tools.utils"
local utils = require "kong.tools.utils"
local tablex = require "pl.tablex"

local function it_content_types(title, fn)
local test_form_encoded = fn("application/x-www-form-urlencoded")
Expand Down Expand Up @@ -970,4 +971,32 @@ describe("Admin API #" .. strategy, function()
end)
end)


describe("/upstreams/{upstream}/targets/{target}/(un)healthy not available in hybrid mode", function()
lazy_setup(function()
assert(helpers.start_kong({
role = "control_plane",
cluster_cert = "spec/fixtures/kong_clustering.crt",
cluster_cert_key = "spec/fixtures/kong_clustering.key",
database = strategy,
}))
end)

lazy_teardown(function()
assert(helpers.stop_kong())
end)

it("healthcheck endpoints not included in /endpoints", function()
local admin_client = assert(helpers.admin_client())

local res = admin_client:get("/endpoints")
local body = assert.res_status(200, res)
local json = cjson.decode(body)
assert.is_nil(tablex.find(json.data, '/upstreams/{upstreams}/targets/{targets}/healthy'))
assert.is_nil(tablex.find(json.data, '/upstreams/{upstreams}/targets/{targets}/unhealthy'))
assert.is_nil(tablex.find(json.data, '/upstreams/{upstreams}/targets/{targets}/{address}/healthy'))
assert.is_nil(tablex.find(json.data, '/upstreams/{upstreams}/targets/{targets}/{address}/unhealthy'))
end)
end)

end

0 comments on commit 62d113d

Please sign in to comment.