Initial support for sticky config in annotations
This commit is contained in:
parent
d0c4e0d713
commit
1dbe65ecb6
6 changed files with 239 additions and 0 deletions
96
core/pkg/ingress/annotations/stickysession/main.go
Normal file
96
core/pkg/ingress/annotations/stickysession/main.go
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
/*
|
||||
Copyright 2016 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package stickysession
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
|
||||
"k8s.io/kubernetes/pkg/apis/extensions"
|
||||
|
||||
"k8s.io/ingress/core/pkg/ingress/annotations/parser"
|
||||
ing_errors "k8s.io/ingress/core/pkg/ingress/errors"
|
||||
)
|
||||
|
||||
const (
|
||||
stickyEnabled = "ingress.kubernetes.io/sticky-enabled"
|
||||
stickyName = "ingress.kubernetes.io/sticky-name"
|
||||
stickyHash = "ingress.kubernetes.io/sticky-hash"
|
||||
defaultStickyHash = "md5"
|
||||
defaultStickyName = "route"
|
||||
)
|
||||
|
||||
var (
|
||||
stickyHashRegex = regexp.MustCompile(`index|md5|sha1`)
|
||||
)
|
||||
|
||||
// StickyConfig describes the per ingress sticky session config
|
||||
type StickyConfig struct {
|
||||
// The name of the cookie that will be used as stickness router.
|
||||
Name string `json:"name"`
|
||||
// If sticky must or must not be enabled
|
||||
Enabled bool `json:"enabled"`
|
||||
// The hash that will be used to encode the cookie
|
||||
Hash string `json:"hash"`
|
||||
}
|
||||
|
||||
type sticky struct {
|
||||
}
|
||||
|
||||
// NewParser creates a new Sticky annotation parser
|
||||
func NewParser() parser.IngressAnnotation {
|
||||
return sticky{}
|
||||
}
|
||||
|
||||
// ParseAnnotations parses the annotations contained in the ingress
|
||||
// rule used to configure the sticky directives
|
||||
func (a sticky) Parse(ing *extensions.Ingress) (interface{}, error) {
|
||||
// Check if the sticky is enabled
|
||||
se, err := parser.GetBoolAnnotation(stickyEnabled, ing)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Get the Sticky Cookie Name
|
||||
sn, err := parser.GetStringAnnotation(stickyName, ing)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if sn == "" {
|
||||
sn = defaultStickyName
|
||||
}
|
||||
|
||||
sh, err := parser.GetStringAnnotation(stickyHash, ing)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if sh == "" {
|
||||
sh = defaultStickyHash
|
||||
}
|
||||
|
||||
if !stickyHashRegex.MatchString(sh) {
|
||||
return nil, ing_errors.NewInvalidAnnotationContent(stickyHash, sh)
|
||||
}
|
||||
|
||||
return &StickyConfig{
|
||||
Name: sn,
|
||||
Enabled: se,
|
||||
Hash: sh,
|
||||
}, nil
|
||||
}
|
||||
88
core/pkg/ingress/annotations/stickysession/main_test.go
Normal file
88
core/pkg/ingress/annotations/stickysession/main_test.go
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
/*
|
||||
Copyright 2016 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package stickysession
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/apis/extensions"
|
||||
"k8s.io/kubernetes/pkg/util/intstr"
|
||||
)
|
||||
|
||||
func buildIngress() *extensions.Ingress {
|
||||
defaultBackend := extensions.IngressBackend{
|
||||
ServiceName: "default-backend",
|
||||
ServicePort: intstr.FromInt(80),
|
||||
}
|
||||
|
||||
return &extensions.Ingress{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Name: "foo",
|
||||
Namespace: api.NamespaceDefault,
|
||||
},
|
||||
Spec: extensions.IngressSpec{
|
||||
Backend: &extensions.IngressBackend{
|
||||
ServiceName: "default-backend",
|
||||
ServicePort: intstr.FromInt(80),
|
||||
},
|
||||
Rules: []extensions.IngressRule{
|
||||
{
|
||||
Host: "foo.bar.com",
|
||||
IngressRuleValue: extensions.IngressRuleValue{
|
||||
HTTP: &extensions.HTTPIngressRuleValue{
|
||||
Paths: []extensions.HTTPIngressPath{
|
||||
{
|
||||
Path: "/foo",
|
||||
Backend: defaultBackend,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func TestIngressHealthCheck(t *testing.T) {
|
||||
ing := buildIngress()
|
||||
|
||||
data := map[string]string{}
|
||||
data[stickyEnabled] = "true"
|
||||
data[stickyHash] = "md5"
|
||||
data[stickyName] = "route1"
|
||||
ing.SetAnnotations(data)
|
||||
|
||||
sti, _ := NewParser().Parse(ing)
|
||||
nginxSti, ok := sti.(*StickyConfig)
|
||||
if !ok {
|
||||
t.Errorf("expected a StickyConfig type")
|
||||
}
|
||||
|
||||
if nginxSti.Hash != "md5" {
|
||||
t.Errorf("expected md5 as sticky-hash but returned %v", nginxSti.Hash)
|
||||
}
|
||||
|
||||
if nginxSti.Hash != "md5" {
|
||||
t.Errorf("expected md5 as sticky-hash but returned %v", nginxSti.Hash)
|
||||
}
|
||||
|
||||
if !nginxSti.Enabled {
|
||||
t.Errorf("expected sticky-enabled but returned %v", nginxSti.Enabled)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue