mirror of
https://github.com/alibaba/higress.git
synced 2026-06-08 20:27:31 +08:00
more compatiable with nginx's rewrite (#636)
This commit is contained in:
@@ -15,6 +15,8 @@
|
|||||||
package annotations
|
package annotations
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"strings"
|
||||||
|
|
||||||
networking "istio.io/api/networking/v1alpha3"
|
networking "istio.io/api/networking/v1alpha3"
|
||||||
"istio.io/istio/pilot/pkg/util/sets"
|
"istio.io/istio/pilot/pkg/util/sets"
|
||||||
listersv1 "k8s.io/client-go/listers/core/v1"
|
listersv1 "k8s.io/client-go/listers/core/v1"
|
||||||
@@ -73,8 +75,17 @@ type Ingress struct {
|
|||||||
Http2Rpc *Http2RpcConfig
|
Http2Rpc *Http2RpcConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Ingress) NeedRegexMatch() bool {
|
func (i *Ingress) NeedRegexMatch(path string) bool {
|
||||||
return i.Rewrite != nil && (i.IsPrefixRegexMatch() || i.IsFullPathRegexMatch())
|
if i.Rewrite == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if strings.ContainsAny(path, `\.+*?()|[]{}^$`) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if strings.ContainsAny(i.Rewrite.RewriteTarget, `$\`) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return i.IsPrefixRegexMatch() || i.IsFullPathRegexMatch()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Ingress) IsPrefixRegexMatch() bool {
|
func (i *Ingress) IsPrefixRegexMatch() bool {
|
||||||
|
|||||||
@@ -18,8 +18,9 @@ import "testing"
|
|||||||
|
|
||||||
func TestNeedRegexMatch(t *testing.T) {
|
func TestNeedRegexMatch(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
input *Ingress
|
input *Ingress
|
||||||
expect bool
|
inputPath string
|
||||||
|
expect bool
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
input: &Ingress{},
|
input: &Ingress{},
|
||||||
@@ -47,12 +48,41 @@ func TestNeedRegexMatch(t *testing.T) {
|
|||||||
},
|
},
|
||||||
expect: false,
|
expect: false,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
input: &Ingress{
|
||||||
|
Rewrite: &RewriteConfig{
|
||||||
|
UseRegex: false,
|
||||||
|
RewriteTarget: "/$1",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expect: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: &Ingress{
|
||||||
|
Rewrite: &RewriteConfig{
|
||||||
|
UseRegex: false,
|
||||||
|
RewriteTarget: "/",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
inputPath: "/.*",
|
||||||
|
expect: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: &Ingress{
|
||||||
|
Rewrite: &RewriteConfig{
|
||||||
|
UseRegex: false,
|
||||||
|
RewriteTarget: "/",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
inputPath: "/",
|
||||||
|
expect: false,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, testCase := range testCases {
|
for _, testCase := range testCases {
|
||||||
t.Run("", func(t *testing.T) {
|
t.Run("", func(t *testing.T) {
|
||||||
if testCase.input.NeedRegexMatch() != testCase.expect {
|
if testCase.input.NeedRegexMatch(testCase.inputPath) != testCase.expect {
|
||||||
t.Fatalf("Should be %t, but actual is %t", testCase.expect, testCase.input.NeedRegexMatch())
|
t.Fatalf("Should be %t, but actual is %t", testCase.expect, !testCase.expect)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -535,11 +535,11 @@ func (c *controller) ConvertHTTPRoute(convertOptions *common.ConvertOptions, wra
|
|||||||
|
|
||||||
var pathType common.PathType
|
var pathType common.PathType
|
||||||
originPath := httpPath.Path
|
originPath := httpPath.Path
|
||||||
if annotationsConfig := wrapper.AnnotationsConfig; annotationsConfig.NeedRegexMatch() {
|
if annotationsConfig := wrapper.AnnotationsConfig; annotationsConfig.NeedRegexMatch(originPath) {
|
||||||
if annotationsConfig.IsPrefixRegexMatch() {
|
if annotationsConfig.IsFullPathRegexMatch() {
|
||||||
pathType = common.PrefixRegex
|
|
||||||
} else if annotationsConfig.IsFullPathRegexMatch() {
|
|
||||||
pathType = common.FullPathRegex
|
pathType = common.FullPathRegex
|
||||||
|
} else {
|
||||||
|
pathType = common.PrefixRegex
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
switch *httpPath.PathType {
|
switch *httpPath.PathType {
|
||||||
@@ -746,11 +746,11 @@ func (c *controller) ApplyCanaryIngress(convertOptions *common.ConvertOptions, w
|
|||||||
|
|
||||||
var pathType common.PathType
|
var pathType common.PathType
|
||||||
originPath := httpPath.Path
|
originPath := httpPath.Path
|
||||||
if annotationsConfig := wrapper.AnnotationsConfig; annotationsConfig.NeedRegexMatch() {
|
if annotationsConfig := wrapper.AnnotationsConfig; annotationsConfig.NeedRegexMatch(originPath) {
|
||||||
if annotationsConfig.IsPrefixRegexMatch() {
|
if annotationsConfig.IsFullPathRegexMatch() {
|
||||||
pathType = common.PrefixRegex
|
|
||||||
} else if annotationsConfig.IsFullPathRegexMatch() {
|
|
||||||
pathType = common.FullPathRegex
|
pathType = common.FullPathRegex
|
||||||
|
} else {
|
||||||
|
pathType = common.PrefixRegex
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
switch *httpPath.PathType {
|
switch *httpPath.PathType {
|
||||||
|
|||||||
@@ -517,11 +517,11 @@ func (c *controller) ConvertHTTPRoute(convertOptions *common.ConvertOptions, wra
|
|||||||
|
|
||||||
var pathType common.PathType
|
var pathType common.PathType
|
||||||
originPath := httpPath.Path
|
originPath := httpPath.Path
|
||||||
if annotationsConfig := wrapper.AnnotationsConfig; annotationsConfig.NeedRegexMatch() {
|
if annotationsConfig := wrapper.AnnotationsConfig; annotationsConfig.NeedRegexMatch(originPath) {
|
||||||
if annotationsConfig.IsPrefixRegexMatch() {
|
if annotationsConfig.IsFullPathRegexMatch() {
|
||||||
pathType = common.PrefixRegex
|
|
||||||
} else if annotationsConfig.IsFullPathRegexMatch() {
|
|
||||||
pathType = common.FullPathRegex
|
pathType = common.FullPathRegex
|
||||||
|
} else {
|
||||||
|
pathType = common.PrefixRegex
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
switch *httpPath.PathType {
|
switch *httpPath.PathType {
|
||||||
@@ -750,11 +750,11 @@ func (c *controller) ApplyCanaryIngress(convertOptions *common.ConvertOptions, w
|
|||||||
|
|
||||||
var pathType common.PathType
|
var pathType common.PathType
|
||||||
originPath := httpPath.Path
|
originPath := httpPath.Path
|
||||||
if annotationsConfig := wrapper.AnnotationsConfig; annotationsConfig.NeedRegexMatch() {
|
if annotationsConfig := wrapper.AnnotationsConfig; annotationsConfig.NeedRegexMatch(originPath) {
|
||||||
if annotationsConfig.IsPrefixRegexMatch() {
|
if annotationsConfig.IsFullPathRegexMatch() {
|
||||||
pathType = common.PrefixRegex
|
|
||||||
} else if annotationsConfig.IsFullPathRegexMatch() {
|
|
||||||
pathType = common.FullPathRegex
|
pathType = common.FullPathRegex
|
||||||
|
} else {
|
||||||
|
pathType = common.PrefixRegex
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
switch *httpPath.PathType {
|
switch *httpPath.PathType {
|
||||||
|
|||||||
@@ -17,7 +17,6 @@ kind: Ingress
|
|||||||
metadata:
|
metadata:
|
||||||
annotations:
|
annotations:
|
||||||
nginx.ingress.kubernetes.io/rewrite-target: "/$1"
|
nginx.ingress.kubernetes.io/rewrite-target: "/$1"
|
||||||
nginx.ingress.kubernetes.io/use-regex: "true"
|
|
||||||
name: httproute-rewrite-path
|
name: httproute-rewrite-path
|
||||||
namespace: higress-conformance-infra
|
namespace: higress-conformance-infra
|
||||||
spec:
|
spec:
|
||||||
|
|||||||
Reference in New Issue
Block a user