Skip to content

Commit

Permalink
fix: support for serviceName and serviceHost
Browse files Browse the repository at this point in the history
  • Loading branch information
haifzhu committed Dec 28, 2024
1 parent 296f0a0 commit 58a02c0
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 25 deletions.
17 changes: 9 additions & 8 deletions plugins/wasm-go/extensions/try-paths/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,22 @@ description: 请求重试

| 名称 | 数据类型 | 填写要求 | 默认值 | 描述 |
| -------- | -------- | -------- | -------- | -------- |
| host | string | 必填 | - | FQDN格式的主机名称,比如<bucket name>.oss-cn-hangzhou.aliyuncs.com |
| servicePort | string | 必填 | - | 服务端口 |
| tryPaths | array of string | 必填 | - | 重试路径,比如`index.html``$uri`, `index.html` |
| tryCodes | array of int | 非必填 | [403, 404] | 重试状态码,可自定义 |
| timeout | int | 非必填 | 1000 | 重试请求的超时时间,单位ms |
| `service_name` | string | 必填 | - | 输入重试服务名称,带服务类型的完整 FQDN 名称,例如 `<bucket name>.oss-cn-hangzhou.aliyuncs.com` |
| `service_port` | int | 非必填 | 80 | 输入重试服务的服务端口 |
| `service_host` | string | 非必填 | - | 请求重试服务时设置的Host头,不填时和FQDN保持一致 |
| `try_paths` | array of string | 必填 | - | 重试路径,比如`index.html``$uri`, `index.html` |
| `try_codes` | array of int | 非必填 | [403, 404] | 重试状态码,可自定义 |
| `timeout` | int | 非必填 | 1000 | 重试请求的超时时间,单位ms |


# 配置示例

## 配置了try-paths插件的场景

```yaml
servicePort: 80
host: "<bucket name>.oss-cn-hangzhou.aliyuncs.com"
tryPaths:
service_port: 80
service_host: "<bucket name>.oss-cn-hangzhou.aliyuncs.com"
try_paths:
- "$uri/"
- "$uri.html"
- "/index.html"
Expand Down
17 changes: 9 additions & 8 deletions plugins/wasm-go/extensions/try-paths/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,21 @@ description: request retry

| Name | Data Type | Fill Requirement | Default Value | Description |
| -------- | -------- | -------- | -------- | -------- |
| host | string | Required | - | the FQDN format of the host, i.e. <bucket name>.oss-cn-hangzhou.aliyuncs.com |
| servicePort | string | Required | - | Service Port |
| tryPaths | array of string | Required | - | Try path list,`index.html``$uri/`, `index.html` for example |
| tryCodes | array of int | Optional | [403, 404] | Try response code,can be customized |
| timeout | int | Optional | 1000 | The timeout for try request,unit is ms |
| `service_name` | string | Required | - | Input the name of the try service, in complete FQDN format, e.g., `<bucket name>.oss-cn-hangzhou.aliyuncs.com` |
| `service_port` | int | No | 80 | Input the port of the try service |
| `service_host` | string | No | - | The Host header set when requesting the try service; remains the same as FQDN if not filled |
| `try_paths` | array of string | Required | - | Try path list,`index.html``$uri/`, `index.html` for example |
| `try_codes` | array of int | Optional | [403, 404] | Try response code,can be customized |
| `timeout` | int | Optional | 1000 | The timeout for try request,unit is ms |

# Configuration Example

## scene with try-paths plugin configured

```yaml
servicePort: 80
host: "<bucket name>.oss-cn-hangzhou.aliyuncs.com"
tryPaths:
service_port: 80
service_name: "<bucket name>.oss-cn-hangzhou.aliyuncs.com"
try_paths:
- "$uri/"
- "$uri.html"
- "/index.html"
Expand Down
6 changes: 3 additions & 3 deletions plugins/wasm-go/extensions/try-paths/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ type TryPathsConfig struct {
}

func parseConfig(json gjson.Result, config *TryPathsConfig, log wrapper.Log) error {
for _, result := range json.Get("tryPaths").Array() {
for _, result := range json.Get("try_paths").Array() {
config.tryPaths = append(config.tryPaths, result.String())
}

if json.Get("tryCodes").String() == "" {
if json.Get("try_codes").String() == "" {
// tryCodes默认值为["404", "403"]
config.tryCodes = []int{http.StatusNotFound, http.StatusForbidden}
} else {
for _, result := range json.Get("code").Array() {
for _, result := range json.Get("try_codes").Array() {
config.tryCodes = append(config.tryCodes, int(result.Int()))
}
}
Expand Down
11 changes: 8 additions & 3 deletions plugins/wasm-go/extensions/try-paths/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,15 @@ func contains(array []int, value int) bool {
}

func Client(json gjson.Result) (wrapper.HttpClient, error) {
host := json.Get("host").String()
servicePort := json.Get("servicePort").Int()
serviceName := json.Get("service_name").String()
serviceHost := json.Get("service_host").String()
servicePort := json.Get("service_port").Int()
if servicePort == 0 {
servicePort = 80
}
return wrapper.NewClusterClient(wrapper.FQDNCluster{
FQDN: host,
FQDN: serviceName,
Host: serviceHost,
Port: servicePort,
}), nil
}
6 changes: 3 additions & 3 deletions test/e2e/conformance/tests/go-wasm-try-paths.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ spec:
defaultConfigDisable: false
matchRules:
- config:
servicePort: 8080
host: infra-backend-v2.higress-conformance-infra.svc.cluster.local
tryPaths:
service_port: 8080
service_name: infra-backend-v2.higress-conformance-infra.svc.cluster.local
try_paths:
- /path1
configDisable: false
ingress:
Expand Down

0 comments on commit 58a02c0

Please sign in to comment.