mirror of
https://github.com/alibaba/higress.git
synced 2026-05-28 06:37:26 +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 ingress
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"hash/fnv"
|
||||
"path"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -494,9 +496,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)
|
||||
@@ -566,13 +572,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 {
|
||||
@@ -607,10 +619,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],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -794,6 +808,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 {
|
||||
@@ -1004,8 +1024,8 @@ func (c *controller) createServiceKey(service *ingress.IngressBackend, namespace
|
||||
}
|
||||
|
||||
func isCanaryRoute(canary, route *common.WrapperHTTPRoute) bool {
|
||||
return route != nil && canary != nil && !route.WrapperConfig.AnnotationsConfig.IsCanary() && canary.OriginPath == route.OriginPath &&
|
||||
canary.OriginPathType == route.OriginPathType
|
||||
return route != nil && canary != nil && !route.WrapperConfig.AnnotationsConfig.IsCanary() &&
|
||||
canary.RuleHash == route.RuleHash && canary.RuleKey == route.RuleKey
|
||||
}
|
||||
|
||||
func (c *controller) backendToRouteDestination(backend *ingress.IngressBackend, namespace string,
|
||||
@@ -1221,3 +1241,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
|
||||
}
|
||||
|
||||
@@ -103,10 +103,10 @@ func testApplyCanaryIngress(t *testing.T, c common.IngressController) {
|
||||
Valid: make(map[string]*common.IngressDomainBuilder),
|
||||
Invalid: make([]model.IngressDomain, 0),
|
||||
},
|
||||
HostAndPath2Ingress: make(map[string]*config.Config),
|
||||
VirtualServices: make(map[string]*common.WrapperVirtualService),
|
||||
Gateways: make(map[string]*common.WrapperGateway),
|
||||
IngressRouteCache: &common.IngressRouteCache{},
|
||||
Route2Ingress: map[uint32]*common.WrapperConfigWithRuleKey{},
|
||||
VirtualServices: make(map[string]*common.WrapperVirtualService),
|
||||
Gateways: make(map[string]*common.WrapperGateway),
|
||||
IngressRouteCache: &common.IngressRouteCache{},
|
||||
HTTPRoutes: map[string][]*common.WrapperHTTPRoute{
|
||||
"test1": make([]*common.WrapperHTTPRoute, 0),
|
||||
},
|
||||
@@ -193,11 +193,11 @@ func testApplyDefaultBackend(t *testing.T, c common.IngressController) {
|
||||
Valid: make(map[string]*common.IngressDomainBuilder),
|
||||
Invalid: make([]model.IngressDomain, 0),
|
||||
},
|
||||
HostAndPath2Ingress: make(map[string]*config.Config),
|
||||
VirtualServices: make(map[string]*common.WrapperVirtualService),
|
||||
Gateways: make(map[string]*common.WrapperGateway),
|
||||
IngressRouteCache: &common.IngressRouteCache{},
|
||||
HTTPRoutes: make(map[string][]*common.WrapperHTTPRoute),
|
||||
Route2Ingress: map[uint32]*common.WrapperConfigWithRuleKey{},
|
||||
VirtualServices: make(map[string]*common.WrapperVirtualService),
|
||||
Gateways: make(map[string]*common.WrapperGateway),
|
||||
IngressRouteCache: &common.IngressRouteCache{},
|
||||
HTTPRoutes: make(map[string][]*common.WrapperHTTPRoute),
|
||||
},
|
||||
wrapperConfig: &common.WrapperConfig{Config: &config.Config{
|
||||
Spec: ingress.IngressSpec{Rules: []ingress.IngressRule{
|
||||
@@ -385,11 +385,11 @@ func testConvertHTTPRoute(t *testing.T, c common.IngressController) {
|
||||
Valid: make(map[string]*common.IngressDomainBuilder),
|
||||
Invalid: make([]model.IngressDomain, 0),
|
||||
},
|
||||
HostAndPath2Ingress: make(map[string]*config.Config),
|
||||
VirtualServices: make(map[string]*common.WrapperVirtualService),
|
||||
Gateways: make(map[string]*common.WrapperGateway),
|
||||
IngressRouteCache: &common.IngressRouteCache{},
|
||||
HTTPRoutes: make(map[string][]*common.WrapperHTTPRoute),
|
||||
Route2Ingress: map[uint32]*common.WrapperConfigWithRuleKey{},
|
||||
VirtualServices: make(map[string]*common.WrapperVirtualService),
|
||||
Gateways: make(map[string]*common.WrapperGateway),
|
||||
IngressRouteCache: &common.IngressRouteCache{},
|
||||
HTTPRoutes: make(map[string][]*common.WrapperHTTPRoute),
|
||||
},
|
||||
wrapperConfig: &common.WrapperConfig{Config: &config.Config{
|
||||
Spec: ingress.IngressSpec{Rules: []ingress.IngressRule{
|
||||
@@ -474,7 +474,7 @@ func testConvertTrafficPolicy(t *testing.T, c common.IngressController) {
|
||||
Valid: make(map[string]*common.IngressDomainBuilder),
|
||||
Invalid: make([]model.IngressDomain, 0),
|
||||
},
|
||||
HostAndPath2Ingress: make(map[string]*config.Config),
|
||||
Route2Ingress: map[uint32]*common.WrapperConfigWithRuleKey{},
|
||||
VirtualServices: make(map[string]*common.WrapperVirtualService),
|
||||
Gateways: make(map[string]*common.WrapperGateway),
|
||||
IngressRouteCache: &common.IngressRouteCache{},
|
||||
|
||||
Reference in New Issue
Block a user