From 21e23afaa3cfc03429a1a6605a0d8765b6450125 Mon Sep 17 00:00:00 2001 From: zenghuilin <850125665@qq.com> Date: Thu, 3 Mar 2022 23:36:05 +0800 Subject: [PATCH 1/5] feat: support for reading environment variables from yaml configuration files (#5244) --- apisix/cli/file.lua | 17 +++++++ apisix/core/config_yaml.lua | 7 +++ t/cli/test_yaml_config_variables.sh | 70 +++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+) create mode 100755 t/cli/test_yaml_config_variables.sh diff --git a/apisix/cli/file.lua b/apisix/cli/file.lua index 5d066e0dadcd..66600b54b41b 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 0a57828d61b8..89753f0f1e0e 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_yaml_config_variables.sh b/t/cli/test_yaml_config_variables.sh new file mode 100755 index 000000000000..4af6a2af7185 --- /dev/null +++ b/t/cli/test_yaml_config_variables.sh @@ -0,0 +1,70 @@ +#!/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 + +# check supported environment variables in apisix.yaml + +yaml_config_variables_clean_up() { + git checkout conf/config.yaml + git checkout conf/apisix.yaml +} + +trap yaml_config_variables_clean_up EXIT + +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: variable is not valid" + exit 1 +fi + +make stop + +echo "passed: resolve variables" From 90bb471f4986417adae671d137ed808473d21c79 Mon Sep 17 00:00:00 2001 From: zenghuilin <850125665@qq.com> Date: Fri, 4 Mar 2022 21:20:26 +0800 Subject: [PATCH 2/5] fix: use original clean up --- t/cli/test_yaml_config_variables.sh | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/t/cli/test_yaml_config_variables.sh b/t/cli/test_yaml_config_variables.sh index 4af6a2af7185..89f64cdf4d02 100755 --- a/t/cli/test_yaml_config_variables.sh +++ b/t/cli/test_yaml_config_variables.sh @@ -22,7 +22,7 @@ # check supported environment variables in apisix.yaml yaml_config_variables_clean_up() { - git checkout conf/config.yaml + clean_up git checkout conf/apisix.yaml } @@ -65,6 +65,4 @@ if [ ! $code -eq 200 ]; then exit 1 fi -make stop - echo "passed: resolve variables" From ace151468d0157e4b6d3e9a34f1187e79d991ba6 Mon Sep 17 00:00:00 2001 From: zenghuilin <850125665@qq.com> Date: Sun, 6 Mar 2022 20:09:09 +0800 Subject: [PATCH 3/5] fix: test file rename --- .../{test_yaml_config_variables.sh => test_standalone.sh} | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) rename t/cli/{test_yaml_config_variables.sh => test_standalone.sh} (92%) diff --git a/t/cli/test_yaml_config_variables.sh b/t/cli/test_standalone.sh similarity index 92% rename from t/cli/test_yaml_config_variables.sh rename to t/cli/test_standalone.sh index 89f64cdf4d02..aeef8f7a0836 100755 --- a/t/cli/test_yaml_config_variables.sh +++ b/t/cli/test_standalone.sh @@ -19,15 +19,14 @@ . ./t/cli/common.sh -# check supported environment variables in apisix.yaml - -yaml_config_variables_clean_up() { +standalone() { clean_up git checkout conf/apisix.yaml } -trap yaml_config_variables_clean_up EXIT +trap standalone EXIT +# support environment variables echo ' apisix: enable_admin: false From 0c7f98b537466d309ec087b3ad34ccada44a9386 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BD=97=E6=B3=BD=E8=BD=A9?= Date: Tue, 8 Mar 2022 10:50:17 +0800 Subject: [PATCH 4/5] Apply suggestions from code review Co-authored-by: tzssangglass --- t/cli/test_standalone.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/t/cli/test_standalone.sh b/t/cli/test_standalone.sh index aeef8f7a0836..01f8ce09ed02 100755 --- a/t/cli/test_standalone.sh +++ b/t/cli/test_standalone.sh @@ -60,8 +60,8 @@ 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: variable is not valid" + echo "failed: esolve variables in apisix.yaml conf failed" exit 1 fi -echo "passed: resolve variables" +echo "passed: resolve variables in apisix.yaml conf success" From a8eb15b3b4f00baa1bf4d2eff50513e9807a7c3f Mon Sep 17 00:00:00 2001 From: zenghuilin <850125665@qq.com> Date: Tue, 8 Mar 2022 12:30:34 +0800 Subject: [PATCH 5/5] fix: fix spelling errors --- t/cli/test_standalone.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/cli/test_standalone.sh b/t/cli/test_standalone.sh index 01f8ce09ed02..b4e6f3955420 100755 --- a/t/cli/test_standalone.sh +++ b/t/cli/test_standalone.sh @@ -60,7 +60,7 @@ 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: esolve variables in apisix.yaml conf failed" + echo "failed: resolve variables in apisix.yaml conf failed" exit 1 fi