Skip to content

Commit

Permalink
Adding cache API - CLI Rewrite - Serf integration/Clustering - Hooks/…
Browse files Browse the repository at this point in the history
…Events - Invalidations
  • Loading branch information
subnetmarco committed Nov 25, 2015
1 parent f4b9be2 commit 2193fe4
Show file tree
Hide file tree
Showing 117 changed files with 4,260 additions and 983 deletions.
2 changes: 1 addition & 1 deletion .ci/setup_kong.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

KONG_VERSION=0.5.0
KONG_VERSION=0.5.2

sudo apt-get update

Expand Down
2 changes: 1 addition & 1 deletion .luacheckrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
redefined = false
unused_args = false
globals = {"ngx", "dao", "app", "configuration"}
globals = {"ngx", "dao", "app", "configuration", "events", "build"}

files["kong/"] = {
std = "luajit"
Expand Down
25 changes: 13 additions & 12 deletions bin/kong
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,20 @@
-- This script is not parsed by lapp due to limitations of the said framework as it
-- is currently implemented.

local cutils = require "kong.cli.utils"
local infos = cutils.get_kong_infos()
local luarocks = require "kong.cli.utils.luarocks"
local infos = luarocks.get_kong_infos()
local commands = {
db = "kong.cli.db",
stop = "kong.cli.stop",
quit = "kong.cli.quit",
start = "kong.cli.start",
reload = "kong.cli.reload",
config = "kong.cli.config",
restart = "kong.cli.restart",
version = "kong.cli.version",
["--version"] = "kong.cli.version",
migrations = "kong.cli.migrations"
db = "kong.cli.cmds.db",
stop = "kong.cli.cmds.stop",
quit = "kong.cli.cmds.quit",
start = "kong.cli.cmds.start",
reload = "kong.cli.cmds.reload",
config = "kong.cli.cmds.config",
restart = "kong.cli.cmds.restart",
version = "kong.cli.cmds.version",
["--version"] = "kong.cli.cmds.version",
migrations = "kong.cli.cmds.migrations",
cluster = "kong.cli.cmds.cluster"
}

local help_message = string.format([[
Expand Down
52 changes: 32 additions & 20 deletions kong-0.5.3-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ dependencies = {
"ansicolors ~> 1.0.2-3",
"lbase64 ~> 20120820-1",
"lua-resty-iputils ~> 0.2.0-1",
"mediator_lua ~> 1.1.2-0",

"luasocket ~> 2.0.2-6",
"lrexlib-pcre ~> 2.7.2-1",
Expand All @@ -45,20 +46,26 @@ build = {

["kong.constants"] = "kong/constants.lua",

["kong.cli.utils"] = "kong/cli/utils/utils.lua",
["kong.cli.utils.dnsmasq"] = "kong/cli/utils/dnsmasq.lua",
["kong.cli.utils.logger"] = "kong/cli/utils/logger.lua",
["kong.cli.utils.luarocks"] = "kong/cli/utils/luarocks.lua",
["kong.cli.utils.ssl"] = "kong/cli/utils/ssl.lua",
["kong.cli.utils.signal"] = "kong/cli/utils/signal.lua",
["kong.cli.utils.input"] = "kong/cli/utils/input.lua",
["kong.cli.db"] = "kong/cli/db.lua",
["kong.cli.config"] = "kong/cli/config.lua",
["kong.cli.quit"] = "kong/cli/quit.lua",
["kong.cli.stop"] = "kong/cli/stop.lua",
["kong.cli.start"] = "kong/cli/start.lua",
["kong.cli.reload"] = "kong/cli/reload.lua",
["kong.cli.restart"] = "kong/cli/restart.lua",
["kong.cli.version"] = "kong/cli/version.lua",
["kong.cli.migrations"] = "kong/cli/migrations.lua",
["kong.cli.utils.services"] = "kong/cli/utils/services.lua",
["kong.cli.utils.configuration"] = "kong/cli/utils/configuration.lua",
["kong.cli.cmds.db"] = "kong/cli/cmds/db.lua",
["kong.cli.cmds.config"] = "kong/cli/cmds/config.lua",
["kong.cli.cmds.quit"] = "kong/cli/cmds/quit.lua",
["kong.cli.cmds.stop"] = "kong/cli/cmds/stop.lua",
["kong.cli.cmds.start"] = "kong/cli/cmds/start.lua",
["kong.cli.cmds.reload"] = "kong/cli/cmds/reload.lua",
["kong.cli.cmds.restart"] = "kong/cli/cmds/restart.lua",
["kong.cli.cmds.version"] = "kong/cli/cmds/version.lua",
["kong.cli.cmds.migrations"] = "kong/cli/cmds/migrations.lua",
["kong.cli.cmds.cluster"] = "kong/cli/cmds/cluster.lua",
["kong.cli.services.base_service"] = "kong/cli/services/base_service.lua",
["kong.cli.services.dnsmasq"] = "kong/cli/services/dnsmasq.lua",
["kong.cli.services.serf"] = "kong/cli/services/serf.lua",
["kong.cli.services.nginx"] = "kong/cli/services/nginx.lua",

["kong.tools.io"] = "kong/tools/io.lua",
["kong.tools.utils"] = "kong/tools/utils.lua",
Expand All @@ -79,19 +86,23 @@ build = {
["kong.core.certificate"] = "kong/core/certificate.lua",
["kong.core.resolver"] = "kong/core/resolver.lua",
["kong.core.plugins_iterator"] = "kong/core/plugins_iterator.lua",
["kong.core.hooks"] = "kong/core/hooks.lua",
["kong.core.reports"] = "kong/core/reports.lua",
["kong.core.events"] = "kong/core/events.lua",

["kong.dao.cassandra.schema.migrations"] = "kong/dao/cassandra/schema/migrations.lua",
["kong.dao.error"] = "kong/dao/error.lua",
["kong.dao.schemas_validation"] = "kong/dao/schemas_validation.lua",
["kong.dao.schemas.apis"] = "kong/dao/schemas/apis.lua",
["kong.dao.schemas.nodes"] = "kong/dao/schemas/nodes.lua",
["kong.dao.schemas.consumers"] = "kong/dao/schemas/consumers.lua",
["kong.dao.schemas.plugins"] = "kong/dao/schemas/plugins.lua",
["kong.dao.cassandra.factory"] = "kong/dao/cassandra/factory.lua",
["kong.dao.cassandra.query_builder"] = "kong/dao/cassandra/query_builder.lua",
["kong.dao.cassandra.base_dao"] = "kong/dao/cassandra/base_dao.lua",
["kong.dao.cassandra.migrations"] = "kong/dao/cassandra/migrations.lua",
["kong.dao.cassandra.apis"] = "kong/dao/cassandra/apis.lua",
["kong.dao.cassandra.nodes"] = "kong/dao/cassandra/nodes.lua",
["kong.dao.cassandra.consumers"] = "kong/dao/cassandra/consumers.lua",
["kong.dao.cassandra.plugins"] = "kong/dao/cassandra/plugins.lua",

Expand All @@ -102,19 +113,22 @@ build = {
["kong.plugins.basic-auth.handler"] = "kong/plugins/basic-auth/handler.lua",
["kong.plugins.basic-auth.access"] = "kong/plugins/basic-auth/access.lua",
["kong.plugins.basic-auth.schema"] = "kong/plugins/basic-auth/schema.lua",
["kong.plugins.basic-auth.hooks"] = "kong/plugins/basic-auth/hooks.lua",
["kong.plugins.basic-auth.api"] = "kong/plugins/basic-auth/api.lua",
["kong.plugins.basic-auth.daos"] = "kong/plugins/basic-auth/daos.lua",

["kong.plugins.key-auth.migrations.cassandra"] = "kong/plugins/key-auth/migrations/cassandra.lua",
["kong.plugins.key-auth.handler"] = "kong/plugins/key-auth/handler.lua",
["kong.plugins.key-auth.access"] = "kong/plugins/key-auth/access.lua",
["kong.plugins.key-auth.hooks"] = "kong/plugins/key-auth/hooks.lua",
["kong.plugins.key-auth.schema"] = "kong/plugins/key-auth/schema.lua",
["kong.plugins.key-auth.api"] = "kong/plugins/key-auth/api.lua",
["kong.plugins.key-auth.daos"] = "kong/plugins/key-auth/daos.lua",

["kong.plugins.oauth2.migrations.cassandra"] = "kong/plugins/oauth2/migrations/cassandra.lua",
["kong.plugins.oauth2.handler"] = "kong/plugins/oauth2/handler.lua",
["kong.plugins.oauth2.access"] = "kong/plugins/oauth2/access.lua",
["kong.plugins.oauth2.hooks"] = "kong/plugins/oauth2/hooks.lua",
["kong.plugins.oauth2.schema"] = "kong/plugins/oauth2/schema.lua",
["kong.plugins.oauth2.daos"] = "kong/plugins/oauth2/daos.lua",
["kong.plugins.oauth2.api"] = "kong/plugins/oauth2/api.lua",
Expand Down Expand Up @@ -177,6 +191,7 @@ build = {
["kong.plugins.ssl.handler"] = "kong/plugins/ssl/handler.lua",
["kong.plugins.ssl.certificate"] = "kong/plugins/ssl/certificate.lua",
["kong.plugins.ssl.access"] = "kong/plugins/ssl/access.lua",
["kong.plugins.ssl.hooks"] = "kong/plugins/ssl/hooks.lua",
["kong.plugins.ssl.ssl_util"] = "kong/plugins/ssl/ssl_util.lua",
["kong.plugins.ssl.schema"] = "kong/plugins/ssl/schema.lua",

Expand All @@ -189,13 +204,7 @@ build = {
["kong.plugins.acl.handler"] = "kong/plugins/acl/handler.lua",
["kong.plugins.acl.access"] = "kong/plugins/acl/access.lua",
["kong.plugins.acl.schema"] = "kong/plugins/acl/schema.lua",
["kong.plugins.acl.api"] = "kong/plugins/acl/api.lua",
["kong.plugins.acl.daos"] = "kong/plugins/acl/daos.lua",

["kong.plugins.acl.migrations.cassandra"] = "kong/plugins/acl/migrations/cassandra.lua",
["kong.plugins.acl.handler"] = "kong/plugins/acl/handler.lua",
["kong.plugins.acl.access"] = "kong/plugins/acl/access.lua",
["kong.plugins.acl.schema"] = "kong/plugins/acl/schema.lua",
["kong.plugins.acl.hooks"] = "kong/plugins/acl/hooks.lua",
["kong.plugins.acl.api"] = "kong/plugins/acl/api.lua",
["kong.plugins.acl.daos"] = "kong/plugins/acl/daos.lua",

Expand All @@ -206,12 +215,14 @@ build = {
["kong.api.routes.apis"] = "kong/api/routes/apis.lua",
["kong.api.routes.consumers"] = "kong/api/routes/consumers.lua",
["kong.api.routes.plugins"] = "kong/api/routes/plugins.lua",
["kong.api.routes.plugins"] = "kong/api/routes/plugins.lua",
["kong.api.routes.cache"] = "kong/api/routes/cache.lua",
["kong.api.routes.cluster"] = "kong/api/routes/cluster.lua",

["kong.plugins.jwt.migrations.cassandra"] = "kong/plugins/jwt/migrations/cassandra.lua",
["kong.plugins.jwt.handler"] = "kong/plugins/jwt/handler.lua",
["kong.plugins.jwt.access"] = "kong/plugins/jwt/access.lua",
["kong.plugins.jwt.schema"] = "kong/plugins/jwt/schema.lua",
["kong.plugins.jwt.hooks"] = "kong/plugins/jwt/hooks.lua",
["kong.plugins.jwt.api"] = "kong/plugins/jwt/api.lua",
["kong.plugins.jwt.daos"] = "kong/plugins/jwt/daos.lua",
["kong.plugins.jwt.jwt_parser"] = "kong/plugins/jwt/jwt_parser.lua",
Expand All @@ -220,6 +231,7 @@ build = {
["kong.plugins.hmac-auth.handler"] = "kong/plugins/hmac-auth/handler.lua",
["kong.plugins.hmac-auth.access"] = "kong/plugins/hmac-auth/access.lua",
["kong.plugins.hmac-auth.schema"] = "kong/plugins/hmac-auth/schema.lua",
["kong.plugins.hmac-auth.hooks"] = "kong/plugins/hmac-auth/hooks.lua",
["kong.plugins.hmac-auth.api"] = "kong/plugins/hmac-auth/api.lua",
["kong.plugins.hmac-auth.daos"] = "kong/plugins/hmac-auth/daos.lua",

Expand Down
60 changes: 54 additions & 6 deletions kong.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,60 @@
## manage your Kong infrastructure. It needs to be secured appropriatly.
# admin_api_port: 8001

######
## Port on which Kong will start dnsmasq.
# dnsmasq_port: 8053

######
## Cluster settings
# cluster:

######
## Instructs this node to auto-join the nodes that are sharing the same datastore on startup.
# auto-join: true

######
## Kong by default is configured to run in a LAN or Local Area Network. However, there are cases
## in which a user may want to use Kong over the Internet or (WAN), or even just locally.
## To support setting the correct configuration values for each environment, you can select a
## timing profile. The current choices are "lan", "wan", and "local". This defaults to "lan".
## If a "lan" or "local" profile is used over the Internet, or a "local" profile over the LAN,
## a high rate of false failures is risked, as the timing constrains are too tight.
# profile: "lan"

######
## Address to bind network listeners to.
# bind: "0.0.0.0:7946"

######
## Network interface to bind to. Can be used instead of bind if the interface is known but
## not the address. If both are provided, then Kong verifies that the interface has the bind
## address that is provided.
# iface: ""

######
## Address to bind the RPC listener.
# rpc-addr: "127.0.0.1:7373"

######
## The advertise flag is used to change the address that we advertise to other nodes in the
## cluster. By default, the bind address is advertised. However, in some cases (specifically
## NAT traversal), there may be a routable address that cannot be bound to. This flag enables
## gossiping a different address to support this
# advertise: ""

######
## Key for encrypting network traffic within Kong. Must be a base64-encoded 16-byte key.
# encrypt: "foo"

######
## You can optionally tag the current node with an arbitrary number of tags, in the form
## of key-value pairs.
# tags:
# datacenter: "DC-US"
# role: "api-gateway"
# custom: "value"

######
## Specify which database to use from the databases_available property.
# database: cassandra
Expand Down Expand Up @@ -114,12 +168,6 @@
# user: cassandra
# password: cassandra

######
## Time (in seconds) for which entities from the database (APIs, plugins configurations...)
## are cached by Kong. Increase this value if you want to lower the number of requests made
## to your database.
# database_cache_expiration: 5

######
## SSL certificates to use.
# ssl_cert_path: /path/to/certificate.pem
Expand Down
2 changes: 1 addition & 1 deletion kong/api/app.lua
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ local function attach_routes(routes)
end

-- Load core routes
for _, v in ipairs({"kong", "apis", "consumers", "plugins"}) do
for _, v in ipairs({"kong", "apis", "consumers", "plugins", "cache", "cluster" }) do
local routes = require("kong.api.routes."..v)
attach_routes(routes)
end
Expand Down
2 changes: 1 addition & 1 deletion kong/api/crud_helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,4 @@ function _M.delete(where_t, dao_collection)
end
end

return _M
return _M
8 changes: 0 additions & 8 deletions kong/api/route_helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,6 @@ local stringy = require "stringy"

local _M = {}

function _M.get_hostname()
local f = io.popen ("/bin/hostname")
local hostname = f:read("*a") or ""
f:close()
hostname = string.gsub(hostname, "\n$", "")
return hostname
end

function _M.parse_status(value)
local result = {}
local parts = stringy.split(value, "\n")
Expand Down
33 changes: 33 additions & 0 deletions kong/api/routes/cache.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
local responses = require "kong.tools.responses"
local cache = require "kong.tools.database_cache"

return {
["/cache/"] = {
DELETE = function(self, dao_factory)
cache.delete_all()
return responses.send_HTTP_OK()
end
},

["/cache/:key"] = {
GET = function(self, dao_factory)
if self.params.key then
local cached_item = cache.get(self.params.key)
if cached_item then
return responses.send_HTTP_OK(cached_item)
end
end

return responses.send_HTTP_NOT_FOUND()
end,

DELETE = function(self, dao_factory)
if self.params.key then
cache.delete(self.params.key)
return responses.send_HTTP_OK()
else
return responses.send_HTTP_NOT_FOUND()
end
end
}
}
52 changes: 52 additions & 0 deletions kong/api/routes/cluster.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
local crud = require "kong.api.crud_helpers"
local responses = require "kong.tools.responses"

return {
["/cluster/"] = {
GET = function(self, dao_factory)
crud.paginated_set(self, dao_factory.nodes)
end,

POST = function(self, dao_factory)
local serf = require("kong.cli.services.serf")(configuration)
if self.params.address then
local _, err = serf:invoke_signal("join", { self.params.address })
if err then
return responses.send_HTTP_BAD_REQUEST(err)
else
return responses.send_HTTP_OK()
end
else
return responses.send_HTTP_BAD_REQUEST("Missing \"address\"")
end
end
},
["/cluster/events/"] = {
POST = function(self, dao_factory)
ngx.log(ngx.DEBUG, " received cluster event "..(self.params.type and self.params.type or "UNKNOWN"))

local message_t = self.params

-- The type is always upper case
if message_t.type then
message_t.type = string.upper(message_t.type)
end

-- If it's an update, load the new entity too so it's available in the hooks
if message_t.type == events.TYPES.ENTITY_UPDATED then
message_t.old_entity = message_t.entity
message_t.entity = dao[message_t.collection]:find_by_primary_key({id=message_t.old_entity.id})
if not message_t.entity then
-- This means that the entity has been deleted immediately after an update in the meanwhile that
-- the system was still processing the update. A delete invalidation will come immediately after
-- so we can ignore this event
return responses.send_HTTP_OK()
end
end

-- Trigger event in the node
events:publish(message_t.type, message_t)
return responses.send_HTTP_OK()
end
}
}
3 changes: 2 additions & 1 deletion kong/api/routes/kong.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local constants = require "kong.constants"
local route_helpers = require "kong.api.route_helpers"
local utils = require "kong.tools.utils"

return {
["/"] = {
Expand All @@ -12,7 +13,7 @@ return {
return helpers.responses.send_HTTP_OK({
tagline = "Welcome to Kong",
version = constants.VERSION,
hostname = route_helpers.get_hostname(),
hostname = utils.get_hostname(),
plugins = {
available_on_server = configuration.plugins_available,
enabled_in_cluster = db_plugins
Expand Down
Loading

0 comments on commit 2193fe4

Please sign in to comment.