Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(revert): Revert "feat(dao) use cache_key for target uniqueness detection" #8705

Merged
merged 1 commit into from
Apr 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,6 @@

### Additions

#### Core

- Added `cache_key` on target entity for uniqueness detection.
[#8179](https://github.com/Kong/kong/pull/8179)

#### Plugins

- **Zipkin**: add support for including HTTP path in span name
Expand Down
23 changes: 23 additions & 0 deletions kong/api/routes/upstreams.lua
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,21 @@ local function target_endpoint(self, db, callback)
end


local function update_existent_target(self, db)
local upstream = endpoints.select_entity(self, db, db.upstreams.schema)
local filter = { target = unescape_uri(self.params.target) }
local opts = endpoints.extract_options(self.args.uri, db.targets.schema, "select")
local target = db.targets:select_by_upstream_filter(upstream, filter, opts)

if target then
self.params.targets = db.targets.schema:extract_pk_values(target)
return endpoints.update_entity(self, db, db.targets.schema)
end

return nil
end


return {
["/upstreams/:upstreams/health"] = {
GET = function(self, db)
Expand Down Expand Up @@ -166,6 +181,14 @@ return {
"upstream",
"page_for_upstream"),
PUT = function(self, db)
local entity, _, err_t = update_existent_target(self, db)
if err_t then
return endpoints.handle_error(err_t)
end
if entity then
return kong.response.exit(200, entity, { ["Deprecation"] = "true" })
end

local create = endpoints.post_collection_endpoint(kong.db.targets.schema,
kong.db.upstreams.schema, "upstream")
return create(self, db)
Expand Down
9 changes: 9 additions & 0 deletions kong/db/dao/targets.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ function _TARGETS:insert(entity, options)
entity.target = formatted_target
end

local workspace = workspaces.get_workspace_id()
local opts = { nulls = true, workspace = workspace }
for existent in self:each_for_upstream(entity.upstream, nil, opts) do
if existent.target == entity.target then
local err_t = self.errors:unique_violation({ target = existent.target })
return nil, tostring(err_t), err_t
end
end

return self.super.insert(self, entity, options)
end

Expand Down
120 changes: 0 additions & 120 deletions kong/db/migrations/core/016_280_to_300.lua

This file was deleted.

1 change: 0 additions & 1 deletion kong/db/migrations/core/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@ return {
"013_220_to_230",
"014_230_to_270",
"015_270_to_280",
"016_280_to_300"
}
1 change: 0 additions & 1 deletion kong/db/schema/entities/targets.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ return {
name = "targets",
dao = "kong.db.dao.targets",
primary_key = { "id" },
cache_key = { "upstream", "target" },
endpoint_key = "target",
workspaceable = true,
fields = {
Expand Down
11 changes: 8 additions & 3 deletions spec/02-integration/04-admin_api/08-targets_routes_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ describe("Admin API #" .. strategy, function()
end
end)

it_content_types("refuses to create duplicated targets", function(content_type)
it_content_types("updates and does not create duplicated targets (#deprecated)", function(content_type)
return function()
local upstream = bp.upstreams:insert { slots = 10 }
local res = assert(client:send {
Expand All @@ -159,9 +159,10 @@ describe("Admin API #" .. strategy, function()
assert.equal("single-target.test:8080", json.target)
assert.is_number(json.created_at)
assert.is_string(json.id)
local id = json.id
assert.are.equal(1, json.weight)

local res2 = assert(client:send {
local res = assert(client:send {
method = "PUT",
path = "/upstreams/" .. upstream.name .. "/targets/",
body = {
Expand All @@ -170,7 +171,11 @@ describe("Admin API #" .. strategy, function()
},
headers = {["Content-Type"] = content_type}
})
assert.response(res2).has.status(409)
local body = assert.response(res).has.status(200)
local json = cjson.decode(body)
assert.are.equal(100, json.weight)
assert.are.equal(id, json.id)
assert.equal("true", res.headers["Deprecation"])
end
end)

Expand Down