You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by sp...@apache.org on 2020/11/14 11:39:39 UTC

[apisix] branch master updated: feat: support ENV variable in configuration (#2743)

This is an automated email from the ASF dual-hosted git repository.

spacewander pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git


The following commit(s) were added to refs/heads/master by this push:
     new 6b52811  feat: support ENV variable in configuration (#2743)
6b52811 is described below

commit 6b52811557fb9db004204acf0343c9ba455822ef
Author: 罗泽轩 <sp...@gmail.com>
AuthorDate: Sat Nov 14 19:39:29 2020 +0800

    feat: support ENV variable in configuration (#2743)
    
    Close #2675.
---
 .travis/apisix_cli_test.sh | 44 ++++++++++++++++++++++++++++++++++++++++++++
 bin/apisix                 | 34 ++++++++++++++++++++++++++++++++++
 conf/config.yaml           |  7 +++++++
 3 files changed, 85 insertions(+)

diff --git a/.travis/apisix_cli_test.sh b/.travis/apisix_cli_test.sh
index 101bd54..5ebb1e8 100755
--- a/.travis/apisix_cli_test.sh
+++ b/.travis/apisix_cli_test.sh
@@ -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
 
diff --git a/bin/apisix b/bin/apisix
index cfc5ba6..eef0306 100755
--- a/bin/apisix
+++ b/bin/apisix
@@ -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
@@ -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
 
diff --git a/conf/config.yaml b/conf/config.yaml
index 06374a3..335998b 100644
--- a/conf/config.yaml
+++ b/conf/config.yaml
@@ -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:
     -