From 202b6e7eb1dcfccd33e7a5b72dc7f45ee9bdcc3a Mon Sep 17 00:00:00 2001 From: Sanskar Jaiswal Date: Sat, 4 Feb 2023 02:18:08 +0530 Subject: [PATCH] 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 --- pkg/router/istio.go | 7 +++++-- pkg/router/istio_test.go | 6 +++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/pkg/router/istio.go b/pkg/router/istio.go index 7c78c6d9..5cf21e42 100644 --- a/pkg/router/istio.go +++ b/pkg/router/istio.go @@ -21,6 +21,7 @@ import ( "encoding/json" "fmt" "math/rand" + "strings" "time" "github.com/google/go-cmp/cmp" @@ -443,10 +444,11 @@ func (ir *IstioRouter) SetRoutes( weightedRoute.Route[i] = routeDest } + cookieKeyAndVal := strings.Split(canary.Status.SessionAffinityCookie, "=") cookieMatch := istiov1alpha3.HTTPMatchRequest{ Headers: map[string]istiov1alpha1.StringMatch{ cookieHeader: { - Exact: canary.Status.SessionAffinityCookie, + Regex: fmt.Sprintf(".*%s.*%s.*", cookieKeyAndVal[0], cookieKeyAndVal[1]), }, }, } @@ -465,10 +467,11 @@ func (ir *IstioRouter) SetRoutes( // Match against the previous session cookie and delete that cookie if previousCookie != "" { + cookieKeyAndVal := strings.Split(previousCookie, "=") cookieMatch := istiov1alpha3.HTTPMatchRequest{ Headers: map[string]istiov1alpha1.StringMatch{ cookieHeader: { - Exact: previousCookie, + Regex: fmt.Sprintf(".*%s.*%s.*", cookieKeyAndVal[0], cookieKeyAndVal[1]), }, }, } diff --git a/pkg/router/istio_test.go b/pkg/router/istio_test.go index f2bb63f0..f74dc2f1 100644 --- a/pkg/router/istio_test.go +++ b/pkg/router/istio_test.go @@ -166,7 +166,7 @@ func TestIstioRouter_SetRoutes(t *testing.T) { for _, match := range stickyRoute.Match { if val, ok := match.Headers[cookieHeader]; ok { found = true - assert.True(t, strings.HasPrefix(val.Exact, cookieKey)) + assert.True(t, strings.Contains(val.Regex, cookieKey)) for _, routeDest := range stickyRoute.Route { if routeDest.Destination.Host == pHost { assert.Equal(t, 0, routeDest.Weight) @@ -223,7 +223,7 @@ func TestIstioRouter_SetRoutes(t *testing.T) { for _, match := range stickyRoute.Match { if val, ok := match.Headers[cookieHeader]; ok { found = true - assert.True(t, strings.HasPrefix(val.Exact, cookieKey)) + assert.True(t, strings.Contains(val.Regex, cookieKey)) for _, routeDest := range stickyRoute.Route { if routeDest.Destination.Host == pHost { assert.Equal(t, 0, routeDest.Weight) @@ -266,7 +266,7 @@ func TestIstioRouter_SetRoutes(t *testing.T) { for _, match := range stickyRoute.Match { if val, ok := match.Headers[cookieHeader]; ok { found = true - assert.True(t, strings.HasPrefix(val.Exact, cookieKey)) + assert.True(t, strings.Contains(val.Regex, cookieKey)) for _, routeDest := range stickyRoute.Route { if routeDest.Destination.Host == pHost { assert.Equal(t, 100, routeDest.Weight)