Skip to content

Commit

Permalink
Add labels for upstream object
Browse files Browse the repository at this point in the history
labels can be used to specify additional attributes.

Signed-off-by: imjoey <majunjiev@gmail.com>
  • Loading branch information
imjoey committed Sep 22, 2020
1 parent 6a1fe8a commit 654f8a7
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 0 deletions.
4 changes: 4 additions & 0 deletions apisix/schema_def.lua
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,10 @@ local upstream_schema = {
description = "enable websocket for request",
type = "boolean"
},
labels = {
description = "key/value pairs to specify attributes",
type = "table"
},
pass_host = {
description = "mod of host passing",
type = "string",
Expand Down
1 change: 1 addition & 0 deletions doc/architecture-design.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ In addition to the basic complex equalization algorithm selection, APISIX's Upst
|enable_websocket|optional| enable `websocket`(boolean), default `false`.|
|timeout|optional| Set the timeout for connection, sending and receiving messages. |
|desc |optional|Identifies route names, usage scenarios, and more.|
|labels |optional|The key/value pairs to specify attributes. |
|pass_host |optional|`pass` pass the client request host, `node` not pass the client request host, using the upstream node host, `rewrite` rewrite host by the configured `upstream_host`.|
|upstream_host |optional|This option is only valid if the `pass_host` is `rewrite`.|

Expand Down
1 change: 1 addition & 0 deletions doc/zh-cn/architecture-design.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ APISIX 的 Upstream 除了基本的复杂均衡算法选择外,还支持对上
|checks |可选|配置健康检查的参数,详细可参考[health-check](../health-check.md)|
|retries |可选|使用底层的 Nginx 重试机制将请求传递给下一个上游,默认 APISIX 会启用重试机制,根据配置的后端节点个数设置重试次数,如果此参数显式被设置将会覆盖系统默认设置的重试次数。|
|enable_websocket|可选| 是否启用 `websocket`(布尔值),默认不启用|
|labels |可选| 用于标识属性的键值对。 |
|pass_host |可选|`pass` 透传客户端请求的 host, `node` 不透传客户端请求的 host, 使用 upstream node 配置的 host, `rewrite` 使用 `upstream_host` 配置的值重写 host 。|
|upstream_host |可选|只在 `pass_host` 配置为 `rewrite` 时有效。|

Expand Down
134 changes: 134 additions & 0 deletions t/admin/upstream.t
Original file line number Diff line number Diff line change
Expand Up @@ -1637,3 +1637,137 @@ GET /t
--- error_code: 400
--- no_error_log
[error]



=== TEST 50: set upstream(with labels)
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/upstreams/1',
ngx.HTTP_PUT,
[[{
"nodes": {
"127.0.0.1:8080": 1
},
"type": "roundrobin",
"labels": {
"key1": "value1",
"key2": "value2",
"key3": "value3"
}
}]],
[[{
"node": {
"value": {
"nodes": {
"127.0.0.1:8080": 1
},
"type": "roundrobin",
"labels": {
"key1": "value1",
"key2": "value2",
"key3": "value3"
}
},
"key": "/apisix/upstreams/1"
},
"action": "set"
}]]
)

ngx.status = code
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
--- no_error_log
[error]



=== TEST 51: get upstream(with labels)
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/upstreams/1',
ngx.HTTP_GET,
nil,
[[{
"node": {
"value": {
"nodes": {
"127.0.0.1:8080": 1
},
"type": "roundrobin",
"labels": {
"key1": "value1",
"key2": "value2",
"key3": "value3"
}
},
"key": "/apisix/upstreams/1"
},
"action": "get"
}]]
)

ngx.status = code
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
--- no_error_log
[error]



=== TEST 52: patch upstream(only labels)
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/upstreams/1',
ngx.HTTP_PATCH,
[[{
"labels": {
"key1": "new-value1"
}
}]],
[[{
"node": {
"value": {
"nodes": {
"127.0.0.1:8080": 1
},
"type": "roundrobin",
"labels": {
"key1": "new-value1",
"key2": "value2",
"key3": "value3"
}
},
"key": "/apisix/upstreams/1"
},
"action": "compareAndSwap"
}]]
)

ngx.status = code
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
--- no_error_log
[error]

0 comments on commit 654f8a7

Please sign in to comment.