mirror of
https://github.com/alibaba/higress.git
synced 2026-05-08 04:17:27 +08:00
feat: add more precedence rules for route matching (#214)
Signed-off-by: charlie <qianglin98@qq.com>
This commit is contained in:
@@ -17,9 +17,11 @@ package ingressv1
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"hash/fnv"
|
||||
"path"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -476,9 +478,13 @@ func (c *controller) ConvertHTTPRoute(convertOptions *common.ConvertOptions, wra
|
||||
// When the host, pathType, path of two rule are same, we think there is a conflict event.
|
||||
definedRules := sets.NewSet()
|
||||
|
||||
// But in across ingresses case, we will restrict this limit.
|
||||
// When the host, path of two rule in different ingress are same, we think there is a conflict event.
|
||||
var tempHostAndPath []string
|
||||
var (
|
||||
// But in across ingresses case, we will restrict this limit.
|
||||
// When the {host, path, headers, method, params} of two rule in different ingress are same, we think there is a conflict event.
|
||||
tempRuleHash []uint32
|
||||
tempRuleKey []string
|
||||
)
|
||||
|
||||
for _, rule := range ingressV1.Rules {
|
||||
if rule.HTTP == nil || len(rule.HTTP.Paths) == 0 {
|
||||
IngressLog.Warnf("invalid ingress rule %s:%s for host %q in cluster %s, no paths defined", cfg.Namespace, cfg.Name, rule.Host, c.options.ClusterId)
|
||||
@@ -548,13 +554,19 @@ func (c *controller) ConvertHTTPRoute(convertOptions *common.ConvertOptions, wra
|
||||
|
||||
ingressRouteBuilder := convertOptions.IngressRouteCache.New(wrapperHttpRoute)
|
||||
|
||||
// host and path overlay check across different ingresses.
|
||||
hostAndPath := wrapperHttpRoute.BasePathFormat()
|
||||
if preIngress, exist := convertOptions.HostAndPath2Ingress[hostAndPath]; exist {
|
||||
ingressRouteBuilder.PreIngress = preIngress
|
||||
hostAndPath := wrapperHttpRoute.PathFormat()
|
||||
hash, key, err := createRuleKey(cfg.Annotations, hostAndPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
wrapperHttpRoute.RuleKey = key
|
||||
wrapperHttpRoute.RuleHash = hash
|
||||
if WrapPreIngress, exist := convertOptions.Route2Ingress[hash]; exist {
|
||||
ingressRouteBuilder.PreIngress = WrapPreIngress.Config
|
||||
ingressRouteBuilder.Event = common.DuplicatedRoute
|
||||
}
|
||||
tempHostAndPath = append(tempHostAndPath, hostAndPath)
|
||||
tempRuleHash = append(tempRuleHash, hash)
|
||||
tempRuleKey = append(tempRuleKey, key)
|
||||
|
||||
// Two duplicated rules in the same ingress.
|
||||
if ingressRouteBuilder.Event == common.Normal {
|
||||
@@ -589,10 +601,12 @@ func (c *controller) ConvertHTTPRoute(convertOptions *common.ConvertOptions, wra
|
||||
convertOptions.IngressRouteCache.Add(ingressRouteBuilder)
|
||||
}
|
||||
|
||||
for _, item := range tempHostAndPath {
|
||||
// We only record the first
|
||||
if _, exist := convertOptions.HostAndPath2Ingress[item]; !exist {
|
||||
convertOptions.HostAndPath2Ingress[item] = cfg
|
||||
for idx, item := range tempRuleHash {
|
||||
if val, exist := convertOptions.Route2Ingress[item]; !exist || strings.Compare(val.RuleKey, tempRuleKey[idx]) != 0 {
|
||||
convertOptions.Route2Ingress[item] = &common.WrapperConfigWithRuleKey{
|
||||
Config: cfg,
|
||||
RuleKey: tempRuleKey[idx],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -763,6 +777,12 @@ func (c *controller) ApplyCanaryIngress(convertOptions *common.ConvertOptions, w
|
||||
convertOptions.IngressRouteCache.Add(ingressRouteBuilder)
|
||||
continue
|
||||
}
|
||||
hash, key, err := createRuleKey(canary.WrapperConfig.Config.Annotations, canary.PathFormat())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
canary.RuleKey = key
|
||||
canary.RuleHash = hash
|
||||
|
||||
canaryConfig := wrapper.AnnotationsConfig.Canary
|
||||
if byWeight {
|
||||
@@ -960,8 +980,7 @@ func (c *controller) createServiceKey(service *ingress.IngressServiceBackend, na
|
||||
}
|
||||
|
||||
func isCanaryRoute(canary, route *common.WrapperHTTPRoute) bool {
|
||||
return !strings.HasSuffix(route.HTTPRoute.Name, "-canary") && canary.OriginPath == route.OriginPath &&
|
||||
canary.OriginPathType == route.OriginPathType
|
||||
return !route.WrapperConfig.AnnotationsConfig.IsCanary() && canary.RuleHash == route.RuleHash && canary.RuleKey == route.RuleKey
|
||||
}
|
||||
|
||||
func (c *controller) backendToRouteDestination(backend *ingress.IngressBackend, namespace string,
|
||||
@@ -1171,3 +1190,50 @@ func setDefaultMSEIngressOptionalField(ing *ingress.Ingress) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// createRuleKey according to the pathType, path, methods, headers, params of rules
|
||||
func createRuleKey(annots map[string]string, hostAndPath string) (uint32, string, error) {
|
||||
var (
|
||||
headers []string
|
||||
params []string
|
||||
sb strings.Builder
|
||||
)
|
||||
|
||||
// path
|
||||
sb.WriteString(hostAndPath)
|
||||
|
||||
// methods
|
||||
if str, ok := annots[annotations.HigressAnnotationsPrefix+"/"+annotations.MatchMethod]; ok {
|
||||
sb.WriteString(str)
|
||||
}
|
||||
// headers && params
|
||||
for k, _ := range annots {
|
||||
if idx := strings.Index(k, annotations.MatchHeader); idx != -1 {
|
||||
headers = append(headers, k)
|
||||
}
|
||||
if idx := strings.Index(k, annotations.MatchQuery); idx != -1 {
|
||||
params = append(params, k)
|
||||
}
|
||||
}
|
||||
sort.SliceStable(headers, func(i, j int) bool {
|
||||
return headers[i] < headers[j]
|
||||
})
|
||||
sort.SliceStable(params, func(i, j int) bool {
|
||||
return params[i] < params[j]
|
||||
})
|
||||
for idx := range headers {
|
||||
sb.WriteString(headers[idx])
|
||||
sb.WriteString(annots[headers[idx]])
|
||||
}
|
||||
for idx := range params {
|
||||
sb.WriteString(params[idx])
|
||||
sb.WriteString(annots[params[idx]])
|
||||
}
|
||||
|
||||
str, hash := sb.String(), fnv.New32()
|
||||
if _, err := hash.Write([]byte(str)); err != nil {
|
||||
return 0, "", err
|
||||
}
|
||||
|
||||
return hash.Sum32(), str, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user