Skip to content

Commit

Permalink
Sync inner fix (#634)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnlanni authored Nov 14, 2023
1 parent cb04795 commit 5174397
Show file tree
Hide file tree
Showing 14 changed files with 226 additions and 99 deletions.
6 changes: 1 addition & 5 deletions pkg/ingress/kube/annotations/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,7 @@ type Ingress struct {
}

func (i *Ingress) NeedRegexMatch() bool {
if i.Rewrite == nil {
return false
}

return i.Rewrite.RewriteTarget != "" || i.IsPrefixRegexMatch() || i.IsFullPathRegexMatch()
return i.Rewrite != nil && (i.IsPrefixRegexMatch() || i.IsFullPathRegexMatch())
}

func (i *Ingress) IsPrefixRegexMatch() bool {
Expand Down
6 changes: 3 additions & 3 deletions pkg/ingress/kube/annotations/annotations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,18 @@ func TestNeedRegexMatch(t *testing.T) {
{
input: &Ingress{
Rewrite: &RewriteConfig{
RewriteTarget: "/test",
UseRegex: true,
},
},
expect: true,
},
{
input: &Ingress{
Rewrite: &RewriteConfig{
UseRegex: true,
UseRegex: false,
},
},
expect: true,
expect: false,
},
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/ingress/kube/annotations/canary.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ func ApplyByWeight(canary, route *networking.HTTPRoute, canaryIngress *Ingress)

// canary route use the header control applied on itself.
headerControl{}.ApplyRoute(canary, canaryIngress)
// reset
canary.Route[0].FallbackClusters = nil
// Move route level to destination level
canary.Route[0].Headers = canary.Headers

Expand All @@ -127,8 +129,6 @@ func ApplyByHeader(canary, route *networking.HTTPRoute, canaryIngress *Ingress)

// Inherit configuration from non-canary rule
route.DeepCopyInto(canary)
// Assign temp copied canary route match
canary.Match = temp.Match
// Assign temp copied canary route destination
canary.Route = temp.Route

Expand Down Expand Up @@ -165,7 +165,7 @@ func ApplyByHeader(canary, route *networking.HTTPRoute, canaryIngress *Ingress)
match.Headers = map[string]*networking.StringMatch{
"cookie": {
MatchType: &networking.StringMatch_Regex{
Regex: "^(.\\*?;)?(" + canaryConfig.Cookie + "=always)(;.\\*)?$",
Regex: "^(.*?;\\s*)?(" + canaryConfig.Cookie + "=always)(;.*)?$",
},
},
}
Expand Down
21 changes: 18 additions & 3 deletions pkg/ingress/kube/annotations/header_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package annotations

import (
"regexp"
"strings"

networking "istio.io/api/networking/v1alpha3"
Expand All @@ -37,6 +38,8 @@ const (
var (
_ Parser = headerControl{}
_ RouteHandler = headerControl{}

pattern = regexp.MustCompile(`\s+`)
)

type HeaderOperation struct {
Expand Down Expand Up @@ -138,6 +141,18 @@ func needHeaderControlConfig(annotations Annotations) bool {
annotations.HasHigress(responseHeaderRemove)
}

func trimQuotes(s string) string {
if len(s) >= 2 {
if s[0] == '"' && s[len(s)-1] == '"' {
return s[1 : len(s)-1]
}
if s[0] == '\'' && s[len(s)-1] == '\'' {
return s[1 : len(s)-1]
}
}
return s
}

func convertAddOrUpdate(headers string) map[string]string {
result := map[string]string{}
parts := strings.Split(headers, "\n")
Expand All @@ -147,13 +162,13 @@ func convertAddOrUpdate(headers string) map[string]string {
continue
}

keyValue := strings.Fields(part)
keyValue := pattern.Split(part, 2)
if len(keyValue) != 2 {
IngressLog.Errorf("Header format %s is invalid.", keyValue)
continue
}
key := strings.TrimSpace(keyValue[0])
value := strings.TrimSpace(keyValue[1])
key := trimQuotes(strings.TrimSpace(keyValue[0]))
value := trimQuotes(strings.TrimSpace(keyValue[1]))
result[key] = value
}
return result
Expand Down
40 changes: 26 additions & 14 deletions pkg/ingress/kube/annotations/header_control_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ func TestHeaderControlParse(t *testing.T) {
},
{
input: map[string]string{
buildHigressAnnotationKey(requestHeaderAdd): "one 1\n two 2\nthree 3 \n",
buildHigressAnnotationKey(requestHeaderUpdate): "two 2",
buildHigressAnnotationKey(requestHeaderAdd): "one 1\n two 2\nthree 3 \nx-test mse; test=true\nx-pro mse; pro=true\n",
buildHigressAnnotationKey(requestHeaderUpdate): "two 2\n set-cookie name=test; sameage=111\nset-stage name=stage; stage=true\n",
buildHigressAnnotationKey(requestHeaderRemove): "one, two,three\n",
buildHigressAnnotationKey(responseHeaderAdd): "A a\nB b\n",
buildHigressAnnotationKey(responseHeaderUpdate): "X x\nY y\n",
Expand All @@ -58,12 +58,16 @@ func TestHeaderControlParse(t *testing.T) {
expect: &HeaderControlConfig{
Request: &HeaderOperation{
Add: map[string]string{
"one": "1",
"two": "2",
"three": "3",
"one": "1",
"two": "2",
"three": "3",
"x-test": "mse; test=true",
"x-pro": "mse; pro=true",
},
Update: map[string]string{
"two": "2",
"two": "2",
"set-cookie": "name=test; sameage=111",
"set-stage": "name=stage; stage=true",
},
Remove: []string{"one", "two", "three"},
},
Expand Down Expand Up @@ -122,12 +126,16 @@ func TestHeaderControlApplyRoute(t *testing.T) {
HeaderControl: &HeaderControlConfig{
Request: &HeaderOperation{
Add: map[string]string{
"one": "1",
"two": "2",
"three": "3",
"one": "1",
"two": "2",
"three": "3",
"x-test": "mse; test=true",
"x-pro": "mse; pro=true",
},
Update: map[string]string{
"two": "2",
"two": "2",
"set-cookie": "name=test; sameage=111",
"set-stage": "name=stage; sameage=111",
},
Remove: []string{"one", "two", "three"},
},
Expand All @@ -138,12 +146,16 @@ func TestHeaderControlApplyRoute(t *testing.T) {
Headers: &networking.Headers{
Request: &networking.Headers_HeaderOperations{
Add: map[string]string{
"one": "1",
"two": "2",
"three": "3",
"one": "1",
"two": "2",
"three": "3",
"x-test": "mse; test=true",
"x-pro": "mse; pro=true",
},
Set: map[string]string{
"two": "2",
"two": "2",
"set-cookie": "name=test; sameage=111",
"set-stage": "name=stage; sameage=111",
},
Remove: []string{"one", "two", "three"},
},
Expand Down
11 changes: 8 additions & 3 deletions pkg/ingress/kube/annotations/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,13 @@ func (r retry) Parse(annotations Annotations, config *Ingress, _ *GlobalContext)
}

if retryOn, err := annotations.ParseStringASAP(retryOn); err == nil {
extraConfigs := splitBySeparator(retryOn, ",")
conditions := toSet(extraConfigs)
var retryOnConditions []string
if strings.Contains(retryOn, ",") {
retryOnConditions = splitBySeparator(retryOn, ",")
} else {
retryOnConditions = strings.Fields(retryOn)
}
conditions := toSet(retryOnConditions)
if len(conditions) > 0 {
if conditions.Contains("off") {
retryConfig.retryCount = 0
Expand All @@ -88,7 +93,7 @@ func (r retry) Parse(annotations Annotations, config *Ingress, _ *GlobalContext)
stringBuilder.WriteString("non_idempotent,")
}
// Append the status codes.
statusCodes := convertStatusCodes(extraConfigs)
statusCodes := convertStatusCodes(retryOnConditions)
if len(statusCodes) > 0 {
stringBuilder.WriteString(retryStatusCode + ",")
for _, code := range statusCodes {
Expand Down
31 changes: 26 additions & 5 deletions pkg/ingress/kube/annotations/retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ func TestRetryParse(t *testing.T) {
},
expect: &RetryConfig{
retryCount: 1,
perRetryTimeout: &types.Duration{},
retryOn: "5xx",
perRetryTimeout: &types.Duration{},
},
},
{
Expand All @@ -60,8 +60,8 @@ func TestRetryParse(t *testing.T) {
},
expect: &RetryConfig{
retryCount: 0,
perRetryTimeout: &types.Duration{},
retryOn: "5xx",
perRetryTimeout: &types.Duration{},
},
},
{
Expand All @@ -71,8 +71,19 @@ func TestRetryParse(t *testing.T) {
},
expect: &RetryConfig{
retryCount: 2,
retryOn: "5xx",
perRetryTimeout: &types.Duration{},
},
},
{
input: map[string]string{
buildNginxAnnotationKey(retryCount): "2",
buildNginxAnnotationKey(retryOn): "error timeout",
},
expect: &RetryConfig{
retryCount: 2,
retryOn: "5xx",
perRetryTimeout: &types.Duration{},
},
},
{
Expand All @@ -81,8 +92,18 @@ func TestRetryParse(t *testing.T) {
},
expect: &RetryConfig{
retryCount: 3,
retryOn: "5xx,non_idempotent",
perRetryTimeout: &types.Duration{},
},
},
{
input: map[string]string{
buildNginxAnnotationKey(retryOn): "timeout non_idempotent",
},
expect: &RetryConfig{
retryCount: 3,
retryOn: "5xx,non_idempotent",
perRetryTimeout: &types.Duration{},
},
},
{
Expand All @@ -91,18 +112,18 @@ func TestRetryParse(t *testing.T) {
},
expect: &RetryConfig{
retryCount: 3,
perRetryTimeout: &types.Duration{},
retryOn: "5xx,retriable-status-codes,503,502,404",
perRetryTimeout: &types.Duration{},
},
},
{
input: map[string]string{
buildNginxAnnotationKey(retryOn): "timeout,http_505,http_503,http_502,http_404,http_403",
buildNginxAnnotationKey(retryOn): "timeout http_503 http_502 http_404",
},
expect: &RetryConfig{
retryCount: 3,
retryOn: "5xx,retriable-status-codes,503,502,404",
perRetryTimeout: &types.Duration{},
retryOn: "5xx,retriable-status-codes,505,503,502,404,403",
},
},
}
Expand Down
25 changes: 16 additions & 9 deletions pkg/ingress/kube/annotations/rewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,6 @@ func (r rewrite) Parse(annotations Annotations, config *Ingress, _ *GlobalContex
rewriteConfig.RewritePath, _ = annotations.ParseStringForHigress(rewritePath)

if rewriteConfig.RewritePath == "" && rewriteConfig.RewriteTarget != "" {
// When rewrite target is present and not empty,
// we will enforce regex match on all rules in this ingress.
if !rewriteConfig.UseRegex && !rewriteConfig.FullPathRegex {
rewriteConfig.UseRegex = true
}

// We should convert nginx regex rule to envoy regex rule.
rewriteConfig.RewriteTarget = convertToRE2(rewriteConfig.RewriteTarget)
}
Expand Down Expand Up @@ -92,9 +86,22 @@ func (r rewrite) ApplyRoute(route *networking.HTTPRoute, config *Ingress) {
}
}
} else if rewriteConfig.RewriteTarget != "" {
route.Rewrite.UriRegex = &networking.RegexMatchAndSubstitute{
Pattern: route.Match[0].Uri.GetRegex(),
Substitution: rewriteConfig.RewriteTarget,
uri := route.Match[0].Uri
if uri.GetExact() != "" {
route.Rewrite.UriRegex = &networking.RegexMatchAndSubstitute{
Pattern: uri.GetExact(),
Substitution: rewriteConfig.RewriteTarget,
}
} else if uri.GetPrefix() != "" {
route.Rewrite.UriRegex = &networking.RegexMatchAndSubstitute{
Pattern: uri.GetPrefix(),
Substitution: rewriteConfig.RewriteTarget,
}
} else {
route.Rewrite.UriRegex = &networking.RegexMatchAndSubstitute{
Pattern: uri.GetRegex(),
Substitution: rewriteConfig.RewriteTarget,
}
}
}

Expand Down
Loading

0 comments on commit 5174397

Please sign in to comment.