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 2022/03/08 06:42:03 UTC

[apisix] branch master updated: feat: support for reading environment variables from yaml configuration files #5244 (#6505)

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 cbeb6eb  feat: support for reading environment variables from yaml configuration files #5244 (#6505)
cbeb6eb is described below

commit cbeb6ebe3609a0ce9b912c99942dc32e3f9322d8
Author: zenghuilin <85...@qq.com>
AuthorDate: Tue Mar 8 14:41:54 2022 +0800

    feat: support for reading environment variables from yaml configuration files #5244 (#6505)
---
 apisix/cli/file.lua         | 17 ++++++++++++
 apisix/core/config_yaml.lua |  7 +++++
 t/cli/test_standalone.sh    | 67 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 91 insertions(+)

diff --git a/apisix/cli/file.lua b/apisix/cli/file.lua
index 5d066e0..66600b5 100644
--- a/apisix/cli/file.lua
+++ b/apisix/cli/file.lua
@@ -112,6 +112,9 @@ local function resolve_conf_var(conf)
 end
 
 
+_M.resolve_conf_var = resolve_conf_var
+
+
 local function tinyyaml_type(t)
     local mt = getmetatable(t)
     if mt then
@@ -234,6 +237,20 @@ function _M.read_yaml_conf(apisix_home)
         end
     end
 
+    if default_conf.apisix.config_center == "yaml" then
+        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)
+            if apisix_conf then
+                local ok, err = resolve_conf_var(apisix_conf)
+                if not ok then
+                    return nil, err
+                end
+            end
+        end
+    end
+
     return default_conf
 end
 
diff --git a/apisix/core/config_yaml.lua b/apisix/core/config_yaml.lua
index 0a57828..89753f0 100644
--- a/apisix/core/config_yaml.lua
+++ b/apisix/core/config_yaml.lua
@@ -27,6 +27,7 @@ local new_tab      = require("table.new")
 local check_schema = require("apisix.core.schema").check
 local profile      = require("apisix.core.profile")
 local lfs          = require("lfs")
+local file         = require("apisix.cli.file")
 local exiting      = ngx.worker.exiting
 local insert_tab   = table.insert
 local type         = type
@@ -105,6 +106,12 @@ local function read_apisix_yaml(premature, pre_mtime)
         return
     end
 
+    local ok, err = file.resolve_conf_var(apisix_yaml_new)
+    if not ok then
+        log.error("failed: failed to resolve variables:" .. err)
+        return
+    end
+
     apisix_yaml = apisix_yaml_new
     apisix_yaml_ctime = last_change_time
 end
diff --git a/t/cli/test_standalone.sh b/t/cli/test_standalone.sh
new file mode 100755
index 0000000..b4e6f39
--- /dev/null
+++ b/t/cli/test_standalone.sh
@@ -0,0 +1,67 @@
+#!/usr/bin/env bash
+
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+. ./t/cli/common.sh
+
+standalone() {
+    clean_up
+    git checkout conf/apisix.yaml
+}
+
+trap standalone EXIT
+
+# support environment variables
+echo '
+apisix:
+  enable_admin: false
+  config_center: yaml
+' > conf/config.yaml
+
+echo '
+routes:
+  -
+    uri: ${{var_test_path}}
+    plugins:
+      proxy-rewrite:
+        uri: ${{var_test_proxy_rewrite_uri:=/apisix/nginx_status}}
+    upstream:
+      nodes:
+        "127.0.0.1:9091": 1
+      type: roundrobin
+#END
+' > conf/apisix.yaml
+
+# check for resolve variables
+var_test_path=/test make init
+
+if ! grep "env var_test_path=/test;" conf/nginx.conf > /dev/null; then
+    echo "failed: failed to resolve variables"
+    exit 1
+fi
+
+# variable is valid
+var_test_path=/test make run
+sleep 0.1
+code=$(curl -o /dev/null -s -m 5 -w %{http_code} http://127.0.0.1:9080/test)
+if [ ! $code -eq 200 ]; then
+    echo "failed: resolve variables in apisix.yaml conf failed"
+    exit 1
+fi
+
+echo "passed: resolve variables in apisix.yaml conf success"