Added new affinity mode for maximum session stickyness. Fixes kubernetes/ingress-nginx#4475
This commit is contained in:
parent
8740c1b661
commit
9170591185
16 changed files with 541 additions and 55 deletions
|
|
@ -199,11 +199,12 @@ func TestAffinitySession(t *testing.T) {
|
|||
fooAnns := []struct {
|
||||
annotations map[string]string
|
||||
affinitytype string
|
||||
affinitymode string
|
||||
name string
|
||||
}{
|
||||
{map[string]string{annotationAffinityType: "cookie", annotationAffinityCookieName: "route"}, "cookie", "route"},
|
||||
{map[string]string{annotationAffinityType: "cookie", annotationAffinityCookieName: "route1"}, "cookie", "route1"},
|
||||
{map[string]string{annotationAffinityType: "cookie", annotationAffinityCookieName: ""}, "cookie", "INGRESSCOOKIE"},
|
||||
{map[string]string{annotationAffinityType: "cookie", annotationAffinityMode: "balanced", annotationAffinityCookieName: "route"}, "cookie", "balanced", "route"},
|
||||
{map[string]string{annotationAffinityType: "cookie", annotationAffinityMode: "persistent", annotationAffinityCookieName: "route1"}, "cookie", "persistent", "route1"},
|
||||
{map[string]string{annotationAffinityType: "cookie", annotationAffinityMode: "balanced", annotationAffinityCookieName: ""}, "cookie", "balanced", "INGRESSCOOKIE"},
|
||||
{map[string]string{}, "", ""},
|
||||
{nil, "", ""},
|
||||
}
|
||||
|
|
@ -213,6 +214,10 @@ func TestAffinitySession(t *testing.T) {
|
|||
r := ec.Extract(ing).SessionAffinity
|
||||
t.Logf("Testing pass %v %v", foo.affinitytype, foo.name)
|
||||
|
||||
if (r.Mode != foo.affinitymode) {
|
||||
t.Errorf("Returned %v but expected %v for Name", r.Mode, foo.affinitymode)
|
||||
}
|
||||
|
||||
if r.Cookie.Name != foo.name {
|
||||
t.Errorf("Returned %v but expected %v for Name", r.Cookie.Name, foo.name)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,6 +28,7 @@ import (
|
|||
|
||||
const (
|
||||
annotationAffinityType = "affinity"
|
||||
annotationAffinityMode = "affinity-mode"
|
||||
// If a cookie with this name exists,
|
||||
// its value is used as an index into the list of available backends.
|
||||
annotationAffinityCookieName = "session-cookie-name"
|
||||
|
|
@ -57,6 +58,8 @@ var (
|
|||
type Config struct {
|
||||
// The type of affinity that will be used
|
||||
Type string `json:"type"`
|
||||
// The affinity mode, i.e. how sticky a session is
|
||||
Mode string `json:"mode"`
|
||||
Cookie
|
||||
}
|
||||
|
||||
|
|
@ -136,6 +139,12 @@ func (a affinity) Parse(ing *networking.Ingress) (interface{}, error) {
|
|||
at = ""
|
||||
}
|
||||
|
||||
// Check the afinity mode that will be used
|
||||
am, err := parser.GetStringAnnotation(annotationAffinityMode, ing)
|
||||
if err != nil {
|
||||
am = ""
|
||||
}
|
||||
|
||||
switch at {
|
||||
case "cookie":
|
||||
cookie = a.cookieAffinityParse(ing)
|
||||
|
|
@ -146,6 +155,7 @@ func (a affinity) Parse(ing *networking.Ingress) (interface{}, error) {
|
|||
|
||||
return &Config{
|
||||
Type: at,
|
||||
Mode: am,
|
||||
Cookie: *cookie,
|
||||
}, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -67,6 +67,7 @@ func TestIngressAffinityCookieConfig(t *testing.T) {
|
|||
|
||||
data := map[string]string{}
|
||||
data[parser.GetAnnotationWithPrefix(annotationAffinityType)] = "cookie"
|
||||
data[parser.GetAnnotationWithPrefix(annotationAffinityMode)] = "balanced"
|
||||
data[parser.GetAnnotationWithPrefix(annotationAffinityCookieName)] = "INGRESSCOOKIE"
|
||||
data[parser.GetAnnotationWithPrefix(annotationAffinityCookieExpires)] = "4500"
|
||||
data[parser.GetAnnotationWithPrefix(annotationAffinityCookieMaxAge)] = "3000"
|
||||
|
|
@ -84,6 +85,10 @@ func TestIngressAffinityCookieConfig(t *testing.T) {
|
|||
t.Errorf("expected cookie as affinity but returned %v", nginxAffinity.Type)
|
||||
}
|
||||
|
||||
if nginxAffinity.Mode != "balanced" {
|
||||
t.Errorf("expected balanced as affinity mode but returned %v", nginxAffinity.Mode)
|
||||
}
|
||||
|
||||
if nginxAffinity.Cookie.Name != "INGRESSCOOKIE" {
|
||||
t.Errorf("expected INGRESSCOOKIE as session-cookie-name but returned %v", nginxAffinity.Cookie.Name)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue