You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by to...@apache.org on 2021/07/07 12:06:19 UTC
[apisix] branch master updated: feat(cli): check listen port
conflicts with each other (#4540)
This is an automated email from the ASF dual-hosted git repository.
tokers 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 765b7f1 feat(cli): check listen port conflicts with each other (#4540)
765b7f1 is described below
commit 765b7f192cf81ca9928dd2b008042a1446be0f8b
Author: Arthur.Zhang <zh...@qq.com>
AuthorDate: Wed Jul 7 20:06:08 2021 +0800
feat(cli): check listen port conflicts with each other (#4540)
---
apisix/cli/ops.lua | 70 +++++++++++++++++++++++++++++++-----------------
t/cli/test_control.sh | 27 ++++++++++++++++---
t/cli/test_prometheus.sh | 30 +++++++++++++++++++++
3 files changed, 98 insertions(+), 29 deletions(-)
diff --git a/apisix/cli/ops.lua b/apisix/cli/ops.lua
index f02b93e..73166a6 100644
--- a/apisix/cli/ops.lua
+++ b/apisix/cli/ops.lua
@@ -407,11 +407,16 @@ Please modify "admin_key" in conf/config.yaml .
util.die("missing apisix.proxy_cache for plugin proxy-cache\n")
end
- local control_port
+ local ports_to_check = {}
+
local control_server_addr
if yaml_conf.apisix.enable_control then
if not yaml_conf.apisix.control then
+ if ports_to_check[9090] ~= nil then
+ util.die("control port 9090 conflicts with ", ports_to_check[9090], "\n")
+ end
control_server_addr = "127.0.0.1:9090"
+ ports_to_check[9090] = "control"
else
local ip = yaml_conf.apisix.control.ip
local port = tonumber(yaml_conf.apisix.control.port)
@@ -424,16 +429,45 @@ Please modify "admin_key" in conf/config.yaml .
port = 9090
end
+ if ports_to_check[port] ~= nil then
+ util.die("control port ", port, " conflicts with ", ports_to_check[port], "\n")
+ end
+
control_server_addr = ip .. ":" .. port
- control_port = port
+ ports_to_check[port] = "control"
+ end
+ end
+
+ local prometheus_server_addr
+ if yaml_conf.plugin_attr.prometheus then
+ local prometheus = yaml_conf.plugin_attr.prometheus
+ if prometheus.enable_export_server then
+ local ip = prometheus.export_addr.ip
+ local port = tonumber(prometheus.export_addr.port)
+
+ if ip == nil then
+ ip = "127.0.0.1"
+ end
+
+ if not port then
+ port = 9091
+ end
+
+ if ports_to_check[port] ~= nil then
+ util.die("prometheus port ", port, " conflicts with ", ports_to_check[port], "\n")
+ end
+
+ prometheus_server_addr = ip .. ":" .. port
+ ports_to_check[port] = "prometheus"
end
end
-- support multiple ports listen, compatible with the original style
if type(yaml_conf.apisix.node_listen) == "number" then
- if yaml_conf.apisix.node_listen == control_port then
- util.die("control port conflicts with node_listen port\n")
+ if ports_to_check[yaml_conf.apisix.node_listen] ~= nil then
+ util.die("node_listen port ", yaml_conf.apisix.node_listen,
+ " conflicts with ", ports_to_check[yaml_conf.apisix.node_listen], "\n")
end
local node_listen = {{port = yaml_conf.apisix.node_listen}}
@@ -443,15 +477,17 @@ Please modify "admin_key" in conf/config.yaml .
for index, value in ipairs(yaml_conf.apisix.node_listen) do
if type(value) == "number" then
- if value == control_port then
- util.die("control port conflicts with node_listen port\n")
+ if ports_to_check[value] ~= nil then
+ util.die("node_listen port ", value, " conflicts with ",
+ ports_to_check[value], "\n")
end
table_insert(node_listen, index, {port = value})
elseif type(value) == "table" then
- if type(value.port) == "number" and value.port == control_port then
- util.die("control port conflicts with node_listen port\n")
+ if type(value.port) == "number" and ports_to_check[value.port] ~= nil then
+ util.die("node_listen port ", value.port, " conflicts with ",
+ ports_to_check[value.port], "\n")
end
table_insert(node_listen, index, value)
@@ -538,6 +574,7 @@ Please modify "admin_key" in conf/config.yaml .
dubbo_upstream_multiplex_count = dubbo_upstream_multiplex_count,
tcp_enable_ssl = tcp_enable_ssl,
control_server_addr = control_server_addr,
+ prometheus_server_addr = prometheus_server_addr,
}
if not yaml_conf.apisix then
@@ -561,23 +598,6 @@ Please modify "admin_key" in conf/config.yaml .
sys_conf[k] = v
end
- if yaml_conf.plugin_attr.prometheus then
- local prometheus = yaml_conf.plugin_attr.prometheus
- if prometheus.enable_export_server then
- local ip = prometheus.export_addr.ip
- local port = tonumber(prometheus.export_addr.port)
-
- if ip == nil then
- ip = "127.0.0.1"
- end
-
- if not port then
- port = 9091
- end
-
- sys_conf.prometheus_server_addr = ip .. ":" .. port
- end
- end
local wrn = sys_conf["worker_rlimit_nofile"]
local wc = sys_conf["event"]["worker_connections"]
diff --git a/t/cli/test_control.sh b/t/cli/test_control.sh
index ab12c45..fd924ac 100755
--- a/t/cli/test_control.sh
+++ b/t/cli/test_control.sh
@@ -81,12 +81,12 @@ echo '
apisix:
enable_control: true
control:
- port: 9091
+ port: 9092
' > conf/config.yaml
make init
-if ! grep "listen 127.0.0.1:9091;" conf/nginx.conf > /dev/null; then
+if ! grep "listen 127.0.0.1:9092;" conf/nginx.conf > /dev/null; then
echo "failed: customize address for control server"
exit 1
fi
@@ -94,7 +94,7 @@ fi
make run
sleep 0.1
-code=$(curl -v -k -i -m 20 -o /dev/null -s -w %{http_code} http://127.0.0.1:9091/v1/schema)
+code=$(curl -v -k -i -m 20 -o /dev/null -s -w %{http_code} http://127.0.0.1:9092/v1/schema)
if [ ! $code -eq 200 ]; then
echo "failed: access control server"
@@ -124,7 +124,26 @@ apisix:
' > conf/config.yaml
out=$(make init 2>&1 || true)
-if ! echo "$out" | grep "control port conflicts with node_listen port"; then
+if ! echo "$out" | grep "node_listen port 9090 conflicts with control"; then
+ echo "failed: can't detect port conflicts"
+ exit 1
+fi
+
+echo '
+apisix:
+ node_listen: 9080
+ enable_control: true
+ control:
+ port: 9091
+plugin_attr:
+ prometheus:
+ export_addr:
+ ip: "127.0.0.1"
+ port: 9091
+' > conf/config.yaml
+
+out=$(make init 2>&1 || true)
+if ! echo "$out" | grep "prometheus port 9091 conflicts with control"; then
echo "failed: can't detect port conflicts"
exit 1
fi
diff --git a/t/cli/test_prometheus.sh b/t/cli/test_prometheus.sh
index 96287ed..206b76d 100755
--- a/t/cli/test_prometheus.sh
+++ b/t/cli/test_prometheus.sh
@@ -90,4 +90,34 @@ fi
make stop
+echo '
+plugin_attr:
+ prometheus:
+ export_addr:
+ ip: ${{IP}}
+ port: ${{PORT}}
+' > conf/config.yaml
+
+out=$(IP=127.0.0.1 PORT=9090 make init 2>&1 || true)
+if ! echo "$out" | grep "prometheus port 9090 conflicts with control"; then
+ echo "failed: can't detect port conflicts"
+ exit 1
+fi
+
+echo '
+apisix:
+ node_listen: ${{PORT}}
+plugin_attr:
+ prometheus:
+ export_addr:
+ ip: ${{IP}}
+ port: ${{PORT}}
+' > conf/config.yaml
+
+out=$(IP=127.0.0.1 PORT=9092 make init 2>&1 || true)
+if ! echo "$out" | grep "node_listen port 9092 conflicts with prometheus"; then
+ echo "failed: can't detect port conflicts"
+ exit 1
+fi
+
echo "passed: should listen at previous prometheus address"