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 ENV variable in configuration #2743

Merged
merged 2 commits into from
Nov 14, 2020
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
44 changes: 44 additions & 0 deletions .travis/apisix_cli_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,50 @@ fi

echo "passed: change default env"

# support environment variables
echo '
nginx_config:
envs:
- ${{var_test}}_${{FOO}}
' > conf/config.yaml

var_test=TEST FOO=bar make init

if ! grep "env TEST_bar;" conf/nginx.conf > /dev/null; then
echo "failed: failed to resolve variables"
exit 1
fi

echo "passed: resolve variables"

echo '
nginx_config:
worker_rlimit_nofile: ${{nofile9}}
' > conf/config.yaml

nofile9=99999 make init

if ! grep "worker_rlimit_nofile 99999;" conf/nginx.conf > /dev/null; then
echo "failed: failed to resolve variables as integer"
exit 1
fi

echo "passed: resolve variables as integer"

echo '
apisix:
enable_admin: ${{admin}}
' > conf/config.yaml

admin=false make init

if grep "location /apisix/admin" conf/nginx.conf > /dev/null; then
echo "failed: failed to resolve variables as boolean"
exit 1
fi

echo "passed: resolve variables as boolean"

# check nameserver imported
git checkout conf/config.yaml

Expand Down
34 changes: 34 additions & 0 deletions bin/apisix
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,39 @@ local function tab_is_array(t)
end


local function resolve_conf_var(conf)
for key, val in pairs(conf) do
if type(val) == "table" then
resolve_conf_var(val)
elseif type(val) == "string" then
local var_used = false
-- we use '${{var}}' because '$var' and '${var}' are taken
-- by Nginx
local new_val = val:gsub("%$%{%{([%w_]+)%}%}", function(var)
local v = os.getenv(var)
if v then
var_used = true
return v
end
error("failed to handle configuration: can't find environment variable " .. var)
end)

if var_used then
if tonumber(new_val) ~= nil then
new_val = tonumber(new_val)
elseif new_val == "true" then
new_val = true
elseif new_val == "false" then
new_val = false
end
end

conf[key] = new_val
end
end
end


local function merge_conf(base, new_tab)
for key, val in pairs(new_tab) do
if type(val) == "table" then
Expand Down Expand Up @@ -138,6 +171,7 @@ local function read_yaml_conf()
return nil, "invalid config.yaml file"
end

resolve_conf_var(user_conf)
merge_conf(default_conf, user_conf)
end

Expand Down
7 changes: 7 additions & 0 deletions conf/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@
# host:
# - "http://127.0.0.1:2379"
#
# To configure via environment variables, you can use `${{VAR}}` syntax. For instance:
#
# etcd:
# host:
# - "http://${{ETCD_HOST}}:2379"
#
# If the configured environment variable can't be found, an error will be thrown.
apisix:
admin_key:
-
Expand Down