[frontend-gray] Increase gray types according to the ratio-weight gray (#1291)

This commit is contained in:
mamba
2024-09-22 16:49:54 +08:00
committed by GitHub
parent ffc5458a91
commit ee67553816
6 changed files with 387 additions and 118 deletions

View File

@@ -1,16 +1,17 @@
package config
import (
"strings"
"github.com/tidwall/gjson"
)
const (
XHigressTag = "x-higress-tag"
XPreHigressTag = "x-pre-higress-tag"
XMseTag = "x-mse-tag"
IsHTML = "is_html"
IsIndex = "is_index"
NotFound = "not_found"
XHigressTag = "x-higress-tag"
XUniqueClientId = "x-unique-client"
XPreHigressTag = "x-pre-higress-tag"
IsPageRequest = "is-page-request"
IsNotFound = "is-not-found"
)
type LogInfo func(format string, args ...interface{})
@@ -22,16 +23,12 @@ type GrayRule struct {
GrayTagValue []string
}
type BaseDeployment struct {
Name string
Version string
}
type GrayDeployment struct {
type Deployment struct {
Name string
Enabled bool
Version string
BackendVersion string
Weight int
}
type Rewrite struct {
@@ -41,13 +38,27 @@ type Rewrite struct {
File map[string]string
}
type Injection struct {
Head []string
Body *BodyInjection
}
type BodyInjection struct {
First []string
Last []string
}
type GrayConfig struct {
GrayKey string
GraySubKey string
Rules []*GrayRule
Rewrite *Rewrite
BaseDeployment *BaseDeployment
GrayDeployments []*GrayDeployment
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 {
@@ -71,6 +82,17 @@ func JsonToGrayConfig(json gjson.Result, grayConfig *GrayConfig) {
// 解析 GrayKey
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"
}
// 解析 Rules
rules := json.Get("rules").Array()
@@ -94,16 +116,30 @@ func JsonToGrayConfig(json gjson.Result, grayConfig *GrayConfig) {
baseDeployment := json.Get("baseDeployment")
grayDeployments := json.Get("grayDeployments").Array()
grayConfig.BaseDeployment = &BaseDeployment{
grayConfig.BaseDeployment = &Deployment{
Name: baseDeployment.Get("name").String(),
Version: baseDeployment.Get("version").String(),
Version: strings.Trim(baseDeployment.Get("version").String(), " "),
}
for _, item := range grayDeployments {
grayConfig.GrayDeployments = append(grayConfig.GrayDeployments, &GrayDeployment{
if !item.Get("enabled").Bool() {
continue
}
grayWeight := int(item.Get("weight").Int())
grayConfig.GrayDeployments = append(grayConfig.GrayDeployments, &Deployment{
Name: item.Get("name").String(),
Enabled: item.Get("enabled").Bool(),
Version: item.Get("version").String(),
Version: strings.Trim(item.Get("version").String(), " "),
BackendVersion: item.Get("backendVersion").String(),
Weight: grayWeight,
})
grayConfig.TotalGrayWeight += grayWeight
}
grayConfig.Injection = &Injection{
Head: convertToStringList(json.Get("injection.head").Array()),
Body: &BodyInjection{
First: convertToStringList(json.Get("injection.body.first").Array()),
Last: convertToStringList(json.Get("injection.body.last").Array()),
},
}
}