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 <jaiswalsanskar078@gmail.com>
This commit is contained in:
Sanskar Jaiswal
2023-02-08 21:41:19 +05:30
parent e59e3aedd4
commit 202b6e7eb1
2 changed files with 8 additions and 5 deletions
+5 -2
View File
@@ -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]),
},
},
}
+3 -3
View File
@@ -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)