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

feature: added serverless plugins. #86

Merged
merged 7 commits into from
Aug 26, 2019
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ For more detailed information, see the [White Paper](https://www.iresty.com/down
- **OpenTracing: [Zipkin](doc/plugins/zipkin.md)**
- **Monitoring and Metrics**: [Prometheus](doc/plugins/prometheus.md)
- **[gRPC transcoding](doc/plugins/grpc-transcoding.md)**:Supports protocol transcoding so that clients can access your gRPC API by using HTTP/JSON.
- Serverless: Invoke functions in each phase in APISIX.
- **Custom plugins**: Allows hooking of common phases, such as `rewrite`, `access`, `header filer`, `body filter` and `log`, also allows to hook the `balancer` stage.
- **Dashboard**: Built-in dashboard to control APISIX.
- **Version Control**: Supports rollbacks of operations.
Expand Down
1 change: 1 addition & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ APISIX 通过插件机制,提供动态负载平衡、身份验证、限流限
- **OpenTracing: [Zipkin](doc/plugins/zipkin.md)**
- **监控和指标**: [Prometheus](doc/plugins/prometheus-cn.md)
- **[gRPC 协议转换](doc/plugins/grpc-transcoding-cn.md)**:支持协议的转换,这样客户端可以通过 HTTP/JSON 来访问你的 gRPC API。
- Serverless: 在 APISIX 的每一个阶段,你都可以添加并调用自己编写的函数。
- **自定义插件**: 允许挂载常见阶段,例如`rewrite`,`access`,`header filer`,`body filter`和`log`,还允许挂载 `balancer` 阶段。
- **控制台**: 内置控制台来操作 APISIX 集群。
- **版本控制**:支持操作的多次回滚。
Expand Down
2 changes: 2 additions & 0 deletions conf/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@ plugins: # plugin list
- zipkin
- ip-restriction
- grpc-transcode
- serverless-pre-function
- serverless-post-function
1 change: 1 addition & 0 deletions lua/apisix/plugins/serverless-post-function.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
return require("apisix.plugins.serverless")("serverless-post-function", -2000)
1 change: 1 addition & 0 deletions lua/apisix/plugins/serverless-pre-function.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
return require("apisix.plugins.serverless")("serverless-pre-function", 10000)
97 changes: 97 additions & 0 deletions lua/apisix/plugins/serverless.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
local ipairs = ipairs
local pcall = pcall
local loadstring = loadstring
local require = require

return function(plugin_name, priority)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like this style, how about to use new method to create new instance?

local core = require("apisix.core")

local schema = {
type = "object",
properties = {
phase = {
type = "string",
-- the default phase is access
enum = {"rewrite", "access", "header_filer", "body_filter",
"log", "balancer"}
},
functions = {
type = "array",
items = {type = "string"},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing check to functions, it should be a valid Lua code.

minItems = 1
},
},
required = {"functions"}
}

local _M = {
version = 0.1,
priority = priority,
name = plugin_name,
}

local function load_funcs(functions)
local funcs = core.table.new(#functions, 0)

local index = 1
for _, func_str in ipairs(functions) do
local _, func = pcall(loadstring(func_str))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it maybe failed, we need to capture the error message.

funcs[index] = func
index = index + 1
end

return funcs
end

local function call_funcs(phase, conf, ctx)
if phase ~= conf.phase then
return
end

local functions = core.lrucache.plugin_ctx(plugin_name, ctx,
load_funcs, conf.functions)

for _, func in ipairs(functions) do
func()
end
end

function _M.check_schema(conf)
local ok, err = core.schema.check(schema, conf)
if not ok then
return false, err
end

if not conf.phase then
conf.phase = 'access'
end

return true
end

function _M.rewrite(conf, ctx)
call_funcs('rewrite', conf, ctx)
end

function _M.access(conf, ctx)
call_funcs('access', conf, ctx)
end

function _M.balancer(conf, ctx)
call_funcs('balancer', conf, ctx)
end

function _M.header_filer(conf, ctx)
call_funcs('header_filer', conf, ctx)
end

function _M.body_filter(conf, ctx)
call_funcs('body_filter', conf, ctx)
end

function _M.log(conf, ctx)
call_funcs('log', conf, ctx)
end

return _M
end
2 changes: 1 addition & 1 deletion lua/apisix/plugins/zipkin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ local schema = {

local _M = {
version = 0.1,
priority = -1000, -- last running plugin
priority = -1000, -- last running plugin, but before serverless post func
name = plugin_name,
schema = schema,
}
Expand Down
2 changes: 1 addition & 1 deletion t/admin/plugins.t
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ __DATA__
--- request
GET /apisix/admin/plugins/list
--- response_body_like eval
qr/\["limit-req","limit-count","limit-conn","key-auth","prometheus","node-status","jwt-auth","zipkin","ip-restriction","grpc-transcode"\]/
qr/\["limit-req","limit-count","limit-conn","key-auth","prometheus","node-status","jwt-auth","zipkin","ip-restriction","grpc-transcode","serverless-pre-function","serverless-post-function"\]/
--- no_error_log
[error]
2 changes: 2 additions & 0 deletions t/debug-mode.t
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ done
--- grep_error_log eval
qr/loaded plugin and sort by priority: [-\d]+ name: [\w-]+/
--- grep_error_log_out
loaded plugin and sort by priority: 10000 name: serverless-pre-function
loaded plugin and sort by priority: 3000 name: ip-restriction
loaded plugin and sort by priority: 2510 name: jwt-auth
loaded plugin and sort by priority: 2500 name: key-auth
Expand All @@ -50,6 +51,7 @@ loaded plugin and sort by priority: 506 name: grpc-transcode
loaded plugin and sort by priority: 500 name: prometheus
loaded plugin and sort by priority: 0 name: example-plugin
loaded plugin and sort by priority: -1000 name: zipkin
loaded plugin and sort by priority: -2000 name: serverless-post-function



Expand Down
Loading