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
3 changes: 3 additions & 0 deletions apisix/cli/ngx_tpl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ worker_shutdown_timeout {* worker_shutdown_timeout *};
env APISIX_PROFILE;
env PATH; # for searching external plugin runner's binary

# reserved environment variables for configuration
env APISIX_DEPLOYMENT_ETCD_HOST;
Copy link
Member

Choose a reason for hiding this comment

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

Can we find a way to cover it in the CI? So we don't need to spend time verifying it locally next time.


{% if envs then %}
{% for _, name in ipairs(envs) do %}
env {*name*};
Expand Down
36 changes: 36 additions & 0 deletions t/cli/test_main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,42 @@ 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:2333"
' > conf/config.yaml

failed_msg="failed: failed to configure etcd host with reserved environment variable"

out=$(APISIX_DEPLOYMENT_ETCD_HOST='["http://127.0.0.1:2379"]' make init 2>&1 || true)
if echo "$out" | grep "connection refused" > /dev/null; then
echo $failed_msg
exit 1
fi

out=$(APISIX_DEPLOYMENT_ETCD_HOST='["http://127.0.0.1:2379"]' make run 2>&1 || true)
if echo "$out" | grep "connection refused" > /dev/null; then
echo $failed_msg
An-DJ marked this conversation as resolved.
Show resolved Hide resolved
exit 1
fi

if ! grep "env APISIX_DEPLOYMENT_ETCD_HOST;" conf/nginx.conf > /dev/null; then
echo "failed: 'env APISIX_DEPLOYMENT_ETCD_HOST;' not in nginx.conf"
echo $failed_msg
An-DJ marked this conversation as resolved.
Show resolved Hide resolved
exit 1
fi

make stop

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

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