Added support for annotation session-cookie-change-on-failure

1. Session cookie is updated on previous attempt failure when `session-cookie-change-on-failure = true` (default value is `false`).
2. Added tests to check both cases.
3. Updated docs.

Co-Authored-By: Vladimir Grishin <yadolov@users.noreply.github.com>
This commit is contained in:
Eugene Fedunin 2019-04-29 16:20:30 +03:00
parent 24cb0e5d0b
commit 254629cf16
9 changed files with 179 additions and 21 deletions

View file

@ -44,10 +44,14 @@ const (
// This is used to control the cookie path when use-regex is set to true
annotationAffinityCookiePath = "session-cookie-path"
// This is used to control the cookie change after request failure
annotationAffinityCookieChangeOnFailure = "session-cookie-change-on-failure"
)
var (
affinityCookieExpiresRegex = regexp.MustCompile(`(^0|-?[1-9]\d*$)`)
affinityCookieExpiresRegex = regexp.MustCompile(`(^0|-?[1-9]\d*$)`)
affinityCookieChangeOnFailureRegex = regexp.MustCompile(`^(true|false)$`)
)
// Config describes the per ingress session affinity config
@ -67,6 +71,8 @@ type Cookie struct {
MaxAge string `json:"maxage"`
// The path that a cookie will be set on
Path string `json:"path"`
// Flag that allows cookie regeneration on request failure
ChangeOnFailure string `json:"changeonfailure"`
}
// cookieAffinityParse gets the annotation values related to Cookie Affinity
@ -99,6 +105,11 @@ func (a affinity) cookieAffinityParse(ing *extensions.Ingress) *Cookie {
klog.V(3).Infof("Invalid or no annotation value found in Ingress %v: %v. Ignoring it", ing.Name, annotationAffinityCookieMaxAge)
}
cookie.ChangeOnFailure, err = parser.GetStringAnnotation(annotationAffinityCookieChangeOnFailure, ing)
if err != nil || !affinityCookieChangeOnFailureRegex.MatchString(cookie.ChangeOnFailure) {
klog.V(3).Infof("Invalid or no annotation value found in Ingress %v: %v. Ignoring it", ing.Name, annotationAffinityCookieChangeOnFailure)
}
return cookie
}

View file

@ -71,6 +71,7 @@ func TestIngressAffinityCookieConfig(t *testing.T) {
data[parser.GetAnnotationWithPrefix(annotationAffinityCookieExpires)] = "4500"
data[parser.GetAnnotationWithPrefix(annotationAffinityCookieMaxAge)] = "3000"
data[parser.GetAnnotationWithPrefix(annotationAffinityCookiePath)] = "/foo"
data[parser.GetAnnotationWithPrefix(annotationAffinityCookieChangeOnFailure)] = "true"
ing.SetAnnotations(data)
affin, _ := NewParser(&resolver.Mock{}).Parse(ing)
@ -98,4 +99,8 @@ func TestIngressAffinityCookieConfig(t *testing.T) {
if nginxAffinity.Cookie.Path != "/foo" {
t.Errorf("expected /foo as session-cookie-path but returned %v", nginxAffinity.Cookie.Path)
}
if nginxAffinity.Cookie.ChangeOnFailure != "true" {
t.Errorf("expected change of failure parameter set to true but returned %v", nginxAffinity.Cookie.ChangeOnFailure)
}
}