Skip to content
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 resolve default value when environment not set #5675

Merged
merged 11 commits into from
Dec 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions apisix/cli/file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,16 @@ local function resolve_conf_var(conf)
local var_used = false
-- we use '${{var}}' because '$var' and '${var}' are taken
-- by Nginx
local new_val = val:gsub("%$%{%{%s*([%w_]+)%s*%}%}", function(var)
local v = getenv(var)
local new_val = val:gsub("%$%{%{%s*([%w_]+[%:%=]?.-)%s*%}%}", function(var)
local i, j = var:find("%:%=")
local default
if i and j then
default = var:sub(i + 2, #var)
default = default:gsub('^%s*(.-)%s*$', '%1')
var = var:sub(1, i - 1)
end

local v = getenv(var) or default
if v then
if not exported_vars then
exported_vars = {}
Expand Down
10 changes: 10 additions & 0 deletions conf/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@
# And then run `export ETCD_HOST=$your_host` before `make init`.
#
# If the configured environment variable can't be found, an error will be thrown.
#
# Also, If you want to use default value when the environment variable not set,
# Use `${{VAR:=default_value}}` instead. For instance:
#
# etcd:
# host:
# - http://${{ETCD_HOST:=localhost}}:2379
#
# This will find environment variable `ETCD_HOST` first, and if it's not exist it will use `localhost` as default value.
#
apisix:
admin_key:
- name: admin
Expand Down
39 changes: 39 additions & 0 deletions t/cli/test_main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,45 @@ fi

echo "pass: support environment variables in local_conf"

# support default value when environment not set
echo '
tests:
key: ${{TEST_ENV:=1.1.1.1}}
' > conf/config.yaml

make init

if ! grep "env TEST_ENV=1.1.1.1;" conf/nginx.conf > /dev/null; then
echo "failed: should use default value when environment not set"
exit 1
fi

echo '
tests:
key: ${{TEST_ENV:=very-long-domain-with-many-symbols.absolutely-non-exists-123ss.com:1234/path?param1=value1}}
' > conf/config.yaml

make init

if ! grep "env TEST_ENV=very-long-domain-with-many-symbols.absolutely-non-exists-123ss.com:1234/path?param1=value1;" conf/nginx.conf > /dev/null; then
echo "failed: should use default value when environment not set"
exit 1
fi

echo '
tests:
key: ${{TEST_ENV:=192.168.1.1}}
' > conf/config.yaml

TEST_ENV=127.0.0.1 make init

if ! grep "env TEST_ENV=127.0.0.1;" conf/nginx.conf > /dev/null; then
echo "failed: should use environment variable when environment is set"
exit 1
fi

echo "pass: support default value when environment not set"

# support merging worker_processes
echo '
nginx_config:
Expand Down