-
Notifications
You must be signed in to change notification settings - Fork 0
/
kong.lua
280 lines (225 loc) · 7.41 KB
/
kong.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
-- Kong, the biggest ape in town
--
-- /\ ____
-- <> ( oo )
-- <>_| ^^ |_
-- <> @ \
-- /~~\ . . _ |
-- /~~~~\ | |
-- /~~~~~~\/ _| |
-- |[][][]/ / [m]
-- |[][][[m]
-- |[][][]|
-- |[][][]|
-- |[][][]|
-- |[][][]|
-- |[][][]|
-- |[][][]|
-- |[][][]|
-- |[][][]|
-- |[|--|]|
-- |[| |]|
-- ========
-- ==========
-- |[[ ]]|
-- ==========
require("kong.core.globalpatches")()
local dns = require "kong.tools.dns"
local core = require "kong.core.handler"
local Serf = require "kong.serf"
local utils = require "kong.tools.utils"
local Events = require "kong.core.events"
local responses = require "kong.tools.responses"
local constants = require "kong.constants"
local singletons = require "kong.singletons"
local DAOFactory = require "kong.dao.factory"
local ngx_balancer = require "ngx.balancer"
local plugins_iterator = require "kong.core.plugins_iterator"
local balancer_execute = require("kong.core.balancer").execute
local ipairs = ipairs
local get_last_failure = ngx_balancer.get_last_failure
local set_current_peer = ngx_balancer.set_current_peer
local set_more_tries = ngx_balancer.set_more_tries
local function attach_hooks(events, hooks)
for k, v in pairs(hooks) do
events:subscribe(k, v)
end
end
local function load_plugins(kong_conf, dao, events)
local in_db_plugins, sorted_plugins = {}, {}
ngx.log(ngx.DEBUG, "Discovering used plugins")
local rows, err_t = dao.plugins:find_all()
if not rows then return nil, tostring(err_t) end
for _, row in ipairs(rows) do in_db_plugins[row.name] = true end
-- check all plugins in DB are enabled/installed
for plugin in pairs(in_db_plugins) do
if not kong_conf.plugins[plugin] then
return nil, plugin.." plugin is in use but not enabled"
end
end
-- load installed plugins
for plugin in pairs(kong_conf.plugins) do
local ok, handler = utils.load_module_if_exists("kong.plugins."..plugin..".handler")
if not ok then
return nil, plugin.." plugin is enabled but not installed;\n"..handler
end
local ok, schema = utils.load_module_if_exists("kong.plugins."..plugin..".schema")
if not ok then
return nil, "no configuration schema found for plugin: "..plugin
end
ngx.log(ngx.DEBUG, "Loading plugin: "..plugin)
sorted_plugins[#sorted_plugins+1] = {
name = plugin,
handler = handler(),
schema = schema
}
-- Attaching hooks
local ok, hooks = utils.load_module_if_exists("kong.plugins."..plugin..".hooks")
if ok then
attach_hooks(events, hooks)
end
end
-- sort plugins by order of execution
table.sort(sorted_plugins, function(a, b)
local priority_a = a.handler.PRIORITY or 0
local priority_b = b.handler.PRIORITY or 0
return priority_a > priority_b
end)
-- add reports plugin if not disabled
if kong_conf.anonymous_reports then
local reports = require "kong.core.reports"
reports.toggle(true)
sorted_plugins[#sorted_plugins+1] = {
name = "reports",
handler = reports
}
end
return sorted_plugins
end
-- Kong public context handlers.
-- @section kong_handlers
local Kong = {}
function Kong.init()
local pl_path = require "pl.path"
local conf_loader = require "kong.conf_loader"
-- retrieve kong_config
local conf_path = pl_path.join(ngx.config.prefix(), "kong.conf")
local config = assert(conf_loader(conf_path))
local events = Events() -- retrieve node plugins
local dao = assert(DAOFactory.new(config, events)) -- instanciate long-lived DAO
assert(dao:init())
assert(dao:run_migrations()) -- migrating in case embedded in custom nginx
-- populate singletons
singletons.dns = dns(config)
singletons.loaded_plugins = assert(load_plugins(config, dao, events))
singletons.serf = Serf.new(config, dao)
singletons.dao = dao
singletons.events = events
singletons.configuration = config
attach_hooks(events, require "kong.core.hooks")
assert(core.build_router())
end
function Kong.init_worker()
-- special math.randomseed from kong.core.globalpatches
-- not taking any argument. Must be called only once
-- and in the init_worker phase, to avoid duplicated
-- seeds.
math.randomseed()
-- init DAO
local ok, err = singletons.dao:init_worker()
if not ok then
ngx.log(ngx.CRIT, "could not init DB: ", err)
return
end
-- init inter-worker events
local worker_events = require "resty.worker.events"
local handler = function(data, event, source, pid)
if data and data.collection == "apis" then
assert(core.build_router())
elseif source and source == constants.CACHE.CLUSTER then
singletons.events:publish(event, data)
end
end
worker_events.register(handler)
local ok, err = worker_events.configure {
shm = "process_events", -- defined by "lua_shared_dict"
timeout = 5, -- life time of event data in shm
interval = 1, -- poll interval (seconds)
wait_interval = 0.010, -- wait before retry fetching event data
wait_max = 0.5, -- max wait time before discarding event
}
if not ok then
ngx.log(ngx.CRIT, "could not start inter-worker events: ", err)
return
end
core.init_worker.before()
-- run plugins init_worker context
for _, plugin in ipairs(singletons.loaded_plugins) do
plugin.handler:init_worker()
end
end
function Kong.ssl_certificate()
core.certificate.before()
for plugin, plugin_conf in plugins_iterator(singletons.loaded_plugins, true) do
plugin.handler:certificate(plugin_conf)
end
end
function Kong.balancer()
local addr = ngx.ctx.balancer_address
addr.tries = addr.tries + 1
if addr.tries > 1 then
-- only call balancer on retry, first one is done in `core.access.before` which runs
-- in the ACCESS context and hence has less limitations than this BALANCER context
-- where the retries are executed
-- record failure data
addr.failures = addr.failures or {}
local state, code = get_last_failure()
addr.failures[addr.tries-1] = { name = state, code = code }
local ok, err = balancer_execute(addr)
if not ok then
return responses.send_HTTP_INTERNAL_SERVER_ERROR("failed to retry the "..
"dns/balancer resolver for '"..addr.upstream.host..
"' with: "..tostring(err))
end
else
-- first try, so set the max number of retries
local retries = addr.retries
if retries > 0 then
set_more_tries(retries)
end
end
-- set the targets as resolved
local ok, err = set_current_peer(addr.ip, addr.port)
if not ok then
ngx.log(ngx.ERR, "failed to set the current peer (address:'",
tostring(addr.ip),"' port:",tostring(addr.port),"): ", tostring(err))
return responses.send_HTTP_INTERNAL_SERVER_ERROR()
end
end
function Kong.access()
core.access.before()
for plugin, plugin_conf in plugins_iterator(singletons.loaded_plugins, true) do
plugin.handler:access(plugin_conf)
end
core.access.after()
end
function Kong.header_filter()
core.header_filter.before()
for plugin, plugin_conf in plugins_iterator(singletons.loaded_plugins) do
plugin.handler:header_filter(plugin_conf)
end
core.header_filter.after()
end
function Kong.body_filter()
for plugin, plugin_conf in plugins_iterator(singletons.loaded_plugins) do
plugin.handler:body_filter(plugin_conf)
end
core.body_filter.after()
end
function Kong.log()
for plugin, plugin_conf in plugins_iterator(singletons.loaded_plugins) do
plugin.handler:log(plugin_conf)
end
core.log.after()
end
return Kong