feat(apisix): add Cloudron package
- Implements Apache APISIX packaging for Cloudron platform. - Includes Dockerfile, CloudronManifest.json, and start.sh. - Configured to use Cloudron's etcd addon. 🤖 Generated with Gemini CLI Co-Authored-By: Gemini <noreply@google.com>
This commit is contained in:
129
CloudronPackages/APISIX/apisix-source/t/xds-library/config_xds.t
Normal file
129
CloudronPackages/APISIX/apisix-source/t/xds-library/config_xds.t
Normal file
@@ -0,0 +1,129 @@
|
||||
#
|
||||
# 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);
|
||||
|
||||
if (!$block->yaml_config) {
|
||||
my $yaml_config = <<_EOC_;
|
||||
apisix:
|
||||
node_listen: 1984
|
||||
deployment:
|
||||
role: data_plane
|
||||
role_data_plane:
|
||||
config_provider: xds
|
||||
_EOC_
|
||||
|
||||
$block->set_value("yaml_config", $yaml_config);
|
||||
}
|
||||
});
|
||||
|
||||
run_tests;
|
||||
|
||||
__DATA__
|
||||
|
||||
=== TEST 1: load xDS library successfully
|
||||
--- 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 xDS library
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
-- wait for xds library sync data
|
||||
ngx.sleep(1.5)
|
||||
local core = require("apisix.core")
|
||||
local value = ngx.shared["xds-config"]:get("/routes/1")
|
||||
local route_conf, err = core.json.decode(value)
|
||||
local json_encode = require("toolkit.json").encode
|
||||
ngx.say(json_encode(route_conf.uri))
|
||||
}
|
||||
}
|
||||
--- response_body
|
||||
"/hello"
|
||||
|
||||
|
||||
|
||||
=== TEST 3: read conf version
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local core = require("apisix.core")
|
||||
local version
|
||||
for i = 1, 5 do
|
||||
version = ngx.shared["xds-config-version"]:get("version")
|
||||
if version then
|
||||
ngx.say(version)
|
||||
break
|
||||
end
|
||||
-- wait for xds library sync data
|
||||
ngx.sleep(1.5)
|
||||
end
|
||||
}
|
||||
}
|
||||
--- response_body eval
|
||||
qr/^\d{13}$/
|
@@ -0,0 +1,239 @@
|
||||
#
|
||||
# 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 ((!defined $block->error_log) && (!defined $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);
|
||||
|
||||
if (!$block->yaml_config) {
|
||||
my $yaml_config = <<_EOC_;
|
||||
apisix:
|
||||
node_listen: 1984
|
||||
deployment:
|
||||
role: data_plane
|
||||
role_data_plane:
|
||||
config_provider: xds
|
||||
_EOC_
|
||||
|
||||
$block->set_value("yaml_config", $yaml_config);
|
||||
}
|
||||
});
|
||||
|
||||
run_tests;
|
||||
|
||||
__DATA__
|
||||
|
||||
=== TEST 1: proxy request using data written by xds(id = 1)
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local http = require "resty.http"
|
||||
local httpc = http.new()
|
||||
local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello"
|
||||
local res, err = httpc:request_uri(uri, { method = "GET"})
|
||||
|
||||
if not res then
|
||||
ngx.say(err)
|
||||
return
|
||||
end
|
||||
ngx.print(res.body)
|
||||
}
|
||||
}
|
||||
--- response_body
|
||||
hello world
|
||||
|
||||
|
||||
|
||||
=== TEST 2: proxy request using data written by xds(id = 2, upstream_id = 1)
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local http = require "resty.http"
|
||||
local httpc = http.new()
|
||||
local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello1"
|
||||
local res, err = httpc:request_uri(uri, { method = "GET"})
|
||||
|
||||
if not res then
|
||||
ngx.say(err)
|
||||
return
|
||||
end
|
||||
ngx.print(res.body)
|
||||
}
|
||||
}
|
||||
--- response_body
|
||||
hello1 world
|
||||
|
||||
|
||||
|
||||
=== TEST 3: proxy request using data written by xds(id = 3, upstream_id = 2)
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
ngx.sleep(1.5)
|
||||
local core = require("apisix.core")
|
||||
local value = ngx.shared["xds-config"]:flush_all()
|
||||
ngx.sleep(1.5)
|
||||
local http = require "resty.http"
|
||||
local httpc = http.new()
|
||||
local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello"
|
||||
local res, err = httpc:request_uri(uri, { method = "GET"})
|
||||
|
||||
if not res then
|
||||
ngx.say(err)
|
||||
return
|
||||
end
|
||||
ngx.print(res.body)
|
||||
}
|
||||
}
|
||||
--- response_body
|
||||
hello world
|
||||
|
||||
|
||||
|
||||
=== TEST 4: flush all keys in xds config
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local core = require("apisix.core")
|
||||
ngx.shared["xds-config"]:flush_all()
|
||||
ngx.update_time()
|
||||
ngx.shared["xds-config-version"]:set("version", ngx.now())
|
||||
ngx.sleep(1.5)
|
||||
|
||||
local http = require "resty.http"
|
||||
local httpc = http.new()
|
||||
local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello"
|
||||
local res, err = httpc:request_uri(uri, { method = "GET"})
|
||||
|
||||
if not res then
|
||||
ngx.say(err)
|
||||
return
|
||||
end
|
||||
ngx.status = res.status
|
||||
ngx.print(res.body)
|
||||
}
|
||||
}
|
||||
--- error_code: 404
|
||||
--- response_body
|
||||
{"error_msg":"404 Route Not Found"}
|
||||
|
||||
|
||||
|
||||
=== TEST 5: bad format json
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local data = [[{
|
||||
upstream = {
|
||||
type = "roundrobin"
|
||||
nodes = {
|
||||
["127.0.0.1:1980"] = 1,
|
||||
}
|
||||
},
|
||||
uri = "/bad_json"
|
||||
}]]
|
||||
ngx.shared["xds-config"]:set("/routes/3", data)
|
||||
ngx.update_time()
|
||||
ngx.shared["xds-config-version"]:set("version", ngx.now())
|
||||
ngx.sleep(1.5)
|
||||
|
||||
local http = require "resty.http"
|
||||
local httpc = http.new()
|
||||
local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/bad_json"
|
||||
local res, err = httpc:request_uri(uri, { method = "GET"})
|
||||
|
||||
if not res then
|
||||
ngx.say(err)
|
||||
return
|
||||
end
|
||||
ngx.status = res.status
|
||||
}
|
||||
}
|
||||
--- wait: 2
|
||||
--- error_code: 404
|
||||
--- error_log
|
||||
decode the conf of [/routes/3] failed, err: Expected object key string but found invalid token
|
||||
|
||||
|
||||
|
||||
=== TEST 6: schema check fail
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local core = require("apisix.core")
|
||||
local data = {
|
||||
upstream = {
|
||||
type = "roundrobin",
|
||||
nodes = {
|
||||
["127.0.0.1:65536"] = 1,
|
||||
}
|
||||
}
|
||||
}
|
||||
local data_str = core.json.encode(data)
|
||||
ngx.shared["xds-config"]:set("/routes/3", data_str)
|
||||
ngx.update_time()
|
||||
ngx.shared["xds-config-version"]:set("version", ngx.now())
|
||||
ngx.sleep(1.5)
|
||||
}
|
||||
}
|
||||
--- no_error_log
|
||||
[alert]
|
||||
-- wait: 2
|
||||
--- error_log
|
||||
failed to check the conf of [/routes/3] err:allOf 1 failed: value should match only one schema, but matches none
|
||||
|
||||
|
||||
|
||||
=== TEST 7: not table
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local data = "/not_table"
|
||||
ngx.shared["xds-config"]:set("/routes/3", data)
|
||||
ngx.update_time()
|
||||
ngx.shared["xds-config-version"]:set("version", ngx.now())
|
||||
ngx.sleep(1.5)
|
||||
}
|
||||
}
|
||||
--- no_error_log
|
||||
[alert]
|
||||
-- wait: 2
|
||||
--- error_log
|
||||
invalid conf of [/routes/3], conf: nil, it should be an object
|
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* 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
|
||||
|
||||
import "C"
|
||||
import "unsafe"
|
||||
|
||||
//export initial
|
||||
func initial(config_zone unsafe.Pointer, version_zone unsafe.Pointer) {
|
||||
write_config(config_zone, version_zone)
|
||||
}
|
137
CloudronPackages/APISIX/apisix-source/t/xds-library/main.go
Normal file
137
CloudronPackages/APISIX/apisix-source/t/xds-library/main.go
Normal file
@@ -0,0 +1,137 @@
|
||||
/*
|
||||
* 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 -ldl
|
||||
#include "xds.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
extern void ngx_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 (
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
func main() {
|
||||
}
|
||||
|
||||
func write_config(config_zone unsafe.Pointer, version_zone unsafe.Pointer) {
|
||||
route_key := "/routes/1"
|
||||
route_value := fmt.Sprintf(`{
|
||||
"status": 1,
|
||||
"update_time": 1647250524,
|
||||
"create_time": 1646972532,
|
||||
"uri": "/hello",
|
||||
"priority": 0,
|
||||
"id": "1",
|
||||
"upstream": {
|
||||
"nodes": [
|
||||
{
|
||||
"port": 1980,
|
||||
"priority": 0,
|
||||
"host": "127.0.0.1",
|
||||
"weight": 1
|
||||
}
|
||||
],
|
||||
"type": "roundrobin",
|
||||
"hash_on": "vars",
|
||||
"pass_host": "pass",
|
||||
"scheme": "http"
|
||||
}
|
||||
}`)
|
||||
|
||||
upstream_key := "/upstreams/1"
|
||||
upstream_value := fmt.Sprintf(`{
|
||||
"id": "1",
|
||||
"nodes": {
|
||||
"127.0.0.1:1980": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
}`)
|
||||
|
||||
r_u_key := "/routes/2"
|
||||
r_u_value := fmt.Sprintf(`{
|
||||
"status": 1,
|
||||
"update_time": 1647250524,
|
||||
"create_time": 1646972532,
|
||||
"uri": "/hello1",
|
||||
"priority": 0,
|
||||
"id": "2",
|
||||
"upstream_id": "1"
|
||||
}`)
|
||||
|
||||
write_shdict(route_key, route_value, config_zone)
|
||||
write_shdict(upstream_key, upstream_value, config_zone)
|
||||
write_shdict(r_u_key, r_u_value, config_zone)
|
||||
update_conf_version(version_zone)
|
||||
|
||||
}
|
||||
|
||||
func get_version() string {
|
||||
return strconv.FormatInt(time.Now().UnixNano()/1e6, 10)
|
||||
}
|
||||
|
||||
func update_conf_version(zone unsafe.Pointer) {
|
||||
ctx := context.Background()
|
||||
key := "version"
|
||||
write_shdict(key, get_version(), zone)
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-time.After(time.Second * 5):
|
||||
write_shdict("version", get_version(), zone)
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func write_shdict(key string, value string, zone unsafe.Pointer) {
|
||||
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_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)),
|
||||
)
|
||||
}
|
55
CloudronPackages/APISIX/apisix-source/t/xds-library/xds.h
Normal file
55
CloudronPackages/APISIX/apisix-source/t/xds-library/xds.h
Normal file
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef XDS_H
|
||||
#define XDS_H
|
||||
#include <dlfcn.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
void ngx_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)
|
||||
{
|
||||
static void* dlhandle;
|
||||
static void (*fp)(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);
|
||||
|
||||
if (!dlhandle) {
|
||||
dlhandle = dlopen(NULL, RTLD_NOW);
|
||||
}
|
||||
if (!dlhandle) {
|
||||
return;
|
||||
}
|
||||
|
||||
fp = dlsym(dlhandle, "ngx_http_lua_ffi_shdict_store");
|
||||
if (!fp) {
|
||||
fp = dlsym(dlhandle, "ngx_meta_lua_ffi_shdict_store");
|
||||
}
|
||||
|
||||
fp(zone, op, key, key_len, value_type, str_value_buf, str_value_len,
|
||||
num_value, exptime, user_flags, errmsg, forcible);
|
||||
}
|
||||
|
||||
|
||||
#endif // XDS_H
|
Reference in New Issue
Block a user