Skip to content

Commit 202b6e7

Browse files
committed
use regex to match against headers in istio
Use regex filtering to match against session affinity cookie headers when using Istio instead of an exact match. Signed-off-by: Sanskar Jaiswal <[email protected]>
1 parent e59e3ae commit 202b6e7

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

pkg/router/istio.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"encoding/json"
2222
"fmt"
2323
"math/rand"
24+
"strings"
2425
"time"
2526

2627
"github.com/google/go-cmp/cmp"
@@ -443,10 +444,11 @@ func (ir *IstioRouter) SetRoutes(
443444
weightedRoute.Route[i] = routeDest
444445
}
445446

447+
cookieKeyAndVal := strings.Split(canary.Status.SessionAffinityCookie, "=")
446448
cookieMatch := istiov1alpha3.HTTPMatchRequest{
447449
Headers: map[string]istiov1alpha1.StringMatch{
448450
cookieHeader: {
449-
Exact: canary.Status.SessionAffinityCookie,
451+
Regex: fmt.Sprintf(".*%s.*%s.*", cookieKeyAndVal[0], cookieKeyAndVal[1]),
450452
},
451453
},
452454
}
@@ -465,10 +467,11 @@ func (ir *IstioRouter) SetRoutes(
465467

466468
// Match against the previous session cookie and delete that cookie
467469
if previousCookie != "" {
470+
cookieKeyAndVal := strings.Split(previousCookie, "=")
468471
cookieMatch := istiov1alpha3.HTTPMatchRequest{
469472
Headers: map[string]istiov1alpha1.StringMatch{
470473
cookieHeader: {
471-
Exact: previousCookie,
474+
Regex: fmt.Sprintf(".*%s.*%s.*", cookieKeyAndVal[0], cookieKeyAndVal[1]),
472475
},
473476
},
474477
}

pkg/router/istio_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ func TestIstioRouter_SetRoutes(t *testing.T) {
166166
for _, match := range stickyRoute.Match {
167167
if val, ok := match.Headers[cookieHeader]; ok {
168168
found = true
169-
assert.True(t, strings.HasPrefix(val.Exact, cookieKey))
169+
assert.True(t, strings.Contains(val.Regex, cookieKey))
170170
for _, routeDest := range stickyRoute.Route {
171171
if routeDest.Destination.Host == pHost {
172172
assert.Equal(t, 0, routeDest.Weight)
@@ -223,7 +223,7 @@ func TestIstioRouter_SetRoutes(t *testing.T) {
223223
for _, match := range stickyRoute.Match {
224224
if val, ok := match.Headers[cookieHeader]; ok {
225225
found = true
226-
assert.True(t, strings.HasPrefix(val.Exact, cookieKey))
226+
assert.True(t, strings.Contains(val.Regex, cookieKey))
227227
for _, routeDest := range stickyRoute.Route {
228228
if routeDest.Destination.Host == pHost {
229229
assert.Equal(t, 0, routeDest.Weight)
@@ -266,7 +266,7 @@ func TestIstioRouter_SetRoutes(t *testing.T) {
266266
for _, match := range stickyRoute.Match {
267267
if val, ok := match.Headers[cookieHeader]; ok {
268268
found = true
269-
assert.True(t, strings.HasPrefix(val.Exact, cookieKey))
269+
assert.True(t, strings.Contains(val.Regex, cookieKey))
270270
for _, routeDest := range stickyRoute.Route {
271271
if routeDest.Destination.Host == pHost {
272272
assert.Equal(t, 100, routeDest.Weight)

0 commit comments

Comments
 (0)