2015-11-01 15:35:01 +00:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2016-06-02 13:17:04 +00:00
|
|
|
"fmt"
|
2016-08-16 17:13:18 +00:00
|
|
|
"github.com/docker/libkv/store"
|
2016-05-30 13:05:58 +00:00
|
|
|
"github.com/ryanuber/go-glob"
|
2015-11-01 15:35:01 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2015-11-01 18:15:05 +00:00
|
|
|
// Backend holds backend configuration.
|
2015-11-01 15:35:01 +00:00
|
|
|
type Backend struct {
|
|
|
|
Servers map[string]Server `json:"servers,omitempty"`
|
|
|
|
CircuitBreaker *CircuitBreaker `json:"circuitBreaker,omitempty"`
|
|
|
|
LoadBalancer *LoadBalancer `json:"loadBalancer,omitempty"`
|
2016-04-13 08:11:36 +00:00
|
|
|
MaxConn *MaxConn `json:"maxConn,omitempty"`
|
|
|
|
}
|
|
|
|
|
2016-04-21 22:38:44 +00:00
|
|
|
// MaxConn holds maximum connection configuration
|
2016-04-13 08:11:36 +00:00
|
|
|
type MaxConn struct {
|
|
|
|
Amount int64 `json:"amount,omitempty"`
|
|
|
|
ExtractorFunc string `json:"extractorFunc,omitempty"`
|
2015-11-01 15:35:01 +00:00
|
|
|
}
|
|
|
|
|
2015-11-01 18:15:05 +00:00
|
|
|
// LoadBalancer holds load balancing configuration.
|
2015-11-01 15:35:01 +00:00
|
|
|
type LoadBalancer struct {
|
|
|
|
Method string `json:"method,omitempty"`
|
2016-05-13 14:22:11 +00:00
|
|
|
Sticky bool `json:"sticky,omitempty"`
|
2015-11-01 15:35:01 +00:00
|
|
|
}
|
|
|
|
|
2015-11-01 18:15:05 +00:00
|
|
|
// CircuitBreaker holds circuit breaker configuration.
|
2015-11-01 15:35:01 +00:00
|
|
|
type CircuitBreaker struct {
|
|
|
|
Expression string `json:"expression,omitempty"`
|
|
|
|
}
|
|
|
|
|
2015-11-01 18:15:05 +00:00
|
|
|
// Server holds server configuration.
|
2015-11-01 15:35:01 +00:00
|
|
|
type Server struct {
|
|
|
|
URL string `json:"url,omitempty"`
|
2016-06-06 20:30:23 +00:00
|
|
|
Weight int `json:"weight"`
|
2015-11-01 15:35:01 +00:00
|
|
|
}
|
|
|
|
|
2015-11-01 18:15:05 +00:00
|
|
|
// Route holds route configuration.
|
2015-11-01 15:35:01 +00:00
|
|
|
type Route struct {
|
2016-03-27 00:05:17 +00:00
|
|
|
Rule string `json:"rule,omitempty"`
|
2015-11-01 15:35:01 +00:00
|
|
|
}
|
|
|
|
|
2015-11-01 18:15:05 +00:00
|
|
|
// Frontend holds frontend configuration.
|
2015-11-01 15:35:01 +00:00
|
|
|
type Frontend struct {
|
2016-01-29 19:34:17 +00:00
|
|
|
EntryPoints []string `json:"entryPoints,omitempty"`
|
2015-11-01 15:35:01 +00:00
|
|
|
Backend string `json:"backend,omitempty"`
|
|
|
|
Routes map[string]Route `json:"routes,omitempty"`
|
|
|
|
PassHostHeader bool `json:"passHostHeader,omitempty"`
|
2016-06-06 20:30:23 +00:00
|
|
|
Priority int `json:"priority"`
|
2015-11-01 15:35:01 +00:00
|
|
|
}
|
|
|
|
|
2015-11-01 18:15:05 +00:00
|
|
|
// LoadBalancerMethod holds the method of load balancing to use.
|
2015-11-01 15:35:01 +00:00
|
|
|
type LoadBalancerMethod uint8
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Wrr (default) = Weighted Round Robin
|
|
|
|
Wrr LoadBalancerMethod = iota
|
|
|
|
// Drr = Dynamic Round Robin
|
|
|
|
Drr
|
|
|
|
)
|
|
|
|
|
|
|
|
var loadBalancerMethodNames = []string{
|
|
|
|
"Wrr",
|
|
|
|
"Drr",
|
|
|
|
}
|
|
|
|
|
2015-11-01 18:15:05 +00:00
|
|
|
// NewLoadBalancerMethod create a new LoadBalancerMethod from a given LoadBalancer.
|
2015-11-01 15:35:01 +00:00
|
|
|
func NewLoadBalancerMethod(loadBalancer *LoadBalancer) (LoadBalancerMethod, error) {
|
|
|
|
if loadBalancer != nil {
|
|
|
|
for i, name := range loadBalancerMethodNames {
|
|
|
|
if strings.EqualFold(name, loadBalancer.Method) {
|
|
|
|
return LoadBalancerMethod(i), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Wrr, ErrInvalidLoadBalancerMethod
|
|
|
|
}
|
|
|
|
|
2015-11-01 18:15:05 +00:00
|
|
|
// ErrInvalidLoadBalancerMethod is thrown when the specified load balancing method is invalid.
|
2015-11-01 15:35:01 +00:00
|
|
|
var ErrInvalidLoadBalancerMethod = errors.New("Invalid method, using default")
|
|
|
|
|
2015-11-01 18:15:05 +00:00
|
|
|
// Configuration of a provider.
|
2015-11-01 15:35:01 +00:00
|
|
|
type Configuration struct {
|
|
|
|
Backends map[string]*Backend `json:"backends,omitempty"`
|
|
|
|
Frontends map[string]*Frontend `json:"frontends,omitempty"`
|
|
|
|
}
|
|
|
|
|
2015-11-01 18:15:05 +00:00
|
|
|
// ConfigMessage hold configuration information exchanged between parts of traefik.
|
2015-11-01 15:35:01 +00:00
|
|
|
type ConfigMessage struct {
|
|
|
|
ProviderName string
|
|
|
|
Configuration *Configuration
|
|
|
|
}
|
2016-05-30 13:05:58 +00:00
|
|
|
|
|
|
|
// Constraint hold a parsed constraint expresssion
|
|
|
|
type Constraint struct {
|
|
|
|
Key string
|
|
|
|
// MustMatch is true if operator is "==" or false if operator is "!="
|
|
|
|
MustMatch bool
|
2016-05-31 07:54:42 +00:00
|
|
|
// TODO: support regex
|
|
|
|
Regex string
|
2016-05-30 13:05:58 +00:00
|
|
|
}
|
|
|
|
|
2016-05-20 15:17:38 +00:00
|
|
|
// NewConstraint receive a string and return a *Constraint, after checking syntax and parsing the constraint expression
|
2016-05-30 13:05:58 +00:00
|
|
|
func NewConstraint(exp string) (*Constraint, error) {
|
|
|
|
sep := ""
|
|
|
|
constraint := &Constraint{}
|
|
|
|
|
|
|
|
if strings.Contains(exp, "==") {
|
|
|
|
sep = "=="
|
|
|
|
constraint.MustMatch = true
|
|
|
|
} else if strings.Contains(exp, "!=") {
|
|
|
|
sep = "!="
|
|
|
|
constraint.MustMatch = false
|
|
|
|
} else {
|
|
|
|
return nil, errors.New("Constraint expression missing valid operator: '==' or '!='")
|
|
|
|
}
|
|
|
|
|
|
|
|
kv := strings.SplitN(exp, sep, 2)
|
|
|
|
if len(kv) == 2 {
|
|
|
|
// At the moment, it only supports tags
|
|
|
|
if kv[0] != "tag" {
|
|
|
|
return nil, errors.New("Constraint must be tag-based. Syntax: tag==us-*")
|
|
|
|
}
|
|
|
|
|
|
|
|
constraint.Key = kv[0]
|
|
|
|
constraint.Regex = kv[1]
|
|
|
|
return constraint, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, errors.New("Incorrect constraint expression: " + exp)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Constraint) String() string {
|
|
|
|
if c.MustMatch {
|
|
|
|
return c.Key + "==" + c.Regex
|
|
|
|
}
|
|
|
|
return c.Key + "!=" + c.Regex
|
|
|
|
}
|
|
|
|
|
2016-09-20 14:56:29 +00:00
|
|
|
// UnmarshalText define how unmarshal in TOML parsing
|
|
|
|
func (c *Constraint) UnmarshalText(text []byte) error {
|
|
|
|
constraint, err := NewConstraint(string(text))
|
|
|
|
*c = *constraint
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-05-20 15:17:38 +00:00
|
|
|
// MatchConstraintWithAtLeastOneTag tests a constraint for one single service
|
2016-05-30 13:05:58 +00:00
|
|
|
func (c *Constraint) MatchConstraintWithAtLeastOneTag(tags []string) bool {
|
|
|
|
for _, tag := range tags {
|
|
|
|
if glob.Glob(c.Regex, tag) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2016-06-02 13:17:04 +00:00
|
|
|
|
|
|
|
//Set []*Constraint
|
|
|
|
func (cs *Constraints) Set(str string) error {
|
|
|
|
exps := strings.Split(str, ",")
|
|
|
|
if len(exps) == 0 {
|
|
|
|
return errors.New("Bad Constraint format: " + str)
|
|
|
|
}
|
|
|
|
for _, exp := range exps {
|
|
|
|
constraint, err := NewConstraint(exp)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*cs = append(*cs, *constraint)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Constraints holds a Constraint parser
|
|
|
|
type Constraints []Constraint
|
|
|
|
|
|
|
|
//Get []*Constraint
|
|
|
|
func (cs *Constraints) Get() interface{} { return []Constraint(*cs) }
|
|
|
|
|
|
|
|
//String returns []*Constraint in string
|
|
|
|
func (cs *Constraints) String() string { return fmt.Sprintf("%+v", *cs) }
|
|
|
|
|
|
|
|
//SetValue sets []*Constraint into the parser
|
|
|
|
func (cs *Constraints) SetValue(val interface{}) {
|
|
|
|
*cs = Constraints(val.(Constraints))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type exports the Constraints type as a string
|
|
|
|
func (cs *Constraints) Type() string {
|
|
|
|
return fmt.Sprint("constraint")
|
|
|
|
}
|
2016-07-20 22:29:00 +00:00
|
|
|
|
2016-08-16 17:13:18 +00:00
|
|
|
// Store holds KV store cluster config
|
|
|
|
type Store struct {
|
|
|
|
store.Store
|
|
|
|
Prefix string // like this "prefix" (without the /)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cluster holds cluster config
|
|
|
|
type Cluster struct {
|
2016-08-18 12:20:11 +00:00
|
|
|
Node string `description:"Node name"`
|
2016-08-18 11:03:10 +00:00
|
|
|
Store *Store
|
2016-08-16 17:13:18 +00:00
|
|
|
}
|
|
|
|
|
2016-07-20 22:29:00 +00:00
|
|
|
// Auth holds authentication configuration (BASIC, DIGEST, users)
|
|
|
|
type Auth struct {
|
|
|
|
Basic *Basic
|
|
|
|
Digest *Digest
|
|
|
|
}
|
|
|
|
|
|
|
|
// Users authentication users
|
|
|
|
type Users []string
|
|
|
|
|
|
|
|
// Basic HTTP basic authentication
|
|
|
|
type Basic struct {
|
|
|
|
Users
|
|
|
|
}
|
|
|
|
|
|
|
|
// Digest HTTP authentication
|
|
|
|
type Digest struct {
|
|
|
|
Users
|
|
|
|
}
|