Merge pull request #4048 from fedunineyu/change-upstream-on-error-with-sticky-session

Change upstream on error when sticky session balancer is used
This commit is contained in:
Kubernetes Prow Robot 2019-06-06 07:22:17 -07:00 committed by GitHub
commit 286ff13af2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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)
}
}

View file

@ -572,6 +572,7 @@ func (n *NGINXController) getBackendServers(ingresses []*ingress.Ingress) ([]*in
ups.SessionAffinity.CookieSessionAffinity.Expires = anns.SessionAffinity.Cookie.Expires
ups.SessionAffinity.CookieSessionAffinity.MaxAge = anns.SessionAffinity.Cookie.MaxAge
ups.SessionAffinity.CookieSessionAffinity.Path = cookiePath
ups.SessionAffinity.CookieSessionAffinity.ChangeOnFailure = anns.SessionAffinity.Cookie.ChangeOnFailure
locs := ups.SessionAffinity.CookieSessionAffinity.Locations
if _, ok := locs[host]; !ok {

View file

@ -144,11 +144,12 @@ type SessionAffinityConfig struct {
// CookieSessionAffinity defines the structure used in Affinity configured by Cookies.
// +k8s:deepcopy-gen=true
type CookieSessionAffinity struct {
Name string `json:"name"`
Expires string `json:"expires,omitempty"`
MaxAge string `json:"maxage,omitempty"`
Locations map[string][]string `json:"locations,omitempty"`
Path string `json:"path,omitempty"`
Name string `json:"name"`
Expires string `json:"expires,omitempty"`
MaxAge string `json:"maxage,omitempty"`
Locations map[string][]string `json:"locations,omitempty"`
Path string `json:"path,omitempty"`
ChangeOnFailure string `json:"changeonfailure"`
}
// UpstreamHashByConfig described setting from the upstream-hash-by* annotations.