Skip to content

Commit

Permalink
feat: support ENV variable in configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
spacewander committed Nov 13, 2020
1 parent 358d83a commit 24ac21e
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
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 varialbes
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 varialbes"
exit 1
fi

echo "passed: resolve varialbes"

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 varialbes as integer"
exit 1
fi

echo "passed: resolve varialbes 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 varialbes as boolean"
exit 1
fi

echo "passed: resolve varialbes 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

0 comments on commit 24ac21e

Please sign in to comment.