diff --git a/autodoc/admin-api/data/admin-api.lua b/autodoc/admin-api/data/admin-api.lua index 9a47e717eecd..59f650fb7da4 100644 --- a/autodoc/admin-api/data/admin-api.lua +++ b/autodoc/admin-api/data/admin-api.lua @@ -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 = [[
/upstreams/{upstream name or id}/targets/{target or id}/healthy
@@ -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 = [[
/upstreams/{upstream name or id}/targets/{target or id}/unhealthy
@@ -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 = [[
/upstreams/{upstream name or id}/targets/{target or id}/{address}/healthy
@@ -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 = [[
/upstreams/{upstream name or id}/targets/{target or id}/unhealthy
diff --git a/kong/api/routes/upstreams.lua b/kong/api/routes/upstreams.lua index c10cac5a2905..83393a24cde1 100644 --- a/kong/api/routes/upstreams.lua +++ b/kong/api/routes/upstreams.lua @@ -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) @@ -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 diff --git a/spec/02-integration/04-admin_api/08-targets_routes_spec.lua b/spec/02-integration/04-admin_api/08-targets_routes_spec.lua index 7404b1d7a9fb..efa98c38b3e1 100644 --- a/spec/02-integration/04-admin_api/08-targets_routes_spec.lua +++ b/spec/02-integration/04-admin_api/08-targets_routes_spec.lua @@ -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") @@ -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