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(cli): support reserved environment variable "APISIX_DEPLOYMENT_E… #8898

Merged
merged 15 commits into from
Mar 1, 2023
19 changes: 19 additions & 0 deletions apisix/cli/file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
local yaml = require("tinyyaml")
local profile = require("apisix.core.profile")
local util = require("apisix.cli.util")
local dkjson = require("dkjson")

local pairs = pairs
local type = type
Expand All @@ -27,6 +28,7 @@ local getenv = os.getenv
local str_gmatch = string.gmatch
local str_find = string.find
local str_sub = string.sub
local print = print

local _M = {}
local exported_vars
Expand Down Expand Up @@ -115,6 +117,21 @@ end
_M.resolve_conf_var = resolve_conf_var


local function replace_by_reserved_env_vars(conf)
-- TODO: support more reserved environment variables
local v = getenv("APISIX_DEPLOYMENT_ETCD_HOST")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it's an array, let's call it APISIX_DEPLOYMENT_ETCD_HOSTS.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps it is a better idea that the variable name matches the configuration item?

deployment:
    etcd:
       host:
          - xxx
=======
APISIX_DEPLOYMENT_ETCD_HOST

It's easier to be understood, and there is no need to add more detailed documents to describe this feature.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏻 Good point.

if v and conf["deployment"] and conf["deployment"]["etcd"] then
local val, _, err = dkjson.decode(v)
if err or not val then
print("parse ${APISIX_DEPLOYMENT_ETCD_HOST} failed, error:", err)
return
end

conf["deployment"]["etcd"]["host"] = val
end
end


local function tinyyaml_type(t)
local mt = getmetatable(t)
if mt then
Expand Down Expand Up @@ -283,6 +300,8 @@ function _M.read_yaml_conf(apisix_home)
end
end

replace_by_reserved_env_vars(default_conf)

return default_conf
end

Expand Down
26 changes: 26 additions & 0 deletions t/cli/test_main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,32 @@ fi

echo "passed: resolve variables"

# support reserved environment variable APISIX_DEPLOYMENT_ETCD_HOST

echo '
deployment:
role: traditional
role_traditional:
config_provider: etcd
etcd:
host:
- "http://127.0.0.1:2379"
' > conf/config.yaml

out=$(APISIX_DEPLOYMENT_ETCD_HOST='["http://127.0.0.1:2333"]' make init 2>&1 || true)
An-DJ marked this conversation as resolved.
Show resolved Hide resolved
if ! echo "$out" | grep "connection refused" > /dev/null; then
echo "failed: failed to configure etcd host with reserved environment variable"
exit 1
fi

out=$(APISIX_DEPLOYMENT_ETCD_HOST='["http://127.0.0.1:2333"]' make run 2>&1 || true)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you configure a connectable address and check if it works?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

if ! echo "$out" | grep "connection refused" > /dev/null; then
echo "failed: failed to configure etcd host with reserved environment variable"
exit 1
fi

echo "passed: configure etcd host with reserved environment variable"

echo '
nginx_config:
worker_rlimit_nofile: ${{nofile9}}
Expand Down