Skip to content

Commit

Permalink
feat: move tinyyaml to lyaml (#11312)
Browse files Browse the repository at this point in the history
  • Loading branch information
bzp2010 authored Jun 3, 2024
1 parent 08cb3ad commit cf84292
Show file tree
Hide file tree
Showing 35 changed files with 157 additions and 215 deletions.
1 change: 0 additions & 1 deletion apisix-master-0.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ dependencies = {
"lua-protobuf = 0.5.0-1",
"lua-resty-openidc = 1.7.6-3",
"luafilesystem = 1.7.0-2",
"api7-lua-tinyyaml = 0.4.4",
"nginx-lua-prometheus-api7 = 0.20240201-1",
"jsonschema = 0.9.8",
"lua-resty-ipmatcher = 0.6.1",
Expand Down
19 changes: 5 additions & 14 deletions apisix/cli/file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@
-- limitations under the License.
--

local yaml = require("tinyyaml")
local yaml = require("lyaml")
local profile = require("apisix.core.profile")
local util = require("apisix.cli.util")
local dkjson = require("dkjson")

local pairs = pairs
local type = type
local tonumber = tonumber
local getmetatable = getmetatable
local getenv = os.getenv
local str_gmatch = string.gmatch
local str_find = string.find
Expand Down Expand Up @@ -157,14 +156,6 @@ local function replace_by_reserved_env_vars(conf)
end


local function tinyyaml_type(t)
local mt = getmetatable(t)
if mt then
return mt.__type
end
end


local function path_is_multi_type(path, type_val)
if str_sub(path, 1, 14) == "nginx_config->" and
(type_val == "number" or type_val == "string") then
Expand All @@ -188,7 +179,7 @@ local function merge_conf(base, new_tab, ppath)

for key, val in pairs(new_tab) do
if type(val) == "table" then
if tinyyaml_type(val) == "null" then
if val == yaml.null then
base[key] = nil

elseif tab_is_array(val) then
Expand Down Expand Up @@ -243,7 +234,7 @@ function _M.read_yaml_conf(apisix_home)
return nil, err
end

local default_conf = yaml.parse(default_conf_yaml)
local default_conf = yaml.load(default_conf_yaml)
if not default_conf then
return nil, "invalid config-default.yaml file"
end
Expand All @@ -266,7 +257,7 @@ function _M.read_yaml_conf(apisix_home)
end

if not is_empty_file then
local user_conf = yaml.parse(user_conf_yaml)
local user_conf = yaml.load(user_conf_yaml)
if not user_conf then
return nil, "invalid config.yaml file"
end
Expand Down Expand Up @@ -306,7 +297,7 @@ function _M.read_yaml_conf(apisix_home)
local apisix_conf_path = profile:yaml_path("apisix")
local apisix_conf_yaml, _ = util.read_file(apisix_conf_path)
if apisix_conf_yaml then
local apisix_conf = yaml.parse(apisix_conf_yaml)
local apisix_conf = yaml.load(apisix_conf_yaml)
if apisix_conf then
local ok, err = resolve_conf_var(apisix_conf)
if not ok then
Expand Down
4 changes: 2 additions & 2 deletions apisix/core/config_yaml.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

local config_local = require("apisix.core.config_local")
local config_util = require("apisix.core.config_util")
local yaml = require("tinyyaml")
local yaml = require("lyaml")
local log = require("apisix.core.log")
local json = require("apisix.core.json")
local new_tab = require("table.new")
Expand Down Expand Up @@ -100,7 +100,7 @@ local function read_apisix_yaml(premature, pre_mtime)
local yaml_config = f:read("*a")
f:close()

local apisix_yaml_new = yaml.parse(yaml_config)
local apisix_yaml_new = yaml.load(yaml_config)
if not apisix_yaml_new then
log.error("failed to parse the content of file " .. apisix_yaml_path)
return
Expand Down
4 changes: 2 additions & 2 deletions apisix/debug.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
-- limitations under the License.
--
local require = require
local yaml = require("tinyyaml")
local yaml = require("lyaml")
local log = require("apisix.core.log")
local profile = require("apisix.core.profile")
local lfs = require("lfs")
Expand Down Expand Up @@ -130,7 +130,7 @@ local function read_debug_yaml()
local yaml_config = f:read("*a")
f:close()

local debug_yaml_new = yaml.parse(yaml_config)
local debug_yaml_new = yaml.load(yaml_config)
if not debug_yaml_new then
log.error("failed to parse the content of file " .. debug_yaml_path)
return
Expand Down
4 changes: 2 additions & 2 deletions conf/config-default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ apisix:
memory_size: 50m # Size of the memory to store the cache index.
disk_size: 1G # Size of the disk to store the cache data.
disk_path: /tmp/disk_cache_one # Path to the cache file for disk cache.
cache_levels: 1:2 # Cache hierarchy levels of disk cache.
cache_levels: "1:2" # Cache hierarchy levels of disk cache.
# - name: disk_cache_two
# memory_size: 50m
# disk_size: 1G
Expand All @@ -73,7 +73,7 @@ apisix:
ssl: radixtree_sni # radixtree_sni: match route by SNI

# http is the default proxy mode. proxy_mode can be one of `http`, `stream`, or `http&stream`
proxy_mode: http
proxy_mode: "http"
# stream_proxy: # TCP/UDP L4 proxy
# tcp:
# - addr: 9100 # Set the TCP proxy listening ports.
Expand Down
4 changes: 2 additions & 2 deletions docs/en/latest/plugins/body-transformer.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ For example, parse YAML to JSON yourself:

```
{%
local yaml = require("tinyyaml")
local body = yaml.parse(_body)
local yaml = require("lyaml")
local body = yaml.load(_body)
%}
{"foobar":"{{body.foobar.foo .. " " .. body.foobar.bar}}"}
```
Expand Down
3 changes: 1 addition & 2 deletions t/cli/test_admin.sh
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,7 @@ deployment:
admin:
allow_admin: ~
admin_key:
-
name: "admin"
- name: "admin"
key: ''
role: admin
' > conf/config.yaml
Expand Down
8 changes: 4 additions & 4 deletions t/cli/test_main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -664,8 +664,8 @@ echo "passed: disable ssl_session_tickets by default"
# support 3rd-party plugin
echo '
apisix:
extra_lua_path: "\$prefix/example/?.lua"
extra_lua_cpath: "\$prefix/example/?.lua"
extra_lua_path: "$prefix/example/?.lua"
extra_lua_cpath: "$prefix/example/?.lua"
plugins:
- 3rd-party
stream_plugins:
Expand Down Expand Up @@ -716,7 +716,7 @@ echo "passed: bad lua_module_hook should be rejected"
echo '
apisix:
proxy_mode: http&stream
extra_lua_path: "\$prefix/example/?.lua"
extra_lua_path: "$prefix/example/?.lua"
lua_module_hook: "my_hook"
stream_proxy:
tcp:
Expand Down Expand Up @@ -838,7 +838,7 @@ apisix:
disk_path: /tmp/disk_cache_one
disk_size: 100m
memory_size: 20m
cache_levels: 1:2
cache_levels: "1:2"
' > conf/config.yaml

make init
Expand Down
18 changes: 9 additions & 9 deletions t/cli/test_prometheus_run_in_privileged.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ rm logs/error.log || true

echo '
apisix:
extra_lua_path: "\$prefix/t/lib/?.lua"
extra_lua_path: "$prefix/t/lib/?.lua"
nginx_config:
error_log_level: info
' > conf/config.yaml
Expand All @@ -53,10 +53,10 @@ echo "prometheus run in privileged agent successfully when only http is enabled"
sleep 0.5
rm logs/error.log || true

echo "
echo '
apisix:
proxy_mode: http&stream
extra_lua_path: "\$prefix/t/lib/?.lua"
proxy_mode: "http&stream"
extra_lua_path: "$prefix/t/lib/?.lua"
enable_admin: true
stream_proxy:
tcp:
Expand All @@ -65,7 +65,7 @@ stream_plugins:
- prometheus
nginx_config:
error_log_level: info
" > conf/config.yaml
' > conf/config.yaml

make run
sleep 0.1
Expand All @@ -86,10 +86,10 @@ make stop
sleep 0.5
rm logs/error.log || true

echo "
echo '
apisix:
proxy_mode: http&stream
extra_lua_path: "\$prefix/t/lib/?.lua"
proxy_mode: "http&stream"
extra_lua_path: "$prefix/t/lib/?.lua"
enable_admin: false
stream_proxy:
tcp:
Expand All @@ -98,7 +98,7 @@ stream_plugins:
- prometheus
nginx_config:
error_log_level: info
" > conf/config.yaml
' > conf/config.yaml

make run
sleep 0.1
Expand Down
19 changes: 6 additions & 13 deletions t/config-center-yaml/consumer.t
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,6 @@ deployment:
_EOC_

$block->set_value("yaml_config", $yaml_config);

my $routes = <<_EOC_;
routes:
-
uri: /hello
upstream:
nodes:
"127.0.0.1:1980": 1
type: roundrobin
#END
_EOC_

$block->set_value("apisix_yaml", $block->apisix_yaml . $routes);
});

run_tests();
Expand All @@ -57,6 +44,12 @@ __DATA__
--- apisix_yaml
consumers:
- username: jwt-auth
routes:
- uri: /hello
upstream:
nodes:
"127.0.0.1:1980": 1
type: roundrobin
#END
--- request
GET /hello
Expand Down
8 changes: 4 additions & 4 deletions t/config-center-yaml/plugin-configs.t
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ routes:
plugin_config_id: 1
upstream:
nodes:
"127.0.0.1:1980":1
"127.0.0.1:1980": 1
type: roundrobin
#END
--- response_body
Expand All @@ -74,7 +74,7 @@ routes:
plugin_config_id: 1
upstream:
nodes:
"127.0.0.1:1980":1
"127.0.0.1:1980": 1
type: roundrobin
#END
--- error_code: 503
Expand Down Expand Up @@ -105,7 +105,7 @@ routes:
body: "world\n"
upstream:
nodes:
"127.0.0.1:1980":1
"127.0.0.1:1980": 1
type: roundrobin
#END
--- request
Expand Down Expand Up @@ -135,7 +135,7 @@ routes:
plugin_config_id: 1
upstream:
nodes:
"127.0.0.1:1980":1
"127.0.0.1:1980": 1
type: roundrobin
#END
--- error_code: 503
Expand Down
2 changes: 1 addition & 1 deletion t/config-center-yaml/plugin-metadata.t
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ routes:
plugin_metadata:
- id: http-logger
log_format:
host: "$host",
host: "$host"
remote_addr: "$remote_addr"
#END
--- request
Expand Down
29 changes: 17 additions & 12 deletions t/config-center-yaml/plugin.t
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,33 @@ _EOC_

$block->set_value("yaml_config", $yaml_config);

my $routes = <<_EOC_;
if (!$block->apisix_yaml) {
my $routes = <<_EOC_;
routes:
-
uri: /hello
- uri: /hello
upstream:
nodes:
"127.0.0.1:1980": 1
type: roundrobin
nodes:
"127.0.0.1:1980": 1
type: roundrobin
#END
_EOC_

$block->set_value("apisix_yaml", $block->apisix_yaml . $routes);
$block->set_value("apisix_yaml", $block->extra_apisix_yaml . $routes);
}
});

our $debug_config = t::APISIX::read_file("conf/debug.yaml");
$debug_config =~ s/basic:\n enable: false/basic:\n enable: true/;

run_tests();

## TODO: extra_apisix_yaml is specific to this document and is not standard behavior for
## the APISIX testing framework, so it should be standardized or replaced later.

__DATA__
=== TEST 1: sanity
--- apisix_yaml
--- extra_apisix_yaml
plugins:
- name: ip-restriction
- name: jwt-auth
Expand Down Expand Up @@ -111,7 +115,7 @@ plugins:
- jwt-auth
stream_plugins:
- mqtt-proxy
--- apisix_yaml
--- extra_apisix_yaml
plugins:
- name: ip-restriction
- name: jwt-auth
Expand Down Expand Up @@ -144,7 +148,7 @@ qr/(loaded plugin and sort by priority: (3000 name: ip-restriction|2510 name: jw
=== TEST 3: disable plugin and its router
--- apisix_yaml
--- extra_apisix_yaml
plugins:
- name: jwt-auth
--- request
Expand All @@ -162,6 +166,7 @@ routes:
plugins:
- name: public-api
- name: prometheus
#END
--- request
GET /apisix/prometheus/metrics
Expand All @@ -181,7 +186,7 @@ plugins:
- jwt-auth
stream_plugins:
- mqtt-proxy
--- apisix_yaml
--- extra_apisix_yaml
plugins:
- name: xxx
stream: ip-restriction
Expand All @@ -197,7 +202,7 @@ load(): plugins not changed
=== TEST 6: empty plugin list
--- apisix_yaml
--- extra_apisix_yaml
plugins:
stream_plugins:
--- debug_config eval: $::debug_config
Expand Down
Loading

0 comments on commit cf84292

Please sign in to comment.