2017-02-10 03:00:17 +00:00
|
|
|
/*
|
|
|
|
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
|
2017-02-10 03:33:23 +00:00
|
|
|
se, _ := parser.GetBoolAnnotation(stickyEnabled, ing)
|
2017-02-10 03:00:17 +00:00
|
|
|
|
|
|
|
// Get the Sticky Cookie Name
|
2017-02-10 03:33:23 +00:00
|
|
|
sn, _ := parser.GetStringAnnotation(stickyName, ing)
|
2017-02-10 03:00:17 +00:00
|
|
|
|
|
|
|
if sn == "" {
|
|
|
|
sn = defaultStickyName
|
|
|
|
}
|
|
|
|
|
2017-02-10 03:33:23 +00:00
|
|
|
sh, _ := parser.GetStringAnnotation(stickyHash, ing)
|
2017-02-10 03:00:17 +00:00
|
|
|
|
|
|
|
if sh == "" {
|
|
|
|
sh = defaultStickyHash
|
|
|
|
}
|
|
|
|
|
|
|
|
if !stickyHashRegex.MatchString(sh) {
|
2017-02-10 03:33:23 +00:00
|
|
|
return &StickyConfig{
|
|
|
|
Name: "",
|
|
|
|
Enabled: false,
|
|
|
|
Hash: "",
|
|
|
|
}, ing_errors.NewInvalidAnnotationContent(stickyHash, sh)
|
2017-02-10 03:00:17 +00:00
|
|
|
|
|
|
|
return &StickyConfig{
|
|
|
|
Name: sn,
|
|
|
|
Enabled: se,
|
|
|
|
Hash: sh,
|
|
|
|
}, nil
|
|
|
|
}
|