-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
feat: support reading configuration form xds(mvp) #6614
Changes from 25 commits
079eefb
808bd6e
5ffd2bf
815074d
1ca0254
dd1315e
6fa5a6b
3123d81
ede18da
5b8726f
d4c8c19
ed3fa36
87e97c1
519a823
3345a31
a80954b
9a4cb1f
62190bc
82fbcdc
b0fa534
39cca64
add82f7
7465395
3123324
8a2a67d
f1fee3b
a9c9ba0
9b31b34
0166d0a
513c2b3
57c88d1
41abb0a
aee463f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,120 @@ | ||||||
-- | ||||||
-- Licensed to the Apache Software Foundation (ASF) under one or more | ||||||
-- contributor license agreements. See the NOTICE file distributed with | ||||||
-- this work for additional information regarding copyright ownership. | ||||||
-- The ASF licenses this file to You under the Apache License, Version 2.0 | ||||||
-- (the "License"); you may not use this file except in compliance with | ||||||
-- the License. You may obtain a copy of the License at | ||||||
-- | ||||||
-- http://www.apache.org/licenses/LICENSE-2.0 | ||||||
-- | ||||||
-- Unless required by applicable law or agreed to in writing, software | ||||||
-- distributed under the License is distributed on an "AS IS" BASIS, | ||||||
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||
-- See the License for the specific language governing permissions and | ||||||
-- limitations under the License. | ||||||
-- | ||||||
|
||||||
--- Get configuration form ngx.shared.DICT. | ||||||
-- | ||||||
-- @module core.config_xds | ||||||
|
||||||
local base = require("resty.core.base") | ||||||
local config_local = require("apisix.core.config_local") | ||||||
local table = table | ||||||
local error = error | ||||||
local is_http = ngx.config.subsystem == "http" | ||||||
local io = io | ||||||
local io_open = io.open | ||||||
local io_close = io.close | ||||||
local package = package | ||||||
local new_tab = base.new_tab | ||||||
local ffi = require ("ffi") | ||||||
local C = ffi.C | ||||||
local router_config = ngx.shared["router-config"] | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is not configuration for router, but for route. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated |
||||||
local ngx_re_match = ngx.re.match | ||||||
local ngx_re_gmatch = ngx.re.gmatch | ||||||
|
||||||
local xds_lib_name = "libxds.so" | ||||||
|
||||||
|
||||||
local process | ||||||
if is_http then | ||||||
process = require("ngx.process") | ||||||
end | ||||||
|
||||||
|
||||||
ffi.cdef[[ | ||||||
extern void initial(void* router_zone_ptr); | ||||||
]] | ||||||
|
||||||
|
||||||
local _M = { | ||||||
version = 0.1, | ||||||
local_conf = config_local.local_conf, | ||||||
} | ||||||
|
||||||
|
||||||
-- todo: refactor this function in chash.lua and radixtree.lua | ||||||
local function load_shared_lib(lib_name) | ||||||
local cpath = package.cpath | ||||||
local tried_paths = new_tab(32, 0) | ||||||
local i = 1 | ||||||
|
||||||
local iter, err = ngx_re_gmatch(cpath, "[^;]+", "jo") | ||||||
if not iter then | ||||||
error("failed to gmatch: " .. err) | ||||||
end | ||||||
|
||||||
while true do | ||||||
local it = iter() | ||||||
local fpath | ||||||
fpath, err = ngx_re_match(it[0], "(.*/)", "jo") | ||||||
if err then | ||||||
error("failed to match: " .. err) | ||||||
end | ||||||
local spath = fpath[0] .. lib_name | ||||||
|
||||||
local f = io_open(spath) | ||||||
if f ~= nil then | ||||||
io_close(f) | ||||||
return ffi.load(spath) | ||||||
end | ||||||
tried_paths[i] = spath | ||||||
i = i + 1 | ||||||
|
||||||
if not it then | ||||||
break | ||||||
end | ||||||
end | ||||||
|
||||||
return nil, tried_paths | ||||||
end | ||||||
|
||||||
|
||||||
local function load_libxds(lib_name) | ||||||
local xdsagent, tried_paths = load_shared_lib(lib_name) | ||||||
|
||||||
if not xdsagent then | ||||||
tried_paths[#tried_paths + 1] = 'tried above paths but can not load ' .. lib_name | ||||||
error("can not load Amesh library, tried paths: " .. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated |
||||||
table.concat(tried_paths, '\r\n', 1, #tried_paths)) | ||||||
end | ||||||
|
||||||
local router_zone = C.ngx_http_lua_ffi_shdict_udata_to_zone(router_config[1]) | ||||||
local router_shd_cdata = ffi.cast("void*", router_zone) | ||||||
xdsagent.initial(router_shd_cdata) | ||||||
end | ||||||
|
||||||
|
||||||
|
||||||
function _M.init_worker() | ||||||
if process.type() == "privileged agent" then | ||||||
load_libxds(xds_lib_name) | ||||||
end | ||||||
|
||||||
return true | ||||||
end | ||||||
|
||||||
|
||||||
return _M |
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -110,6 +110,11 @@ function _M.http_init_worker() | |||
end | ||||
require("apisix.balancer").init_worker() | ||||
load_balancer = require("apisix.balancer") | ||||
|
||||
if core.config == require("apisix.core.config_xds") then | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can call it like: Line 83 in 9d450d7
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated |
||||
core.config.init_worker() | ||||
end | ||||
|
||||
require("apisix.admin.init").init_worker() | ||||
|
||||
require("apisix.timers").init_worker() | ||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
# | ||
# Licensed to the Apache Software Foundation (ASF) under one or more | ||
# contributor license agreements. See the NOTICE file distributed with | ||
# this work for additional information regarding copyright ownership. | ||
# The ASF licenses this file to You under the Apache License, Version 2.0 | ||
# (the "License"); you may not use this file except in compliance with | ||
# the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
use t::APISIX 'no_plan'; | ||
|
||
use Cwd qw(cwd); | ||
my $apisix_home = $ENV{APISIX_HOME} || cwd(); | ||
|
||
repeat_each(1); | ||
no_long_string(); | ||
no_root_location(); | ||
log_level("info"); | ||
|
||
add_block_preprocessor(sub { | ||
my ($block) = @_; | ||
|
||
if (!$block->request) { | ||
$block->set_value("request", "GET /t"); | ||
} | ||
|
||
if (!$block->no_error_log) { | ||
$block->set_value("no_error_log", "[error]\n[alert]"); | ||
} | ||
|
||
my $lua_deps_path = $block->lua_deps_path // <<_EOC_; | ||
lua_package_path "$apisix_home/?.lua;$apisix_home/?/init.lua;$apisix_home/deps/share/lua/5.1/?/init.lua;$apisix_home/deps/share/lua/5.1/?.lua;$apisix_home/apisix/?.lua;$apisix_home/t/?.lua;;"; | ||
lua_package_cpath "$apisix_home/?.so;$apisix_home/t/xds-library/?.so;$apisix_home/deps/lib/lua/5.1/?.so;$apisix_home/deps/lib64/lua/5.1/?.so;;"; | ||
_EOC_ | ||
|
||
$block->set_value("lua_deps_path", $lua_deps_path); | ||
|
||
my $extra_init_by_lua = <<_EOC_; | ||
-- | ||
local config_xds = require("apisix.core.config_xds") | ||
|
||
local inject = function(mod, name) | ||
local old_f = mod[name] | ||
mod[name] = function (...) | ||
ngx.log(ngx.WARN, "config_xds run ", name) | ||
return { true } | ||
end | ||
end | ||
|
||
inject(config_xds, "new") | ||
|
||
_EOC_ | ||
|
||
$block->set_value("extra_init_by_lua", $extra_init_by_lua); | ||
}); | ||
|
||
run_tests; | ||
|
||
__DATA__ | ||
|
||
=== TEST 1: load Amesh library so successfully | ||
--- yaml_config | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's set the yaml_config in the file level? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated |
||
apisix: | ||
node_listen: 1984 | ||
config_center: xds | ||
enable_admin: false | ||
--- config | ||
location /t { | ||
content_by_lua_block { | ||
ngx.say("ok") | ||
} | ||
} | ||
--- no_error_log eval | ||
qr/can not load xDS library/ | ||
|
||
|
||
|
||
=== TEST 2: read data form shdict that wirted by Amesh library | ||
--- yaml_config | ||
apisix: | ||
node_listen: 1984 | ||
config_center: xds | ||
enable_admin: false | ||
--- config | ||
location /t { | ||
content_by_lua_block { | ||
-- wait for Amesh library sync data | ||
ngx.sleep(1.5) | ||
local core = require("apisix.core") | ||
local value = ngx.shared["router-config"]:get("/apisix/routes/1") | ||
local route_conf, err = core.json.decode(value) | ||
local json_encode = require("toolkit.json").encode | ||
ngx.say(json_encode(route_conf)) | ||
} | ||
} | ||
--- response_body | ||
{"create_time":1646972532,"id":"1","priority":0,"status":1,"update_time":1647250524,"upstream":{"hash_on":"vars","nodes":[{"host":"127.0.0.1","port":80,"priority":0,"weight":1}],"pass_host":"pass","scheme":"http","type":"roundrobin"},"uri":"/hello"} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
/* | ||
#cgo LDFLAGS: -shared | ||
#include <stdlib.h> | ||
|
||
extern void ngx_http_lua_ffi_shdict_store(void *zone, int op, | ||
const unsigned char *key, size_t key_len, | ||
int value_type, | ||
const unsigned char *str_value_buf, size_t str_value_len, | ||
double num_value, long exptime, int user_flags, char **errmsg, | ||
int *forcible); | ||
*/ | ||
import "C" | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
"unsafe" | ||
) | ||
|
||
func main() { | ||
} | ||
|
||
|
||
//export initial | ||
func initial(zone unsafe.Pointer) { | ||
time.Sleep(time.Second) | ||
value := fmt.Sprintf(`{ | ||
"status": 1, | ||
"update_time": 1647250524, | ||
"create_time": 1646972532, | ||
"uri": "/hello", | ||
"priority": 0, | ||
"id": "1", | ||
"upstream": { | ||
"nodes": [ | ||
{ | ||
"port": 80, | ||
"priority": 0, | ||
"host": "127.0.0.1", | ||
"weight": 1 | ||
} | ||
], | ||
"type": "roundrobin", | ||
"hash_on": "vars", | ||
"pass_host": "pass", | ||
"scheme": "http" | ||
} | ||
}`) | ||
|
||
write_route(zone, "/apisix/routes/1", value) | ||
} | ||
|
||
func write_route(zone unsafe.Pointer, key, value string) { | ||
var keyCStr = C.CString(key) | ||
defer C.free(unsafe.Pointer(keyCStr)) | ||
var keyLen = C.size_t(len(key)) | ||
|
||
var valueCStr = C.CString(value) | ||
defer C.free(unsafe.Pointer(valueCStr)) | ||
var valueLen = C.size_t(len(value)) | ||
|
||
errMsgBuf := make([]*C.char, 1) | ||
var forcible = 0 | ||
|
||
C.ngx_http_lua_ffi_shdict_store(zone, 0x0004, | ||
(*C.uchar)(unsafe.Pointer(keyCStr)), keyLen, | ||
4, | ||
(*C.uchar)(unsafe.Pointer(valueCStr)), valueLen, | ||
0, 0, 0, | ||
(**C.char)(unsafe.Pointer(&errMsgBuf[0])), | ||
(*C.int)(unsafe.Pointer(&forcible)), | ||
) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would that be better?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated