mirror of
https://github.com/alibaba/higress.git
synced 2026-04-21 20:17:29 +08:00
[frontend-gray] 重构业务逻辑,对于微前端和多版本支持更加友好 (#2011)
This commit is contained in:
@@ -1,23 +1,23 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
const (
|
||||
XHigressTag = "x-higress-tag"
|
||||
XUniqueClientId = "x-unique-client"
|
||||
XPreHigressTag = "x-pre-higress-tag"
|
||||
IsPageRequest = "is-page-request"
|
||||
IsNotFound = "is-not-found"
|
||||
EnabledGray = "enabled-gray"
|
||||
SecFetchMode = "sec-fetch-mode"
|
||||
XHigressTag = "x-higress-tag"
|
||||
PreHigressVersion = "pre-higress-version"
|
||||
IsHtmlRequest = "is-html-request"
|
||||
IsIndexRequest = "is-index-request"
|
||||
EnabledGray = "enabled-gray"
|
||||
)
|
||||
|
||||
type LogInfo func(format string, args ...interface{})
|
||||
|
||||
type GrayRule struct {
|
||||
Name string
|
||||
GrayKeyValue []string
|
||||
@@ -35,15 +35,22 @@ type Deployment struct {
|
||||
}
|
||||
|
||||
type Rewrite struct {
|
||||
Host string
|
||||
NotFound string
|
||||
Index map[string]string
|
||||
File map[string]string
|
||||
Host string
|
||||
Index map[string]string
|
||||
File map[string]string
|
||||
}
|
||||
|
||||
type Injection struct {
|
||||
Head []string
|
||||
Body *BodyInjection
|
||||
GlobalConfig *GlobalConfig
|
||||
Head []string
|
||||
Body *BodyInjection
|
||||
}
|
||||
|
||||
type GlobalConfig struct {
|
||||
Key string
|
||||
FeatureKey string
|
||||
Value string
|
||||
Enabled bool
|
||||
}
|
||||
|
||||
type BodyInjection struct {
|
||||
@@ -52,8 +59,7 @@ type BodyInjection struct {
|
||||
}
|
||||
|
||||
type GrayConfig struct {
|
||||
UserStickyMaxAge string
|
||||
TotalGrayWeight int
|
||||
StoreMaxAge int
|
||||
GrayKey string
|
||||
LocalStorageGrayKey string
|
||||
GraySubKey string
|
||||
@@ -63,10 +69,26 @@ type GrayConfig struct {
|
||||
BaseDeployment *Deployment
|
||||
GrayDeployments []*Deployment
|
||||
BackendGrayTag string
|
||||
UniqueGrayTag string
|
||||
Injection *Injection
|
||||
SkippedPathPrefixes []string
|
||||
IncludePathPrefixes []string
|
||||
SkippedPaths []string
|
||||
SkippedByHeaders map[string]string
|
||||
IndexPaths []string
|
||||
GrayWeight int
|
||||
}
|
||||
|
||||
func isValidName(s string) bool {
|
||||
// 定义一个正则表达式,匹配字母、数字和下划线
|
||||
re := regexp.MustCompile(`^[a-zA-Z0-9_]+$`)
|
||||
return re.MatchString(s)
|
||||
}
|
||||
|
||||
func GetWithDefault(json gjson.Result, path, defaultValue string) string {
|
||||
res := json.Get(path)
|
||||
if res.Exists() {
|
||||
return res.String()
|
||||
}
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
func convertToStringList(results []gjson.Result) []string {
|
||||
@@ -77,6 +99,22 @@ func convertToStringList(results []gjson.Result) []string {
|
||||
return interfaces
|
||||
}
|
||||
|
||||
func compatibleConvertToStringList(results []gjson.Result, compatibleResults []gjson.Result) []string {
|
||||
// 优先使用兼容模式的数据
|
||||
if len(compatibleResults) == 0 {
|
||||
interfaces := make([]string, len(results)) // 预分配切片容量
|
||||
for i, result := range results {
|
||||
interfaces[i] = result.String() // 使用 String() 方法直接获取字符串
|
||||
}
|
||||
return interfaces
|
||||
}
|
||||
compatibleInterfaces := make([]string, len(compatibleResults)) // 预分配切片容量
|
||||
for i, result := range compatibleResults {
|
||||
compatibleInterfaces[i] = filepath.Join(result.String(), "**")
|
||||
}
|
||||
return compatibleInterfaces
|
||||
}
|
||||
|
||||
func convertToStringMap(result gjson.Result) map[string]string {
|
||||
m := make(map[string]string)
|
||||
result.ForEach(func(key, value gjson.Result) bool {
|
||||
@@ -86,7 +124,7 @@ func convertToStringMap(result gjson.Result) map[string]string {
|
||||
return m
|
||||
}
|
||||
|
||||
func JsonToGrayConfig(json gjson.Result, grayConfig *GrayConfig) {
|
||||
func JsonToGrayConfig(json gjson.Result, grayConfig *GrayConfig) error {
|
||||
// 解析 GrayKey
|
||||
grayConfig.LocalStorageGrayKey = json.Get("localStorageGrayKey").String()
|
||||
grayConfig.GrayKey = json.Get("grayKey").String()
|
||||
@@ -94,22 +132,18 @@ func JsonToGrayConfig(json gjson.Result, grayConfig *GrayConfig) {
|
||||
grayConfig.GrayKey = grayConfig.LocalStorageGrayKey
|
||||
}
|
||||
grayConfig.GraySubKey = json.Get("graySubKey").String()
|
||||
grayConfig.BackendGrayTag = json.Get("backendGrayTag").String()
|
||||
grayConfig.UserStickyMaxAge = json.Get("userStickyMaxAge").String()
|
||||
grayConfig.BackendGrayTag = GetWithDefault(json, "backendGrayTag", "x-mse-tag")
|
||||
grayConfig.UniqueGrayTag = GetWithDefault(json, "uniqueGrayTag", "x-higress-uid")
|
||||
grayConfig.StoreMaxAge = 60 * 60 * 24 * 365 // 默认一年
|
||||
storeMaxAge, err := strconv.Atoi(GetWithDefault(json, "StoreMaxAge", strconv.Itoa(grayConfig.StoreMaxAge)))
|
||||
if err != nil {
|
||||
grayConfig.StoreMaxAge = storeMaxAge
|
||||
}
|
||||
|
||||
grayConfig.Html = json.Get("html").String()
|
||||
grayConfig.SkippedPathPrefixes = convertToStringList(json.Get("skippedPathPrefixes").Array())
|
||||
grayConfig.SkippedPaths = compatibleConvertToStringList(json.Get("skippedPaths").Array(), json.Get("skippedPathPrefixes").Array())
|
||||
grayConfig.IndexPaths = compatibleConvertToStringList(json.Get("indexPaths").Array(), json.Get("includePathPrefixes").Array())
|
||||
grayConfig.SkippedByHeaders = convertToStringMap(json.Get("skippedByHeaders"))
|
||||
grayConfig.IncludePathPrefixes = convertToStringList(json.Get("includePathPrefixes").Array())
|
||||
|
||||
if grayConfig.UserStickyMaxAge == "" {
|
||||
// 默认值2天
|
||||
grayConfig.UserStickyMaxAge = "172800"
|
||||
}
|
||||
|
||||
if grayConfig.BackendGrayTag == "" {
|
||||
grayConfig.BackendGrayTag = "x-mse-tag"
|
||||
}
|
||||
|
||||
// 解析 Rules
|
||||
rules := json.Get("rules").Array()
|
||||
for _, rule := range rules {
|
||||
@@ -122,10 +156,9 @@ func JsonToGrayConfig(json gjson.Result, grayConfig *GrayConfig) {
|
||||
grayConfig.Rules = append(grayConfig.Rules, &grayRule)
|
||||
}
|
||||
grayConfig.Rewrite = &Rewrite{
|
||||
Host: json.Get("rewrite.host").String(),
|
||||
NotFound: json.Get("rewrite.notFoundUri").String(),
|
||||
Index: convertToStringMap(json.Get("rewrite.indexRouting")),
|
||||
File: convertToStringMap(json.Get("rewrite.fileRouting")),
|
||||
Host: json.Get("rewrite.host").String(),
|
||||
Index: convertToStringMap(json.Get("rewrite.indexRouting")),
|
||||
File: convertToStringMap(json.Get("rewrite.fileRouting")),
|
||||
}
|
||||
|
||||
// 解析 deployment
|
||||
@@ -134,6 +167,7 @@ func JsonToGrayConfig(json gjson.Result, grayConfig *GrayConfig) {
|
||||
|
||||
grayConfig.BaseDeployment = &Deployment{
|
||||
Name: baseDeployment.Get("name").String(),
|
||||
BackendVersion: baseDeployment.Get("backendVersion").String(),
|
||||
Version: strings.Trim(baseDeployment.Get("version").String(), " "),
|
||||
VersionPredicates: convertToStringMap(baseDeployment.Get("versionPredicates")),
|
||||
}
|
||||
@@ -141,16 +175,28 @@ func JsonToGrayConfig(json gjson.Result, grayConfig *GrayConfig) {
|
||||
if !item.Get("enabled").Bool() {
|
||||
continue
|
||||
}
|
||||
grayWeight := int(item.Get("weight").Int())
|
||||
weight := int(item.Get("weight").Int())
|
||||
grayConfig.GrayDeployments = append(grayConfig.GrayDeployments, &Deployment{
|
||||
Name: item.Get("name").String(),
|
||||
Enabled: item.Get("enabled").Bool(),
|
||||
Version: strings.Trim(item.Get("version").String(), " "),
|
||||
BackendVersion: item.Get("backendVersion").String(),
|
||||
Weight: grayWeight,
|
||||
Weight: weight,
|
||||
VersionPredicates: convertToStringMap(item.Get("versionPredicates")),
|
||||
})
|
||||
grayConfig.TotalGrayWeight += grayWeight
|
||||
if weight > 0 {
|
||||
grayConfig.GrayWeight = weight
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
injectGlobalFeatureKey := GetWithDefault(json, "injection.globalConfig.featureKey", "FEATURE_STATUS")
|
||||
injectGlobalKey := GetWithDefault(json, "injection.globalConfig.key", "HIGRESS_CONSOLE_CONFIG")
|
||||
if !isValidName(injectGlobalFeatureKey) {
|
||||
return errors.New("injection.globalConfig.featureKey is invalid")
|
||||
}
|
||||
if !isValidName(injectGlobalKey) {
|
||||
return errors.New("injection.globalConfig.featureKey is invalid")
|
||||
}
|
||||
|
||||
grayConfig.Injection = &Injection{
|
||||
@@ -159,5 +205,12 @@ func JsonToGrayConfig(json gjson.Result, grayConfig *GrayConfig) {
|
||||
First: convertToStringList(json.Get("injection.body.first").Array()),
|
||||
Last: convertToStringList(json.Get("injection.body.last").Array()),
|
||||
},
|
||||
GlobalConfig: &GlobalConfig{
|
||||
FeatureKey: injectGlobalFeatureKey,
|
||||
Key: injectGlobalKey,
|
||||
Value: json.Get("injection.globalConfig.value").String(),
|
||||
Enabled: json.Get("injection.globalConfig.enabled").Bool(),
|
||||
},
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user