Skip to content

Commit

Permalink
chore: 🤖 优化CR中提出的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
heimanba committed Sep 18, 2024
1 parent 102f5f6 commit 7a2926e
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 35 deletions.
1 change: 1 addition & 0 deletions plugins/wasm-go/extensions/frontend-gray/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
|----------------|--------------|----|-----|----------------------------------------------------------------------------------------------------|
| `grayKey` | string | 非必填 | - | 用户ID的唯一标识,可以来自Cookie或者Header中,比如 userid,如果没有填写则使用`rules[].grayTagKey``rules[].grayTagValue`过滤灰度规则 |
| `graySubKey` | string | 非必填 | - | 用户身份信息可能以JSON形式透出,比如:`userInfo:{ userCode:"001" }`,当前例子`graySubKey`取值为`userCode` |
| `userStickyMaxAge` | string | 非必填 | 172800 | 用户粘滞的时长:单位为秒,默认为`172800`,2天时间 |
| `rules` | array of object | 必填 | - | 用户定义不同的灰度规则,适配不同的灰度场景 |
| `rewrite` | object | 必填 | - | 重写配置,一般用于OSS/CDN前端部署的重写配置 |
| `baseDeployment` | object | 非必填 | - | 配置Base基线规则的配置 |
Expand Down
29 changes: 17 additions & 12 deletions plugins/wasm-go/extensions/frontend-gray/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ const (
XHigressTag = "x-higress-tag"
XUniqueClient = "x-unique-client"
XPreHigressTag = "x-pre-higress-tag"
IsIndex = "is-index"
IsPageRequest = "is-page-request"
IsNotFound = "is-not-found"
// 2 days
MaxAgeCookie = "172800"
)

type LogInfo func(format string, args ...interface{})
Expand Down Expand Up @@ -51,15 +49,16 @@ type BodyInjection struct {
}

type GrayConfig struct {
TotalGrayWeight int
GrayKey string
GraySubKey string
Rules []*GrayRule
Rewrite *Rewrite
BaseDeployment *Deployment
GrayDeployments []*Deployment
BackendGrayTag string
Injection *Injection
UserStickyMaxAge string
TotalGrayWeight int
GrayKey string
GraySubKey string
Rules []*GrayRule
Rewrite *Rewrite
BaseDeployment *Deployment
GrayDeployments []*Deployment
BackendGrayTag string
Injection *Injection
}

func convertToStringList(results []gjson.Result) []string {
Expand All @@ -84,6 +83,12 @@ func JsonToGrayConfig(json gjson.Result, grayConfig *GrayConfig) {
grayConfig.GrayKey = json.Get("grayKey").String()
grayConfig.GraySubKey = json.Get("graySubKey").String()
grayConfig.BackendGrayTag = json.Get("backendGrayTag").String()
grayConfig.UserStickyMaxAge = json.Get("userStickyMaxAge").String()

if grayConfig.UserStickyMaxAge == "" {
// 默认值2天
grayConfig.UserStickyMaxAge = "172800"
}

if grayConfig.BackendGrayTag == "" {
grayConfig.BackendGrayTag = "x-mse-tag"
Expand Down
1 change: 1 addition & 0 deletions plugins/wasm-go/extensions/frontend-gray/envoy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ static_resources:
{
"grayKey": "userId",
"backendGrayTag": "x-mse-tag",
"userStickyMaxAge": 172800,
"rules": [
{
"name": "inner-user",
Expand Down
26 changes: 13 additions & 13 deletions plugins/wasm-go/extensions/frontend-gray/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, grayConfig config.GrayConfig,
path, _ := proxywasm.GetHttpRequestHeader(":path")
fetchMode, _ := proxywasm.GetHttpRequestHeader("sec-fetch-mode")

isIndex := util.IsIndexRequest(fetchMode, path)
isPageRequest := util.GetIsPageRequest(fetchMode, path)
hasRewrite := len(grayConfig.Rewrite.File) > 0 || len(grayConfig.Rewrite.Index) > 0
grayKeyValueByCookie := util.ExtractCookieValueByKey(cookies, grayConfig.GrayKey)
grayKeyValueByHeader, _ := proxywasm.GetHttpRequestHeader(grayConfig.GrayKey)
Expand Down Expand Up @@ -70,7 +70,7 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, grayConfig config.GrayConfig,
}

// 如果没有配置比例,则进行灰度规则匹配
if isIndex {
if isPageRequest {
log.Infof("grayConfig.TotalGrayWeight==== %v", grayConfig.TotalGrayWeight)
if grayConfig.TotalGrayWeight > 0 {
deployment = util.FilterGrayWeight(&grayConfig, preVersions, uniqueClientId)
Expand All @@ -79,13 +79,13 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, grayConfig config.GrayConfig,
}
log.Infof("index deployment: %v, path: %v, backend: %v, xPreHigressVersion: %v", deployment, path, deployment.BackendVersion, xPreHigressVersion)
} else {
deployment = util.GetVersion(grayConfig, deployment, preVersions[0], isIndex)
deployment = util.GetVersion(grayConfig, deployment, preVersions[0], isPageRequest)
}
proxywasm.AddHttpRequestHeader(config.XHigressTag, deployment.Version)

ctx.SetContext(config.XPreHigressTag, deployment.Version)
ctx.SetContext(grayConfig.BackendGrayTag, deployment.BackendVersion)
ctx.SetContext(config.IsIndex, isIndex)
ctx.SetContext(config.IsPageRequest, isPageRequest)
ctx.SetContext(config.XUniqueClient, uniqueClientId)

rewrite := grayConfig.Rewrite
Expand All @@ -95,7 +95,7 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, grayConfig config.GrayConfig,

if hasRewrite {
rewritePath := path
if isIndex {
if isPageRequest {
rewritePath = util.IndexRewrite(path, deployment.Version, grayConfig.Rewrite.Index)
} else {
rewritePath = util.PrefixFileRewrite(path, deployment.Version, grayConfig.Rewrite.File)
Expand All @@ -119,11 +119,11 @@ func onHttpResponseHeader(ctx wrapper.HttpContext, grayConfig config.GrayConfig,
proxywasm.RemoveHttpResponseHeader("Content-Disposition")
}

isIndex := ctx.GetContext(config.IsIndex).(bool)
isPageRequest := ctx.GetContext(config.IsPageRequest).(bool)

if err != nil || status != "200" {
if status == "404" {
if grayConfig.Rewrite.NotFound != "" && isIndex {
if grayConfig.Rewrite.NotFound != "" && isPageRequest {
ctx.SetContext(config.IsNotFound, true)
responseHeaders, _ := proxywasm.GetHttpResponseHeaders()
headersMap := util.ConvertHeaders(responseHeaders)
Expand Down Expand Up @@ -153,7 +153,7 @@ func onHttpResponseHeader(ctx wrapper.HttpContext, grayConfig config.GrayConfig,
// 删除content-length,可能要修改Response返回值
proxywasm.RemoveHttpResponseHeader("Content-Length")

if strings.HasPrefix(contentType, "text/html") || isIndex {
if strings.HasPrefix(contentType, "text/html") || isPageRequest {
// 不会进去Streaming 的Body处理
ctx.BufferResponseBody()

Expand All @@ -163,11 +163,11 @@ func onHttpResponseHeader(ctx wrapper.HttpContext, grayConfig config.GrayConfig,
xUniqueClient := ctx.GetContext(config.XUniqueClient).(string)

// 设置前端的版本
proxywasm.AddHttpResponseHeader("Set-Cookie", fmt.Sprintf("%s=%s,%s; Max-Age=%s; Path=/;", config.XPreHigressTag, frontendVersion, xUniqueClient, config.MaxAgeCookie))
proxywasm.AddHttpResponseHeader("Set-Cookie", fmt.Sprintf("%s=%s,%s; Max-Age=%s; Path=/;", config.XPreHigressTag, frontendVersion, xUniqueClient, grayConfig.UserStickyMaxAge))
// 设置后端的版本
if util.IsBackendGrayEnabled(grayConfig) {
backendVersion := ctx.GetContext(grayConfig.BackendGrayTag).(string)
proxywasm.AddHttpResponseHeader("Set-Cookie", fmt.Sprintf("%s=%s; Max-Age=%s; Path=/;", grayConfig.BackendGrayTag, backendVersion, config.MaxAgeCookie))
proxywasm.AddHttpResponseHeader("Set-Cookie", fmt.Sprintf("%s=%s; Max-Age=%s; Path=/;", grayConfig.BackendGrayTag, backendVersion, grayConfig.UserStickyMaxAge))
}
}
return types.ActionContinue
Expand All @@ -177,11 +177,11 @@ func onHttpResponseBody(ctx wrapper.HttpContext, grayConfig config.GrayConfig, b
if !util.IsGrayEnabled(grayConfig) {
return types.ActionContinue
}
isIndex := ctx.GetContext(config.IsIndex).(bool)
isPageRequest := ctx.GetContext(config.IsPageRequest).(bool)
frontendVersion := ctx.GetContext(config.XPreHigressTag).(string)

isNotFound := ctx.GetContext(config.IsNotFound)
if isIndex && isNotFound != nil && isNotFound.(bool) && grayConfig.Rewrite.Host != "" && grayConfig.Rewrite.NotFound != "" {
if isPageRequest && isNotFound != nil && isNotFound.(bool) && grayConfig.Rewrite.Host != "" && grayConfig.Rewrite.NotFound != "" {
client := wrapper.NewClusterClient(wrapper.RouteCluster{Host: grayConfig.Rewrite.Host})

client.Get(strings.Replace(grayConfig.Rewrite.NotFound, "{version}", frontendVersion, -1), nil, func(statusCode int, responseHeaders http.Header, responseBody []byte) {
Expand All @@ -191,7 +191,7 @@ func onHttpResponseBody(ctx wrapper.HttpContext, grayConfig config.GrayConfig, b
return types.ActionPause
}

if isIndex {
if isPageRequest {
// 将原始字节转换为字符串
newBody := string(body)

Expand Down
7 changes: 3 additions & 4 deletions plugins/wasm-go/extensions/frontend-gray/util/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ var indexSuffixes = []string{
".html", ".htm", ".jsp", ".php", ".asp", ".aspx", ".erb", ".ejs", ".twig",
}

// IsIndexRequest determines if the request is an index request
func IsIndexRequest(fetchMode string, p string) bool {
func GetIsPageRequest(fetchMode string, p string) bool {
if fetchMode == "cors" {
return false
}
Expand Down Expand Up @@ -154,8 +153,8 @@ func PrefixFileRewrite(path, version string, matchRules map[string]string) strin
return filepath.Clean(newPath)
}

func GetVersion(grayConfig config.GrayConfig, deployment *config.Deployment, xPreHigressVersion string, isIndex bool) *config.Deployment {
if isIndex {
func GetVersion(grayConfig config.GrayConfig, deployment *config.Deployment, xPreHigressVersion string, isPageRequest bool) *config.Deployment {
if isPageRequest {
return deployment
}
// cookie 中为空,返回当前版本
Expand Down
10 changes: 4 additions & 6 deletions plugins/wasm-go/extensions/frontend-gray/util/utils_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package util

import (
"os"
"testing"

"github.com/alibaba/higress/plugins/wasm-go/extensions/frontend-gray/config"
Expand Down Expand Up @@ -83,7 +82,7 @@ func TestPrefixFileRewrite(t *testing.T) {
}
}

func TestIsIndexRequest(t *testing.T) {
func TestGetIsPageRequest(t *testing.T) {
var tests = []struct {
fetchMode string
p string
Expand All @@ -100,7 +99,7 @@ func TestIsIndexRequest(t *testing.T) {
for _, test := range tests {
testPath := test.p
t.Run(testPath, func(t *testing.T) {
output := IsIndexRequest(test.fetchMode, testPath)
output := GetIsPageRequest(test.fetchMode, testPath)
assert.Equal(t, test.output, output)
})
}
Expand All @@ -117,10 +116,9 @@ func TestFilterGrayWeight(t *testing.T) {
testName := test.name
t.Run(testName, func(t *testing.T) {
grayConfig := &config.GrayConfig{}
os.Setenv("TEST_MODE", "true")
config.JsonToGrayConfig(gjson.Parse(test.input), grayConfig)
reslut := FilterGrayWeight(grayConfig, []string{"base", "1.0.1"}, "192.168.1.1")
t.Logf("reslut-----: %v", reslut)
result := FilterGrayWeight(grayConfig, []string{"base", "1.0.1"}, "192.168.1.1")
t.Logf("result-----: %v", result)
})
}
}

0 comments on commit 7a2926e

Please sign in to comment.