diff --git a/core/pkg/ingress/annotations/sessionaffinity/main.go b/core/pkg/ingress/annotations/sessionaffinity/main.go index 551cde2a3..3cf5181e9 100644 --- a/core/pkg/ingress/annotations/sessionaffinity/main.go +++ b/core/pkg/ingress/annotations/sessionaffinity/main.go @@ -31,7 +31,7 @@ const ( // If a cookie with this name exists, // its value is used as an index into the list of available backends. annotationAffinityCookieName = "ingress.kubernetes.io/session-cookie-name" - defaultAffinityCookieName = "route" + defaultAffinityCookieName = "INGRESSCOOKIE" // This is the algorithm used by nginx to generate a value for the session cookie, if // one isn't supplied and affintiy is set to "cookie". annotationAffinityCookieHash = "ingress.kubernetes.io/session-cookie-hash" @@ -45,24 +45,21 @@ var ( // AffinityConfig describes the per ingress session affinity config type AffinityConfig struct { // The type of affinity that will be used - AffinityType string `json:"type"` - CookieAffinityConfig CookieAffinityConfig `json:"cookieconfig"` + AffinityType string `json:"type"` + CookieConfig } -// CookieAffinityConfig describes the Config of cookie type affinity -type CookieAffinityConfig struct { +// CookieConfig describes the Config of cookie type affinity +type CookieConfig struct { // The name of the cookie that will be used in case of cookie affinity type. Name string `json:"name"` // The hash that will be used to encode the cookie in case of cookie affinity type Hash string `json:"hash"` } -type affinity struct { -} - // CookieAffinityParse gets the annotation values related to Cookie Affinity // It also sets default values when no value or incorrect value is found -func CookieAffinityParse(ing *extensions.Ingress) *CookieAffinityConfig { +func CookieAffinityParse(ing *extensions.Ingress) *CookieConfig { sn, err := parser.GetStringAnnotation(annotationAffinityCookieName, ing) @@ -78,7 +75,7 @@ func CookieAffinityParse(ing *extensions.Ingress) *CookieAffinityConfig { sh = defaultAffinityCookieHash } - return &CookieAffinityConfig{ + return &CookieConfig{ Name: sn, Hash: sh, } @@ -89,19 +86,22 @@ func NewParser() parser.IngressAnnotation { return affinity{} } +type affinity struct { +} + // ParseAnnotations parses the annotations contained in the ingress // rule used to configure the affinity directives func (a affinity) Parse(ing *extensions.Ingress) (interface{}, error) { - var cookieAffinityConfig *CookieAffinityConfig - cookieAffinityConfig = &CookieAffinityConfig{} + var cookieAffinityConfig *CookieConfig + cookieAffinityConfig = &CookieConfig{} // Check the type of affinity that will be used at, err := parser.GetStringAnnotation(annotationAffinityType, ing) if err != nil { at = "" } - //cookieAffinityConfig = CookieAffinityParse(ing) + switch at { case "cookie": cookieAffinityConfig = CookieAffinityParse(ing) @@ -111,8 +111,8 @@ func (a affinity) Parse(ing *extensions.Ingress) (interface{}, error) { } return &AffinityConfig{ - AffinityType: at, - CookieAffinityConfig: *cookieAffinityConfig, + AffinityType: at, + CookieConfig: *cookieAffinityConfig, }, nil } diff --git a/core/pkg/ingress/annotations/sessionaffinity/main_test.go b/core/pkg/ingress/annotations/sessionaffinity/main_test.go index b18f86835..b898813ec 100644 --- a/core/pkg/ingress/annotations/sessionaffinity/main_test.go +++ b/core/pkg/ingress/annotations/sessionaffinity/main_test.go @@ -65,7 +65,7 @@ func TestIngressAffinityCookieConfig(t *testing.T) { data := map[string]string{} data[annotationAffinityType] = "cookie" data[annotationAffinityCookieHash] = "sha123" - data[annotationAffinityCookieName] = "route" + data[annotationAffinityCookieName] = "INGRESSCOOKIE" ing.SetAnnotations(data) affin, _ := NewParser().Parse(ing) @@ -82,7 +82,7 @@ func TestIngressAffinityCookieConfig(t *testing.T) { t.Errorf("expected md5 as sticky-hash but returned %v", nginxAffinity.CookieAffinityConfig.Hash) } - if nginxAffinity.CookieAffinityConfig.Name != "route" { + if nginxAffinity.CookieAffinityConfig.Name != "INGRESSCOOKIE" { t.Errorf("expected route as sticky-name but returned %v", nginxAffinity.CookieAffinityConfig.Name) } } diff --git a/core/pkg/ingress/controller/annotations_test.go b/core/pkg/ingress/controller/annotations_test.go index 6927f2733..1b3b9b08d 100644 --- a/core/pkg/ingress/controller/annotations_test.go +++ b/core/pkg/ingress/controller/annotations_test.go @@ -195,7 +195,7 @@ func TestAffinitySession(t *testing.T) { }{ {map[string]string{annotationAffinityType: "cookie", annotationAffinityCookieHash: "md5", annotationAffinityCookieName: "route"}, "cookie", "md5", "route"}, {map[string]string{annotationAffinityType: "cookie", annotationAffinityCookieHash: "xpto", annotationAffinityCookieName: "route1"}, "cookie", "md5", "route1"}, - {map[string]string{annotationAffinityType: "cookie", annotationAffinityCookieHash: "", annotationAffinityCookieName: ""}, "cookie", "md5", "route"}, + {map[string]string{annotationAffinityType: "cookie", annotationAffinityCookieHash: "", annotationAffinityCookieName: ""}, "cookie", "md5", "INGRESSCOOKIE"}, {map[string]string{}, "", "", ""}, {nil, "", "", ""}, } @@ -209,12 +209,12 @@ func TestAffinitySession(t *testing.T) { continue } - if r.CookieAffinityConfig.Hash != foo.hash { - t.Errorf("Returned %v but expected %v for Hash", r.CookieAffinityConfig.Hash, foo.hash) + if r.CookieConfig.Hash != foo.hash { + t.Errorf("Returned %v but expected %v for Hash", r.CookieConfig.Hash, foo.hash) } - if r.CookieAffinityConfig.Name != foo.name { - t.Errorf("Returned %v but expected %v for Name", r.CookieAffinityConfig.Name, foo.name) + if r.CookieConfig.Name != foo.name { + t.Errorf("Returned %v but expected %v for Name", r.CookieConfig.Name, foo.name) } } } diff --git a/core/pkg/ingress/controller/controller.go b/core/pkg/ingress/controller/controller.go index 1eb0b6f2c..2a91640fc 100644 --- a/core/pkg/ingress/controller/controller.go +++ b/core/pkg/ingress/controller/controller.go @@ -743,8 +743,8 @@ func (ic *GenericController) createUpstreams(data []interface{}) map[string]*ing if upstreams[name].SessionAffinity.AffinityType == "" { upstreams[name].SessionAffinity.AffinityType = affinity.AffinityType if affinity.AffinityType == "cookie" { - upstreams[name].SessionAffinity.CookieSessionAffinity.Name = affinity.CookieAffinityConfig.Name - upstreams[name].SessionAffinity.CookieSessionAffinity.Hash = affinity.CookieAffinityConfig.Hash + upstreams[name].SessionAffinity.CookieSessionAffinity.Name = affinity.CookieConfig.Name + upstreams[name].SessionAffinity.CookieSessionAffinity.Hash = affinity.CookieConfig.Hash } } diff --git a/examples/affinity/cookie/nginx/README.md b/examples/affinity/cookie/nginx/README.md index 2a18f5f66..ea5a3d3b2 100644 --- a/examples/affinity/cookie/nginx/README.md +++ b/examples/affinity/cookie/nginx/README.md @@ -16,6 +16,7 @@ Using a deployment with only one replica doesn't set the 'sticky' cookie. Session stickyness is achieved through 3 annotations on the Ingress, as shown in the [example](sticky-ingress.yaml). |Name|Description|Values| +| --- | --- | --- | |ingress.kubernetes.io/affinity|Sets the affinity type|string (in NGINX only ``cookie`` is possible| |ingress.kubernetes.io/session-cookie-name|Name of the cookie that will be used|string (default to route)| |ingress.kubernetes.io/session-cookie-hash|Type of hash that will be used in cookie value|sha1/md5/index|