Initial support for sticky config in annotations

This commit is contained in:
Ricardo Pchevuzinske Katz 2017-02-10 01:00:17 -02:00
parent d0c4e0d713
commit 1dbe65ecb6
6 changed files with 239 additions and 0 deletions

View file

@ -34,6 +34,7 @@ import (
"k8s.io/ingress/core/pkg/ingress/annotations/rewrite"
"k8s.io/ingress/core/pkg/ingress/annotations/secureupstream"
"k8s.io/ingress/core/pkg/ingress/annotations/sslpassthrough"
"k8s.io/ingress/core/pkg/ingress/annotations/stickysession"
"k8s.io/ingress/core/pkg/ingress/errors"
"k8s.io/ingress/core/pkg/ingress/resolver"
)
@ -63,6 +64,7 @@ func newAnnotationExtractor(cfg extractorConfig) annotationExtractor {
"Redirect": rewrite.NewParser(cfg),
"SecureUpstream": secureupstream.NewParser(),
"SSLPassthrough": sslpassthrough.NewParser(),
"StickySession": stickysession.NewParser(),
},
}
}
@ -99,6 +101,7 @@ const (
secureUpstream = "SecureUpstream"
healthCheck = "HealthCheck"
sslPassthrough = "SSLPassthrough"
stickySession = "StickySession"
)
func (e *annotationExtractor) SecureUpstream(ing *extensions.Ingress) bool {
@ -115,3 +118,8 @@ func (e *annotationExtractor) SSLPassthrough(ing *extensions.Ingress) bool {
val, _ := e.annotations[sslPassthrough].Parse(ing)
return val.(bool)
}
func (e *annotationExtractor) StickySession(ing *extensions.Ingress) *stickysession.StickyConfig {
val, _ := e.annotations[stickySession].Parse(ing)
return val.(*stickysession.StickyConfig)
}

View file

@ -32,6 +32,9 @@ const (
annotationUpsMaxFails = "ingress.kubernetes.io/upstream-max-fails"
annotationUpsFailTimeout = "ingress.kubernetes.io/upstream-fail-timeout"
annotationPassthrough = "ingress.kubernetes.io/ssl-passthrough"
annotationStickyEnabled = "ingress.kubernetes.io/sticky-enabled"
annotationStickyName = "ingress.kubernetes.io/sticky-name"
annotationStickyHash = "ingress.kubernetes.io/sticky-hash"
)
type mockCfg struct {
@ -179,3 +182,37 @@ func TestSSLPassthrough(t *testing.T) {
}
}
}
func TestStickySession(t *testing.T) {
ec := newAnnotationExtractor(mockCfg{})
ing := buildIngress()
fooAnns := []struct {
annotations map[string]string
enabled bool
hash string
name string
}{
{map[string]string{annotationStickyEnabled: "true", annotationStickyHash: "md5", annotationStickyName: "route"}, true, "md5", "route"},
{map[string]string{annotationStickyEnabled: "true", annotationStickyHash: "", annotationStickyName: "xpto"}, true, "md5", "xpto"},
{map[string]string{annotationStickyEnabled: "true", annotationStickyHash: "", annotationStickyName: ""}, true, "md5", "route"},
}
for _, foo := range fooAnns {
ing.SetAnnotations(foo.annotations)
r := ec.StickySession(ing)
if r == nil {
t.Errorf("Returned nil but expected a StickySesion.StickyConfig")
continue
}
if r.Hash != foo.hash {
t.Errorf("Returned %v but expected %v for Hash", r.Hash, foo.hash)
}
if r.Name != foo.name {
t.Errorf("Returned %v but expected %v for Name", r.Name, foo.name)
}
}
}

View file

@ -700,6 +700,7 @@ func (ic *GenericController) createUpstreams(data []interface{}) map[string]*ing
secUpstream := ic.annotations.SecureUpstream(ing)
hz := ic.annotations.HealthCheck(ing)
sticky := ic.annotations.StickySession(ing)
var defBackend string
if ing.Spec.Backend != nil {
@ -739,6 +740,12 @@ func (ic *GenericController) createUpstreams(data []interface{}) map[string]*ing
if !upstreams[name].Secure {
upstreams[name].Secure = secUpstream
}
if !upstreams[name].StickySession.Enabled || upstreams[name].StickySession.Name == "" || upstreams[name].StickySession.Hash == "" {
upstreams[name].StickySession.Enabled = sticky.Enabled
upstreams[name].StickySession.Name = sticky.Name
upstreams[name].StickySession.Hash = sticky.Hash
}
svcKey := fmt.Sprintf("%v/%v", ing.GetNamespace(), path.Backend.ServiceName)
endp, err := ic.serviceEndpoints(svcKey, path.Backend.ServicePort.String(), hz)
if err != nil {