Skip to content

Commit

Permalink
Feat/plugins mesh field 1/2 (#3930)
Browse files Browse the repository at this point in the history
This PR includes:
* A fix on the schema, found by chance while studying how to best implement the new field
* A new field called `run_on` to tag plugins with `first`, `second` or `all`.
  • Loading branch information
kikito committed Nov 14, 2018
1 parent 9bd8035 commit 237a068
Show file tree
Hide file tree
Showing 23 changed files with 64 additions and 5 deletions.
2 changes: 1 addition & 1 deletion kong/cmd/migrations.lua
Expand Up @@ -80,7 +80,7 @@ local function execute(args)
conf.cassandra_timeout = args.db_timeout -- connect + send + read
conf.cassandra_schema_consensus_timeout = args.db_timeout

local db = DB.new(conf)
local db = assert(DB.new(conf))
assert(db:init_connector())

local schema_state = assert(db:schema_state())
Expand Down
16 changes: 15 additions & 1 deletion kong/db/migrations/core/001_14_to_15.lua
Expand Up @@ -123,6 +123,15 @@ return {
END;
$$;
DO $$
BEGIN
ALTER TABLE IF EXISTS ONLY "plugins" ADD "run_on" TEXT;
EXCEPTION WHEN DUPLICATE_COLUMN THEN
-- Do nothing, accept existing state
END;
$$;
CREATE INDEX IF NOT EXISTS "plugins_run_on_idx" ON "plugins" ("run_on");
ALTER TABLE IF EXISTS ONLY "apis"
Expand Down Expand Up @@ -190,14 +199,14 @@ return {
CREATE INDEX IF NOT EXISTS routes_name_idx ON routes(name);
CREATE TABLE IF NOT EXISTS plugins_temp(
id uuid,
created_at timestamp,
api_id uuid,
route_id uuid,
service_id uuid,
consumer_id uuid,
run_on text,
name text,
config text, -- serialized plugin configuration
enabled boolean,
Expand Down Expand Up @@ -241,6 +250,7 @@ return {
route_id = "uuid",
service_id = "uuid",
consumer_id = "uuid",
run_on = "text",
created_at = "timestamp",
enabled = "boolean",
cache_key = "text",
Expand Down Expand Up @@ -285,6 +295,7 @@ return {
route_id uuid,
service_id uuid,
consumer_id uuid,
run_on text,
name text,
config text, -- serialized plugin configuration
enabled boolean,
Expand All @@ -298,6 +309,7 @@ return {
CREATE INDEX IF NOT EXISTS ON plugins(service_id);
CREATE INDEX IF NOT EXISTS ON plugins(consumer_id);
CREATE INDEX IF NOT EXISTS ON plugins(cache_key);
CREATE INDEX IF NOT EXISTS ON plugins(run_on);
]]))

plugins_def = {
Expand All @@ -310,6 +322,7 @@ return {
route_id = "uuid",
service_id = "uuid",
consumer_id = "uuid",
run_on = "text",
created_at = "timestamp",
enabled = "boolean",
cache_key = "text",
Expand All @@ -325,6 +338,7 @@ return {
route_id = "route_id",
service_id = "service_id",
consumer_id = "consumer_id",
run_on = "run_on",
created_at = "created_at",
enabled = "enabled",
cache_key = "cache_key",
Expand Down
1 change: 1 addition & 0 deletions kong/db/schema/entities/plugins.lua
Expand Up @@ -20,6 +20,7 @@ return {
{ service = { type = "foreign", reference = "services", default = null, on_delete = "cascade", }, },
{ consumer = { type = "foreign", reference = "consumers", default = null, on_delete = "cascade", }, },
{ config = { type = "record", abstract = true, }, },
{ run_on = typedefs.run_on },
{ enabled = { type = "boolean", default = true, }, },
},

Expand Down
3 changes: 0 additions & 3 deletions kong/db/schema/init.lua
Expand Up @@ -844,9 +844,6 @@ local function compatible_fields(f1, f2)
if t1 == "array" or t1 == "set" then
return f1.elements.type == f2.elements.type
end
if t1 == "array" or t1 == "set" then
return f1.elements.type == f2.elements.type
end
if t1 == "map" then
return f1.keys.type == f2.keys.type and f1.values.type == f2.values.type
end
Expand Down
13 changes: 13 additions & 0 deletions kong/db/schema/typedefs.lua
Expand Up @@ -252,6 +252,19 @@ typedefs.key = Schema.define {
}


typedefs.run_on = Schema.define {
type = "string",
default = "first",
one_of = { "first", "second", "all" },
}

typedefs.run_on_first = Schema.define {
type = "string",
default = "first",
one_of = { "first" },
}


setmetatable(typedefs, {
__index = function(_, k)
error("schema typedef error: definition " .. k .. " does not exist", 2)
Expand Down
1 change: 1 addition & 0 deletions kong/plugins/acl/schema.lua
Expand Up @@ -5,6 +5,7 @@ return {
name = "acl",
fields = {
{ consumer = typedefs.no_consumer },
{ run_on = typedefs.run_on_first },
{ config = {
type = "record",
fields = {
Expand Down
1 change: 1 addition & 0 deletions kong/plugins/aws-lambda/schema.lua
Expand Up @@ -17,6 +17,7 @@ local REGIONS = {
return {
name = "aws-lambda",
fields = {
{ run_on = typedefs.run_on_first },
{ config = {
type = "record",
fields = {
Expand Down
1 change: 1 addition & 0 deletions kong/plugins/basic-auth/schema.lua
Expand Up @@ -5,6 +5,7 @@ return {
name = "basic-auth",
fields = {
{ consumer = typedefs.no_consumer },
{ run_on = typedefs.run_on_first },
{ config = {
type = "record",
fields = {
Expand Down
1 change: 1 addition & 0 deletions kong/plugins/bot-detection/schema.lua
Expand Up @@ -4,6 +4,7 @@ return {
name = "bot-detection",
fields = {
{ consumer = typedefs.no_consumer },
{ run_on = typedefs.run_on_first },
{ config = {
type = "record",
fields = {
Expand Down
4 changes: 4 additions & 0 deletions kong/plugins/correlation-id/schema.lua
@@ -1,6 +1,10 @@
local typedefs = require "kong.db.schema.typedefs"


return {
name = "correlation-id",
fields = {
{ run_on = typedefs.run_on_first },
{ config = {
type = "record",
fields = {
Expand Down
1 change: 1 addition & 0 deletions kong/plugins/cors/schema.lua
Expand Up @@ -14,6 +14,7 @@ return {
name = "cors",
fields = {
{ consumer = typedefs.no_consumer },
{ run_on = typedefs.run_on_first },
{ config = {
type = "record",
fields = {
Expand Down
1 change: 1 addition & 0 deletions kong/plugins/hmac-auth/schema.lua
Expand Up @@ -13,6 +13,7 @@ return {
name = "hmac-auth",
fields = {
{ consumer = typedefs.no_consumer },
{ run_on = typedefs.run_on_first },
{ config = {
type = "record",
fields = {
Expand Down
2 changes: 2 additions & 0 deletions kong/plugins/ip-restriction/schema.lua
@@ -1,4 +1,5 @@
local iputils = require "resty.iputils"
local typedefs = require "kong.db.schema.typedefs"


local function validate_ip(ip)
Expand All @@ -17,6 +18,7 @@ local ip = { type = "string", custom_validator = validate_ip }
return {
name = "ip-restriction",
fields = {
{ run_on = typedefs.run_on_first },
{ config = {
type = "record",
fields = {
Expand Down
4 changes: 4 additions & 0 deletions kong/plugins/jwt/schema.lua
@@ -1,6 +1,10 @@
local typedefs = require "kong.db.schema.typedefs"


return {
name = "jwt",
fields = {
{ run_on = typedefs.run_on_first },
{ config = {
type = "record",
fields = {
Expand Down
1 change: 1 addition & 0 deletions kong/plugins/key-auth/schema.lua
Expand Up @@ -5,6 +5,7 @@ return {
name = "key-auth",
fields = {
{ consumer = typedefs.no_consumer },
{ run_on = typedefs.run_on_first },
{ config = {
type = "record",
fields = {
Expand Down
1 change: 1 addition & 0 deletions kong/plugins/ldap-auth/schema.lua
Expand Up @@ -7,6 +7,7 @@ return {
name = "ldap-auth",
fields = {
{ consumer = typedefs.no_consumer },
{ run_on = typedefs.run_on_first },
{ config = {
type = "record",
fields = {
Expand Down
1 change: 1 addition & 0 deletions kong/plugins/oauth2/schema.lua
Expand Up @@ -16,6 +16,7 @@ return {
name = "oauth2",
fields = {
{ consumer = typedefs.no_consumer },
{ run_on = typedefs.run_on_first },
{ config = {
type = "record",
fields = {
Expand Down
1 change: 1 addition & 0 deletions kong/plugins/rate-limiting/schema.lua
Expand Up @@ -26,6 +26,7 @@ end
return {
name = "rate-limiting",
fields = {
{ run_on = typedefs.run_on { one_of = { "first", "second" } } },
{ config = {
type = "record",
fields = {
Expand Down
4 changes: 4 additions & 0 deletions kong/plugins/request-size-limiting/schema.lua
@@ -1,6 +1,10 @@
local typedefs = require "kong.db.schema.typedefs"


return {
name = "request-size-limiting",
fields = {
{ run_on = typedefs.run_on_first },
{ config = {
type = "record",
fields = {
Expand Down
4 changes: 4 additions & 0 deletions kong/plugins/request-termination/schema.lua
@@ -1,3 +1,6 @@
local typedefs = require "kong.db.schema.typedefs"


local is_present = function(v)
return type(v) == "string" and #v > 0
end
Expand All @@ -6,6 +9,7 @@ end
return {
name = "request-termination",
fields = {
{ run_on = typedefs.run_on_first },
{ config = {
type = "record",
fields = {
Expand Down
1 change: 1 addition & 0 deletions kong/plugins/request-transformer/schema.lua
Expand Up @@ -38,6 +38,7 @@ local colon_strings_array_record = {
return {
name = "request-transformer",
fields = {
{ run_on = typedefs.run_on_first },
{ config = {
type = "record",
fields = {
Expand Down
1 change: 1 addition & 0 deletions kong/plugins/response-ratelimiting/schema.lua
Expand Up @@ -26,6 +26,7 @@ end
return {
name = "response-ratelimiting",
fields = {
{ run_on = typedefs.run_on { one_of = { "first", "second" } } },
{ config = {
type = "record",
fields = {
Expand Down
4 changes: 4 additions & 0 deletions kong/plugins/response-transformer/schema.lua
@@ -1,3 +1,6 @@
local typedefs = require "kong.db.schema.typedefs"


local string_array = {
type = "array",
default = {},
Expand Down Expand Up @@ -34,6 +37,7 @@ local colon_string_record = {
return {
name = "response-transformer",
fields = {
{ run_on = typedefs.run_on_first },
{ config = {
type = "record",
fields = {
Expand Down

0 comments on commit 237a068

Please sign in to comment.