-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathsyncer_k8s.lua
320 lines (272 loc) · 7.81 KB
/
syncer_k8s.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
local _M = {}
local http = require "http"
local json = require "cjson"
local log = ngx.log
local ERR = ngx.ERR
local WARN = ngx.WARN
local INFO = ngx.INFO
local ngx_time = ngx.time
local ngx_timer_at = ngx.timer.at
local ngx_worker_id = ngx.worker.id
local ngx_worker_exiting = ngx.worker.exiting
local ngx_sleep = ngx.sleep
local DEFAULT_HTTP_PORT = 8080
local DEFAULT_HTTP_WEIGHT = 100
local DEFAULT_CHECK_URL = "/health"
local DEFAULT_SLOW_START = 30
local PEER_STATUS_UP = "up"
local WATCH_TIMEOUT = 10000 -- by ms
local WATCH_LOCK_TIMEOUT = WATCH_TIMEOUT / 1000 + 2 -- by second
_M.data = {}
local function info(...)
log(INFO, "syncer_k8s: ", ...)
end
local function warn(...)
log(WARN, "syncer_k8s: ", ...)
end
local function errlog(...)
log(ERR, "syncer_k8s: ", ...)
end
local function peersEqual(ta, tb)
if not ta or not tb then
return false
end
if #ta ~= #tb then
return false
end
local comp = {}
for i, a in pairs(ta) do
comp[a.host .. ":" .. a.port] = true
end
for j, b in pairs(tb) do
if not comp[b.host .. ":" .. b.port] then
return false
end
comp[b.host .. ":" .. b.port] = nil
end
return 0 == #comp
end
local function getLock()
-- only the worker who get the lock can sync from k8s.
-- the lock keeps 10 seconds, little longer than http fetch timeout
if _M.lock == true then
return true
end
local ok, err = _M.conf.storage:add("lock", true, WATCH_LOCK_TIMEOUT)
if not ok then
if err == "exists" then
return nil
end
errlog("GET LOCK: failed to add key \"lock\": " .. err)
return nil
end
_M.lock = true
return true
end
local function refreshLock()
local ok, err = _M.conf.storage:set("lock", true, WATCH_LOCK_TIMEOUT)
if not ok then
if err == "exists" then
return nil
end
errlog("REFRESH LOCK: failed to set \"lock\"" .. err)
return nil
end
return true
end
local function releaseLock()
_M.conf.storage:delete("lock")
_M.lock = nil
return true
end
local function newPeers(sets)
if not sets or #sets == 0 or not sets[1].addresses then
return nil
end
local peers = {}
local port = DEFAULT_HTTP_PORT
for _, p in pairs(sets[1].ports) do
if p.name == "http" then
port = p.port
break
end
end
for _, addr in ipairs(sets[1].addresses) do
table.insert(peers, {
host = addr.ip,
port = port,
weight = DEFAULT_HTTP_WEIGHT,
status = PEER_STATUS_UP,
check_url = DEFAULT_CHECK_URL,
slow_start = DEFAULT_SLOW_START
})
end
return peers
end
local function saveList()
local allname = {}
for name, _ in pairs(_M.data) do
table.insert(allname, name)
end
_M.conf.storage:set("_allname", table.concat(allname, "|"))
return
end
local function updateService(data)
local name = data.object.metadata.name
local nver = tonumber(data.object.metadata.resourceVersion)
if _M.data[name] and _M.data[name].version == nver then
info("same version, skip update ", name)
return
end
local peers = newPeers(data.object.subsets)
if _M.data[name] and peersEqual(peers, _M.data[name].peers) then
info("same peers, skip update ", name)
return
end
_M.data[name] = {
version = nver,
peers = peers
}
-- update peers before version, in case of picker read version first,
-- may cause picker get new ver and old peer list
_M.conf.storage:set(name .. "|peers", json.encode(peers))
_M.conf.storage:set(name .. "|version", nver)
saveList()
errlog("update service: " .. name)
return
end
local function deleteService(data)
local name = data.object.metadata.name
_M.data[name] = nil
_M.conf.storage:delete(name .. "|version")
_M.conf.storage:delete(name .. "|peers")
saveList()
errlog("delete service: " .. name)
return
end
local function handleChunk(chunk)
local err
for line in chunk:gmatch("[^\n]+") do
local ok, data = pcall(json.decode, line)
-- the last line maybe not a full line
-- return the half line
if not ok and chunk:sub(-1) ~= "\n" then
return line, nil
end
if data.type == "ERROR" then
errlog("got error response: ", line)
err = "error message"
elseif data.type == "ADDED" or data.type == "MODIFIED" then
err = updateService(data)
elseif data.type == "DELETED" then
err = deleteService(data)
else
errlog("got unkown message: ", line)
err = "unknown message"
end
end
return "", err
end
local function watch(premature)
local conf = _M.conf
if premature or ngx_worker_exiting() then
releaseLock()
return
end
-- If we cannot acquire the lock, wait 1 second
if not getLock() then
errlog("waiting 1s for prev worker to exit...")
local ok, err = ngx_timer_at(1, watch)
if not ok then
errlog("cannot start k8s watcher: ", err)
end
return
end
refreshLock()
local client = http:new()
client:set_timeout(WATCH_TIMEOUT)
local ok, err = client:connect(conf.apiserver_host, conf.apiserver_port)
if not ok then
errlog("cannot connect to apiserver: ", err)
return
end
local session, err = client:ssl_handshake(nil, conf.apiserver_host .. ":" .. conf.apiserver_port, false)
local path = "/api/v1/namespaces/" .. conf.namespace .. "/endpoints/?watch=true"
local res, err = client:request({
path = path,
method="GET",
headers={ ["Authorization"] = "Bearer " .. conf.token },
ssl_verify = false
})
if err then
errlog("restart watcher because request fail: ", err)
ngx_timer_at(1, watch)
return
end
if res.status ~= 200 then
errlog("restart watcher because status not 200: ", err)
ngx_timer_at(1, watch)
return
end
local reader = res.body_reader
local buffer = ""
local chunk = ""
local err = nil
repeat
chunk, err = reader(8192)
if err then
if err ~= "timeout" then
errlog("cannot read from response: ", err)
end
break
end
if chunk then
buffer, err = handleChunk(buffer .. chunk)
if err then
errlog("cannot process chunked response: ", buffer .. chunk)
break
end
end
until not chunk
-- restart the timer for next loop
local ok, err = ngx_timer_at(0, watch)
if not ok then
errlog("cannot start k8s watcher: ", err)
end
return
end
function _M.init(conf)
-- Only one worker start the syncer, here will use worker_id == 0
if ngx_worker_id() ~= 0 then
return
end
-- Verify the config
if conf.apiserver == "" or
conf.namespace == "" or
conf.token == "" or
conf.storage == "" then
errlog("syncer_k8s init config invalid")
return
end
_M.conf = conf
local dict = _M.conf.storage
local allname = dict:get("_allname")
-- Load data from shm
if allname then
_M.data = {}
for name in allname:gmatch("[^|]+") do
upst = dict:get(name .. "|peers")
vers = dict:get(name .. "|version")
local ok, data = pcall(json.decode, upst)
if ok then
_M.data[name] = {version = vers, peers = data}
end
end
end
-- Start the k8s watcher
local ok, err = ngx_timer_at(0, watch)
if not ok then
errlog("cannot start k8s watcher: ", err)
end
end
return _M