Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

more compatiable with nginx's rewrite #636

Merged
merged 2 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions pkg/ingress/kube/annotations/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package annotations

import (
"strings"

networking "istio.io/api/networking/v1alpha3"
"istio.io/istio/pilot/pkg/util/sets"
listersv1 "k8s.io/client-go/listers/core/v1"
Expand Down Expand Up @@ -73,8 +75,17 @@ type Ingress struct {
Http2Rpc *Http2RpcConfig
}

func (i *Ingress) NeedRegexMatch() bool {
return i.Rewrite != nil && (i.IsPrefixRegexMatch() || i.IsFullPathRegexMatch())
func (i *Ingress) NeedRegexMatch(path string) bool {
if i.Rewrite == nil {
return false
}
if strings.ContainsAny(path, `\.+*?()|[]{}^$`) {
return true
}
if strings.ContainsAny(i.Rewrite.RewriteTarget, `$\`) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$\ 这是什么?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$1 $2是nginx的捕获方式,\1 \2 是 envoy 的捕获方式

return true
}
return i.IsPrefixRegexMatch() || i.IsFullPathRegexMatch()
}

func (i *Ingress) IsPrefixRegexMatch() bool {
Expand Down
38 changes: 34 additions & 4 deletions pkg/ingress/kube/annotations/annotations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ import "testing"

func TestNeedRegexMatch(t *testing.T) {
testCases := []struct {
input *Ingress
expect bool
input *Ingress
inputPath string
expect bool
}{
{
input: &Ingress{},
Expand Down Expand Up @@ -47,12 +48,41 @@ func TestNeedRegexMatch(t *testing.T) {
},
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 {
t.Run("", func(t *testing.T) {
if testCase.input.NeedRegexMatch() != testCase.expect {
t.Fatalf("Should be %t, but actual is %t", testCase.expect, testCase.input.NeedRegexMatch())
if testCase.input.NeedRegexMatch(testCase.inputPath) != testCase.expect {
t.Fatalf("Should be %t, but actual is %t", testCase.expect, !testCase.expect)
}
})
}
Expand Down
16 changes: 8 additions & 8 deletions pkg/ingress/kube/ingress/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,11 +535,11 @@ func (c *controller) ConvertHTTPRoute(convertOptions *common.ConvertOptions, wra

var pathType common.PathType
originPath := httpPath.Path
if annotationsConfig := wrapper.AnnotationsConfig; annotationsConfig.NeedRegexMatch() {
if annotationsConfig.IsPrefixRegexMatch() {
pathType = common.PrefixRegex
} else if annotationsConfig.IsFullPathRegexMatch() {
if annotationsConfig := wrapper.AnnotationsConfig; annotationsConfig.NeedRegexMatch(originPath) {
if annotationsConfig.IsFullPathRegexMatch() {
pathType = common.FullPathRegex
} else {
pathType = common.PrefixRegex
}
} else {
switch *httpPath.PathType {
Expand Down Expand Up @@ -746,11 +746,11 @@ func (c *controller) ApplyCanaryIngress(convertOptions *common.ConvertOptions, w

var pathType common.PathType
originPath := httpPath.Path
if annotationsConfig := wrapper.AnnotationsConfig; annotationsConfig.NeedRegexMatch() {
if annotationsConfig.IsPrefixRegexMatch() {
pathType = common.PrefixRegex
} else if annotationsConfig.IsFullPathRegexMatch() {
if annotationsConfig := wrapper.AnnotationsConfig; annotationsConfig.NeedRegexMatch(originPath) {
if annotationsConfig.IsFullPathRegexMatch() {
pathType = common.FullPathRegex
} else {
pathType = common.PrefixRegex
}
} else {
switch *httpPath.PathType {
Expand Down
16 changes: 8 additions & 8 deletions pkg/ingress/kube/ingressv1/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,11 +517,11 @@ func (c *controller) ConvertHTTPRoute(convertOptions *common.ConvertOptions, wra

var pathType common.PathType
originPath := httpPath.Path
if annotationsConfig := wrapper.AnnotationsConfig; annotationsConfig.NeedRegexMatch() {
if annotationsConfig.IsPrefixRegexMatch() {
pathType = common.PrefixRegex
} else if annotationsConfig.IsFullPathRegexMatch() {
if annotationsConfig := wrapper.AnnotationsConfig; annotationsConfig.NeedRegexMatch(originPath) {
if annotationsConfig.IsFullPathRegexMatch() {
pathType = common.FullPathRegex
} else {
pathType = common.PrefixRegex
}
} else {
switch *httpPath.PathType {
Expand Down Expand Up @@ -750,11 +750,11 @@ func (c *controller) ApplyCanaryIngress(convertOptions *common.ConvertOptions, w

var pathType common.PathType
originPath := httpPath.Path
if annotationsConfig := wrapper.AnnotationsConfig; annotationsConfig.NeedRegexMatch() {
if annotationsConfig.IsPrefixRegexMatch() {
pathType = common.PrefixRegex
} else if annotationsConfig.IsFullPathRegexMatch() {
if annotationsConfig := wrapper.AnnotationsConfig; annotationsConfig.NeedRegexMatch(originPath) {
if annotationsConfig.IsFullPathRegexMatch() {
pathType = common.FullPathRegex
} else {
pathType = common.PrefixRegex
}
} else {
switch *httpPath.PathType {
Expand Down
1 change: 0 additions & 1 deletion test/e2e/conformance/tests/httproute-rewrite-path.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/rewrite-target: "/$1"
nginx.ingress.kubernetes.io/use-regex: "true"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里的去掉的原因?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不带 use-regex 时,通过 target 或 path 里的特殊字符来判定是否启用正则匹配

name: httproute-rewrite-path
namespace: higress-conformance-infra
spec:
Expand Down