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 pulling env vars from yaml keys #9855

Merged
72 changes: 44 additions & 28 deletions apisix/cli/file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,44 +53,60 @@ local function tab_is_array(t)
return #t == count
end

local function var_sub(val)
monkeyDluffy6017 marked this conversation as resolved.
Show resolved Hide resolved
local err
local var_used = false
-- we use '${{var}}' because '$var' and '${var}' are taken
-- by Nginx
local new_val = val:gsub("%$%{%{%s*([%w_]+[%:%=]?.-)%s*%}%}", function(var)
local i, j = var:find("%:%=")
local default
if i and j then
default = var:sub(i + 2, #var)
default = default:gsub('^%s*(.-)%s*$', '%1')
var = var:sub(1, i - 1)
end

local v = getenv(var) or default
if v then
if not exported_vars then
exported_vars = {}
end

exported_vars[var] = v
var_used = true
return v
end

err = "failed to handle configuration: " ..
"can't find environment variable " .. var
return ""
end)
return new_val, var_used, err
end
monkeyDluffy6017 marked this conversation as resolved.
Show resolved Hide resolved

local function resolve_conf_var(conf)
for key, val in pairs(conf) do
-- substitute environment variables from conf keys
if type(key) == "string" then
local new_key, _, err = var_sub(key)
if err then
return nil, err
end
if new_key ~= key then
conf.key = nil
conf[new_key] = val
monkeyDluffy6017 marked this conversation as resolved.
Show resolved Hide resolved
key = new_key
end
end
monkeyDluffy6017 marked this conversation as resolved.
Show resolved Hide resolved
if type(val) == "table" then
local ok, err = resolve_conf_var(val)
if not ok then
return nil, err
end

elseif type(val) == "string" then
local err
local var_used = false
-- we use '${{var}}' because '$var' and '${var}' are taken
-- by Nginx
local new_val = val:gsub("%$%{%{%s*([%w_]+[%:%=]?.-)%s*%}%}", function(var)
local i, j = var:find("%:%=")
local default
if i and j then
default = var:sub(i + 2, #var)
default = default:gsub('^%s*(.-)%s*$', '%1')
var = var:sub(1, i - 1)
end

local v = getenv(var) or default
if v then
if not exported_vars then
exported_vars = {}
end

exported_vars[var] = v
var_used = true
return v
end

err = "failed to handle configuration: " ..
"can't find environment variable " .. var
return ""
end)
local new_val, var_used, err = var_sub(val)

if err then
return nil, err
Expand Down
13 changes: 13 additions & 0 deletions docs/en/latest/admin-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,19 @@ deployment:

This will find the environment variable `ADMIN_KEY` first, and if it does not exist, it will use `edd1c9f034335f136f87ad84b625c8f1` as the default value.

You can also specify environment variables in yaml keys. This is specifically useful in the `standalone` [mode](./deployment-modes.md#standalone) where you can specify the upstream nodes as follows:

```yaml title="./conf/apisix.yaml"
routes:
-
uri: "/test"
upstream:
nodes:
"${{HOST_IP}}:${{PORT}}": 1
type: roundrobin
#END
```

### Force Delete

By default, the Admin API checks for references between resources and will refuse to delete resources in use.
Expand Down
13 changes: 13 additions & 0 deletions docs/zh/latest/admin-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,19 @@ deployment:

首先查找环境变量 `ADMIN_KEY`,如果该环境变量不存在,它将使用 `edd1c9f034335f136f87ad84b625c8f1` 作为默认值。

您还可以在 yaml 键中指定环境变量。这在 `standalone` 模式 中特别有用,您可以在其中指定上游节点,如下所示:

```yaml title="./conf/apisix.yaml"
routes:
-
uri: "/test"
upstream:
nodes:
"${{HOST_IP}}:${{PORT}}": 1
type: roundrobin
#END
```

### 强制删除 {#force-delete}

默认情况下,Admin API 会检查资源间的引用关系,将会拒绝删除正在使用中的资源。
Expand Down
29 changes: 28 additions & 1 deletion t/cli/test_standalone.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ standalone() {

trap standalone EXIT

# support environment variables
# support environment variables in yaml values
echo '
apisix:
enable_admin: false
Expand Down Expand Up @@ -69,6 +69,33 @@ fi

echo "passed: resolve variables in apisix.yaml conf success"

# support environment variables in yaml keys
echo '
routes:
-
uri: "/test"
plugins:
proxy-rewrite:
uri: "/apisix/nginx_status"
upstream:
nodes:
"${{HOST_IP}}:${{PORT}}": 1
type: roundrobin
#END
' > conf/apisix.yaml

# variable is valid
HOST_IP="127.0.0.1" PORT="9091" make init
HOST_IP="127.0.0.1" PORT="9091" 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"
fi

echo "passed: resolve variables in apisix.yaml conf success"

# configure standalone via deployment
echo '
deployment:
Expand Down
Loading