-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(clustering/sync): validate deltas when syncing (#14127)
### Summary 1. validate entity schema * call `kong.db[entity].validate(entity)` over all the entities from deltas 2. validate entity's references (foreign references) * re-implemented logic: traverse delta -> try to find delta's foreign entity in config & LMDB * TODO: decouple this logic completely from declarative config logic: https://konghq.atlassian.net/browse/KAG-6231 3. TODO: report error if deleting delta operation could not find its associated entity in LMDB and deltas: https://konghq.atlassian.net/browse/KAG-6238 KAG-5897
- Loading branch information
Showing
10 changed files
with
396 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
local declarative = require("kong.db.declarative") | ||
local declarative_config = require("kong.db.schema.others.declarative_config") | ||
|
||
|
||
local null = ngx.null | ||
local pk_string = declarative_config.pk_string | ||
local validate_references_sync = declarative_config.validate_references_sync | ||
local pretty_print_error = declarative.pretty_print_error | ||
|
||
|
||
local function validate_deltas(deltas, is_full_sync) | ||
|
||
local errs = {} | ||
|
||
-- generate deltas table mapping primary key string to entity item | ||
local deltas_map = {} | ||
|
||
local db = kong.db | ||
|
||
for _, delta in ipairs(deltas) do | ||
local delta_type = delta.type | ||
local delta_entity = delta.entity | ||
|
||
if delta_entity ~= nil and delta_entity ~= null then | ||
-- table: primary key string -> entity | ||
local schema = db[delta_type].schema | ||
local pk = schema:extract_pk_values(delta_entity) | ||
local pks = pk_string(schema, pk) | ||
|
||
deltas_map[pks] = delta_entity | ||
|
||
-- validate entity | ||
local dao = kong.db[delta_type] | ||
if dao then | ||
-- CP will insert ws_id into the entity, which will be validated as an | ||
-- unknown field. | ||
-- TODO: On the CP side, remove ws_id from the entity and set it only | ||
-- in the delta. | ||
local ws_id = delta_entity.ws_id | ||
delta_entity.ws_id = nil -- clear ws_id | ||
|
||
local ok, err_t = dao.schema:validate(delta_entity) | ||
|
||
delta_entity.ws_id = ws_id -- restore ws_id | ||
|
||
if not ok then | ||
errs[#errs + 1] = { [delta_type] = err_t } | ||
end | ||
end | ||
end | ||
end | ||
|
||
if next(errs) then | ||
return nil, pretty_print_error(errs, "deltas") | ||
end | ||
|
||
-- validate references | ||
local ok, err_t = validate_references_sync(deltas, deltas_map, is_full_sync) | ||
if not ok then | ||
return nil, pretty_print_error(err_t) | ||
end | ||
|
||
return true | ||
end | ||
|
||
|
||
return { | ||
validate_deltas = validate_deltas, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
c85bc75
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bazel Build
Docker image available
kong/kong-dev:c85bc752073ee4a39d22b2c724a1ede41a6ce3c5
Artifacts available https://github.com/Kong/kong/actions/runs/12901689878